Skip to content

Commit 9295833

Browse files
authored
feat(mysql): Add parser support for IS [NOT] NULL (#2651)
to return a boolean column
1 parent fb9e375 commit 9295833

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

internal/compiler/output_columns.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,17 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
173173
name = *res.Name
174174
}
175175
notNull := false
176-
if n.Boolop == ast.BoolExprTypeNot && len(n.Args.Items) == 1 {
177-
sublink, ok := n.Args.Items[0].(*ast.SubLink)
178-
if ok && sublink.SubLinkType == ast.EXISTS_SUBLINK {
176+
if len(n.Args.Items) == 1 {
177+
switch n.Boolop {
178+
case ast.BoolExprTypeIsNull, ast.BoolExprTypeIsNotNull:
179179
notNull = true
180-
if name == "" {
181-
name = "not_exists"
180+
case ast.BoolExprTypeNot:
181+
sublink, ok := n.Args.Items[0].(*ast.SubLink)
182+
if ok && sublink.SubLinkType == ast.EXISTS_SUBLINK {
183+
notNull = true
184+
if name == "" {
185+
name = "not_exists"
186+
}
182187
}
183188
}
184189
}

internal/endtoend/testdata/comparisons/mysql/go/query.sql.go

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/comparisons/mysql/query.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ SELECT count(*) <> 0 FROM bar;
2424
-- name: Equal :many
2525
SELECT count(*) = 0 FROM bar;
2626

27+
-- name: IsNull :many
28+
SELECT id IS NULL FROM bar;
2729

28-
29-
30-
31-
30+
-- name: IsNotNull :many
31+
SELECT id IS NOT NULL FROM bar;

internal/engine/dolphin/convert.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,18 @@ func (c *cc) convertIndexPartSpecification(n *pcast.IndexPartSpecification) ast.
953953
}
954954

955955
func (c *cc) convertIsNullExpr(n *pcast.IsNullExpr) ast.Node {
956-
return todo(n)
956+
op := ast.BoolExprTypeIsNull
957+
if n.Not {
958+
op = ast.BoolExprTypeIsNotNull
959+
}
960+
return &ast.BoolExpr{
961+
Boolop: op,
962+
Args: &ast.List{
963+
Items: []ast.Node{
964+
c.convert(n.Expr),
965+
},
966+
},
967+
}
957968
}
958969

959970
func (c *cc) convertIsTruthExpr(n *pcast.IsTruthExpr) ast.Node {

internal/sql/ast/bool_expr_type.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const (
66
BoolExprTypeAnd
77
BoolExprTypeOr
88
BoolExprTypeNot
9+
10+
// Added for MySQL
11+
BoolExprTypeIsNull
12+
BoolExprTypeIsNotNull
913
)
1014

1115
type BoolExprType uint

0 commit comments

Comments
 (0)