|
@@ -0,0 +1,96 @@
|
|
|
+/*
|
|
|
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
+ * or more contributor license agreements. Licensed under the Elastic License
|
|
|
+ * 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
+ * 2.0.
|
|
|
+ */
|
|
|
+
|
|
|
+package org.elasticsearch.xpack.esql.expression.function.scalar.math;
|
|
|
+
|
|
|
+import org.elasticsearch.compute.ann.Evaluator;
|
|
|
+import org.elasticsearch.compute.operator.EvalOperator;
|
|
|
+import org.elasticsearch.xpack.esql.expression.function.scalar.UnaryScalarFunction;
|
|
|
+import org.elasticsearch.xpack.esql.planner.Mappable;
|
|
|
+import org.elasticsearch.xpack.ql.expression.Expression;
|
|
|
+import org.elasticsearch.xpack.ql.tree.NodeInfo;
|
|
|
+import org.elasticsearch.xpack.ql.tree.Source;
|
|
|
+import org.elasticsearch.xpack.ql.type.DataType;
|
|
|
+import org.elasticsearch.xpack.ql.type.DataTypes;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.DEFAULT;
|
|
|
+import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isNumeric;
|
|
|
+
|
|
|
+public class Sqrt extends UnaryScalarFunction implements Mappable {
|
|
|
+ public Sqrt(Source source, Expression field) {
|
|
|
+ super(source, field);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Supplier<EvalOperator.ExpressionEvaluator> toEvaluator(
|
|
|
+ Function<Expression, Supplier<EvalOperator.ExpressionEvaluator>> toEvaluator
|
|
|
+ ) {
|
|
|
+ Supplier<EvalOperator.ExpressionEvaluator> field = toEvaluator.apply(field());
|
|
|
+ var fieldType = field().dataType();
|
|
|
+ var eval = field.get();
|
|
|
+
|
|
|
+ if (fieldType == DataTypes.DOUBLE) {
|
|
|
+ return () -> new SqrtDoubleEvaluator(eval);
|
|
|
+ }
|
|
|
+ if (fieldType == DataTypes.INTEGER) {
|
|
|
+ return () -> new SqrtIntEvaluator(eval);
|
|
|
+ }
|
|
|
+ if (fieldType == DataTypes.LONG) {
|
|
|
+ return () -> new SqrtLongEvaluator(eval);
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new UnsupportedOperationException("Unsupported type " + fieldType);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Evaluator(extraName = "Double")
|
|
|
+ static double process(double val) {
|
|
|
+ return Math.sqrt(val);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Evaluator(extraName = "Long")
|
|
|
+ static double process(long val) {
|
|
|
+ return Math.sqrt(val);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Evaluator(extraName = "Int")
|
|
|
+ static double process(int val) {
|
|
|
+ return Math.sqrt(val);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public final Expression replaceChildren(List<Expression> newChildren) {
|
|
|
+ return new Sqrt(source(), newChildren.get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected NodeInfo<? extends Expression> info() {
|
|
|
+ return NodeInfo.create(this, Sqrt::new, field());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DataType dataType() {
|
|
|
+ return DataTypes.DOUBLE;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object fold() {
|
|
|
+ return Mappable.super.fold();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected TypeResolution resolveType() {
|
|
|
+ if (childrenResolved() == false) {
|
|
|
+ return new TypeResolution("Unresolved children");
|
|
|
+ }
|
|
|
+
|
|
|
+ return isNumeric(field, sourceText(), DEFAULT);
|
|
|
+ }
|
|
|
+}
|