Skip to content
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
47 changes: 47 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,53 @@ type ScriptTestAssertion struct {
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
// the tests.
var ScriptTests = []ScriptTest{
{
// https://github.com/dolthub/dolt/issues/9935
Dialect: "mysql",
Name: "Incorrect use of negation in AntiJoinIncludingNulls",
SetUpScript: []string{
"CREATE TABLE t0(c0 INT);",
"INSERT INTO t0(c0) VALUES(1);",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT * FROM t0 WHERE (! (1 || (EXISTS (SELECT 1))));",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t0 WHERE (! (0 || (EXISTS (SELECT 1))));",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t0 WHERE (! ((EXISTS (SELECT 1)) || 0));",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t0 WHERE (! ((EXISTS (SELECT 1)) || 1));",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t0 WHERE (! (1 && (EXISTS (SELECT 1))));",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t0 WHERE (! (0 && (EXISTS (SELECT 1))));",
Expected: []sql.Row{{1}},
},
{
Query: "SELECT * FROM t0 WHERE (! (0 || (EXISTS (SELECT 1 FROM t0 WHERE c0 = 2))));",
Expected: []sql.Row{{1}},
},
{
Query: "SELECT * FROM t0 WHERE (! (1 || (EXISTS (SELECT 1 FROM t0 WHERE c0 = 1))));",
Expected: []sql.Row{},
},
{
Query: "SELECT * FROM t0 WHERE (! (0 || (EXISTS (SELECT 1 FROM t0 WHERE c0 = 1))));",
Expected: []sql.Row{},
},
},
},
{
// https://github.com/dolthub/go-mysql-server/issues/3259
Dialect: "mysql",
Expand Down
64 changes: 36 additions & 28 deletions sql/analyzer/optimization_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,37 +237,37 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
expression.NewLessThanOrEqual(e.Val, e.Upper),
), transform.NewTree, nil
case *expression.Or:
if isTrue(e.LeftChild) {
if isTrue(ctx, e.LeftChild) {
return e.LeftChild, transform.NewTree, nil
}

if isTrue(e.RightChild) {
if isTrue(ctx, e.RightChild) {
return e.RightChild, transform.NewTree, nil
}

if isFalse(e.LeftChild) && types.IsBoolean(e.RightChild.Type()) {
if isFalse(ctx, e.LeftChild) && types.IsBoolean(e.RightChild.Type()) {
return e.RightChild, transform.NewTree, nil
}

if isFalse(e.RightChild) && types.IsBoolean(e.LeftChild.Type()) {
if isFalse(ctx, e.RightChild) && types.IsBoolean(e.LeftChild.Type()) {
return e.LeftChild, transform.NewTree, nil
}

return e, transform.SameTree, nil
case *expression.And:
if isFalse(e.LeftChild) {
if isFalse(ctx, e.LeftChild) {
return e.LeftChild, transform.NewTree, nil
}

if isFalse(e.RightChild) {
if isFalse(ctx, e.RightChild) {
return e.RightChild, transform.NewTree, nil
}

if isTrue(e.LeftChild) && types.IsBoolean(e.RightChild.Type()) {
if isTrue(ctx, e.LeftChild) && types.IsBoolean(e.RightChild.Type()) {
return e.RightChild, transform.NewTree, nil
}

if isTrue(e.RightChild) && types.IsBoolean(e.LeftChild.Type()) {
if isTrue(ctx, e.RightChild) && types.IsBoolean(e.LeftChild.Type()) {
return e.LeftChild, transform.NewTree, nil
}

Expand Down Expand Up @@ -326,6 +326,16 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
newRightUpper := expression.NewLiteral(valStr, e.RightChild.Type())
newExpr := expression.NewAnd(expression.NewGreaterThanOrEqual(e.LeftChild, newRightLower), expression.NewLessThanOrEqual(e.LeftChild, newRightUpper))
return newExpr, transform.NewTree, nil
case *expression.Not:
if lit, ok := e.Child.(*expression.Literal); ok {
val, err := sql.ConvertToBool(ctx, lit.Value())
if err != nil {
// error while converting, keep as is
return e, transform.SameTree, nil
}
return expression.NewLiteral(!val, e.Type()), transform.NewTree, nil
}
return e, transform.SameTree, nil
case *expression.Literal, expression.Tuple, *expression.Interval, *expression.CollatedExpression, *expression.MatchAgainst:
return e, transform.SameTree, nil
default:
Expand All @@ -350,12 +360,12 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
return nil, transform.SameTree, err
}

if isFalse(e) {
if isFalse(ctx, e) {
emptyTable := plan.NewEmptyTableWithSchema(filter.Schema())
return emptyTable, transform.NewTree, nil
}

if isTrue(e) {
if isTrue(ctx, e) {
return filter.Child, transform.NewTree, nil
}

Expand All @@ -366,30 +376,28 @@ func simplifyFilters(ctx *sql.Context, a *Analyzer, node sql.Node, scope *plan.S
})
}

func isFalse(e sql.Expression) bool {
func isFalse(ctx *sql.Context, e sql.Expression) bool {
lit, ok := e.(*expression.Literal)
if ok && lit != nil && lit.Type() == types.Boolean && lit.Value() != nil {
switch v := lit.Value().(type) {
case bool:
return !v
case int8:
return v == sql.False
}
if !ok || lit == nil || lit.Value() == nil {
return false
}
return false
val, err := sql.ConvertToBool(ctx, lit.Value())
if err != nil {
return false
}
return !val
}

func isTrue(e sql.Expression) bool {
func isTrue(ctx *sql.Context, e sql.Expression) bool {
lit, ok := e.(*expression.Literal)
if ok && lit != nil && lit.Type() == types.Boolean && lit.Value() != nil {
switch v := lit.Value().(type) {
case bool:
return v
case int8:
return v != sql.False
}
if !ok || lit == nil || lit.Value() == nil {
return false
}
val, err := sql.ConvertToBool(ctx, lit.Value())
if err != nil {
return false
}
return false
return val
}

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