Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing support for ANY/SOME/ALL comparison modifiers. #16080

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading