Skip to content

Commit

Permalink
Add parsing support for ANY/SOME/ALL comparison modifiers. (#16080
Browse files Browse the repository at this point in the history
)

Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Manan Gupta <manan@planetscale.com>
Co-authored-by: Andres Taylor <andres@planetscale.com>
Co-authored-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
3 people committed Jun 11, 2024
1 parent 2531cd0 commit dbb8863
Show file tree
Hide file tree
Showing 15 changed files with 10,009 additions and 9,824 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
13 changes: 12 additions & 1 deletion go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3798,7 +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)`}}
)

func TestValid(t *testing.T) {
Expand Down Expand Up @@ -4045,6 +4053,9 @@ func TestInvalid(t *testing.T) {
}, {
input: "SELECT 0b2 FROM user",
err: "syntax error at position 11",
}, {
input: "select * from foo where b <=> any (select id from t1)",
err: "syntax error at position 42",
},
}

Expand Down
Loading

0 comments on commit dbb8863

Please sign in to comment.