Skip to content

Commit 940b6f4

Browse files
committed
type checks
1 parent 635e8dd commit 940b6f4

File tree

1 file changed

+16
-1
lines changed
  • x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate

1 file changed

+16
-1
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
import java.util.List;
3535

3636
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
37+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
38+
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.isType;
3741

3842
public class Sample extends AggregateFunction implements ToAggregator {
3943
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Sample", Sample::new);
@@ -105,7 +109,18 @@ protected TypeResolution resolveType() {
105109
if (childrenResolved() == false) {
106110
return new TypeResolution("Unresolved children");
107111
}
108-
int limit = (int) limitField().fold(FoldContext.small());
112+
var typeResolution = isType(
113+
field(),
114+
dt -> dt != DataType.UNSIGNED_LONG,
115+
sourceText(),
116+
FIRST,
117+
"any type except unsigned_long"
118+
).and(isNotNullAndFoldable(limitField(), sourceText(), SECOND))
119+
.and(isType(limitField(), dt -> dt == DataType.INTEGER, sourceText(), SECOND, "integer"));
120+
if (typeResolution.unresolved()) {
121+
return typeResolution;
122+
}
123+
int limit = limitValue();
109124
if (limit <= 0) {
110125
return new TypeResolution(format(null, "Limit must be greater than 0 in [{}], found [{}]", sourceText(), limit));
111126
}

0 commit comments

Comments
 (0)