|
@@ -19,6 +19,8 @@ import org.elasticsearch.xpack.ql.expression.TypeResolutions;
|
|
import org.elasticsearch.xpack.ql.expression.function.scalar.BinaryScalarFunction;
|
|
import org.elasticsearch.xpack.ql.expression.function.scalar.BinaryScalarFunction;
|
|
import org.elasticsearch.xpack.ql.tree.NodeInfo;
|
|
import org.elasticsearch.xpack.ql.tree.NodeInfo;
|
|
import org.elasticsearch.xpack.ql.tree.Source;
|
|
import org.elasticsearch.xpack.ql.tree.Source;
|
|
|
|
+import org.elasticsearch.xpack.ql.type.DataType;
|
|
|
|
+import org.elasticsearch.xpack.ql.type.DataTypes;
|
|
|
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
import java.time.Period;
|
|
import java.time.Period;
|
|
@@ -27,6 +29,7 @@ import java.util.concurrent.TimeUnit;
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
+import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
|
|
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST;
|
|
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST;
|
|
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.SECOND;
|
|
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.SECOND;
|
|
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isDate;
|
|
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isDate;
|
|
@@ -34,8 +37,8 @@ import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType;
|
|
|
|
|
|
public class DateTrunc extends BinaryDateTimeFunction implements EvaluatorMapper {
|
|
public class DateTrunc extends BinaryDateTimeFunction implements EvaluatorMapper {
|
|
|
|
|
|
- public DateTrunc(Source source, Expression field, Expression interval) {
|
|
|
|
- super(source, field, interval);
|
|
|
|
|
|
+ public DateTrunc(Source source, Expression interval, Expression field) {
|
|
|
|
+ super(source, interval, field);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -44,7 +47,12 @@ public class DateTrunc extends BinaryDateTimeFunction implements EvaluatorMapper
|
|
return new TypeResolution("Unresolved children");
|
|
return new TypeResolution("Unresolved children");
|
|
}
|
|
}
|
|
|
|
|
|
- TypeResolution resolution = isDate(timestampField(), sourceText(), FIRST);
|
|
|
|
|
|
+ TypeResolution resolution = argumentTypesAreSwapped();
|
|
|
|
+ if (resolution.unresolved()) {
|
|
|
|
+ return resolution;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resolution = isDate(timestampField(), sourceText(), FIRST);
|
|
if (resolution.unresolved()) {
|
|
if (resolution.unresolved()) {
|
|
return resolution;
|
|
return resolution;
|
|
}
|
|
}
|
|
@@ -52,6 +60,16 @@ public class DateTrunc extends BinaryDateTimeFunction implements EvaluatorMapper
|
|
return isInterval(interval(), sourceText(), SECOND);
|
|
return isInterval(interval(), sourceText(), SECOND);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // TODO: drop check once 8.11 is released
|
|
|
|
+ private TypeResolution argumentTypesAreSwapped() {
|
|
|
|
+ DataType leftType = left().dataType();
|
|
|
|
+ DataType rightType = right().dataType();
|
|
|
|
+ if (leftType == DataTypes.DATETIME && (rightType == EsqlDataTypes.DATE_PERIOD || rightType == EsqlDataTypes.TIME_DURATION)) {
|
|
|
|
+ return new TypeResolution(format(null, "function definition has been updated, please swap arguments in [{}]", sourceText()));
|
|
|
|
+ }
|
|
|
|
+ return TypeResolution.TYPE_RESOLVED;
|
|
|
|
+ }
|
|
|
|
+
|
|
private static TypeResolution isInterval(Expression e, String operationName, TypeResolutions.ParamOrdinal paramOrd) {
|
|
private static TypeResolution isInterval(Expression e, String operationName, TypeResolutions.ParamOrdinal paramOrd) {
|
|
return isType(
|
|
return isType(
|
|
e,
|
|
e,
|
|
@@ -80,11 +98,11 @@ public class DateTrunc extends BinaryDateTimeFunction implements EvaluatorMapper
|
|
|
|
|
|
@Override
|
|
@Override
|
|
protected NodeInfo<? extends Expression> info() {
|
|
protected NodeInfo<? extends Expression> info() {
|
|
- return NodeInfo.create(this, DateTrunc::new, timestampField(), interval());
|
|
|
|
|
|
+ return NodeInfo.create(this, DateTrunc::new, interval(), timestampField());
|
|
}
|
|
}
|
|
|
|
|
|
public Expression interval() {
|
|
public Expression interval() {
|
|
- return right();
|
|
|
|
|
|
+ return left();
|
|
}
|
|
}
|
|
|
|
|
|
static Rounding.Prepared createRounding(final Object interval) {
|
|
static Rounding.Prepared createRounding(final Object interval) {
|