@@ -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
369379func 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
382391func 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
0 commit comments