|
| 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.scalar.histogram; |
| 9 | + |
| 10 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 11 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 12 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 13 | +import org.elasticsearch.compute.ann.Evaluator; |
| 14 | +import org.elasticsearch.compute.data.DoubleBlock; |
| 15 | +import org.elasticsearch.compute.operator.EvalOperator; |
| 16 | +import org.elasticsearch.exponentialhistogram.ExponentialHistogram; |
| 17 | +import org.elasticsearch.exponentialhistogram.ExponentialHistogramQuantile; |
| 18 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 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.function.FunctionInfo; |
| 23 | +import org.elasticsearch.xpack.esql.expression.function.Param; |
| 24 | +import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction; |
| 25 | +import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast; |
| 26 | +import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT; |
| 32 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; |
| 33 | + |
| 34 | +/** |
| 35 | + * Extracts a percentile value from a single histogram value. |
| 36 | + * Note that this function is currently only intended for usage in surrogates and not available as a user-facing function. |
| 37 | + * Therefore, it is intentionally not registered in {@link org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry}. |
| 38 | + */ |
| 39 | +public class HistogramPercentile extends EsqlScalarFunction { |
| 40 | + |
| 41 | + public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry( |
| 42 | + Expression.class, |
| 43 | + "HistogramPercentile", |
| 44 | + HistogramPercentile::new |
| 45 | + ); |
| 46 | + |
| 47 | + private final Expression histogram; |
| 48 | + private final Expression percentile; |
| 49 | + |
| 50 | + @FunctionInfo(returnType = { "double" }) |
| 51 | + public HistogramPercentile( |
| 52 | + Source source, |
| 53 | + @Param(name = "histogram", type = { "exponential_histogram" }) Expression histogram, |
| 54 | + @Param(name = "percentile", type = { "double", "integer", "long", "unsigned_long" }) Expression percentile |
| 55 | + ) { |
| 56 | + super(source, List.of(histogram, percentile)); |
| 57 | + this.histogram = histogram; |
| 58 | + this.percentile = percentile; |
| 59 | + } |
| 60 | + |
| 61 | + private HistogramPercentile(StreamInput in) throws IOException { |
| 62 | + this(Source.readFrom((PlanStreamInput) in), in.readNamedWriteable(Expression.class), in.readNamedWriteable(Expression.class)); |
| 63 | + } |
| 64 | + |
| 65 | + Expression histogram() { |
| 66 | + return histogram; |
| 67 | + } |
| 68 | + |
| 69 | + Expression percentile() { |
| 70 | + return percentile; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected TypeResolution resolveType() { |
| 75 | + return isType(histogram, dt -> dt == DataType.EXPONENTIAL_HISTOGRAM, sourceText(), DEFAULT, "exponential_histogram").and( |
| 76 | + isType(percentile, DataType::isNumeric, sourceText(), DEFAULT, "numeric types") |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public DataType dataType() { |
| 82 | + return DataType.DOUBLE; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Expression replaceChildren(List<Expression> newChildren) { |
| 87 | + return new HistogramPercentile(source(), newChildren.get(0), newChildren.get(1)); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean foldable() { |
| 92 | + return histogram.foldable() && percentile.foldable(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected NodeInfo<? extends Expression> info() { |
| 97 | + return NodeInfo.create(this, HistogramPercentile::new, histogram, percentile); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String getWriteableName() { |
| 102 | + return ENTRY.name; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void writeTo(StreamOutput out) throws IOException { |
| 107 | + source().writeTo(out); |
| 108 | + out.writeNamedWriteable(histogram); |
| 109 | + out.writeNamedWriteable(percentile); |
| 110 | + } |
| 111 | + |
| 112 | + @Evaluator(warnExceptions = ArithmeticException.class) |
| 113 | + static void process(DoubleBlock.Builder resultBuilder, ExponentialHistogram value, double percentile) { |
| 114 | + if (percentile < 0.0 || percentile > 100.0) { |
| 115 | + throw new ArithmeticException("Percentile value must be in the range [0, 100], got: " + percentile); |
| 116 | + } |
| 117 | + double result = ExponentialHistogramQuantile.getQuantile(value, percentile / 100.0); |
| 118 | + if (Double.isNaN(result)) { // can happen if the histogram is empty |
| 119 | + resultBuilder.appendNull(); |
| 120 | + } else { |
| 121 | + resultBuilder.appendDouble(result); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) { |
| 127 | + var fieldEvaluator = toEvaluator.apply(histogram); |
| 128 | + var percentileEvaluator = Cast.cast(source(), percentile.dataType(), DataType.DOUBLE, toEvaluator.apply(percentile)); |
| 129 | + return new HistogramPercentileEvaluator.Factory(source(), fieldEvaluator, percentileEvaluator); |
| 130 | + } |
| 131 | +} |
0 commit comments