-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ES|QL] Add CombineBinaryComparisons rule #110548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fang-xing-esql
merged 13 commits into
elastic:main
from
fang-xing-esql:CombineBinaryComparisons
Jul 30, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2422f70
add CombineBinaryComparisons
fang-xing-esql 7c58b68
Update docs/changelog/110548.yaml
fang-xing-esql d321d1d
Merge branch 'main' into CombineBinaryComparisons
fang-xing-esql 277c48f
add more tests from OptimizerRulesTests
fang-xing-esql 75e4140
EsqlCapabilities
fang-xing-esql 925a87b
Merge branch 'main' into CombineBinaryComparisons
fang-xing-esql d36adae
more test as suggested
fang-xing-esql d5b2112
Merge branch 'main' into CombineBinaryComparisons
fang-xing-esql 81892c0
Merge branch 'main' into CombineBinaryComparisons
fang-xing-esql 5bcf8a4
Merge branch 'main' into CombineBinaryComparisons
fang-xing-esql 46d184e
Update docs/changelog/110548.yaml
fang-xing-esql f318b45
Merge branch 'main' into CombineBinaryComparisons
fang-xing-esql 0d297ba
clean up comments
fang-xing-esql File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 110548 | ||
| summary: "[ES|QL] Add `CombineBinaryComparisons` rule" | ||
| area: ES|QL | ||
| type: enhancement | ||
| issues: | ||
| - 108525 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
.../src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/CombineBinaryComparisons.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.optimizer.rules; | ||
|
|
||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
| import org.elasticsearch.xpack.esql.core.expression.predicate.Predicates; | ||
| import org.elasticsearch.xpack.esql.core.expression.predicate.logical.And; | ||
| import org.elasticsearch.xpack.esql.core.expression.predicate.logical.BinaryLogic; | ||
| import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Or; | ||
| import org.elasticsearch.xpack.esql.core.expression.predicate.operator.comparison.BinaryComparison; | ||
| import org.elasticsearch.xpack.esql.core.util.CollectionUtils; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.GreaterThan; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.GreaterThanOrEqual; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.LessThan; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.LessThanOrEqual; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.NotEquals; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public final class CombineBinaryComparisons extends OptimizerRules.OptimizerExpressionRule<BinaryLogic> { | ||
|
|
||
| public CombineBinaryComparisons() { | ||
| super(OptimizerRules.TransformDirection.DOWN); | ||
| } | ||
|
|
||
| @Override | ||
| public Expression rule(BinaryLogic e) { | ||
| if (e instanceof And and) { | ||
| return combine(and); | ||
| } else if (e instanceof Or or) { | ||
| return combine(or); | ||
| } | ||
| return e; | ||
| } | ||
|
|
||
| // combine conjunction | ||
| private static Expression combine(And and) { | ||
| List<BinaryComparison> bcs = new ArrayList<>(); | ||
| List<Expression> exps = new ArrayList<>(); | ||
| boolean changed = false; | ||
| List<Expression> andExps = Predicates.splitAnd(and); | ||
|
|
||
| andExps.sort((o1, o2) -> { | ||
| if (o1 instanceof NotEquals && o2 instanceof NotEquals) { | ||
| return 0; // keep NotEquals' order | ||
| } else if (o1 instanceof NotEquals || o2 instanceof NotEquals) { | ||
| return o1 instanceof NotEquals ? 1 : -1; // push NotEquals up | ||
| } else { | ||
| return 0; // keep non-Ranges' and non-NotEquals' order | ||
| } | ||
| }); | ||
| for (Expression ex : andExps) { | ||
| if (ex instanceof BinaryComparison bc && (ex instanceof Equals || ex instanceof NotEquals) == false) { | ||
| if (bc.right().foldable() && (findExistingComparison(bc, bcs, true))) { | ||
| changed = true; | ||
| } else { | ||
| bcs.add(bc); | ||
| } | ||
| } else if (ex instanceof NotEquals neq) { | ||
| if (neq.right().foldable() && notEqualsIsRemovableFromConjunction(neq, bcs)) { | ||
| // the non-equality can simply be dropped: either superfluous or has been merged with an updated range/inequality | ||
| changed = true; | ||
| } else { // not foldable OR not overlapping | ||
| exps.add(ex); | ||
| } | ||
| } else { | ||
| exps.add(ex); | ||
| } | ||
| } | ||
| return changed ? Predicates.combineAnd(CollectionUtils.combine(exps, bcs)) : and; | ||
| } | ||
|
|
||
| // combine disjunction | ||
| private static Expression combine(Or or) { | ||
| List<BinaryComparison> bcs = new ArrayList<>(); | ||
| List<Expression> exps = new ArrayList<>(); | ||
| boolean changed = false; | ||
| for (Expression ex : Predicates.splitOr(or)) { | ||
| if (ex instanceof BinaryComparison bc) { | ||
| if (bc.right().foldable() && findExistingComparison(bc, bcs, false)) { | ||
| changed = true; | ||
| } else { | ||
| bcs.add(bc); | ||
| } | ||
| } else { | ||
| exps.add(ex); | ||
| } | ||
| } | ||
| return changed ? Predicates.combineOr(CollectionUtils.combine(exps, bcs)) : or; | ||
| } | ||
|
|
||
| /** | ||
| * Find commonalities between the given comparison in the given list. | ||
| * The method can be applied both for conjunctive (AND) or disjunctive purposes (OR). | ||
| */ | ||
| private static boolean findExistingComparison(BinaryComparison main, List<BinaryComparison> bcs, boolean conjunctive) { | ||
| Object value = main.right().fold(); | ||
| // NB: the loop modifies the list (hence why the int is used) | ||
| for (int i = 0; i < bcs.size(); i++) { | ||
| BinaryComparison other = bcs.get(i); | ||
| // skip if cannot evaluate | ||
| if (other.right().foldable() == false) { | ||
| continue; | ||
| } | ||
| // if bc is a higher/lower value or gte vs gt, use it instead | ||
| if ((other instanceof GreaterThan || other instanceof GreaterThanOrEqual) | ||
| && (main instanceof GreaterThan || main instanceof GreaterThanOrEqual)) { | ||
| if (main.left().semanticEquals(other.left())) { | ||
| Integer compare = BinaryComparison.compare(value, other.right().fold()); | ||
| if (compare != null) { | ||
| // AND | ||
| if ((conjunctive && | ||
| // a > 3 AND a > 2 -> a > 3 | ||
| (compare > 0 || | ||
| // a > 2 AND a >= 2 -> a > 2 | ||
| (compare == 0 && main instanceof GreaterThan && other instanceof GreaterThanOrEqual))) || | ||
| // OR | ||
| (conjunctive == false && | ||
| // a > 2 OR a > 3 -> a > 2 | ||
| (compare < 0 || | ||
| // a >= 2 OR a > 2 -> a >= 2 | ||
| (compare == 0 && main instanceof GreaterThanOrEqual && other instanceof GreaterThan)))) { | ||
| bcs.remove(i); | ||
| bcs.add(i, main); | ||
| } | ||
| // found a match | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| // if bc is a lower/higher value or lte vs lt, use it instead | ||
| else if ((other instanceof LessThan || other instanceof LessThanOrEqual) | ||
| && (main instanceof LessThan || main instanceof LessThanOrEqual)) { | ||
| if (main.left().semanticEquals(other.left())) { | ||
| Integer compare = BinaryComparison.compare(value, other.right().fold()); | ||
| if (compare != null) { | ||
| // AND | ||
| if ((conjunctive && | ||
| // a < 2 AND a < 3 -> a < 2 | ||
| (compare < 0 || | ||
| // a < 2 AND a <= 2 -> a < 2 | ||
| (compare == 0 && main instanceof LessThan && other instanceof LessThanOrEqual))) || | ||
| // OR | ||
| (conjunctive == false && | ||
| // a < 2 OR a < 3 -> a < 3 | ||
| (compare > 0 || | ||
| // a <= 2 OR a < 2 -> a <= 2 | ||
| (compare == 0 && main instanceof LessThanOrEqual && other instanceof LessThan)))) { | ||
| bcs.remove(i); | ||
| bcs.add(i, main); | ||
| } | ||
| // found a match | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static boolean notEqualsIsRemovableFromConjunction(NotEquals notEquals, List<BinaryComparison> bcs) { | ||
| Object neqVal = notEquals.right().fold(); | ||
| Integer comp; | ||
|
|
||
| // check on "condition-overlapping" inequalities: | ||
| // a != 2 AND a > 3 -> a > 3 (discard NotEquals) | ||
| // a != 2 AND a >= 2 -> a > 2 (discard NotEquals plus update inequality) | ||
| // a != 2 AND a > 1 -> nop (do nothing) | ||
| // | ||
| // a != 2 AND a < 3 -> nop | ||
| // a != 2 AND a <= 2 -> a < 2 | ||
| // a != 2 AND a < 1 -> a < 1 | ||
| for (int i = 0; i < bcs.size(); i++) { | ||
| BinaryComparison bc = bcs.get(i); | ||
| if (notEquals.left().semanticEquals(bc.left())) { | ||
| if (bc instanceof LessThan || bc instanceof LessThanOrEqual) { | ||
| comp = bc.right().foldable() ? BinaryComparison.compare(neqVal, bc.right().fold()) : null; | ||
| if (comp != null) { | ||
| if (comp >= 0) { | ||
| if (comp == 0 && bc instanceof LessThanOrEqual) { // a != 2 AND a <= 2 -> a < 2 | ||
| bcs.set(i, new LessThan(bc.source(), bc.left(), bc.right(), bc.zoneId())); | ||
| } // else : comp > 0 (a != 2 AND a </<= 1 -> a </<= 1), or == 0 && bc i.of "<" (a != 2 AND a < 2 -> a < 2) | ||
| return true; | ||
| } // else: comp < 0 : a != 2 AND a </<= 3 -> nop | ||
| } // else: non-comparable, nop | ||
| } else if (bc instanceof GreaterThan || bc instanceof GreaterThanOrEqual) { | ||
| comp = bc.right().foldable() ? BinaryComparison.compare(neqVal, bc.right().fold()) : null; | ||
| if (comp != null) { | ||
| if (comp <= 0) { | ||
| if (comp == 0 && bc instanceof GreaterThanOrEqual) { // a != 2 AND a >= 2 -> a > 2 | ||
| bcs.set(i, new GreaterThan(bc.source(), bc.left(), bc.right(), bc.zoneId())); | ||
| } // else: comp < 0 (a != 2 AND a >/>= 3 -> a >/>= 3), or == 0 && bc i.of ">" (a != 2 AND a > 2 -> a > 2) | ||
| return true; | ||
| } // else: comp > 0 : a != 2 AND a >/>= 1 -> nop | ||
| } // else: non-comparable, nop | ||
| } // else: other non-relevant type | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.