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 XOR operator support. #6371

Merged
merged 1 commit into from
Jun 25, 2020
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
11 changes: 11 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ type (
Left, Right Expr
}

// XorExpr represents an XOR expression.
XorExpr struct {
Left, Right Expr
}

// NotExpr represents a NOT expression.
NotExpr struct {
Expr Expr
Expand Down Expand Up @@ -759,6 +764,7 @@ type (
// iExpr ensures that only expressions nodes can be assigned to a Expr
func (*AndExpr) iExpr() {}
func (*OrExpr) iExpr() {}
func (*XorExpr) iExpr() {}
func (*NotExpr) iExpr() {}
func (*ComparisonExpr) iExpr() {}
func (*RangeCond) iExpr() {}
Expand Down Expand Up @@ -1538,6 +1544,11 @@ func (node *OrExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%v or %v", node.Left, node.Right)
}

// Format formats the node.
func (node *XorExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%v xor %v", node.Left, node.Right)
}

// Format formats the node.
func (node *NotExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "not %v", node.Expr)
Expand Down
10 changes: 10 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,16 @@ var (
}, {
input: "select /* OR in select columns */ (a or b) from t where c = 5",
output: "select /* OR in select columns */ a or b from t where c = 5",
}, {
input: "select /* XOR of columns in where */ * from t where a xor b",
}, {
input: "select /* XOR of mixed columns in where */ * from t where a = 5 xor b and c is not null",
}, {
input: "select /* XOR in select columns */ (a xor b) from t where c = 5",
output: "select /* XOR in select columns */ a xor b from t where c = 5",
}, {
input: "select /* XOR in select columns */ * from t where (1 xor c1 > 0)",
output: "select /* XOR in select columns */ * from t where 1 xor c1 > 0",
}, {
input: "select /* bool as select value */ a, true from t",
}, {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/precedence.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func precedenceFor(in Expr) Precendence {
switch node := in.(type) {
case *OrExpr:
return P16
//case *XorExpr: TODO add parser support for XOR
// return P15
case *XorExpr:
return P15
case *AndExpr:
return P14
case *NotExpr:
Expand Down
2 changes: 2 additions & 0 deletions go/vt/sqlparser/precedence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func readable(node Expr) string {
return fmt.Sprintf("(%s or %s)", readable(node.Left), readable(node.Right))
case *AndExpr:
return fmt.Sprintf("(%s and %s)", readable(node.Left), readable(node.Right))
case *XorExpr:
return fmt.Sprintf("(%s xor %s)", readable(node.Left), readable(node.Right))
case *BinaryExpr:
return fmt.Sprintf("(%s %s %s)", readable(node.Left), node.Operator, readable(node.Right))
case *IsExpr:
Expand Down
10 changes: 10 additions & 0 deletions go/vt/sqlparser/random_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (g *generator) booleanExpr() Expr {

options := []exprF{
func() Expr { return g.andExpr() },
func() Expr { return g.xorExpr() },
func() Expr { return g.orExpr() },
func() Expr { return g.comparison(g.intExpr) },
func() Expr { return g.comparison(g.stringExpr) },
Expand Down Expand Up @@ -250,6 +251,15 @@ func (g *generator) orExpr() Expr {
}
}

func (g *generator) xorExpr() Expr {
g.enter()
defer g.exit()
return &XorExpr{
Left: g.booleanExpr(),
Right: g.booleanExpr(),
}
}

func (g *generator) notExpr() Expr {
g.enter()
defer g.exit()
Expand Down
12 changes: 12 additions & 0 deletions go/vt/sqlparser/rewriter.go

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

Loading