Skip to content

Commit

Permalink
normalize unequal infer predicate 5>a to a<5
Browse files Browse the repository at this point in the history
  • Loading branch information
feiniaofeiafei committed Sep 18, 2024
1 parent f1aa1b3 commit 5903fb1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,23 @@ private Set<Expression> generatePredicates(Relation[][] chosen) {
for (int i = 0; i < inputExpressionSize; ++i) {
for (int j = 0; j < inputExpressionSize; ++j) {
if (chosen[i][j] == Relation.GT) {
newPredicates.add(TypeCoercionUtils.processComparisonPredicate(new GreaterThan(
inputExpressions.get(i), inputExpressions.get(j))));
newPredicates.add(TypeCoercionUtils.processComparisonPredicate(
normalizePredicate(new GreaterThan(
inputExpressions.get(i), inputExpressions.get(j)))).withInferred(true));
} else if (chosen[i][j] == Relation.GTE) {
newPredicates.add(TypeCoercionUtils.processComparisonPredicate(new GreaterThanEqual(
inputExpressions.get(i), inputExpressions.get(j))));
newPredicates.add(TypeCoercionUtils.processComparisonPredicate(
normalizePredicate(new GreaterThanEqual(
inputExpressions.get(i), inputExpressions.get(j)))).withInferred(true));
}
}
}
return newPredicates;
}

private ComparisonPredicate normalizePredicate(ComparisonPredicate expr) {
return expr.left().isConstant() && !expr.right().isConstant() ? expr.commute() : expr;
}

private static boolean isSlotOrLiteral(Expression expr) {
return expr instanceof SlotReference || expr instanceof Literal;
}
Expand All @@ -348,6 +354,7 @@ public static Set<Expression> inferUnequalPredicates(Set<Expression> inputs) {
inferGraph.deduce();
InferenceGraph.Relation[][] chosen = inferGraph.getNonEqualPredicates();
Set<Expression> newPredicates = inferGraph.generatePredicates(chosen);

for (ComparisonPredicate comparisonPredicate : inferGraph.getInferUsedExpressions()) {
newPredicates.remove(comparisonPredicate);
newPredicates.remove(comparisonPredicate.commute());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

suite("infer_unequal_predicates") {
sql "drop table test_like1"
sql "drop table if exists test_like1"
sql """
CREATE TABLE `test_like1` (
`a` INT NULL,
Expand Down

0 comments on commit 5903fb1

Please sign in to comment.