-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ES|QL] Consider min/max from predicates when transform date_trunc/bucket to round_to #131341
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
Closed
fang-xing-esql
wants to merge
11
commits into
elastic:main
from
fang-xing-esql:datetrunc-roundto-with-min-max-from-predicates
Closed
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
caa2f73
consider min/max from predicates when transform date_trunc/bucket int…
fang-xing-esql 93c3297
Update docs/changelog/131341.yaml
fang-xing-esql 3563dcb
more tests
fang-xing-esql 2ed82f4
Merge branch 'main' into datetrunc-roundto-with-min-max-from-predicates
fang-xing-esql de99e19
refactor
fang-xing-esql 41a493d
Merge branch 'main' into datetrunc-roundto-with-min-max-from-predicates
fang-xing-esql 5ba54a5
add more comments
fang-xing-esql def5d8d
refactor tests
fang-xing-esql 9f66fa6
Merge branch 'main' into datetrunc-roundto-with-min-max-from-predicates
fang-xing-esql edcde73
update according to review comments
fang-xing-esql 6c296d0
Merge branch 'main' into datetrunc-roundto-with-min-max-from-predicates
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,5 @@ | ||
| pr: 131341 | ||
| summary: Consider min/max from predicates when transform date_trunc/bucket to `round_to` | ||
| area: ES|QL | ||
| type: enhancement | ||
| issues: [] |
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
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| import org.elasticsearch.compute.ann.Fixed; | ||
| import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.core.Tuple; | ||
| import org.elasticsearch.logging.LogManager; | ||
| import org.elasticsearch.logging.Logger; | ||
| import org.elasticsearch.xpack.esql.core.expression.Expression; | ||
|
|
@@ -27,12 +28,19 @@ | |
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.core.type.MultiTypeEsField; | ||
| import org.elasticsearch.xpack.esql.core.util.Holder; | ||
| import org.elasticsearch.xpack.esql.expression.LocalSurrogateExpression; | ||
| import org.elasticsearch.xpack.esql.expression.function.Example; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; | ||
| import org.elasticsearch.xpack.esql.expression.function.Param; | ||
| import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction; | ||
| import org.elasticsearch.xpack.esql.expression.function.scalar.math.RoundTo; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals; | ||
| import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.EsqlBinaryComparison; | ||
| 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.io.stream.PlanStreamInput; | ||
| import org.elasticsearch.xpack.esql.stats.SearchStats; | ||
|
|
||
|
|
@@ -125,7 +133,7 @@ Expression interval() { | |
| return interval; | ||
| } | ||
|
|
||
| Expression field() { | ||
| public Expression field() { | ||
| return timestampField; | ||
| } | ||
|
|
||
|
|
@@ -287,14 +295,15 @@ public static ExpressionEvaluator.Factory evaluator( | |
| } | ||
|
|
||
| @Override | ||
| public Expression surrogate(SearchStats searchStats) { | ||
| public Expression surrogate(SearchStats searchStats, List<EsqlBinaryComparison> binaryComparisons) { | ||
| // LocalSubstituteSurrogateExpressions should make sure this doesn't happen | ||
| assert searchStats != null : "SearchStats cannot be null"; | ||
| return maybeSubstituteWithRoundTo( | ||
| source(), | ||
| field(), | ||
| interval(), | ||
| searchStats, | ||
| binaryComparisons, | ||
| (interval, minValue, maxValue) -> createRounding(interval, DEFAULT_TZ, minValue, maxValue) | ||
| ); | ||
| } | ||
|
|
@@ -304,6 +313,7 @@ public static RoundTo maybeSubstituteWithRoundTo( | |
| Expression field, | ||
| Expression foldableTimeExpression, | ||
| SearchStats searchStats, | ||
| List<EsqlBinaryComparison> binaryComparisons, | ||
| TriFunction<Object, Long, Long, Rounding.Prepared> roundingFunction | ||
| ) { | ||
| if (field instanceof FieldAttribute fa && fa.field() instanceof MultiTypeEsField == false && isDateTime(fa.dataType())) { | ||
|
|
@@ -312,8 +322,19 @@ public static RoundTo maybeSubstituteWithRoundTo( | |
| FieldAttribute.FieldName fieldName = fa.fieldName(); | ||
| var min = searchStats.min(fieldName); | ||
| var max = searchStats.max(fieldName); | ||
| // Extract min/max from query | ||
| Tuple<Long, Long> minMaxFromPredicates = minMaxFromPredicates(binaryComparisons); | ||
| Long minFromPredicates = minMaxFromPredicates.v1(); | ||
| Long maxFromPredicates = minMaxFromPredicates.v2(); | ||
| // Consolidate min/max from SearchStats and query | ||
| if (minFromPredicates instanceof Long minValue) { | ||
| min = min instanceof Long m ? Math.max(m, minValue) : minValue; | ||
| } | ||
| if (maxFromPredicates instanceof Long maxValue) { | ||
| max = max instanceof Long m ? Math.min(m, maxValue) : maxValue; | ||
| } | ||
| // If min/max is available create rounding with them | ||
| if (min instanceof Long minValue && max instanceof Long maxValue && foldableTimeExpression.foldable()) { | ||
| if (min instanceof Long minValue && max instanceof Long maxValue && foldableTimeExpression.foldable() && minValue <= maxValue) { | ||
| Object foldedInterval = foldableTimeExpression.fold(FoldContext.small() /* TODO remove me */); | ||
| Rounding.Prepared rounding = roundingFunction.apply(foldedInterval, minValue, maxValue); | ||
| long[] roundingPoints = rounding.fixedRoundingPoints(); | ||
|
|
@@ -337,4 +358,31 @@ public static RoundTo maybeSubstituteWithRoundTo( | |
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static Tuple<Long, Long> minMaxFromPredicates(List<EsqlBinaryComparison> binaryComparisons) { | ||
| long[] min = new long[] { Long.MIN_VALUE }; | ||
| long[] max = new long[] { Long.MAX_VALUE }; | ||
| Holder<Boolean> foundMinValue = new Holder<>(false); | ||
| Holder<Boolean> foundMaxValue = new Holder<>(false); | ||
| for (EsqlBinaryComparison binaryComparison : binaryComparisons) { | ||
| if (binaryComparison.right() instanceof Literal l) { | ||
| long value = Long.parseLong(l.value().toString()); | ||
|
||
| if (binaryComparison instanceof Equals) { | ||
| return new Tuple<>(value, value); | ||
| } | ||
| if (binaryComparison instanceof GreaterThan || binaryComparison instanceof GreaterThanOrEqual) { | ||
| if (value >= min[0]) { | ||
| min[0] = value; | ||
| foundMinValue.set(true); | ||
| } | ||
| } else if (binaryComparison instanceof LessThan || binaryComparison instanceof LessThanOrEqual) { | ||
| if (value <= max[0]) { | ||
| max[0] = value; | ||
| foundMaxValue.set(true); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return new Tuple<>(foundMinValue.get() ? min[0] : null, foundMaxValue.get() ? max[0] : null); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value here should already be a
Long. Why theinstanceof?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a check for null, not
instanceofhere, I'll change it.