Skip to content

Commit

Permalink
Fix missing some keywords in GRANT PRIVILEGE
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk committed Nov 3, 2023
1 parent 57ca48c commit c49fa96
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 4 deletions.
2 changes: 2 additions & 0 deletions parser/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const (
KeywordMutation = "MUTATION"
KeywordNan_sql = "NAN_SQL"
KeywordNo = "NO"
KeywordNone = "NONE"
KeywordNot = "NOT"
KeywordNull = "NULL"
KeywordNulls = "NULLS"
Expand Down Expand Up @@ -349,6 +350,7 @@ var keywords = NewSet(
KeywordMutation,
KeywordNan_sql,
KeywordNo,
KeywordNone,
KeywordNot,
KeywordNull,
KeywordNulls,
Expand Down
31 changes: 27 additions & 4 deletions parser/parse_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,14 @@ func (p *Parser) parserDropUserOrRole(pos Pos) (*DropUserOrRole, error) {
func (p *Parser) parsePrivilegeSelectOrInsert(pos Pos) (*PrivilegeExpr, error) {
keyword := p.last().String
_ = p.lexer.consumeToken()
params, err := p.parseFunctionParams(p.Pos())
if err != nil {
return nil, err

var err error
var params *ParamExprList
if p.matchTokenKind("(") {
params, err = p.parseFunctionParams(p.Pos())
if err != nil {
return nil, err
}
}
return &PrivilegeExpr{
PrivilegePos: pos,
Expand Down Expand Up @@ -794,6 +799,15 @@ func (p *Parser) parsePrivilegeSystem(pos Pos) (*PrivilegeExpr, error) {
}

func (p *Parser) parsePrivilege(pos Pos) (*PrivilegeExpr, error) {
if p.matchTokenKind(TokenIdent) {
if p.last().String == "dictGet" {
_ = p.lexer.consumeToken()
return &PrivilegeExpr{
PrivilegePos: pos,
Keywords: []string{"dictGet"},
}, nil
}
}
switch {
case p.matchKeyword(KeywordSelect), p.matchKeyword(KeywordInsert):
return p.parsePrivilegeSelectOrInsert(pos)
Expand All @@ -805,7 +819,8 @@ func (p *Parser) parsePrivilege(pos Pos) (*PrivilegeExpr, error) {
return p.parsePrivilegeDrop(pos)
case p.tryConsumeKeyword(KeywordShow) != nil:
return p.parsePrivilegeShow(pos)
case p.tryConsumeKeyword(KeywordAll) != nil:
case p.matchKeyword(KeywordAll), p.matchTokenKind(KeywordNone):
_ = p.lexer.consumeToken()
return &PrivilegeExpr{
PrivilegePos: pos,
Keywords: []string{KeywordAll},
Expand All @@ -820,6 +835,14 @@ func (p *Parser) parsePrivilege(pos Pos) (*PrivilegeExpr, error) {
}, nil
case p.tryConsumeKeyword(KeywordSystem) != nil:
return p.parsePrivilegeSystem(pos)
case p.tryConsumeKeyword(KeywordAdmin) != nil:
if err := p.consumeKeyword(KeywordOption); err != nil {
return nil, err
}
return &PrivilegeExpr{
PrivilegePos: pos,
Keywords: []string{KeywordAdmin, KeywordOption},
}, nil
case p.matchKeyword(KeywordOptimize), p.matchKeyword(KeywordTruncate):
keyword := p.last().String
_ = p.lexer.consumeToken()
Expand Down
9 changes: 9 additions & 0 deletions parser/testdata/ddl/format/grant_privilege.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ GRANT SELECT(x,y) ON *.* TO john;
GRANT SELECT(x,y) ON *.table TO CURRENT_USER;
GRANT SELECT(x,y) ON *.table TO CURRENT_USER,john,mary;
GRANT ALL ON *.* TO admin_role WITH GRANT OPTION;
GRANT SELECT,INSERT ON database.table_1 TO table_1_select_role;
GRANT SELECT(x, y, z),INSERT ON database.table_1 TO table_1_select_role;
GRANT SELECT, dictGet ON *.* TO select_all_role;
GRANT ADMIN OPTION ON *.* TO select_all_role;



-- Format SQL:
Expand All @@ -18,3 +23,7 @@ GRANT SELECT(x, y) ON *.* TO john;
GRANT SELECT(x, y) ON *.table TO CURRENT_USER;
GRANT SELECT(x, y) ON *.table TO CURRENT_USER, john, mary;
GRANT ALL ON *.* TO admin_role WITH GRANT OPTION;
GRANT SELECT, INSERT ON database.table_1 TO table_1_select_role;
GRANT SELECT(x, y, z), INSERT ON database.table_1 TO table_1_select_role;
GRANT SELECT, dictGet ON *.* TO select_all_role;
GRANT ADMIN OPTION ON *.* TO select_all_role;
5 changes: 5 additions & 0 deletions parser/testdata/ddl/grant_privilege.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ GRANT SELECT(x,y) ON *.* TO john;
GRANT SELECT(x,y) ON *.table TO CURRENT_USER;
GRANT SELECT(x,y) ON *.table TO CURRENT_USER,john,mary;
GRANT ALL ON *.* TO admin_role WITH GRANT OPTION;
GRANT SELECT,INSERT ON database.table_1 TO table_1_select_role;
GRANT SELECT(x, y, z),INSERT ON database.table_1 TO table_1_select_role;
GRANT SELECT, dictGet ON *.* TO select_all_role;
GRANT ADMIN OPTION ON *.* TO select_all_role;

206 changes: 206 additions & 0 deletions parser/testdata/ddl/output/grant_privilege.sql.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -480,5 +480,211 @@
"WithOptions": [
"GRANT"
]
},
{
"GrantPos": 373,
"StatementEnd": 435,
"OnCluster": null,
"Privileges": [
{
"PrivilegePos": 379,
"PrivilegeEnd": 0,
"Keywords": [
"SELECT"
],
"Params": null
},
{
"PrivilegePos": 386,
"PrivilegeEnd": 0,
"Keywords": [
"INSERT"
],
"Params": null
}
],
"On": {
"Database": {
"Name": "database",
"Unquoted": false,
"NamePos": 396,
"NameEnd": 404
},
"Table": {
"Name": "table_1",
"Unquoted": false,
"NamePos": 405,
"NameEnd": 412
}
},
"To": [
{
"Name": "table_1_select_role",
"Unquoted": false,
"NamePos": 416,
"NameEnd": 435
}
],
"WithOptions": []
},
{
"GrantPos": 437,
"StatementEnd": 508,
"OnCluster": null,
"Privileges": [
{
"PrivilegePos": 443,
"PrivilegeEnd": 0,
"Keywords": [
"SELECT"
],
"Params": {
"LeftParenPos": 449,
"RightParenPos": 457,
"Items": {
"ListPos": 450,
"ListEnd": 457,
"HasDistinct": false,
"Items": [
{
"Name": "x",
"Unquoted": false,
"NamePos": 450,
"NameEnd": 451
},
{
"Name": "y",
"Unquoted": false,
"NamePos": 453,
"NameEnd": 454
},
{
"Name": "z",
"Unquoted": false,
"NamePos": 456,
"NameEnd": 457
}
]
},
"ColumnArgList": null
}
},
{
"PrivilegePos": 459,
"PrivilegeEnd": 0,
"Keywords": [
"INSERT"
],
"Params": null
}
],
"On": {
"Database": {
"Name": "database",
"Unquoted": false,
"NamePos": 469,
"NameEnd": 477
},
"Table": {
"Name": "table_1",
"Unquoted": false,
"NamePos": 478,
"NameEnd": 485
}
},
"To": [
{
"Name": "table_1_select_role",
"Unquoted": false,
"NamePos": 489,
"NameEnd": 508
}
],
"WithOptions": []
},
{
"GrantPos": 510,
"StatementEnd": 558,
"OnCluster": null,
"Privileges": [
{
"PrivilegePos": 516,
"PrivilegeEnd": 0,
"Keywords": [
"SELECT"
],
"Params": null
},
{
"PrivilegePos": 524,
"PrivilegeEnd": 0,
"Keywords": [
"dictGet"
],
"Params": null
}
],
"On": {
"Database": {
"Name": "*",
"Unquoted": false,
"NamePos": 535,
"NameEnd": 536
},
"Table": {
"Name": "*",
"Unquoted": false,
"NamePos": 537,
"NameEnd": 538
}
},
"To": [
{
"Name": "select_all_role",
"Unquoted": false,
"NamePos": 543,
"NameEnd": 558
}
],
"WithOptions": []
},
{
"GrantPos": 560,
"StatementEnd": 605,
"OnCluster": null,
"Privileges": [
{
"PrivilegePos": 566,
"PrivilegeEnd": 0,
"Keywords": [
"ADMIN",
"OPTION"
],
"Params": null
}
],
"On": {
"Database": {
"Name": "*",
"Unquoted": false,
"NamePos": 582,
"NameEnd": 583
},
"Table": {
"Name": "*",
"Unquoted": false,
"NamePos": 584,
"NameEnd": 585
}
},
"To": [
{
"Name": "select_all_role",
"Unquoted": false,
"NamePos": 590,
"NameEnd": 605
}
],
"WithOptions": []
}
]

0 comments on commit c49fa96

Please sign in to comment.