Skip to content

Commit

Permalink
*: add Not flag for ExistsSubqueryExpr (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
eurekaka authored and alivxxx committed Jul 17, 2019
1 parent 98d0068 commit 366176d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ type ExistsSubqueryExpr struct {
exprNode
// Sel is the subquery, may be rewritten to other type of expression.
Sel ExprNode
// Not is true, the expression is "not exists".
Not bool
}

// Format the ExprNode into a Writer.
Expand Down
8 changes: 7 additions & 1 deletion parser.go

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

8 changes: 7 additions & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2568,7 +2568,13 @@ Expression:
}
| "NOT" Expression %prec not
{
$$ = &ast.UnaryOperationExpr{Op: opcode.Not, V: $2}
expr, ok := $2.(*ast.ExistsSubqueryExpr)
if ok {
expr.Not = true
$$ = $2
} else {
$$ = &ast.UnaryOperationExpr{Op: opcode.Not, V: $2}
}
}
| BoolPri IsOrNotOp trueKwd %prec is
{
Expand Down
17 changes: 17 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2704,3 +2704,20 @@ func (s *testParserSuite) TestFieldText(c *C) {
c.Assert(traceStmt.Stmt.Text(), Equals, "select a from t")
}
}

func (s *testParserSuite) TestNotExistsSubquery(c *C) {
table := []testCase{
{`select * from t1 where not exists (select * from t2 where t1.a = t2.a)`, true},
}

parser := New()
for _, tt := range table {
stmt, _, err := parser.Parse(tt.src, "", "")
c.Assert(err, IsNil)

sel := stmt[0].(*ast.SelectStmt)
exists, ok := sel.Where.(*ast.ExistsSubqueryExpr)
c.Assert(ok, IsTrue)
c.Assert(exists.Not, Equals, tt.ok)
}
}

0 comments on commit 366176d

Please sign in to comment.