|
17 | 17 | import org.elasticsearch.compute.aggregation.SampleIntAggregatorFunctionSupplier; |
18 | 18 | import org.elasticsearch.compute.aggregation.SampleLongAggregatorFunctionSupplier; |
19 | 19 | import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException; |
| 20 | +import org.elasticsearch.xpack.esql.capabilities.PostOptimizationVerificationAware; |
| 21 | +import org.elasticsearch.xpack.esql.common.Failures; |
20 | 22 | import org.elasticsearch.xpack.esql.core.expression.Expression; |
21 | | -import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
22 | 23 | import org.elasticsearch.xpack.esql.core.expression.Literal; |
23 | 24 | import org.elasticsearch.xpack.esql.core.tree.NodeInfo; |
24 | 25 | import org.elasticsearch.xpack.esql.core.tree.Source; |
25 | 26 | import org.elasticsearch.xpack.esql.core.type.DataType; |
26 | 27 | import org.elasticsearch.xpack.esql.expression.function.Example; |
27 | 28 | import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; |
28 | 29 | import org.elasticsearch.xpack.esql.expression.function.FunctionType; |
| 30 | +import org.elasticsearch.xpack.esql.expression.function.FunctionUtils; |
29 | 31 | import org.elasticsearch.xpack.esql.expression.function.Param; |
30 | 32 | import org.elasticsearch.xpack.esql.planner.PlannerUtils; |
31 | 33 | import org.elasticsearch.xpack.esql.planner.ToAggregator; |
32 | 34 |
|
33 | 35 | import java.io.IOException; |
34 | 36 | import java.util.List; |
35 | 37 |
|
36 | | -import static org.elasticsearch.common.logging.LoggerMessageFormat.format; |
37 | 38 | import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST; |
38 | 39 | import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND; |
39 | | -import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNullAndFoldable; |
| 40 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNull; |
40 | 41 | import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; |
| 42 | +import static org.elasticsearch.xpack.esql.expression.function.FunctionUtils.postOptimizationVerificationLimit; |
| 43 | +import static org.elasticsearch.xpack.esql.expression.function.FunctionUtils.resolveTypeLimit; |
41 | 44 |
|
42 | | -public class Sample extends AggregateFunction implements ToAggregator { |
| 45 | +public class Sample extends AggregateFunction implements ToAggregator, PostOptimizationVerificationAware { |
43 | 46 | public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Sample", Sample::new); |
44 | 47 |
|
45 | 48 | @FunctionInfo( |
@@ -110,14 +113,14 @@ protected TypeResolution resolveType() { |
110 | 113 | return new TypeResolution("Unresolved children"); |
111 | 114 | } |
112 | 115 | var typeResolution = isType(field(), dt -> dt != DataType.UNSIGNED_LONG, sourceText(), FIRST, "any type except unsigned_long").and( |
113 | | - isNotNullAndFoldable(limitField(), sourceText(), SECOND) |
| 116 | + isNotNull(limitField(), sourceText(), SECOND) |
114 | 117 | ).and(isType(limitField(), dt -> dt == DataType.INTEGER, sourceText(), SECOND, "integer")); |
115 | 118 | if (typeResolution.unresolved()) { |
116 | 119 | return typeResolution; |
117 | 120 | } |
118 | | - int limit = limitValue(); |
119 | | - if (limit <= 0) { |
120 | | - return new TypeResolution(format(null, "Limit must be greater than 0 in [{}], found [{}]", sourceText(), limit)); |
| 121 | + TypeResolution result = resolveTypeLimit(limitField(), sourceText()); |
| 122 | + if (result.equals(TypeResolution.TYPE_RESOLVED) == false) { |
| 123 | + return result; |
121 | 124 | } |
122 | 125 | return TypeResolution.TYPE_RESOLVED; |
123 | 126 | } |
@@ -164,11 +167,15 @@ Expression limitField() { |
164 | 167 | } |
165 | 168 |
|
166 | 169 | private int limitValue() { |
167 | | - return (int) limitField().fold(FoldContext.small() /* TODO remove me */); |
| 170 | + return FunctionUtils.limitValue(limitField(), sourceText()); |
168 | 171 | } |
169 | 172 |
|
170 | 173 | Expression uuid() { |
171 | 174 | return parameters().get(1); |
172 | 175 | } |
173 | 176 |
|
| 177 | + @Override |
| 178 | + public void postOptimizationVerification(Failures failures) { |
| 179 | + postOptimizationVerificationLimit(failures, limitField(), sourceText()); |
| 180 | + } |
174 | 181 | } |
0 commit comments