Skip to content

Commit d1808b9

Browse files
committed
add dialect and mv impl layer
1 parent 35c9d48 commit d1808b9

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

enginetest/queries/script_queries.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ type ScriptTestAssertion struct {
124124
var ScriptTests = []ScriptTest{
125125
{
126126
// https://github.com/dolthub/dolt/issues/9935
127-
Name: "Incorrect use of negation in AntiJoinIncludingNulls",
127+
Dialect: "mysql",
128+
Name: "Incorrect use of negation in AntiJoinIncludingNulls",
128129
SetUpScript: []string{
129130
"CREATE TABLE t0(c0 INT);",
130131
"INSERT INTO t0(c0) VALUES(1);",
@@ -138,6 +139,22 @@ var ScriptTests = []ScriptTest{
138139
Query: "SELECT * FROM t0 WHERE (! (0 || (EXISTS (SELECT 1))));",
139140
Expected: []sql.Row{},
140141
},
142+
{
143+
Query: "SELECT * FROM t0 WHERE (! ((EXISTS (SELECT 1)) || 0));",
144+
Expected: []sql.Row{},
145+
},
146+
{
147+
Query: "SELECT * FROM t0 WHERE (! ((EXISTS (SELECT 1)) || 1));",
148+
Expected: []sql.Row{},
149+
},
150+
{
151+
Query: "SELECT * FROM t0 WHERE (! (1 && (EXISTS (SELECT 1))));",
152+
Expected: []sql.Row{},
153+
},
154+
{
155+
Query: "SELECT * FROM t0 WHERE (! (0 && (EXISTS (SELECT 1))));",
156+
Expected: []sql.Row{{1}},
157+
},
141158
{
142159
Query: "SELECT * FROM t0 WHERE (! (0 || (EXISTS (SELECT 1 FROM t0 WHERE c0 = 2))));",
143160
Expected: []sql.Row{{1}},

sql/analyzer/optimization_rules.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
326326
newRightUpper := expression.NewLiteral(valStr, e.RightChild.Type())
327327
newExpr := expression.NewAnd(expression.NewGreaterThanOrEqual(e.LeftChild, newRightLower), expression.NewLessThanOrEqual(e.LeftChild, newRightUpper))
328328
return newExpr, transform.NewTree, nil
329+
case *expression.Not:
330+
if lit, ok := e.Child.(*expression.Literal); ok {
331+
val, err := sql.ConvertToBool(ctx, lit.Value())
332+
if err != nil {
333+
// non-const, keep as is
334+
return e, transform.SameTree, nil
335+
}
336+
return expression.NewLiteral(!val, e.Type()), transform.NewTree, nil
337+
}
338+
return e, transform.SameTree, nil
329339
case *expression.Literal, expression.Tuple, *expression.Interval, *expression.CollatedExpression, *expression.MatchAgainst:
330340
return e, transform.SameTree, nil
331341
default:
@@ -368,28 +378,26 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
368378

369379
func isFalse(e sql.Expression) bool {
370380
lit, ok := e.(*expression.Literal)
371-
if ok && lit != nil && lit.Type() == types.Boolean && lit.Value() != nil {
372-
switch v := lit.Value().(type) {
373-
case bool:
374-
return !v
375-
case int8:
376-
return v == sql.False
377-
}
381+
if !ok || lit == nil || lit.Value() == nil {
382+
return false
378383
}
379-
return false
384+
val, err := sql.ConvertToBool(sql.NewEmptyContext(), lit.Value())
385+
if err != nil {
386+
return false
387+
}
388+
return !val
380389
}
381390

382391
func isTrue(e sql.Expression) bool {
383392
lit, ok := e.(*expression.Literal)
384-
if ok && lit != nil && lit.Type() == types.Boolean && lit.Value() != nil {
385-
switch v := lit.Value().(type) {
386-
case bool:
387-
return v
388-
case int8:
389-
return v != sql.False
390-
}
393+
if !ok || lit == nil || lit.Value() == nil {
394+
return false
395+
}
396+
val, err := sql.ConvertToBool(sql.NewEmptyContext(), lit.Value())
397+
if err != nil {
398+
return false
391399
}
392-
return false
400+
return val
393401
}
394402

395403
// pushNotFilters applies De'Morgan's laws to push NOT expressions as low

sql/analyzer/unnest_exists_subqueries.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,6 @@ func unnestExistSubqueries(ctx *sql.Context, scope *plan.Scope, a *Analyzer, fil
123123
var s *hoistSubquery
124124
var err error
125125

126-
if not, ok := f.(*expression.Not); ok {
127-
if lit, ok := not.Child.(*expression.Literal); ok {
128-
val, err := sql.ConvertToBool(ctx, lit.Value())
129-
if err != nil {
130-
// non-const
131-
} else if val {
132-
return plan.NewEmptyTableWithSchema(ret.Schema()), transform.NewTree, nil
133-
} else {
134-
continue // always true
135-
}
136-
}
137-
}
138-
139126
// match subquery expression
140127
joinType := plan.JoinTypeSemi
141128
var sq *plan.Subquery

0 commit comments

Comments
 (0)