Skip to content

Commit

Permalink
Addition of testing and comments
Browse files Browse the repository at this point in the history
Signed-off-by: Florent Poinsard <[email protected]>
  • Loading branch information
frouioui committed Nov 22, 2023
1 parent b83f02f commit 5fc26ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
8 changes: 5 additions & 3 deletions go/vt/sqlparser/predicate_rewriting.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ func simplifyNot(expr *NotExpr) (Expr, rewriteState) {
return expr, noChange{}
}

// ExtractINFromOR TODO:
// ExtractINFromOR rewrites the OR expression into an IN clause.
// Each side of each ORs has to be an equality comparison expression and the column names have to
// match for all sides of each comparison.
// This rewriter takes a query that looks like this WHERE a = 1 and b = 11 or a = 2 and b = 12 or a = 3 and b = 13
// And rewrite that to WHERE (a, b) IN ((1,11), (2,12), (3,13))
func ExtractINFromOR(expr *OrExpr) []Expr {
// This rewriter takes a query that looks like this WHERE a = 1 and b = 11 or a = 2 and b = 12 or a = 3 and b = 13
// And rewrite that to WHERE (a, b) IN ((1,11), (2,12), (3,13))
var varNames []*ColName
var values []Exprs
orSlice := orToSlice(expr)
Expand Down
38 changes: 14 additions & 24 deletions go/vt/sqlparser/predicate_rewriting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ func TestRewritePredicate(in *testing.T) {
// this has too many OR expressions in it, so we don't even try the CNF rewriting
in: "a = 1 and b = 41 or a = 2 and b = 42 or a = 3 and b = 43 or a = 4 and b = 44 or a = 5 and b = 45 or a = 6 and b = 46",
expected: "a = 1 and b = 41 or a = 2 and b = 42 or a = 3 and b = 43 or a = 4 and b = 44 or a = 5 and b = 45 or a = 6 and b = 46",
}, {
in: "a = 5 and B or a = 6 and C",
expected: "a in (5, 6) and (a = 5 or C) and ((B or a = 6) and (B or C))",
}, {
in: "(a = 5 and b = 1 or b = 2 and a = 6)",
expected: "(a = 5 or b = 2) and a in (5, 6) and (b in (1, 2) and (b = 1 or a = 6))",
}, {
in: "(a in (1,5) and B or C and a = 6)",
expected: "(a in (1, 5) or C) and a in (1, 5, 6) and ((B or C) and (B or a = 6))",
}, {
in: "(a in (1, 5) and B or C and a in (5, 7))",
expected: "(a in (1, 5) or C) and a in (1, 5, 7) and ((B or C) and (B or a in (5, 7)))",
}}

for _, tc := range tests {
Expand All @@ -158,30 +170,8 @@ func TestExtractINFromOR(in *testing.T) {
in string
expected string
}{{
in: "(A and B) or (B and A)",
expected: "<nil>",
}, {
in: "(a = 5 and B) or A",
expected: "<nil>",
}, {
in: "a = 5 and B or a = 6 and C",
expected: "a in (5, 6)",
}, {
in: "(a = 5 and b = 1 or b = 2 and a = 6)",
expected: "a in (5, 6) and b in (1, 2)",
}, {
in: "(a in (1,5) and B or C and a = 6)",
expected: "a in (1, 5, 6)",
}, {
in: "(a in (1, 5) and B or C and a in (5, 7))",
expected: "a in (1, 5, 7)",
}, {
in: "(a = 5 and b = 1 or b = 2 and a = 6 or b = 3 and a = 4)",
expected: "<nil>",
}, {
// this has too many OR expressions in it, so we don't even try the CNF rewriting
in: "a = 1 and b = 41 or a = 2 and b = 42 or a = 3 and b = 43 or a = 4 and b = 44 or a = 5 and b = 45",
expected: "toto",
in: "a = 1 and b = 41 or a = 2 and b = 42 or a = 3 and b = 43 or a = 4 and b = 44 or a = 5 and b = 45 or a = 6 and b = 46",
expected: "(a, b) in ((1, 41), (2, 42), (3, 43), (4, 44), (5, 45), (6, 46))",
}}

for _, tc := range tests {
Expand Down

0 comments on commit 5fc26ad

Please sign in to comment.