Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] Allow DateTime as the third and fourth inputs to auto_bucket #104547

Merged
merged 8 commits into from
Jan 25, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

package org.elasticsearch.xpack.esql.expression.function.scalar.math;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Rounding;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.mapper.DateFieldMapper;
Expand All @@ -20,6 +20,7 @@
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Div;
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.Mul;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Foldables;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.TypeResolutions;
import org.elasticsearch.xpack.ql.expression.function.scalar.ScalarFunction;
Expand All @@ -40,7 +41,6 @@
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isFoldable;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isInteger;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isNumeric;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isString;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType;

/**
Expand Down Expand Up @@ -115,8 +115,8 @@ public ExpressionEvaluator.Factory toEvaluator(Function<Expression, ExpressionEv
int b = ((Number) buckets.fold()).intValue();

if (field.dataType() == DataTypes.DATETIME) {
long f = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(((BytesRef) from.fold()).utf8ToString());
long t = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(((BytesRef) to.fold()).utf8ToString());
long f = convertToLong(from);
long t = convertToLong(to);
return DateTrunc.evaluator(
source(),
toEvaluator.apply(field),
Expand Down Expand Up @@ -182,7 +182,7 @@ protected TypeResolution resolveType() {
}

if (field.dataType() == DataTypes.DATETIME) {
return resolveType((e, o) -> isString(e, sourceText(), o));
return resolveType((e, o) -> isStringOrDate(e, sourceText(), o));
}
if (field.dataType().isNumeric()) {
return resolveType((e, o) -> isNumeric(e, sourceText(), o));
Expand Down Expand Up @@ -216,6 +216,24 @@ private TypeResolution resolveType(BiFunction<Expression, TypeResolutions.ParamO
return isFoldable(to, sourceText(), FOURTH);
}

public static TypeResolution isStringOrDate(Expression e, String operationName, TypeResolutions.ParamOrdinal paramOrd) {
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
return TypeResolutions.isType(
e,
exp -> { return DataTypes.isString(exp) || DataTypes.isDateTime(exp); },
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
operationName,
paramOrd,
"datetime",
"string"
);
}

private long convertToLong(Expression e) {
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
Object value = Foldables.valueOf(e);
return DataTypes.isDateTime(e.dataType())
? ((Number) value).longValue()
: DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(BytesRefs.toString(value));
}

@Override
public DataType dataType() {
if (field.dataType().isNumeric()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,22 @@ private Expression build(Source source, Expression arg) {
Literal from;
Literal to;
if (arg.dataType() == DataTypes.DATETIME) {
from = new Literal(Source.EMPTY, new BytesRef("2023-02-01T00:00:00.00Z"), DataTypes.KEYWORD);
to = new Literal(Source.EMPTY, new BytesRef("2023-03-01T00:00:00.00Z"), DataTypes.KEYWORD);
from = fromOrTo("2023-02-01T00:00:00.00Z");
to = fromOrTo("2023-03-01T09:00:00.00Z");
} else {
from = new Literal(Source.EMPTY, 0, DataTypes.DOUBLE);
to = new Literal(Source.EMPTY, 1000, DataTypes.DOUBLE);
}
return new AutoBucket(source, arg, new Literal(Source.EMPTY, 50, DataTypes.INTEGER), from, to);
}

private Literal fromOrTo(String date) {
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
if (randomBoolean()) {
fang-xing-esql marked this conversation as resolved.
Show resolved Hide resolved
return new Literal(Source.EMPTY, new BytesRef(date), randomBoolean() ? DataTypes.KEYWORD : DataTypes.TEXT);
}
return new Literal(Source.EMPTY, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(date), DataTypes.DATETIME);
}

@Override
protected DataType expectedType(List<DataType> argTypes) {
if (argTypes.get(0).isNumeric()) {
Expand Down