|
@@ -8,6 +8,7 @@ package org.elasticsearch.xpack.sql.expression;
|
|
|
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
|
|
|
import org.elasticsearch.xpack.sql.expression.Expression.TypeResolution;
|
|
|
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
|
|
|
+import org.elasticsearch.xpack.sql.type.DataType;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
@@ -16,15 +17,10 @@ import java.util.function.Predicate;
|
|
|
|
|
|
import static java.util.Collections.emptyList;
|
|
|
import static java.util.Collections.emptyMap;
|
|
|
-import static java.util.stream.Collectors.toList;
|
|
|
|
|
|
-public abstract class Expressions {
|
|
|
+public final class Expressions {
|
|
|
|
|
|
- public static List<NamedExpression> asNamed(List<? extends Expression> exp) {
|
|
|
- return exp.stream()
|
|
|
- .map(NamedExpression.class::cast)
|
|
|
- .collect(toList());
|
|
|
- }
|
|
|
+ private Expressions() {}
|
|
|
|
|
|
public static NamedExpression wrapAsNamed(Expression exp) {
|
|
|
return exp instanceof NamedExpression ? (NamedExpression) exp : new Alias(exp.location(), exp.nodeName(), exp);
|
|
@@ -126,7 +122,16 @@ public abstract class Expressions {
|
|
|
}
|
|
|
|
|
|
public static TypeResolution typeMustBeNumeric(Expression e) {
|
|
|
- return e.dataType().isNumeric()? TypeResolution.TYPE_RESOLVED : new TypeResolution(
|
|
|
- "Argument required to be numeric ('" + Expressions.name(e) + "' of type '" + e.dataType().esType + "')");
|
|
|
+ return e.dataType().isNumeric() ? TypeResolution.TYPE_RESOLVED : new TypeResolution(numericErrorMessage(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static TypeResolution typeMustBeNumericOrDate(Expression e) {
|
|
|
+ return e.dataType().isNumeric() || e.dataType() == DataType.DATE ?
|
|
|
+ TypeResolution.TYPE_RESOLVED :
|
|
|
+ new TypeResolution(numericErrorMessage(e));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String numericErrorMessage(Expression e) {
|
|
|
+ return "Argument required to be numeric ('" + Expressions.name(e) + "' of type '" + e.dataType().esType + "')";
|
|
|
}
|
|
|
}
|