Skip to content

Commit

Permalink
Add parsing support for ANY/SOME/ALL comparison modifiers.
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
  • Loading branch information
arthurschreiber committed Jun 7, 2024
1 parent 87ecae7 commit 300bb28
Show file tree
Hide file tree
Showing 9 changed files with 7,004 additions and 6,860 deletions.
4 changes: 4 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2273,13 +2273,17 @@ type (
// ComparisonExpr represents a two-value comparison expression.
ComparisonExpr struct {
Operator ComparisonExprOperator
Modifier ComparisonModifier
Left, Right Expr
Escape Expr
}

// ComparisonExprOperator is an enum for ComparisonExpr.Operator
ComparisonExprOperator int8

// ComparisonModifier is an enum for ComparisonExpr.Modifier
ComparisonModifier int8

// BetweenExpr represents a BETWEEN or a NOT BETWEEN expression.
BetweenExpr struct {
IsBetween bool
Expand Down
1 change: 1 addition & 0 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,13 @@ func (node *NotExpr) Format(buf *TrackedBuffer) {

// Format formats the node.
func (node *ComparisonExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%l %s %r", node.Left, node.Operator.ToString(), node.Right)
buf.astPrintf(node, "%l %s", node.Left, node.Operator.ToString())
if node.Modifier == All {
buf.literal(" all")
} else if node.Modifier == Any {
buf.literal(" any")
}
buf.astPrintf(node, " %r", node.Right)
if node.Escape != nil {
buf.astPrintf(node, " escape %v", node.Escape)
}
Expand Down
5 changes: 5 additions & 0 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,12 @@ const (
NotRegexpOp
)

const (
Missing ComparisonModifier = iota
Any
All
)

func (op ComparisonExprOperator) Inverse() ComparisonExprOperator {
switch op {
case EqualOp:
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ var keywords = []keyword{
{"always", ALWAYS},
{"analyze", ANALYZE},
{"and", AND},
{"any", ANY},
{"any_value", ANY_VALUE},
{"array", ARRAY},
{"as", AS},
Expand Down Expand Up @@ -584,6 +585,7 @@ var keywords = []keyword{
{"slow", SLOW},
{"smallint", SMALLINT},
{"snapshot", SNAPSHOT},
{"some", SOME},
{"spatial", SPATIAL},
{"specific", UNUSED},
{"sql", SQL},
Expand Down
27 changes: 18 additions & 9 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3004,25 +3004,25 @@ var (
}, {
input: `SELECT * FROM JSON_TABLE('[ {"c1": null} ]','$[*]' COLUMNS( c1 INT PATH '$.c1' ERROR ON ERROR )) as jt`,
output: `select * from json_table('[ {\"c1\": null} ]', '$[*]' columns(
c1 INT path '$.c1' error on error
c1 INT path '$.c1' error on error
)
) as jt`,
}, {
input: `SELECT * FROM JSON_TABLE( '[{"a": 1, "b": [11,111]}, {"a": 2, "b": [22,222]}]', '$[*]' COLUMNS(a INT PATH '$.a', NESTED PATH '$.b[*]' COLUMNS (b1 INT PATH '$'), NESTED PATH '$.b[*]' COLUMNS (b2 INT PATH '$'))) AS jt`,
output: `select * from json_table('[{\"a\": 1, \"b\": [11,111]}, {\"a\": 2, \"b\": [22,222]}]', '$[*]' columns(
a INT path '$.a' ,
nested path '$.b[*]' columns(
b1 INT path '$'
b1 INT path '$'
),
nested path '$.b[*]' columns(
b2 INT path '$'
b2 INT path '$'
)
)
) as jt`,
}, {
input: `SELECT * FROM JSON_TABLE('[ {"c1": null} ]','$[*]' COLUMNS( c1 INT PATH '$.c1' ERROR ON ERROR )) as jt`,
output: `select * from json_table('[ {\"c1\": null} ]', '$[*]' columns(
c1 INT path '$.c1' error on error
c1 INT path '$.c1' error on error
)
) as jt`,
}, {
Expand All @@ -3031,23 +3031,23 @@ var (
rowid for ordinality,
ac VARCHAR(100) path '$.a' default '111' on empty default '999' on error ,
aj JSON path '$.a' default '{\"x\": 333}' on empty ,
bx INT exists path '$.b'
bx INT exists path '$.b'
)
) as tt`,
}, {
input: `SELECT * FROM JSON_TABLE( '[ {"a": 1, "b": [11,111]}, {"a": 2, "b": [22,222]}, {"a":3}]', '$[*]' COLUMNS( a INT PATH '$.a', NESTED PATH '$.b[*]' COLUMNS (b INT PATH '$') ) ) AS jt WHERE b IS NOT NULL`,
output: `select * from json_table('[ {\"a\": 1, \"b\": [11,111]}, {\"a\": 2, \"b\": [22,222]}, {\"a\":3}]', '$[*]' columns(
a INT path '$.a' ,
nested path '$.b[*]' columns(
b INT path '$'
b INT path '$'
)
)
) as jt where b is not null`,
}, {
input: `SELECT * FROM JSON_TABLE( '[{"x":2,"y":"8"},{"x":"3","y":"7"},{"x":"4","y":6}]', "$[1]" COLUMNS( xval VARCHAR(100) PATH "$.x", yval VARCHAR(100) PATH "$.y" ) ) AS jt1`,
output: `select * from json_table('[{\"x\":2,\"y\":\"8\"},{\"x\":\"3\",\"y\":\"7\"},{\"x\":\"4\",\"y\":6}]', '$[1]' columns(
xval VARCHAR(100) path '$.x' ,
yval VARCHAR(100) path '$.y'
yval VARCHAR(100) path '$.y'
)
) as jt1`,
}, {
Expand All @@ -3059,7 +3059,7 @@ var (
bpath VARCHAR(10) path '$.c' ,
ord for ordinality,
nested path '$.l[*]' columns(
lpath varchar(10) path '$'
lpath varchar(10) path '$'
)
)
)
Expand All @@ -3068,7 +3068,7 @@ var (
input: `SELECT * FROM JSON_TABLE('[{"x":2,"y":"8"},{"x":"3","y":"7"},{"x":"4","y":6}]', "$[1]" COLUMNS( xval VARCHAR(100) PATH "$.x", yval VARCHAR(100) PATH "$.y")) AS jt1;`,
output: `select * from json_table('[{\"x\":2,\"y\":\"8\"},{\"x\":\"3\",\"y\":\"7\"},{\"x\":\"4\",\"y\":6}]', '$[1]' columns(
xval VARCHAR(100) path '$.x' ,
yval VARCHAR(100) path '$.y'
yval VARCHAR(100) path '$.y'
)
) as jt1`,
}, {
Expand Down Expand Up @@ -3798,6 +3798,15 @@ var (
}, {
input: `select * from tbl where foo is unknown or bar is not unknown`,
output: `select * from tbl where foo is null or bar is not null`,
}, {
input: `select * from tbl where foo = any (select foo from tbl2)`,
}, {
input: `select * from tbl where foo = some (select foo from tbl2)`,
output: `select * from tbl where foo = any (select foo from tbl2)`,
}, {
input: `select * from tbl where foo > any (select foo from tbl2)`,
}, {
input: `select * from tbl where foo > all (select foo from tbl2)`,
}}
)

Expand Down
Loading

0 comments on commit 300bb28

Please sign in to comment.