Skip to content

Commit

Permalink
sql: add extra CmpExpr simplification rules to simplifyNotExpr
Browse files Browse the repository at this point in the history
This change adds simplification rules to `simplifyNotExpr`:
```
NotExpr{ComparisonExpr{NotRegMatch,a,b}}       -> ComparisonExpr{RegMatch,a,b}
NotExpr{ComparisonExpr{NotRegIMatch,a,b}}      -> ComparisonExpr{RegIMatch,a,b}
NotExpr{ComparisonExpr{IsDistinctFrom,a,b}}    -> ComparisonExpr{IsNotDistinctFrom,a,b}
NotExpr{ComparisonExpr{IsNotDistinctFrom,a,b}} -> ComparisonExpr{IsDistinctFrom,a,b}
NotExpr{ComparisonExpr{Is,a,b}}                -> ComparisonExpr{IsNot,a,b}
NotExpr{ComparisonExpr{IsNota,b}}              -> ComparisonExpr{Is,a,b}
```
  • Loading branch information
nvanbenschoten committed Oct 8, 2017
1 parent afc8df5 commit f845dd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/sql/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,20 @@ func simplifyNotExpr(evalCtx *parser.EvalContext, n *parser.NotExpr) (parser.Typ
op = parser.SimilarTo
case parser.RegMatch:
op = parser.NotRegMatch
case parser.NotRegMatch:
op = parser.RegMatch
case parser.RegIMatch:
op = parser.NotRegIMatch
case parser.NotRegIMatch:
op = parser.RegIMatch
case parser.IsDistinctFrom:
op = parser.IsNotDistinctFrom
case parser.IsNotDistinctFrom:
op = parser.IsDistinctFrom
case parser.Is:
op = parser.IsNot
case parser.IsNot:
op = parser.Is
default:
return parser.MakeDBool(true), false
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,14 @@ func TestSimplifyNotExpr(t *testing.T) {
{`NOT i NOT LIKE 'foo'`, `i = 'foo'`, false, false},
{`NOT i SIMILAR TO 'foo'`, `true`, false, false},
{`NOT i NOT SIMILAR TO 'foo'`, `i = 'foo'`, false, false},
{`NOT i ~ 'foo'`, `true`, false, false},
{`NOT i !~ 'foo'`, `true`, false, false},
{`NOT i ~* 'foo'`, `true`, false, false},
{`NOT i !~* 'foo'`, `true`, false, false},
{`NOT a IS DISTINCT FROM 1`, `true`, false, false},
{`NOT a IS NOT DISTINCT FROM 1`, `true`, false, false},
{`NOT a IS NULL`, `a IS NOT NULL`, true, true},
{`NOT a IS NOT NULL`, `a IS NULL`, true, true},
{`NOT (a != 1 AND b != 1)`, `(a = 1) OR (b = 1)`, true, true},
{`NOT (a != 1 OR a < 1)`, `a = 1`, true, true},
{`NOT NOT a = 1`, `a = 1`, true, true},
Expand Down

0 comments on commit f845dd9

Please sign in to comment.