diff --git a/presto-main/src/test/java/io/prestosql/sql/planner/assertions/ExpressionVerifier.java b/presto-main/src/test/java/io/prestosql/sql/planner/assertions/ExpressionVerifier.java index 00fa3d9f9c04..b3acc4d8f8d7 100644 --- a/presto-main/src/test/java/io/prestosql/sql/planner/assertions/ExpressionVerifier.java +++ b/presto-main/src/test/java/io/prestosql/sql/planner/assertions/ExpressionVerifier.java @@ -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; } diff --git a/presto-main/src/test/java/io/prestosql/sql/planner/assertions/TestExpressionVerifier.java b/presto-main/src/test/java/io/prestosql/sql/planner/assertions/TestExpressionVerifier.java index e0589ebdf69a..2656013b6070 100644 --- a/presto-main/src/test/java/io/prestosql/sql/planner/assertions/TestExpressionVerifier.java +++ b/presto-main/src/test/java/io/prestosql/sql/planner/assertions/TestExpressionVerifier.java @@ -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));