Skip to content

Commit c88082e

Browse files
authored
fix double negatives (#2919)
1 parent 8d4a7bc commit c88082e

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

enginetest/queries/script_queries.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5165,9 +5165,9 @@ CREATE TABLE tab3 (
51655165
},
51665166
},
51675167
{
5168-
Query: "select unix_timestamp(d), unix_timestamp(tt) from t;",
5168+
Query: "select unix_timestamp(d), substring(cast(unix_timestamp(tt) as char(128)), -6) from t;",
51695169
Expected: []sql.Row{
5170-
{"1577898000", "1743140096.123456"},
5170+
{"1577898000", "123456"},
51715171
},
51725172
},
51735173
},
@@ -7799,6 +7799,40 @@ where
77997799
},
78007800
},
78017801
},
7802+
{
7803+
Name: "not expression optimization",
7804+
Dialect: "mysql",
7805+
SetUpScript: []string{
7806+
"create table t (i int);",
7807+
"insert into t values (123);",
7808+
},
7809+
Assertions: []ScriptTestAssertion{
7810+
{
7811+
Query: "select * from t where 1 = (not(not(i)))",
7812+
Expected: []sql.Row{
7813+
{123},
7814+
},
7815+
},
7816+
{
7817+
Query: "select * from t where true = (not(not(i)))",
7818+
Expected: []sql.Row{
7819+
{123},
7820+
},
7821+
},
7822+
{
7823+
Query: "select * from t where true = (not(not(i = 123)))",
7824+
Expected: []sql.Row{
7825+
{123},
7826+
},
7827+
},
7828+
{
7829+
Query: "select * from t where false = (not(not(i != 123)))",
7830+
Expected: []sql.Row{
7831+
{123},
7832+
},
7833+
},
7834+
},
7835+
},
78027836
}
78037837

78047838
var SpatialScriptTests = []ScriptTest{

sql/analyzer/optimization_rules.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,9 @@ func pushNotFiltersHelper(e sql.Expression) (sql.Expression, error) {
431431
// NOT(NOT(c))=>c
432432
if not, _ := e.(*expression.Not); not != nil {
433433
if f, _ := not.Child.(*expression.Not); f != nil {
434-
return pushNotFiltersHelper(f.Child)
434+
if types.IsBoolean(f.Child.Type()) {
435+
return pushNotFiltersHelper(f.Child)
436+
}
435437
}
436438
}
437439

sql/types/typecheck.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import (
2020
"github.com/dolthub/go-mysql-server/sql"
2121
)
2222

23+
// IsBoolean checks if t is a boolean type.
24+
func IsBoolean(t sql.Type) bool {
25+
return t == Boolean
26+
}
27+
2328
// IsBlobType checks if t is BLOB
2429
func IsBlobType(t sql.Type) bool {
2530
if t == nil {

0 commit comments

Comments
 (0)