Skip to content

Commit

Permalink
[CALCITE-2074] Simplification of point ranges that are open above or …
Browse files Browse the repository at this point in the history
…below yields wrong results

Suppose that R is a point range that is closed at one or both ends,
i.e. [x, x), or (x, x] or (x, y). Such a range is empty, but we were
wrongly simplifying it to a point. It is only valid to simplify
open-open ranges, [x, x], to a point. For example, "5 >= x AND x > 5",
described as the closed-open range "[5, 5)", should simplify to false.
  • Loading branch information
julianhyde committed Dec 2, 2017
1 parent 9bb5400 commit 11da17b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e,
// Term is always satisfied given these predicates
return rexBuilder.makeLiteral(true);
} else if (range2.lowerEndpoint().equals(range2.upperEndpoint())) {
if (range2.lowerBoundType() == BoundType.OPEN
|| range2.upperBoundType() == BoundType.OPEN) {
// range is a point, but does not include its endpoint, therefore is
// effectively empty
return rexBuilder.makeLiteral(false);
}
// range is now a point; it's worth simplifying
return rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, comparison.ref,
rexBuilder.makeLiteral(range2.lowerEndpoint(),
Expand Down
24 changes: 24 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RexProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,30 @@ private void checkExponentialCnf(int n) {
RelOptPredicateList.of(rexBuilder,
ImmutableList.of(lt(bRef, literal10), ge(aRef, literal5))),
">(?0.a, 5)");

// condition "a > 5"
// with pre-condition "a <= 5"
// yields "false"
checkSimplifyFilter(gt(aRef, literal5),
RelOptPredicateList.of(rexBuilder,
ImmutableList.of(le(aRef, literal5))),
"false");

// condition "a > 5"
// with pre-condition "a <= 5 and b <= 5"
// yields "false"
checkSimplifyFilter(gt(aRef, literal5),
RelOptPredicateList.of(rexBuilder,
ImmutableList.of(le(aRef, literal5), le(bRef, literal5))),
"false");

// condition "a > 5 or b > 5"
// with pre-condition "a <= 5 and b <= 5"
// should yield "false" but yields "a = 5 or b = 5"
checkSimplifyFilter(or(gt(aRef, literal5), gt(bRef, literal5)),
RelOptPredicateList.of(rexBuilder,
ImmutableList.of(le(aRef, literal5), le(bRef, literal5))),
"false");
}

/** Unit test for
Expand Down

0 comments on commit 11da17b

Please sign in to comment.