Skip to content

Commit

Permalink
Make expression matcher symmetric for comparison expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
martint committed May 2, 2019
1 parent c92bf50 commit ebcebd0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ protected Boolean visitComparisonExpression(ComparisonExpression actual, Node ex
{
if (expectedExpression instanceof ComparisonExpression) {
ComparisonExpression expected = (ComparisonExpression) expectedExpression;
if (actual.getOperator() == expected.getOperator()) {
return process(actual.getLeft(), expected.getLeft()) && process(actual.getRight(), expected.getRight());
}
return (actual.getOperator() == expected.getOperator() && process(actual.getLeft(), expected.getLeft()) && process(actual.getRight(), expected.getRight())) ||
(actual.getOperator() == expected.getOperator().flip() && process(actual.getLeft(), expected.getRight()) && process(actual.getRight(), expected.getLeft()));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,51 @@ public void testBetween()
assertFalse(verifier.process(expression("orderkey BETWEEN 1 AND 2"), expression("X BETWEEN 4 AND 7")));
}

@Test
public void testSymmetry()
{
SymbolAliases symbolAliases = SymbolAliases.builder()
.put("a", new SymbolReference("x"))
.put("b", new SymbolReference("y"))
.build();

ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);

assertTrue(verifier.process(expression("x > y"), expression("a > b")));
assertTrue(verifier.process(expression("x > y"), expression("b < a")));
assertTrue(verifier.process(expression("y < x"), expression("a > b")));
assertTrue(verifier.process(expression("y < x"), expression("b < a")));

assertFalse(verifier.process(expression("x < y"), expression("a > b")));
assertFalse(verifier.process(expression("x < y"), expression("b < a")));
assertFalse(verifier.process(expression("y > x"), expression("a > b")));
assertFalse(verifier.process(expression("y > x"), expression("b < a")));

assertTrue(verifier.process(expression("x >= y"), expression("a >= b")));
assertTrue(verifier.process(expression("x >= y"), expression("b <= a")));
assertTrue(verifier.process(expression("y <= x"), expression("a >= b")));
assertTrue(verifier.process(expression("y <= x"), expression("b <= a")));

assertFalse(verifier.process(expression("x <= y"), expression("a >= b")));
assertFalse(verifier.process(expression("x <= y"), expression("b <= a")));
assertFalse(verifier.process(expression("y >= x"), expression("a >= b")));
assertFalse(verifier.process(expression("y >= x"), expression("b <= a")));

assertTrue(verifier.process(expression("x = y"), expression("a = b")));
assertTrue(verifier.process(expression("x = y"), expression("b = a")));
assertTrue(verifier.process(expression("y = x"), expression("a = b")));
assertTrue(verifier.process(expression("y = x"), expression("b = a")));
assertTrue(verifier.process(expression("x <> y"), expression("a <> b")));
assertTrue(verifier.process(expression("x <> y"), expression("b <> a")));
assertTrue(verifier.process(expression("y <> x"), expression("a <> b")));
assertTrue(verifier.process(expression("y <> x"), expression("b <> a")));

assertTrue(verifier.process(expression("x IS DISTINCT FROM y"), expression("a IS DISTINCT FROM b")));
assertTrue(verifier.process(expression("x IS DISTINCT FROM y"), expression("b IS DISTINCT FROM a")));
assertTrue(verifier.process(expression("y IS DISTINCT FROM x"), expression("a IS DISTINCT FROM b")));
assertTrue(verifier.process(expression("y IS DISTINCT FROM x"), expression("b IS DISTINCT FROM a")));
}

private Expression expression(String sql)
{
return rewriteIdentifiersToSymbolReferences(parser.createExpression(sql));
Expand Down

0 comments on commit ebcebd0

Please sign in to comment.