|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.expression.function.grouping; |
| 9 | + |
| 10 | +import org.elasticsearch.common.Rounding; |
| 11 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 14 | +import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator; |
| 15 | +import org.elasticsearch.xpack.esql.capabilities.PostOptimizationVerificationAware; |
| 16 | +import org.elasticsearch.xpack.esql.common.Failures; |
| 17 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 18 | +import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
| 19 | +import org.elasticsearch.xpack.esql.core.tree.NodeInfo; |
| 20 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 21 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 22 | +import org.elasticsearch.xpack.esql.expression.LocalSurrogateExpression; |
| 23 | +import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; |
| 24 | +import org.elasticsearch.xpack.esql.expression.function.FunctionType; |
| 25 | +import org.elasticsearch.xpack.esql.expression.function.Param; |
| 26 | +import org.elasticsearch.xpack.esql.expression.function.TwoOptionalArguments; |
| 27 | +import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTrunc; |
| 28 | +import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput; |
| 29 | +import org.elasticsearch.xpack.esql.stats.SearchStats; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND; |
| 35 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; |
| 36 | +import static org.elasticsearch.xpack.esql.expression.Validations.isFoldable; |
| 37 | +import static org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTrunc.maybeSubstituteWithRoundTo; |
| 38 | +import static org.elasticsearch.xpack.esql.session.Configuration.DEFAULT_TZ; |
| 39 | + |
| 40 | +/** |
| 41 | + * Splits dates and numbers into a given number of buckets. There are two ways to invoke |
| 42 | + * this function: with a user-provided span (explicit invocation mode), or a span derived |
| 43 | + * from a number of desired buckets (as a hint) and a range (auto mode). |
| 44 | + * In the former case, two parameters will be provided, in the latter four. |
| 45 | + */ |
| 46 | +public class TBucket extends GroupingFunction.EvaluatableGroupingFunction |
| 47 | + implements |
| 48 | + PostOptimizationVerificationAware, |
| 49 | + TwoOptionalArguments, |
| 50 | + LocalSurrogateExpression { |
| 51 | + |
| 52 | + public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "TBucket", TBucket::new); |
| 53 | + |
| 54 | + private final Expression field; |
| 55 | + private final Expression buckets; |
| 56 | + |
| 57 | + @FunctionInfo( |
| 58 | + returnType = { "double", "date", "date_nanos" }, |
| 59 | + description = """ |
| 60 | + Creates groups of values - buckets - out of a datetime or numeric input. |
| 61 | + The size of the buckets can either be provided directly, or chosen based on a recommended count and values range.""", |
| 62 | + examples = {}, |
| 63 | + type = FunctionType.GROUPING |
| 64 | + ) |
| 65 | + public TBucket( |
| 66 | + Source source, |
| 67 | + @Param( |
| 68 | + name = "field", |
| 69 | + type = { "integer", "long", "double", "date", "date_nanos" }, |
| 70 | + description = "Numeric or date expression from which to derive buckets." |
| 71 | + ) Expression field, |
| 72 | + @Param( |
| 73 | + name = "buckets", |
| 74 | + type = { "integer", "long", "double", "date_period", "time_duration" }, |
| 75 | + description = "Target number of buckets, or desired bucket size if `from` and `to` parameters are omitted." |
| 76 | + ) Expression buckets |
| 77 | + ) { |
| 78 | + super(source, List.of(field, buckets)); |
| 79 | + this.field = field; |
| 80 | + this.buckets = buckets; |
| 81 | + } |
| 82 | + |
| 83 | + private TBucket(StreamInput in) throws IOException { |
| 84 | + this(Source.readFrom((PlanStreamInput) in), in.readNamedWriteable(Expression.class), in.readNamedWriteable(Expression.class)); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void writeTo(StreamOutput out) throws IOException { |
| 89 | + source().writeTo(out); |
| 90 | + out.writeNamedWriteable(field); |
| 91 | + out.writeNamedWriteable(buckets); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getWriteableName() { |
| 96 | + return ENTRY.name; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) { |
| 101 | + Rounding.Prepared preparedRounding = getDateRounding(toEvaluator.foldCtx(), null, null); |
| 102 | + return DateTrunc.evaluator(field.dataType(), source(), toEvaluator.apply(field), preparedRounding); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns the date rounding from this bucket function if the target field is a date type; otherwise, returns null. |
| 107 | + */ |
| 108 | + public Rounding.Prepared getDateRoundingOrNull(FoldContext foldCtx) { |
| 109 | + return getDateRounding(foldCtx, null, null); |
| 110 | + } |
| 111 | + |
| 112 | + private Rounding.Prepared getDateRounding(FoldContext foldContext, Long min, Long max) { |
| 113 | + assert DataType.isTemporalAmount(buckets.dataType()) : "Unexpected span data type [" + buckets.dataType() + "]"; |
| 114 | + return DateTrunc.createRounding(buckets.fold(foldContext), DEFAULT_TZ, min, max); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected TypeResolution resolveType() { |
| 119 | + if (childrenResolved() == false) { |
| 120 | + return new TypeResolution("Unresolved children"); |
| 121 | + } |
| 122 | + return isType(buckets, DataType::isTemporalAmount, sourceText(), SECOND, "date_period", "time_duration"); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void postOptimizationVerification(Failures failures) { |
| 127 | + String operation = sourceText(); |
| 128 | + failures.add(isFoldable(buckets, operation, SECOND)); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public DataType dataType() { |
| 133 | + return field.dataType(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Expression replaceChildren(List<Expression> newChildren) { |
| 138 | + return new TBucket(source(), newChildren.get(0), newChildren.get(1)); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + protected NodeInfo<? extends Expression> info() { |
| 143 | + return NodeInfo.create(this, TBucket::new, field, buckets); |
| 144 | + } |
| 145 | + |
| 146 | + public Expression field() { |
| 147 | + return field; |
| 148 | + } |
| 149 | + |
| 150 | + public Expression buckets() { |
| 151 | + return buckets; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public String toString() { |
| 156 | + return "Bucket{" + "field=" + field + ", buckets=" + buckets + "}"; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public Expression surrogate(SearchStats searchStats) { |
| 161 | + // LocalSubstituteSurrogateExpressions should make sure this doesn't happen |
| 162 | + assert searchStats != null : "SearchStats cannot be null"; |
| 163 | + return maybeSubstituteWithRoundTo( |
| 164 | + source(), |
| 165 | + field(), |
| 166 | + buckets(), |
| 167 | + searchStats, |
| 168 | + (interval, minValue, maxValue) -> getDateRounding(FoldContext.small(), minValue, maxValue) |
| 169 | + ); |
| 170 | + } |
| 171 | +} |
0 commit comments