Skip to content

Commit d019b65

Browse files
Remove folding from CountDistinct
1 parent 2125697 commit d019b65

File tree

2 files changed

+45
-4
lines changed
  • x-pack/plugin

2 files changed

+45
-4
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/CountDistinct.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.compute.aggregation.CountDistinctLongAggregatorFunctionSupplier;
2020
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
2121
import org.elasticsearch.xpack.esql.core.expression.Expression;
22-
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
2322
import org.elasticsearch.xpack.esql.core.expression.Literal;
2423
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
2524
import org.elasticsearch.xpack.esql.core.tree.Source;
@@ -43,6 +42,7 @@
4342
import java.util.Map;
4443
import java.util.function.Function;
4544

45+
import static org.elasticsearch.core.Strings.format;
4646
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT;
4747
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
4848
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isFoldable;
@@ -210,16 +210,23 @@ protected TypeResolution resolveType() {
210210
@Override
211211
public AggregatorFunctionSupplier supplier() {
212212
DataType type = field().dataType();
213-
int precision = this.precision == null
214-
? DEFAULT_PRECISION
215-
: ((Number) this.precision.fold(FoldContext.small() /* TODO remove me */)).intValue();
213+
int precision = this.precision == null ? DEFAULT_PRECISION : precisionValue();
216214
if (SUPPLIERS.containsKey(type) == false) {
217215
// If the type checking did its job, this should never happen
218216
throw EsqlIllegalArgumentException.illegalDataType(type);
219217
}
220218
return SUPPLIERS.get(type).apply(precision);
221219
}
222220

221+
private int precisionValue() {
222+
if (precision instanceof Literal literal) {
223+
return ((Number) literal.value()).intValue();
224+
}
225+
throw new EsqlIllegalArgumentException(
226+
format(null, "Precision value must be a constant integer in [{}], found [{}]", source(), precision)
227+
);
228+
}
229+
223230
@Override
224231
public Expression surrogate() {
225232
var s = source();

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/230_folding.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,37 @@ Percentile with non-foldable value:
581581
FROM employees | STATS p = PERCENTILE(salary, salary + 1) | LIMIT 1
582582
- match: { error.type: "verification_exception" }
583583
- contains: { error.reason: "second argument of [PERCENTILE(salary, salary + 1)] must be a constant, received [salary + 1]" }
584+
585+
---
586+
COUNT_DISTINCT with foldable precision:
587+
- do:
588+
esql.query:
589+
body:
590+
query: |
591+
FROM employees
592+
| STATS c1 = COUNT_DISTINCT(salary, 234 + 543) | LIMIT 1
593+
- match: { columns.0.name: "c1" }
594+
- length: { values: 1 }
595+
596+
---
597+
COUNT_DISTINCT with no precision:
598+
- do:
599+
esql.query:
600+
body:
601+
query: |
602+
FROM employees
603+
| STATS c1 = COUNT_DISTINCT(salary) | LIMIT 1
604+
- match: { columns.0.name: "c1" }
605+
- length: { values: 1 }
606+
607+
---
608+
COUNT_DISTINCT with non-foldable precision:
609+
- do:
610+
catch: bad_request
611+
esql.query:
612+
body:
613+
query: |
614+
FROM employees
615+
| STATS COUNT_DISTINCT(salary, salary + 1) | LIMIT 1
616+
- match: { error.type: "verification_exception" }
617+
- contains: { error.reason: "second argument of [COUNT_DISTINCT(salary, salary + 1)] must be a constant, received [salary + 1]" }

0 commit comments

Comments
 (0)