SQL: Internal refactoring of operators as functions (#34097)

Centralize and simplify the script generation between operators and functions which are currently decoupled. As part of this process most predicates (<, <=, etc...) were made ScalarFunction as their purpose and functionality is quite similar (see % and MOD functions).
Renamed ProcessDefinition to Pipe
Add ScriptWeaver as a mixin-in interface for script customization
Add logic for equals/lte/lt
Improve BinaryOperator/expression toString
Minimize duplication across string functions

Close #33975
This commit is contained in:
Costin Leau 2018-09-27 23:42:57 +03:00 committed by GitHub
parent a2c941806b
commit 15515a616e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
207 changed files with 3056 additions and 2743 deletions

View File

@ -29,7 +29,7 @@ import org.elasticsearch.xpack.sql.expression.function.FunctionRegistry;
import org.elasticsearch.xpack.sql.expression.function.Functions;
import org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.ArithmeticFunction;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.ArithmeticOperation;
import org.elasticsearch.xpack.sql.plan.TableIdentifier;
import org.elasticsearch.xpack.sql.plan.logical.Aggregate;
import org.elasticsearch.xpack.sql.plan.logical.EsRelation;
@ -1007,8 +1007,8 @@ public class Analyzer extends RuleExecutor<LogicalPlan> {
// BinaryOperations are ignored as they are pushed down to ES
// and casting (and thus Aliasing when folding) gets in the way
if (e instanceof ArithmeticFunction) {
ArithmeticFunction f = (ArithmeticFunction) e;
if (e instanceof ArithmeticOperation) {
ArithmeticOperation f = (ArithmeticOperation) e;
left = f.left();
right = f.right();
}

View File

@ -32,11 +32,11 @@ import org.elasticsearch.xpack.sql.execution.search.extractor.ConstantExtractor;
import org.elasticsearch.xpack.sql.execution.search.extractor.FieldHitExtractor;
import org.elasticsearch.xpack.sql.execution.search.extractor.HitExtractor;
import org.elasticsearch.xpack.sql.execution.search.extractor.MetricAggExtractor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.AggExtractorInput;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.AggPathInput;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.HitExtractorInput;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ReferenceInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AggExtractorInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AggPathInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.HitExtractorInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.ReferenceInput;
import org.elasticsearch.xpack.sql.querydsl.agg.Aggs;
import org.elasticsearch.xpack.sql.querydsl.container.ComputedRef;
import org.elasticsearch.xpack.sql.querydsl.container.GlobalCountRef;
@ -275,7 +275,7 @@ public class Querier {
}
if (ref instanceof ComputedRef) {
ProcessorDefinition proc = ((ComputedRef) ref).processor();
Pipe proc = ((ComputedRef) ref).processor();
// wrap only agg inputs
proc = proc.transformDown(l -> {
@ -351,7 +351,7 @@ public class Querier {
}
if (ref instanceof ComputedRef) {
ProcessorDefinition proc = ((ComputedRef) ref).processor();
Pipe proc = ((ComputedRef) ref).processor();
// collect hitNames
Set<String> hitNames = new LinkedHashSet<>();
proc = proc.transformDown(l -> {

View File

@ -9,8 +9,8 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;

View File

@ -5,16 +5,18 @@
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.EsField;
import static java.util.Collections.singletonList;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.singletonList;
/**
* An {@code Alias} is a {@code NamedExpression} that gets renamed to something else through the Alias.
*
@ -91,6 +93,11 @@ public class Alias extends NamedExpression {
return lazyAttribute;
}
@Override
public ScriptTemplate asScript() {
throw new SqlIllegalArgumentException("Encountered a bug; an alias should never be scripted");
}
private Attribute createAttribute() {
if (resolved()) {
Expression c = child();
@ -114,4 +121,4 @@ public class Alias extends NamedExpression {
public String toString() {
return child + " AS " + name() + "#" + id();
}
}
}

View File

@ -5,7 +5,10 @@
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.List;
import java.util.Objects;
@ -60,6 +63,11 @@ public abstract class Attribute extends NamedExpression {
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
}
@Override
public ScriptTemplate asScript() {
throw new SqlIllegalArgumentException("Encountered a bug - an attribute should never be scripted");
}
public String qualifier() {
return qualifier;
}
@ -103,6 +111,11 @@ public abstract class Attribute extends NamedExpression {
return id().hashCode();
}
@Override
protected NodeInfo<? extends Expression> info() {
return null;
}
@Override
public boolean semanticEquals(Expression other) {
return other instanceof Attribute ? id().equals(((Attribute) other).id()) : false;
@ -130,4 +143,4 @@ public abstract class Attribute extends NamedExpression {
}
protected abstract String label();
}
}

View File

@ -1,82 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.tree.Location;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public abstract class BinaryExpression extends Expression {
private final Expression left, right;
protected BinaryExpression(Location location, Expression left, Expression right) {
super(location, Arrays.asList(left, right));
this.left = left;
this.right = right;
}
@Override
public final BinaryExpression replaceChildren(List<Expression> newChildren) {
if (newChildren.size() != 2) {
throw new IllegalArgumentException("expected [2] children but received [" + newChildren.size() + "]");
}
return replaceChildren(newChildren.get(0), newChildren.get(1));
}
protected abstract BinaryExpression replaceChildren(Expression newLeft, Expression newRight);
public Expression left() {
return left;
}
public Expression right() {
return right;
}
@Override
public boolean foldable() {
return left.foldable() && right.foldable();
}
@Override
public boolean nullable() {
return left.nullable() || left.nullable();
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) {
return false;
}
BinaryExpression other = (BinaryExpression) obj;
return Objects.equals(left, other.left)
&& Objects.equals(right, other.right);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(left());
sb.append(" ");
sb.append(symbol());
sb.append(" ");
sb.append(right());
return sb.toString();
}
public abstract String symbol();
public abstract BinaryExpression swapLeftAndRight();
}

View File

@ -132,4 +132,4 @@ public abstract class Expression extends Node<Expression> implements Resolvable
public String toString() {
return nodeName() + "[" + propertiesToString(false) + "]";
}
}
}

View File

@ -5,7 +5,9 @@
*/
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 java.util.ArrayList;
import java.util.Collection;
@ -112,6 +114,13 @@ public abstract class Expressions {
return true;
}
public static Pipe pipe(Expression e) {
if (e instanceof NamedExpression) {
return ((NamedExpression) e).asPipe();
}
throw new SqlIllegalArgumentException("Cannot create pipe for {}", e);
}
public static TypeResolution typeMustBe(Expression e, Predicate<Expression> predicate, String message) {
return predicate.test(e) ? TypeResolution.TYPE_RESOLVED : new TypeResolution(message);
}

View File

@ -6,6 +6,8 @@
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.gen.script.Params;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -77,6 +79,11 @@ public class Literal extends NamedExpression {
return new LiteralAttribute(location(), name(), null, false, id(), false, dataType, this);
}
@Override
public ScriptTemplate asScript() {
return new ScriptTemplate(String.valueOf(value), Params.EMPTY, dataType);
}
@Override
public Expression replaceChildren(List<Expression> newChildren) {
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
@ -87,9 +94,15 @@ public class Literal extends NamedExpression {
return AttributeSet.EMPTY;
}
@Override
protected Expression canonicalize() {
String s = String.valueOf(value);
return name().equals(s) ? this : Literal.of(location(), value);
}
@Override
public int hashCode() {
return Objects.hash(name(), value, dataType);
return Objects.hash(value, dataType);
}
@Override
@ -102,8 +115,7 @@ public class Literal extends NamedExpression {
}
Literal other = (Literal) obj;
return Objects.equals(name(), other.name())
&& Objects.equals(value, other.value)
return Objects.equals(value, other.value)
&& Objects.equals(dataType, other.dataType);
}

View File

@ -5,8 +5,7 @@
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ConstantInput;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -33,12 +32,13 @@ public class LiteralAttribute extends TypedAttribute {
return new LiteralAttribute(location, name, qualifier, nullable, id, synthetic, dataType(), literal);
}
public ProcessorDefinition asProcessorDefinition() {
return new ConstantInput(location(), literal, literal.value());
}
@Override
protected String label() {
return "c";
}
@Override
public Pipe asPipe() {
return literal.asPipe();
}
}

View File

@ -5,16 +5,26 @@
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AttributeInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.ConstantInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import java.util.List;
import java.util.Objects;
/**
* An expression that has a name. Named expressions can be used as a result
* (by converting to an attribute).
*/
public abstract class NamedExpression extends Expression {
private final String name;
private final ExpressionId id;
private final boolean synthetic;
private Pipe lazyPipe = null;
public NamedExpression(Location location, String name, List<Expression> children, ExpressionId id) {
this(location, name, children, id, false);
@ -41,6 +51,20 @@ public abstract class NamedExpression extends Expression {
public abstract Attribute toAttribute();
public Pipe asPipe() {
if (lazyPipe == null) {
lazyPipe = foldable() ? new ConstantInput(location(), this, fold()) : makePipe();
}
return lazyPipe;
}
protected Pipe makePipe() {
return new AttributeInput(location(), this, toAttribute());
}
public abstract ScriptTemplate asScript();
@Override
public int hashCode() {
return Objects.hash(id, name, synthetic);
@ -67,4 +91,4 @@ public abstract class NamedExpression extends Expression {
&& Objects.equals(name, other.name)
&& Objects.equals(children(), other.children());
}
}
}

View File

@ -7,30 +7,53 @@ package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import java.util.List;
import java.util.Objects;
public class Order extends UnaryExpression {
import static java.util.Collections.singletonList;
public class Order extends Expression {
public enum OrderDirection {
ASC, DESC
}
private final Expression child;
private final OrderDirection direction;
public Order(Location location, Expression child, OrderDirection direction) {
super(location, child);
super(location, singletonList(child));
this.child = child;
this.direction = direction;
}
@Override
protected NodeInfo<Order> info() {
return NodeInfo.create(this, Order::new, child(), direction);
return NodeInfo.create(this, Order::new, child, direction);
}
@Override
protected UnaryExpression replaceChild(Expression newChild) {
return new Order(location(), newChild, direction);
public boolean nullable() {
return false;
}
@Override
public DataType dataType() {
return child.dataType();
}
@Override
public Order replaceChildren(List<Expression> newChildren) {
if (newChildren.size() != 1) {
throw new IllegalArgumentException("expected [1] child but received [" + newChildren.size() + "]");
}
return new Order(location(), newChildren.get(0), direction);
}
public Expression child() {
return child;
}
public OrderDirection direction() {
@ -44,7 +67,7 @@ public class Order extends UnaryExpression {
@Override
public int hashCode() {
return Objects.hash(child(), direction);
return Objects.hash(child, direction);
}
@Override
@ -59,6 +82,6 @@ public class Order extends UnaryExpression {
Order other = (Order) obj;
return Objects.equals(direction, other.direction)
&& Objects.equals(child(), other.child());
&& Objects.equals(child, other.child);
}
}
}

View File

@ -5,13 +5,13 @@
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.sql.tree.Location;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.elasticsearch.xpack.sql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.sql.tree.Location;
public abstract class SubQueryExpression extends Expression {
private final LogicalPlan query;

View File

@ -5,21 +5,23 @@
*/
package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import java.util.List;
import java.util.Objects;
import static java.util.Collections.singletonList;
import java.util.List;
public abstract class UnaryExpression extends Expression {
public abstract class UnaryExpression extends NamedExpression {
private final Expression child;
protected UnaryExpression(Location location, Expression child) {
super(location, singletonList(child));
super(location, null, singletonList(child), null);
this.child = child;
}
@ -56,6 +58,21 @@ public abstract class UnaryExpression extends Expression {
return child.dataType();
}
@Override
public Attribute toAttribute() {
throw new SqlIllegalArgumentException("Not supported yet");
}
@Override
public ScriptTemplate asScript() {
throw new SqlIllegalArgumentException("Not supported yet");
}
@Override
protected Pipe makePipe() {
throw new SqlIllegalArgumentException("Not supported yet");
}
@Override
public int hashCode() {
return Objects.hash(child);

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.sql.expression;
import org.elasticsearch.xpack.sql.capabilities.Unresolvable;
import org.elasticsearch.xpack.sql.capabilities.UnresolvedException;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
@ -42,4 +43,9 @@ abstract class UnresolvedNamedExpression extends NamedExpression implements Unre
public Attribute toAttribute() {
throw new UnresolvedException("attribute", this);
}
@Override
public ScriptTemplate asScript() {
throw new UnresolvedException("script", this);
}
}

View File

@ -20,7 +20,6 @@ import org.elasticsearch.xpack.sql.expression.function.aggregate.StddevPop;
import org.elasticsearch.xpack.sql.expression.function.aggregate.Sum;
import org.elasticsearch.xpack.sql.expression.function.aggregate.SumOfSquares;
import org.elasticsearch.xpack.sql.expression.function.aggregate.VarPop;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.Mod;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayName;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayOfMonth;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayOfWeek;
@ -81,6 +80,7 @@ import org.elasticsearch.xpack.sql.expression.function.scalar.string.Right;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.Space;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.Substring;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.UCase;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Mod;
import org.elasticsearch.xpack.sql.parser.ParsingException;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.util.StringUtils;

View File

@ -5,17 +5,19 @@
*/
package org.elasticsearch.xpack.sql.expression.function;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.Function;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import static java.util.Collections.emptyList;
import java.util.List;
import static java.util.Collections.emptyList;
/**
* Function referring to the {@code _score} in a search. Only available
* in the search context, and only at the "root" so it can't be combined
@ -59,4 +61,14 @@ public class Score extends Function {
public int hashCode() {
return location().hashCode();
}
@Override
protected Pipe makePipe() {
throw new SqlIllegalArgumentException("Scoring cannot be computed on the client");
}
@Override
public ScriptTemplate asScript() {
throw new SqlIllegalArgumentException("Scoring cannot be scripted");
}
}

View File

@ -7,7 +7,8 @@ package org.elasticsearch.xpack.sql.expression.function;
import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.ExpressionId;
import org.elasticsearch.xpack.sql.expression.function.FunctionAttribute;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.ScorePipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -41,6 +42,11 @@ public class ScoreAttribute extends FunctionAttribute {
return new ScoreAttribute(location, name, dataType(), qualifier, nullable, id, synthetic);
}
@Override
protected Pipe makePipe() {
return new ScorePipe(location(), this);
}
@Override
protected String label() {
return "SCORE";

View File

@ -10,6 +10,7 @@ import org.elasticsearch.xpack.sql.capabilities.UnresolvedException;
import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -143,6 +144,11 @@ public class UnresolvedFunction extends Function implements Unresolvable {
throw new UnresolvedException("attribute", this);
}
@Override
public ScriptTemplate asScript() {
throw new UnresolvedException("script", this);
}
@Override
public String unresolvedMessage() {
return unresolvedMsg;

View File

@ -5,8 +5,12 @@
*/
package org.elasticsearch.xpack.sql.expression.function.aggregate;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.Function;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AggNameInput;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.util.CollectionUtils;
@ -53,6 +57,17 @@ public abstract class AggregateFunction extends Function {
return lazyAttribute;
}
@Override
protected Pipe makePipe() {
// unresolved AggNameInput (should always get replaced by the folder)
return new AggNameInput(location(), this, name());
}
@Override
public ScriptTemplate asScript() {
throw new SqlIllegalArgumentException("Aggregate functions cannot be scripted");
}
@Override
public boolean equals(Object obj) {
if (false == super.equals(obj)) {
@ -67,4 +82,4 @@ public abstract class AggregateFunction extends Function {
public int hashCode() {
return Objects.hash(field(), parameters());
}
}
}

View File

@ -6,7 +6,7 @@
package org.elasticsearch.xpack.sql.expression.function.scalar;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import java.util.Arrays;

View File

@ -6,12 +6,9 @@
package org.elasticsearch.xpack.sql.expression.function.scalar;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.Params;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -74,18 +71,8 @@ public class Cast extends UnaryScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(ScalarFunctionAttribute scalar) {
return scalar.script();
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(field.name(), Params.EMPTY, field.dataType());
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this, ProcessorDefinitions.toProcessorDefinition(field()),
protected Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()),
new CastProcessor(DataTypeConversion.conversionFor(from(), to())));
}
@ -118,4 +105,4 @@ public class Cast extends UnaryScalarFunction {
sb.insert(sb.length() - 1, " AS " + to().sqlName());
return sb.toString();
}
}
}

View File

@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.type.DataTypeConversion.Conversion;
import java.io.IOException;

View File

@ -7,18 +7,11 @@ package org.elasticsearch.xpack.sql.expression.function.scalar;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry.Entry;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.BinaryArithmeticProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.UnaryArithmeticProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.NamedDateTimeProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.QuarterProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.BucketExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.ChainingProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.ConstantProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringStringProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.ConcatFunctionProcessor;
@ -27,6 +20,14 @@ import org.elasticsearch.xpack.sql.expression.function.scalar.string.LocateFunct
import org.elasticsearch.xpack.sql.expression.function.scalar.string.ReplaceFunctionProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.SubstringFunctionProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.BucketExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.ChainingProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.ConstantProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.BinaryArithmeticProcessor;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.UnaryArithmeticProcessor;
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.BinaryComparisonProcessor;
import java.util.ArrayList;
import java.util.List;
@ -48,6 +49,9 @@ public final class Processors {
entries.add(new Entry(Processor.class, CastProcessor.NAME, CastProcessor::new));
entries.add(new Entry(Processor.class, ChainingProcessor.NAME, ChainingProcessor::new));
// comparators
entries.add(new Entry(Processor.class, BinaryComparisonProcessor.NAME, BinaryComparisonProcessor::new));
// arithmetic
entries.add(new Entry(Processor.class, BinaryArithmeticProcessor.NAME, BinaryArithmeticProcessor::new));
entries.add(new Entry(Processor.class, UnaryArithmeticProcessor.NAME, UnaryArithmeticProcessor::new));

View File

@ -5,23 +5,14 @@
*/
package org.elasticsearch.xpack.sql.expression.function.scalar;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.Function;
import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunctionAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.Params;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptWeaver;
import org.elasticsearch.xpack.sql.tree.Location;
import java.util.List;
import static java.util.Collections.emptyList;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
/**
* A {@code ScalarFunction} is a {@code Function} that takes values from some
@ -29,11 +20,9 @@ import static org.elasticsearch.xpack.sql.expression.function.scalar.script.Scri
* {@code ABS()}, which takes one value at a time, applies a function to the
* value (abs) and returns a new value.
*/
public abstract class ScalarFunction extends Function {
public abstract class ScalarFunction extends Function implements ScriptWeaver {
private ScalarFunctionAttribute lazyAttribute = null;
private ProcessorDefinition lazyProcessor = null;
protected ScalarFunction(Location location) {
super(location, emptyList());
@ -47,74 +36,14 @@ public abstract class ScalarFunction extends Function {
public final ScalarFunctionAttribute toAttribute() {
if (lazyAttribute == null) {
lazyAttribute = new ScalarFunctionAttribute(location(), name(), dataType(), id(), functionId(), asScript(), orderBy(),
asProcessorDefinition());
asPipe());
}
return lazyAttribute;
}
public abstract ScriptTemplate asScript();
// utility methods for creating the actual scripts
protected ScriptTemplate asScript(Expression exp) {
if (exp.foldable()) {
return asScriptFromFoldable(exp);
}
Attribute attr = Expressions.attribute(exp);
if (attr != null) {
if (attr instanceof ScalarFunctionAttribute) {
return asScriptFrom((ScalarFunctionAttribute) attr);
}
if (attr instanceof AggregateFunctionAttribute) {
return asScriptFrom((AggregateFunctionAttribute) attr);
}
if (attr instanceof FieldAttribute) {
return asScriptFrom((FieldAttribute) attr);
}
}
throw new SqlIllegalArgumentException("Cannot evaluate script for expression {}", exp);
}
protected ScriptTemplate asScriptFrom(ScalarFunctionAttribute scalar) {
ScriptTemplate nested = scalar.script();
Params p = paramsBuilder().script(nested.params()).build();
return new ScriptTemplate(formatScript(nested.template()), p, dataType());
}
protected ScriptTemplate asScriptFromFoldable(Expression foldable) {
return new ScriptTemplate(formatScript("{}"),
paramsBuilder().variable(foldable.fold()).build(),
foldable.dataType());
}
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
paramsBuilder().variable(field.name()).build(),
field.dataType());
}
protected ScriptTemplate asScriptFrom(AggregateFunctionAttribute aggregate) {
return new ScriptTemplate(formatScript("{}"),
paramsBuilder().agg(aggregate).build(),
aggregate.dataType());
}
protected String formatScript(String scriptTemplate) {
return formatTemplate(scriptTemplate);
}
public ProcessorDefinition asProcessorDefinition() {
if (lazyProcessor == null) {
lazyProcessor = makeProcessorDefinition();
}
return lazyProcessor;
}
protected abstract ProcessorDefinition makeProcessorDefinition();
// used if the function is monotonic and thus does not have to be computed for ordering purposes
// null means the script needs to be used; expression means the field/expression to be used instead
public Expression orderBy() {
return null;
}
}
}

View File

@ -9,8 +9,8 @@ import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.ExpressionId;
import org.elasticsearch.xpack.sql.expression.function.FunctionAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -21,28 +21,28 @@ public class ScalarFunctionAttribute extends FunctionAttribute {
private final ScriptTemplate script;
private final Expression orderBy;
private final ProcessorDefinition processorDef;
private final Pipe pipe;
ScalarFunctionAttribute(Location location, String name, DataType dataType, ExpressionId id,
String functionId, ScriptTemplate script, Expression orderBy, ProcessorDefinition processorDef) {
String functionId, ScriptTemplate script, Expression orderBy, Pipe processorDef) {
this(location, name, dataType, null, true, id, false, functionId, script, orderBy, processorDef);
}
public ScalarFunctionAttribute(Location location, String name, DataType dataType, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic, String functionId, ScriptTemplate script,
Expression orderBy, ProcessorDefinition processorDef) {
Expression orderBy, Pipe pipe) {
super(location, name, dataType, qualifier, nullable, id, synthetic, functionId);
this.script = script;
this.orderBy = orderBy;
this.processorDef = processorDef;
this.pipe = pipe;
}
@Override
protected NodeInfo<ScalarFunctionAttribute> info() {
return NodeInfo.create(this, ScalarFunctionAttribute::new,
name(), dataType(), qualifier(), nullable(), id(), synthetic(),
functionId(), script, orderBy, processorDef);
functionId(), script, orderBy, pipe);
}
public ScriptTemplate script() {
@ -53,34 +53,41 @@ public class ScalarFunctionAttribute extends FunctionAttribute {
return orderBy;
}
public ProcessorDefinition processorDef() {
return processorDef;
@Override
public Pipe asPipe() {
return pipe;
}
@Override
protected Expression canonicalize() {
return new ScalarFunctionAttribute(location(), "<none>", dataType(), null, true, id(), false,
functionId(), script, orderBy, processorDef);
functionId(), script, orderBy, pipe);
}
@Override
protected Attribute clone(Location location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
return new ScalarFunctionAttribute(location, name, dataType(), qualifier, nullable, id, synthetic,
functionId(), script, orderBy, processorDef);
functionId(), script, orderBy, pipe);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), orderBy);
return Objects.hash(super.hashCode(), script(), pipe, orderBy);
}
@Override
public boolean equals(Object obj) {
return super.equals(obj) && Objects.equals(orderBy, ((ScalarFunctionAttribute) obj).orderBy());
if (super.equals(obj)) {
ScalarFunctionAttribute other = (ScalarFunctionAttribute) obj;
return Objects.equals(script, other.script())
&& Objects.equals(pipe, other.asPipe())
&& Objects.equals(orderBy, other.orderBy());
}
return false;
}
@Override
protected String label() {
return "s->" + functionId();
}
}
}

View File

@ -6,13 +6,13 @@
package org.elasticsearch.xpack.sql.expression.function.scalar;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import static java.util.Collections.singletonList;
import java.util.List;
import static java.util.Collections.singletonList;
public abstract class UnaryScalarFunction extends ScalarFunction {
private final Expression field;

View File

@ -1,91 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.BinaryArithmeticProcessor.BinaryArithmeticOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryNumericFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.DataTypeConversion;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
public abstract class ArithmeticFunction extends BinaryNumericFunction {
private final BinaryArithmeticOperation operation;
ArithmeticFunction(Location location, Expression left, Expression right, BinaryArithmeticOperation operation) {
super(location, left, right);
this.operation = operation;
}
@Override
public BinaryArithmeticOperation operation() {
return operation;
}
@Override
public DataType dataType() {
return DataTypeConversion.commonType(left().dataType(), right().dataType());
}
@Override
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
String op = operation.symbol();
// escape %
if (operation == BinaryArithmeticOperation.MOD) {
op = "%" + op;
}
return new ScriptTemplate(format(Locale.ROOT, "(%s) %s (%s)", leftScript.template(), op, rightScript.template()),
paramsBuilder()
.script(leftScript.params()).script(rightScript.params())
.build(), dataType());
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryArithmeticProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
operation);
}
@Override
public String name() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(Expressions.name(left()));
if (!(left() instanceof Literal)) {
sb.insert(1, "(");
sb.append(")");
}
sb.append(" ");
sb.append(operation);
sb.append(" ");
int pos = sb.length();
sb.append(Expressions.name(right()));
if (!(right() instanceof Literal)) {
sb.insert(pos, "(");
sb.append(")");
}
sb.append(")");
return sb.toString();
}
@Override
public String toString() {
return name() + "#" + functionId();
}
}

View File

@ -1,66 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.BinaryArithmeticProcessor.BinaryArithmeticOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.BinaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
public class BinaryArithmeticProcessorDefinition extends BinaryProcessorDefinition {
private final BinaryArithmeticOperation operation;
public BinaryArithmeticProcessorDefinition(Location location, Expression expression, ProcessorDefinition left,
ProcessorDefinition right, BinaryArithmeticOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryArithmeticProcessorDefinition> info() {
return NodeInfo.create(this, BinaryArithmeticProcessorDefinition::new,
expression(), left(), right(), operation);
}
public BinaryArithmeticOperation operation() {
return operation;
}
@Override
protected BinaryProcessorDefinition replaceChildren(ProcessorDefinition left, ProcessorDefinition right) {
return new BinaryArithmeticProcessorDefinition(location(), expression(), left, right, operation);
}
@Override
public BinaryArithmeticProcessor asProcessor() {
return new BinaryArithmeticProcessor(left().asProcessor(), right().asProcessor(), operation);
}
@Override
public int hashCode() {
return Objects.hash(left(), right(), operation);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BinaryArithmeticProcessorDefinition other = (BinaryArithmeticProcessorDefinition) obj;
return Objects.equals(operation, other.operation)
&& Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right());
}
}

View File

@ -8,9 +8,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunctionAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -62,9 +60,4 @@ abstract class BaseDateTimeFunction extends UnaryScalarFunction {
public boolean foldable() {
return field().foldable();
}
@Override
protected ScriptTemplate asScriptFrom(AggregateFunctionAttribute aggregate) {
throw new UnsupportedOperationException();
}
}

View File

@ -9,7 +9,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.joda.time.ReadableInstant;
import java.io.IOException;

View File

@ -6,13 +6,13 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeProcessor.DateTimeExtractor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import org.joda.time.DateTime;
@ -24,8 +24,7 @@ import java.time.temporal.ChronoField;
import java.util.Objects;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
public abstract class DateTimeFunction extends BaseDateTimeFunction {
@ -49,7 +48,7 @@ public abstract class DateTimeFunction extends BaseDateTimeFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
public ScriptTemplate scriptWithField(FieldAttribute field) {
ParamsBuilder params = paramsBuilder();
String template = null;
@ -67,9 +66,8 @@ public abstract class DateTimeFunction extends BaseDateTimeFunction {
protected abstract ChronoField chronoField();
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this, ProcessorDefinitions.toProcessorDefinition(field()),
new DateTimeProcessor(extractor(), timeZone()));
protected Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new DateTimeProcessor(extractor(), timeZone()));
}
protected abstract DateTimeExtractor extractor();

View File

@ -16,7 +16,6 @@ import java.util.TimeZone;
* Extract the day of the week from a datetime in text format (Monday, Tuesday etc.)
*/
public class DayName extends NamedDateTimeFunction {
protected static final String DAY_NAME_FORMAT = "EEEE";
public DayName(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
@ -32,18 +31,8 @@ public class DayName extends NamedDateTimeFunction {
return new DayName(location(), newChild, timeZone());
}
@Override
protected String dateTimeFormat() {
return DAY_NAME_FORMAT;
}
@Override
protected NameExtractor nameExtractor() {
return NameExtractor.DAY_NAME;
}
@Override
public String extractName(long millis, String tzId) {
return nameExtractor().extract(millis, tzId);
}
}

View File

@ -16,7 +16,6 @@ import java.util.TimeZone;
* Extract the month from a datetime in text format (January, February etc.)
*/
public class MonthName extends NamedDateTimeFunction {
protected static final String MONTH_NAME_FORMAT = "MMMM";
public MonthName(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
@ -32,16 +31,6 @@ public class MonthName extends NamedDateTimeFunction {
return new MonthName(location(), newChild, timeZone());
}
@Override
protected String dateTimeFormat() {
return MONTH_NAME_FORMAT;
}
@Override
public String extractName(long millis, String tzId) {
return nameExtractor().extract(millis, tzId);
}
@Override
protected NameExtractor nameExtractor() {
return NameExtractor.MONTH_NAME;

View File

@ -6,26 +6,26 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.NamedDateTimeProcessor.NameExtractor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.StringUtils;
import org.joda.time.DateTime;
import java.util.Locale;
import java.util.Objects;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/*
* Base class for "naming" date/time functions like month_name and day_name
* Base class for "named" date/time functions like month_name and day_name
*/
abstract class NamedDateTimeFunction extends BaseDateTimeFunction {
@ -40,38 +40,28 @@ abstract class NamedDateTimeFunction extends BaseDateTimeFunction {
return null;
}
return extractName(folded.getMillis(), timeZone().getID());
}
public abstract String extractName(long millis, String tzId);
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
ParamsBuilder params = paramsBuilder();
String template = null;
template = formatTemplate(formatMethodName("{sql}.{method_name}(doc[{}].value.millis, {})"));
params.variable(field.name())
.variable(timeZone().getID());
return new ScriptTemplate(template, params.build(), dataType());
}
private String formatMethodName(String template) {
// the Painless method name will be the enum's lower camelcase name
return template.replace("{method_name}", StringUtils.underscoreToLowerCamelCase(nameExtractor().toString()));
return nameExtractor().extract(folded.getMillis(), timeZone().getID());
}
@Override
protected final ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this, ProcessorDefinitions.toProcessorDefinition(field()),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(
formatTemplate(format(Locale.ROOT, "{sql}.%s(doc[{}].value.millis, {})",
StringUtils.underscoreToLowerCamelCase(nameExtractor().name()))),
paramsBuilder()
.variable(field.name())
.variable(timeZone().getID()).build(),
dataType());
}
@Override
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()),
new NamedDateTimeProcessor(nameExtractor(), timeZone()));
}
protected abstract NameExtractor nameExtractor();
protected abstract String dateTimeFormat();
@Override
public DataType dataType() {
return DataType.KEYWORD;

View File

@ -24,11 +24,11 @@ public class NamedDateTimeProcessor extends BaseDateTimeProcessor {
// for the moment we'll use no specific Locale, but we might consider introducing a Locale parameter, just like the timeZone one
DAY_NAME((Long millis, String tzId) -> {
ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.of(tzId));
return time.format(DateTimeFormatter.ofPattern(DayName.DAY_NAME_FORMAT, Locale.ROOT));
return time.format(DateTimeFormatter.ofPattern(DAY_NAME_FORMAT, Locale.ROOT));
}),
MONTH_NAME((Long millis, String tzId) -> {
ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.of(tzId));
return time.format(DateTimeFormatter.ofPattern(MonthName.MONTH_NAME_FORMAT, Locale.ROOT));
return time.format(DateTimeFormatter.ofPattern(MONTH_NAME_FORMAT, Locale.ROOT));
});
private final BiFunction<Long,String,String> apply;
@ -43,6 +43,8 @@ public class NamedDateTimeProcessor extends BaseDateTimeProcessor {
}
public static final String NAME = "ndt";
private static final String MONTH_NAME_FORMAT = "MMMM";
private static final String DAY_NAME_FORMAT = "EEEE";
private final NameExtractor extractor;

View File

@ -7,12 +7,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.elasticsearch.xpack.sql.type.DataType;
@ -22,8 +21,7 @@ import java.util.Objects;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.expression.function.scalar.datetime.QuarterProcessor.quarter;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
public class Quarter extends BaseDateTimeFunction {
@ -44,15 +42,13 @@ public class Quarter extends BaseDateTimeFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
ParamsBuilder params = paramsBuilder();
String template = null;
template = formatTemplate("{sql}.quarter(doc[{}].value.millis, {})");
params.variable(field.name())
.variable(timeZone().getID());
return new ScriptTemplate(template, params.build(), dataType());
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(formatTemplate("{sql}.quarter(doc[{}].value.millis, {})"),
paramsBuilder()
.variable(field.name())
.variable(timeZone().getID())
.build(),
dataType());
}
@Override
@ -66,9 +62,8 @@ public class Quarter extends BaseDateTimeFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this, ProcessorDefinitions.toProcessorDefinition(field()),
new QuarterProcessor(timeZone()));
protected Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new QuarterProcessor(timeZone()));
}
@Override
@ -90,5 +85,4 @@ public class Quarter extends BaseDateTimeFunction {
public int hashCode() {
return Objects.hash(field(), timeZone());
}
}
}

View File

@ -7,13 +7,9 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.function.BiFunction;
/**
* <a href="https://en.wikipedia.org/wiki/Atan2">Multi-valued inverse tangent</a>
* function.
@ -21,12 +17,7 @@ import java.util.function.BiFunction;
public class ATan2 extends BinaryNumericFunction {
public ATan2(Location location, Expression left, Expression right) {
super(location, left, right);
}
@Override
protected BiFunction<Number, Number, Number> operation() {
return BinaryMathOperation.ATAN2;
super(location, left, right, BinaryMathOperation.ATAN2);
}
@Override
@ -38,12 +29,4 @@ public class ATan2 extends BinaryNumericFunction {
protected ATan2 replaceChildren(Expression newLeft, Expression newRight) {
return new ATan2(location(), newLeft, newRight);
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryMathProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryMathOperation.ATAN2);
}
}

View File

@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.BinaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
/**
* Math operation pipe requiring two arguments.
*/
public class BinaryMathPipe extends BinaryPipe {
private final BinaryMathOperation operation;
public BinaryMathPipe(Location location, Expression expression, Pipe left,
Pipe right, BinaryMathOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryMathPipe> info() {
return NodeInfo.create(this, BinaryMathPipe::new, expression(), left(), right(), operation);
}
public BinaryMathOperation operation() {
return operation;
}
@Override
protected BinaryPipe replaceChildren(Pipe left, Pipe right) {
return new BinaryMathPipe(location(), expression(), left, right, operation);
}
@Override
public BinaryMathProcessor asProcessor() {
return new BinaryMathProcessor(left().asProcessor(), right().asProcessor(), operation);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), operation);
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj)) {
BinaryMathPipe other = (BinaryMathPipe) obj;
return Objects.equals(operation, other.operation);
}
return false;
}
}

View File

@ -10,7 +10,8 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor.MathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Arithmetics;
import java.io.IOException;
import java.util.function.BiFunction;
@ -23,6 +24,7 @@ public class BinaryMathProcessor extends BinaryNumericProcessor<BinaryMathOperat
public enum BinaryMathOperation implements BiFunction<Number, Number, Number> {
ATAN2((l, r) -> Math.atan2(l.doubleValue(), r.doubleValue())),
MOD(Arithmetics::mod),
POWER((l, r) -> Math.pow(l.doubleValue(), r.doubleValue())),
ROUND((l, r) -> {
if (l == null) {
@ -87,4 +89,4 @@ public class BinaryMathProcessor extends BinaryNumericProcessor<BinaryMathOperat
public String getWriteableName() {
return NAME;
}
}
}

View File

@ -1,69 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.BinaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
/**
* Processor definition for math operations requiring two arguments.
*/
public class BinaryMathProcessorDefinition extends BinaryProcessorDefinition {
private final BinaryMathOperation operation;
public BinaryMathProcessorDefinition(Location location, Expression expression, ProcessorDefinition left,
ProcessorDefinition right, BinaryMathOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryMathProcessorDefinition> info() {
return NodeInfo.create(this, BinaryMathProcessorDefinition::new, expression(), left(), right(), operation);
}
public BinaryMathOperation operation() {
return operation;
}
@Override
protected BinaryProcessorDefinition replaceChildren(ProcessorDefinition left, ProcessorDefinition right) {
return new BinaryMathProcessorDefinition(location(), expression(), left, right, operation);
}
@Override
public BinaryMathProcessor asProcessor() {
return new BinaryMathProcessor(left().asProcessor(), right().asProcessor(), operation);
}
@Override
public int hashCode() {
return Objects.hash(left(), right(), operation);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BinaryMathProcessorDefinition other = (BinaryMathProcessorDefinition) obj;
return Objects.equals(operation, other.operation)
&& Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right());
}
}

View File

@ -6,25 +6,28 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
public abstract class BinaryNumericFunction extends BinaryScalarFunction {
protected BinaryNumericFunction(Location location, Expression left, Expression right) {
super(location, left, right);
}
private final BinaryMathOperation operation;
protected abstract BiFunction<Number, Number, Number> operation();
protected BinaryNumericFunction(Location location, Expression left, Expression right, BinaryMathOperation operation) {
super(location, left, right);
this.operation = operation;
}
@Override
public DataType dataType() {
@ -46,14 +49,19 @@ public abstract class BinaryNumericFunction extends BinaryScalarFunction {
}
protected TypeResolution resolveInputType(DataType inputType) {
return inputType.isNumeric() ?
TypeResolution.TYPE_RESOLVED :
return inputType.isNumeric() ?
TypeResolution.TYPE_RESOLVED :
new TypeResolution("'%s' requires a numeric type, received %s", mathFunction(), inputType.esType);
}
@Override
public Object fold() {
return operation().apply((Number) left().fold(), (Number) right().fold());
return operation.apply((Number) left().fold(), (Number) right().fold());
}
@Override
protected Pipe makePipe() {
return new BinaryMathPipe(location(), this, Expressions.pipe(left()), Expressions.pipe(right()), operation);
}
@Override
@ -70,7 +78,7 @@ public abstract class BinaryNumericFunction extends BinaryScalarFunction {
@Override
public int hashCode() {
return Objects.hash(left(), right(), operation());
return Objects.hash(left(), right(), operation);
}
@Override
@ -81,6 +89,6 @@ public abstract class BinaryNumericFunction extends BinaryScalarFunction {
BinaryNumericFunction other = (BinaryNumericFunction) obj;
return Objects.equals(other.left(), left())
&& Objects.equals(other.right(), right())
&& Objects.equals(other.operation(), operation());
&& Objects.equals(other.operation, operation);
}
}

View File

@ -7,8 +7,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.BinaryProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.BinaryProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Locale;

View File

@ -34,8 +34,9 @@ public class Cot extends MathFunction {
}
@Override
protected String formatScript(String template) {
return super.formatScript(format(Locale.ROOT, "1.0 / Math.tan(%s)", template));
public String processScript(String template) {
// FIXME: needs to be null aware
return super.processScript(format(Locale.ROOT, "1.0 / Math.tan(%s)", template));
}
@Override

View File

@ -9,8 +9,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor.MathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.Params;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.Params;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;

View File

@ -6,11 +6,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor.MathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
@ -40,8 +40,8 @@ public abstract class MathFunction extends UnaryScalarFunction {
}
@Override
protected String formatScript(String template) {
return super.formatScript(format(Locale.ROOT, "Math.%s(%s)", mathFunction(), template));
public String processScript(String template) {
return super.processScript(format(Locale.ROOT, "Math.%s(%s)", mathFunction(), template));
}
protected String mathFunction() {
@ -64,9 +64,8 @@ public abstract class MathFunction extends UnaryScalarFunction {
}
@Override
protected final ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(field()), new MathProcessor(operation()));
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new MathProcessor(operation()));
}
protected abstract MathOperation operation();

View File

@ -9,7 +9,7 @@ import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Random;

View File

@ -9,8 +9,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.MathProcessor.MathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.Params;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.Params;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;

View File

@ -7,22 +7,13 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.function.BiFunction;
public class Power extends BinaryNumericFunction {
public Power(Location location, Expression left, Expression right) {
super(location, left, right);
}
@Override
protected BiFunction<Number, Number, Number> operation() {
return BinaryMathOperation.POWER;
super(location, left, right, BinaryMathOperation.POWER);
}
@Override
@ -35,14 +26,6 @@ public class Power extends BinaryNumericFunction {
return new Power(location(), newLeft, newRight);
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryMathProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryMathOperation.POWER);
}
@Override
protected String mathFunction() {
return "pow";

View File

@ -34,9 +34,9 @@ public class Random extends MathFunction {
}
@Override
protected String formatScript(String template) {
public String processScript(String template) {
//TODO: Painless script uses Random since Randomness is not whitelisted
return super.formatScript(
return super.processScript(
format(Locale.ROOT, "%s != null ? new Random((long) %s).nextDouble() : Math.random()", template, template));
}

View File

@ -8,18 +8,15 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import java.util.Locale;
import java.util.function.BiFunction;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Function that takes two parameters: one is the field/value itself, the other is a non-floating point numeric
@ -30,7 +27,7 @@ import static org.elasticsearch.xpack.sql.expression.function.scalar.script.Para
public class Round extends BinaryNumericFunction {
public Round(Location location, Expression left, Expression right) {
super(location, left, right == null ? Literal.of(Location.EMPTY, 0) : right);
super(location, left, right == null ? Literal.of(left.location(), 0) : right, BinaryMathOperation.ROUND);
}
@Override
@ -43,30 +40,11 @@ public class Round extends BinaryNumericFunction {
return new Round(location(), newLeft, newRight);
}
@Override
protected BiFunction<Number, Number, Number> operation() {
return BinaryMathOperation.ROUND;
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryMathProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryMathOperation.ROUND);
}
protected TypeResolution resolveInputType(DataType inputType) {
return inputType.isNumeric() ?
TypeResolution.TYPE_RESOLVED :
new TypeResolution("'%s' requires a numeric type, received %s", mathFunction(), inputType.esType);
}
@Override
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
return new ScriptTemplate(format(Locale.ROOT, ScriptTemplate.formatTemplate("{sql}.%s(%s,%s)"),
mathFunction(),
leftScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
mathFunction(),
leftScript.template(),
rightScript.template()),
paramsBuilder()
.script(leftScript.params()).script(rightScript.params())

View File

@ -8,29 +8,26 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.math;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.scalar.math.BinaryMathProcessor.BinaryMathOperation;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import java.util.Locale;
import java.util.function.BiFunction;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Function that takes two parameters: one is the field/value itself, the other is a non-floating point numeric
* which indicates how the truncation should behave. If positive, it will truncate the number till that
* which indicates how the truncation should behave. If positive, it will truncate the number till that
* parameter count digits after the decimal point. If negative, it will truncate the number till that parameter
* count digits before the decimal point, starting at the decimal point.
*/
public class Truncate extends BinaryNumericFunction {
public Truncate(Location location, Expression left, Expression right) {
super(location, left, right == null ? Literal.of(Location.EMPTY, 0) : right);
super(location, left, right == null ? Literal.of(left.location(), 0) : right, BinaryMathOperation.TRUNCATE);
}
@Override
@ -43,24 +40,11 @@ public class Truncate extends BinaryNumericFunction {
return new Truncate(location(), newLeft, newRight);
}
@Override
protected BiFunction<Number, Number, Number> operation() {
return BinaryMathOperation.TRUNCATE;
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryMathProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryMathOperation.TRUNCATE);
}
@Override
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
return new ScriptTemplate(format(Locale.ROOT, ScriptTemplate.formatTemplate("{sql}.%s(%s,%s)"),
mathFunction(),
leftScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
mathFunction(),
leftScript.template(),
rightScript.template()),
paramsBuilder()
.script(leftScript.params()).script(rightScript.params())

View File

@ -1,32 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.NamedExpression;
import org.elasticsearch.xpack.sql.expression.function.aggregate.AggregateFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
public abstract class ProcessorDefinitions {
public static ProcessorDefinition toProcessorDefinition(Expression ex) {
if (ex.foldable()) {
return new ConstantInput(ex.location(), ex, ex.fold());
}
if (ex instanceof ScalarFunction) {
return ((ScalarFunction) ex).asProcessorDefinition();
}
if (ex instanceof AggregateFunction) {
// unresolved AggNameInput (should always get replaced by the folder)
return new AggNameInput(ex.location(), ex, ((AggregateFunction) ex).name());
}
if (ex instanceof NamedExpression) {
return new AttributeInput(ex.location(), ex, ((NamedExpression) ex).toAttribute());
}
throw new SqlIllegalArgumentException("Cannot extract processor from {}", ex);
}
}

View File

@ -8,18 +8,16 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.StringUtils;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Base class for binary functions that have the first parameter a string, the second parameter a number
@ -61,11 +59,11 @@ public abstract class BinaryStringFunction<T,R> extends BinaryScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
public ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2)
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
StringUtils.underscoreToLowerCamelCase(operation().toString()),
leftScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
operation().toString().toLowerCase(Locale.ROOT),
leftScript.template(),
rightScript.template()),
paramsBuilder()
.script(leftScript.params()).script(rightScript.params())
@ -73,8 +71,8 @@ public abstract class BinaryStringFunction<T,R> extends BinaryScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}

View File

@ -6,6 +6,9 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;
@ -18,13 +21,21 @@ public abstract class BinaryStringNumericFunction extends BinaryStringFunction<N
super(location, left, right);
}
@Override
protected abstract BinaryStringNumericOperation operation();
@Override
protected TypeResolution resolveSecondParameterInputType(DataType inputType) {
return inputType.isNumeric() ?
TypeResolution.TYPE_RESOLVED :
return inputType.isNumeric() ?
TypeResolution.TYPE_RESOLVED :
new TypeResolution("'%s' requires second parameter to be a numeric type, received %s", functionName(), inputType);
}
@Override
protected Pipe makePipe() {
return new BinaryStringNumericPipe(location(), this, Expressions.pipe(left()), Expressions.pipe(right()), operation());
}
@Override
public DataType dataType() {
return DataType.KEYWORD;

View File

@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.BinaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
/**
* String operations pipe requiring one string and one numeric argument.
*/
public class BinaryStringNumericPipe extends BinaryPipe {
private final BinaryStringNumericOperation operation;
public BinaryStringNumericPipe(Location location, Expression expression, Pipe left,
Pipe right, BinaryStringNumericOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryStringNumericPipe> info() {
return NodeInfo.create(this, BinaryStringNumericPipe::new, expression(), left(), right(), operation());
}
public BinaryStringNumericOperation operation() {
return operation;
}
@Override
protected BinaryPipe replaceChildren(Pipe newLeft, Pipe newRight) {
return new BinaryStringNumericPipe(location(), expression(), newLeft, newRight, operation());
}
@Override
public BinaryStringNumericProcessor asProcessor() {
return new BinaryStringNumericProcessor(left().asProcessor(), right().asProcessor(), operation());
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), operation);
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj)) {
BinaryStringNumericPipe other = (BinaryStringNumericPipe) obj;
return Objects.equals(operation, other.operation);
}
return false;
}
}

View File

@ -8,8 +8,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.function.BiFunction;

View File

@ -1,69 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.BinaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
/**
* Processor definition for String operations requiring one string and one numeric argument.
*/
public class BinaryStringNumericProcessorDefinition extends BinaryProcessorDefinition {
private final BinaryStringNumericOperation operation;
public BinaryStringNumericProcessorDefinition(Location location, Expression expression, ProcessorDefinition left,
ProcessorDefinition right, BinaryStringNumericOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryStringNumericProcessorDefinition> info() {
return NodeInfo.create(this, BinaryStringNumericProcessorDefinition::new, expression(), left(), right(), operation());
}
public BinaryStringNumericOperation operation() {
return operation;
}
@Override
protected BinaryProcessorDefinition replaceChildren(ProcessorDefinition newLeft, ProcessorDefinition newRight) {
return new BinaryStringNumericProcessorDefinition(location(), expression(), newLeft, newRight, operation());
}
@Override
public BinaryStringNumericProcessor asProcessor() {
return new BinaryStringNumericProcessor(left().asProcessor(), right().asProcessor(), operation());
}
@Override
public int hashCode() {
return Objects.hash(left(), right(), operation);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BinaryStringNumericProcessorDefinition other = (BinaryStringNumericProcessorDefinition) obj;
return Objects.equals(operation, other.operation)
&& Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right());
}
}

View File

@ -6,8 +6,8 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.BinaryProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.BinaryProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;

View File

@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringStringProcessor.BinaryStringStringOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.BinaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
/**
* String operations pipe requiring two string arguments.
*/
public class BinaryStringStringPipe extends BinaryPipe {
private final BinaryStringStringOperation operation;
public BinaryStringStringPipe(Location location, Expression expression, Pipe left,
Pipe right, BinaryStringStringOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryStringStringPipe> info() {
return NodeInfo.create(this, BinaryStringStringPipe::new, expression(), left(), right(), operation);
}
public BinaryStringStringOperation operation() {
return operation;
}
@Override
protected BinaryPipe replaceChildren(Pipe left, Pipe right) {
return new BinaryStringStringPipe(location(), expression(), left, right, operation);
}
@Override
public BinaryStringStringProcessor asProcessor() {
return new BinaryStringStringProcessor(left().asProcessor(), right().asProcessor(), operation);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), operation);
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj)) {
BinaryStringStringPipe other = (BinaryStringStringPipe) obj;
return Objects.equals(operation, other.operation);
}
return false;
}
}

View File

@ -8,8 +8,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringStringProcessor.BinaryStringStringOperation;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.function.BiFunction;

View File

@ -1,69 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.BinaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringStringProcessor.BinaryStringStringOperation;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
/**
* Processor definition for String operations requiring two string arguments.
*/
public class BinaryStringStringProcessorDefinition extends BinaryProcessorDefinition {
private final BinaryStringStringOperation operation;
public BinaryStringStringProcessorDefinition(Location location, Expression expression, ProcessorDefinition left,
ProcessorDefinition right, BinaryStringStringOperation operation) {
super(location, expression, left, right);
this.operation = operation;
}
@Override
protected NodeInfo<BinaryStringStringProcessorDefinition> info() {
return NodeInfo.create(this, BinaryStringStringProcessorDefinition::new, expression(), left(), right(), operation);
}
public BinaryStringStringOperation operation() {
return operation;
}
@Override
protected BinaryProcessorDefinition replaceChildren(ProcessorDefinition left, ProcessorDefinition right) {
return new BinaryStringStringProcessorDefinition(location(), expression(), left, right, operation);
}
@Override
public BinaryStringStringProcessor asProcessor() {
return new BinaryStringStringProcessor(left().asProcessor(), right().asProcessor(), operation);
}
@Override
public int hashCode() {
return Objects.hash(left(), right(), operation);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BinaryStringStringProcessorDefinition other = (BinaryStringStringProcessorDefinition) obj;
return Objects.equals(operation, other.operation)
&& Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right());
}
}

View File

@ -6,11 +6,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -18,9 +18,8 @@ import org.elasticsearch.xpack.sql.type.DataType;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.function.scalar.string.ConcatFunctionProcessor.doProcessInScripts;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Returns a string that is the result of concatenating the two strings received as parameters.
@ -33,6 +32,7 @@ public class Concat extends BinaryScalarFunction {
super(location, source1, source2);
}
@Override
protected TypeResolution resolveType() {
if (!childrenResolved()) {
return new TypeResolution("Unresolved children");
@ -47,16 +47,13 @@ public class Concat extends BinaryScalarFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new ConcatFunctionProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()));
protected Pipe makePipe() {
return new ConcatFunctionPipe(location(), this, Expressions.pipe(left()), Expressions.pipe(right()));
}
@Override
public boolean foldable() {
return left().foldable()
&& right().foldable();
return left().foldable() && right().foldable();
}
@Override
@ -74,20 +71,12 @@ public class Concat extends BinaryScalarFunction {
return NodeInfo.create(this, Concat::new, left(), right());
}
@Override
public ScriptTemplate asScript() {
ScriptTemplate sourceScript1 = asScript(left());
ScriptTemplate sourceScript2 = asScript(right());
return asScriptFrom(sourceScript1, sourceScript2);
}
@Override
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2)
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
"concat",
leftScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
"concat",
leftScript.template(),
rightScript.template()),
paramsBuilder()
.script(leftScript.params()).script(rightScript.params())
@ -95,8 +84,8 @@ public class Concat extends BinaryScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}
@ -105,5 +94,4 @@ public class Concat extends BinaryScalarFunction {
public DataType dataType() {
return DataType.KEYWORD;
}
}
}

View File

@ -6,28 +6,28 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.BinaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.BinaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.Objects;
public class ConcatFunctionProcessorDefinition extends BinaryProcessorDefinition {
public class ConcatFunctionPipe extends BinaryPipe {
public ConcatFunctionProcessorDefinition(Location location, Expression expression, ProcessorDefinition left,
ProcessorDefinition right) {
public ConcatFunctionPipe(Location location, Expression expression, Pipe left,
Pipe right) {
super(location, expression, left, right);
}
@Override
protected NodeInfo<ConcatFunctionProcessorDefinition> info() {
return NodeInfo.create(this, ConcatFunctionProcessorDefinition::new, expression(), left(), right());
protected NodeInfo<ConcatFunctionPipe> info() {
return NodeInfo.create(this, ConcatFunctionPipe::new, expression(), left(), right());
}
@Override
protected BinaryProcessorDefinition replaceChildren(ProcessorDefinition left, ProcessorDefinition right) {
return new ConcatFunctionProcessorDefinition(location(), expression(), left, right);
protected BinaryPipe replaceChildren(Pipe left, Pipe right) {
return new ConcatFunctionPipe(location(), expression(), left, right);
}
@Override
@ -50,7 +50,7 @@ public class ConcatFunctionProcessorDefinition extends BinaryProcessorDefinition
return false;
}
ConcatFunctionProcessorDefinition other = (ConcatFunctionProcessorDefinition) obj;
ConcatFunctionPipe other = (ConcatFunctionPipe) obj;
return Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right());
}

View File

@ -8,15 +8,15 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.BinaryProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.BinaryProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;
public class ConcatFunctionProcessor extends BinaryProcessor {
public static final String NAME = "cb";
public static final String NAME = "scon";
public ConcatFunctionProcessor(Processor source1, Processor source2) {
super(source1, source2);

View File

@ -6,11 +6,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -20,9 +20,8 @@ import java.util.List;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.function.scalar.string.InsertFunctionProcessor.doProcess;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Returns a character string where length characters have been deleted from the source string, beginning at start,
@ -40,6 +39,7 @@ public class Insert extends ScalarFunction {
this.replacement = replacement;
}
@Override
protected TypeResolution resolveType() {
if (!childrenResolved()) {
return new TypeResolution("Unresolved children");
@ -65,7 +65,7 @@ public class Insert extends ScalarFunction {
@Override
public boolean foldable() {
return source.foldable()
return source.foldable()
&& start.foldable()
&& length.foldable()
&& replacement.foldable();
@ -77,12 +77,12 @@ public class Insert extends ScalarFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new InsertFunctionProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(source),
ProcessorDefinitions.toProcessorDefinition(start),
ProcessorDefinitions.toProcessorDefinition(length),
ProcessorDefinitions.toProcessorDefinition(replacement));
protected Pipe makePipe() {
return new InsertFunctionPipe(location(), this,
Expressions.pipe(source),
Expressions.pipe(start),
Expressions.pipe(length),
Expressions.pipe(replacement));
}
@Override
@ -100,13 +100,12 @@ public class Insert extends ScalarFunction {
return asScriptFrom(sourceScript, startScript, lengthScript, replacementScript);
}
protected ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate startScript,
ScriptTemplate lengthScript, ScriptTemplate replacementScript)
{
private ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate startScript,
ScriptTemplate lengthScript, ScriptTemplate replacementScript) {
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...)
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s,%s)"),
"insert",
sourceScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s,%s)"),
"insert",
sourceScript.template(),
startScript.template(),
lengthScript.template(),
replacementScript.template()),
@ -117,8 +116,8 @@ public class Insert extends ScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}
@ -136,4 +135,4 @@ public class Insert extends ScalarFunction {
return new Insert(location(), newChildren.get(0), newChildren.get(1), newChildren.get(2), newChildren.get(3));
}
}
}

View File

@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -15,13 +15,13 @@ import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
public class InsertFunctionPipe extends Pipe {
private final ProcessorDefinition source, start, length, replacement;
private final Pipe source, start, length, replacement;
public InsertFunctionProcessorDefinition(Location location, Expression expression,
ProcessorDefinition source, ProcessorDefinition start,
ProcessorDefinition length, ProcessorDefinition replacement) {
public InsertFunctionPipe(Location location, Expression expression,
Pipe source, Pipe start,
Pipe length, Pipe replacement) {
super(location, expression, Arrays.asList(source, start, length, replacement));
this.source = source;
this.start = start;
@ -30,7 +30,7 @@ public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 4) {
throw new IllegalArgumentException("expected [4] children but received [" + newChildren.size() + "]");
}
@ -38,11 +38,11 @@ public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
ProcessorDefinition newSource = source.resolveAttributes(resolver);
ProcessorDefinition newStart = start.resolveAttributes(resolver);
ProcessorDefinition newLength = length.resolveAttributes(resolver);
ProcessorDefinition newReplacement = replacement.resolveAttributes(resolver);
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newSource = source.resolveAttributes(resolver);
Pipe newStart = start.resolveAttributes(resolver);
Pipe newLength = length.resolveAttributes(resolver);
Pipe newReplacement = replacement.resolveAttributes(resolver);
if (newSource == source
&& newStart == start
&& newLength == length
@ -65,11 +65,11 @@ public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
return source.resolved() && start.resolved() && length.resolved() && replacement.resolved();
}
protected ProcessorDefinition replaceChildren(ProcessorDefinition newSource,
ProcessorDefinition newStart,
ProcessorDefinition newLength,
ProcessorDefinition newReplacement) {
return new InsertFunctionProcessorDefinition(location(), expression(), newSource, newStart, newLength, newReplacement);
protected Pipe replaceChildren(Pipe newSource,
Pipe newStart,
Pipe newLength,
Pipe newReplacement) {
return new InsertFunctionPipe(location(), expression(), newSource, newStart, newLength, newReplacement);
}
@Override
@ -81,8 +81,8 @@ public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
protected NodeInfo<InsertFunctionProcessorDefinition> info() {
return NodeInfo.create(this, InsertFunctionProcessorDefinition::new, expression(), source, start, length, replacement);
protected NodeInfo<InsertFunctionPipe> info() {
return NodeInfo.create(this, InsertFunctionPipe::new, expression(), source, start, length, replacement);
}
@Override
@ -90,19 +90,19 @@ public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
return new InsertFunctionProcessor(source.asProcessor(), start.asProcessor(), length.asProcessor(), replacement.asProcessor());
}
public ProcessorDefinition source() {
public Pipe source() {
return source;
}
public ProcessorDefinition start() {
public Pipe start() {
return start;
}
public ProcessorDefinition length() {
public Pipe length() {
return length;
}
public ProcessorDefinition replacement() {
public Pipe replacement() {
return replacement;
}
@ -121,7 +121,7 @@ public class InsertFunctionProcessorDefinition extends ProcessorDefinition {
return false;
}
InsertFunctionProcessorDefinition other = (InsertFunctionProcessorDefinition) obj;
InsertFunctionPipe other = (InsertFunctionPipe) obj;
return Objects.equals(source, other.source)
&& Objects.equals(start, other.start)
&& Objects.equals(length, other.length)

View File

@ -8,7 +8,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;
@ -16,7 +16,7 @@ import java.util.Objects;
public class InsertFunctionProcessor implements Processor {
private final Processor source, start, length, replacement;
public static final String NAME = "ins";
public static final String NAME = "si";
public InsertFunctionProcessor(Processor source, Processor start, Processor length, Processor replacement) {
this.source = source;
@ -81,8 +81,8 @@ public class InsertFunctionProcessor implements Processor {
StringBuilder sb = new StringBuilder(source.toString());
String replString = (replacement.toString());
return sb.replace(realStart,
realStart + ((Number) length).intValue(),
return sb.replace(realStart,
realStart + ((Number) length).intValue(),
replString).toString();
}

View File

@ -6,14 +6,10 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.function.BiFunction;
/**
* Returns the leftmost count characters of a string.
*/
@ -24,7 +20,7 @@ public class Left extends BinaryStringNumericFunction {
}
@Override
protected BiFunction<String, Number, String> operation() {
protected BinaryStringNumericOperation operation() {
return BinaryStringNumericOperation.LEFT;
}
@ -33,14 +29,6 @@ public class Left extends BinaryStringNumericFunction {
return new Left(location(), newLeft, newRight);
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryStringNumericProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryStringNumericOperation.LEFT);
}
@Override
protected NodeInfo<Left> info() {
return NodeInfo.create(this, Left::new, left(), right());

View File

@ -6,11 +6,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -20,14 +20,13 @@ import java.util.List;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.function.scalar.string.LocateFunctionProcessor.doProcess;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Returns the starting position of the first occurrence of the pattern within the source string.
* The search for the first occurrence of the pattern begins with the first character position in the source string
* unless the optional argument, start, is specified. If start is specified, the search begins with the character
* unless the optional argument, start, is specified. If start is specified, the search begins with the character
* position indicated by the value of start. The first character position in the source string is indicated by the value 1.
* If the pattern is not found within the source string, the value 0 is returned.
*/
@ -42,6 +41,7 @@ public class Locate extends ScalarFunction {
this.start = start;
}
@Override
protected TypeResolution resolveType() {
if (!childrenResolved()) {
return new TypeResolution("Unresolved children");
@ -61,11 +61,11 @@ public class Locate extends ScalarFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new LocateFunctionProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(pattern),
ProcessorDefinitions.toProcessorDefinition(source),
start == null ? null : ProcessorDefinitions.toProcessorDefinition(start));
protected Pipe makePipe() {
return new LocateFunctionPipe(location(), this,
Expressions.pipe(pattern),
Expressions.pipe(source),
start == null ? null : Expressions.pipe(start));
}
@Override
@ -75,7 +75,7 @@ public class Locate extends ScalarFunction {
@Override
public boolean foldable() {
return pattern.foldable()
return pattern.foldable()
&& source.foldable()
&& (start == null? true : start.foldable());
}
@ -94,22 +94,20 @@ public class Locate extends ScalarFunction {
return asScriptFrom(patternScript, sourceScript, startScript);
}
protected ScriptTemplate asScriptFrom(ScriptTemplate patternScript, ScriptTemplate sourceScript,
ScriptTemplate startScript)
{
private ScriptTemplate asScriptFrom(ScriptTemplate patternScript, ScriptTemplate sourceScript, ScriptTemplate startScript) {
if (start == null) {
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
"locate",
patternScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s)"),
"locate",
patternScript.template(),
sourceScript.template()),
paramsBuilder()
.script(patternScript.params()).script(sourceScript.params())
.build(), dataType());
}
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...)
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"),
"locate",
patternScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"),
"locate",
patternScript.template(),
sourceScript.template(),
startScript.template()),
paramsBuilder()
@ -119,8 +117,8 @@ public class Locate extends ScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}
@ -138,4 +136,4 @@ public class Locate extends ScalarFunction {
return new Locate(location(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
}
}
}

View File

@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -15,12 +15,12 @@ import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
public class LocateFunctionPipe extends Pipe {
private final ProcessorDefinition pattern, source, start;
private final Pipe pattern, source, start;
public LocateFunctionProcessorDefinition(Location location, Expression expression, ProcessorDefinition pattern,
ProcessorDefinition source, ProcessorDefinition start) {
public LocateFunctionPipe(Location location, Expression expression, Pipe pattern,
Pipe source, Pipe start) {
super(location, expression, start == null ? Arrays.asList(pattern, source) : Arrays.asList(pattern, source, start));
this.pattern = pattern;
this.source = source;
@ -28,7 +28,7 @@ public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
int childrenSize = newChildren.size();
if (childrenSize > 3 || childrenSize < 2) {
throw new IllegalArgumentException("expected [2 or 3] children but received [" + newChildren.size() + "]");
@ -37,10 +37,10 @@ public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
ProcessorDefinition newPattern = pattern.resolveAttributes(resolver);
ProcessorDefinition newSource = source.resolveAttributes(resolver);
ProcessorDefinition newStart = start == null ? start : start.resolveAttributes(resolver);
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newPattern = pattern.resolveAttributes(resolver);
Pipe newSource = source.resolveAttributes(resolver);
Pipe newStart = start == null ? start : start.resolveAttributes(resolver);
if (newPattern == pattern && newSource == source && newStart == start) {
return this;
}
@ -58,9 +58,9 @@ public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
return pattern.resolved() && source.resolved() && (start == null || start.resolved());
}
protected ProcessorDefinition replaceChildren(ProcessorDefinition newPattern, ProcessorDefinition newSource,
ProcessorDefinition newStart) {
return new LocateFunctionProcessorDefinition(location(), expression(), newPattern, newSource, newStart);
protected Pipe replaceChildren(Pipe newPattern, Pipe newSource,
Pipe newStart) {
return new LocateFunctionPipe(location(), expression(), newPattern, newSource, newStart);
}
@Override
@ -73,8 +73,8 @@ public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
protected NodeInfo<LocateFunctionProcessorDefinition> info() {
return NodeInfo.create(this, LocateFunctionProcessorDefinition::new, expression(), pattern, source, start);
protected NodeInfo<LocateFunctionPipe> info() {
return NodeInfo.create(this, LocateFunctionPipe::new, expression(), pattern, source, start);
}
@Override
@ -82,15 +82,15 @@ public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
return new LocateFunctionProcessor(pattern.asProcessor(), source.asProcessor(), start == null ? null : start.asProcessor());
}
public ProcessorDefinition source() {
public Pipe source() {
return source;
}
public ProcessorDefinition start() {
public Pipe start() {
return start;
}
public ProcessorDefinition pattern() {
public Pipe pattern() {
return pattern;
}
@ -109,7 +109,7 @@ public class LocateFunctionProcessorDefinition extends ProcessorDefinition {
return false;
}
LocateFunctionProcessorDefinition other = (LocateFunctionProcessorDefinition) obj;
LocateFunctionPipe other = (LocateFunctionPipe) obj;
return Objects.equals(pattern, other.pattern) && Objects.equals(source, other.source) && Objects.equals(start, other.start);
}
}
}

View File

@ -8,7 +8,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;
@ -16,7 +16,7 @@ import java.util.Objects;
public class LocateFunctionProcessor implements Processor {
private final Processor pattern, source, start;
public static final String NAME = "lc";
public static final String NAME = "sloc";
public LocateFunctionProcessor(Processor pattern, Processor source, Processor start) {
this.pattern = pattern;
@ -63,9 +63,9 @@ public class LocateFunctionProcessor implements Processor {
String stringSource = source instanceof Character ? source.toString() : (String) source;
String stringPattern = pattern instanceof Character ? pattern.toString() : (String) pattern;
return (Integer) (1 + (start != null ?
return 1 + (start != null ?
stringSource.indexOf(stringPattern, ((Number) start).intValue() - 1)
: stringSource.indexOf(stringPattern)));
: stringSource.indexOf(stringPattern));
}
@Override

View File

@ -6,9 +6,9 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringStringProcessor.BinaryStringStringOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -34,10 +34,10 @@ public class Position extends BinaryStringStringFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryStringStringProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
protected Pipe makePipe() {
return new BinaryStringStringPipe(location(), this,
Expressions.pipe(left()),
Expressions.pipe(right()),
BinaryStringStringOperation.POSITION);
}
@ -45,5 +45,4 @@ public class Position extends BinaryStringStringFunction {
protected NodeInfo<Position> info() {
return NodeInfo.create(this, Position::new, left(), right());
}
}

View File

@ -6,14 +6,10 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.function.BiFunction;
/**
* Creates a string composed of a string repeated count times.
*/
@ -24,7 +20,7 @@ public class Repeat extends BinaryStringNumericFunction {
}
@Override
protected BiFunction<String, Number, String> operation() {
protected BinaryStringNumericOperation operation() {
return BinaryStringNumericOperation.REPEAT;
}
@ -33,17 +29,8 @@ public class Repeat extends BinaryStringNumericFunction {
return new Repeat(location(), newLeft, newRight);
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryStringNumericProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryStringNumericOperation.REPEAT);
}
@Override
protected NodeInfo<Repeat> info() {
return NodeInfo.create(this, Repeat::new, left(), right());
}
}

View File

@ -6,11 +6,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -20,9 +20,8 @@ import java.util.List;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.function.scalar.string.ReplaceFunctionProcessor.doProcess;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Search the source string for occurrences of the pattern, and replace with the replacement string.
@ -38,6 +37,7 @@ public class Replace extends ScalarFunction {
this.replacement = replacement;
}
@Override
protected TypeResolution resolveType() {
if (!childrenResolved()) {
return new TypeResolution("Unresolved children");
@ -57,11 +57,11 @@ public class Replace extends ScalarFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new ReplaceFunctionProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(source),
ProcessorDefinitions.toProcessorDefinition(pattern),
ProcessorDefinitions.toProcessorDefinition(replacement));
protected Pipe makePipe() {
return new ReplaceFunctionPipe(location(), this,
Expressions.pipe(source),
Expressions.pipe(pattern),
Expressions.pipe(replacement));
}
@Override
@ -71,7 +71,7 @@ public class Replace extends ScalarFunction {
@Override
public boolean foldable() {
return source.foldable()
return source.foldable()
&& pattern.foldable()
&& replacement.foldable();
}
@ -90,13 +90,11 @@ public class Replace extends ScalarFunction {
return asScriptFrom(sourceScript, patternScript, replacementScript);
}
protected ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate patternScript,
ScriptTemplate replacementScript)
{
private ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate patternScript, ScriptTemplate replacementScript) {
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...)
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"),
"replace",
sourceScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"),
"replace",
sourceScript.template(),
patternScript.template(),
replacementScript.template()),
paramsBuilder()
@ -106,8 +104,8 @@ public class Replace extends ScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}
@ -125,4 +123,4 @@ public class Replace extends ScalarFunction {
return new Replace(location(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
}
}
}

View File

@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -15,12 +15,12 @@ import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
public class ReplaceFunctionPipe extends Pipe {
private final ProcessorDefinition source, pattern, replacement;
private final Pipe source, pattern, replacement;
public ReplaceFunctionProcessorDefinition(Location location, Expression expression, ProcessorDefinition source,
ProcessorDefinition pattern, ProcessorDefinition replacement) {
public ReplaceFunctionPipe(Location location, Expression expression, Pipe source,
Pipe pattern, Pipe replacement) {
super(location, expression, Arrays.asList(source, pattern, replacement));
this.source = source;
this.pattern = pattern;
@ -28,7 +28,7 @@ public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 3) {
throw new IllegalArgumentException("expected [3] children but received [" + newChildren.size() + "]");
}
@ -36,10 +36,10 @@ public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
ProcessorDefinition newSource = source.resolveAttributes(resolver);
ProcessorDefinition newPattern = pattern.resolveAttributes(resolver);
ProcessorDefinition newReplacement = replacement.resolveAttributes(resolver);
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newSource = source.resolveAttributes(resolver);
Pipe newPattern = pattern.resolveAttributes(resolver);
Pipe newReplacement = replacement.resolveAttributes(resolver);
if (newSource == source && newPattern == pattern && newReplacement == replacement) {
return this;
}
@ -56,9 +56,9 @@ public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
return source.resolved() && pattern.resolved() && replacement.resolved();
}
protected ProcessorDefinition replaceChildren(ProcessorDefinition newSource, ProcessorDefinition newPattern,
ProcessorDefinition newReplacement) {
return new ReplaceFunctionProcessorDefinition(location(), expression(), newSource, newPattern, newReplacement);
protected Pipe replaceChildren(Pipe newSource, Pipe newPattern,
Pipe newReplacement) {
return new ReplaceFunctionPipe(location(), expression(), newSource, newPattern, newReplacement);
}
@Override
@ -69,8 +69,8 @@ public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
protected NodeInfo<ReplaceFunctionProcessorDefinition> info() {
return NodeInfo.create(this, ReplaceFunctionProcessorDefinition::new, expression(), source, pattern, replacement);
protected NodeInfo<ReplaceFunctionPipe> info() {
return NodeInfo.create(this, ReplaceFunctionPipe::new, expression(), source, pattern, replacement);
}
@Override
@ -78,15 +78,15 @@ public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
return new ReplaceFunctionProcessor(source.asProcessor(), pattern.asProcessor(), replacement.asProcessor());
}
public ProcessorDefinition source() {
public Pipe source() {
return source;
}
public ProcessorDefinition pattern() {
public Pipe pattern() {
return pattern;
}
public ProcessorDefinition replacement() {
public Pipe replacement() {
return replacement;
}
@ -105,7 +105,7 @@ public class ReplaceFunctionProcessorDefinition extends ProcessorDefinition {
return false;
}
ReplaceFunctionProcessorDefinition other = (ReplaceFunctionProcessorDefinition) obj;
ReplaceFunctionPipe other = (ReplaceFunctionPipe) obj;
return Objects.equals(source, other.source)
&& Objects.equals(pattern, other.pattern)
&& Objects.equals(replacement, other.replacement);

View File

@ -9,7 +9,7 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;
@ -17,7 +17,7 @@ import java.util.Objects;
public class ReplaceFunctionProcessor implements Processor {
private final Processor source, pattern, replacement;
public static final String NAME = "r";
public static final String NAME = "srep";
public ReplaceFunctionProcessor(Processor source, Processor pattern, Processor replacement) {
this.source = source;
@ -60,8 +60,8 @@ public class ReplaceFunctionProcessor implements Processor {
throw new SqlIllegalArgumentException("A string/char is required; received [{}]", replacement);
}
return Strings.replace(source instanceof Character ? source.toString() : (String)source,
pattern instanceof Character ? pattern.toString() : (String) pattern,
return Strings.replace(source instanceof Character ? source.toString() : (String)source,
pattern instanceof Character ? pattern.toString() : (String) pattern,
replacement instanceof Character ? replacement.toString() : (String) replacement);
}

View File

@ -6,14 +6,10 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.BinaryStringNumericProcessor.BinaryStringNumericOperation;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import java.util.function.BiFunction;
/**
* Returns the rightmost count characters of a string.
*/
@ -24,7 +20,7 @@ public class Right extends BinaryStringNumericFunction {
}
@Override
protected BiFunction<String, Number, String> operation() {
protected BinaryStringNumericOperation operation() {
return BinaryStringNumericOperation.RIGHT;
}
@ -33,14 +29,6 @@ public class Right extends BinaryStringNumericFunction {
return new Right(location(), newLeft, newRight);
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new BinaryStringNumericProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(left()),
ProcessorDefinitions.toProcessorDefinition(right()),
BinaryStringNumericOperation.RIGHT);
}
@Override
protected NodeInfo<Right> info() {
return NodeInfo.create(this, Right::new, left(), right());

View File

@ -9,7 +9,7 @@ import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Arrays;

View File

@ -6,11 +6,11 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
@ -20,12 +20,11 @@ import java.util.List;
import java.util.Locale;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
import static org.elasticsearch.xpack.sql.expression.function.scalar.string.SubstringFunctionProcessor.doProcess;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Returns a character string that is derived from the source string, beginning at the character position specified by start
* Returns a character string that is derived from the source string, beginning at the character position specified by start
* for length characters.
*/
public class Substring extends ScalarFunction {
@ -39,6 +38,7 @@ public class Substring extends ScalarFunction {
this.length = length;
}
@Override
protected TypeResolution resolveType() {
if (!childrenResolved()) {
return new TypeResolution("Unresolved children");
@ -58,18 +58,16 @@ public class Substring extends ScalarFunction {
}
@Override
protected ProcessorDefinition makeProcessorDefinition() {
return new SubstringFunctionProcessorDefinition(location(), this,
ProcessorDefinitions.toProcessorDefinition(source),
ProcessorDefinitions.toProcessorDefinition(start),
ProcessorDefinitions.toProcessorDefinition(length));
protected Pipe makePipe() {
return new SubstringFunctionPipe(location(), this,
Expressions.pipe(source),
Expressions.pipe(start),
Expressions.pipe(length));
}
@Override
public boolean foldable() {
return source.foldable()
&& start.foldable()
&& length.foldable();
return source.foldable() && start.foldable() && length.foldable();
}
@Override
@ -92,12 +90,11 @@ public class Substring extends ScalarFunction {
}
protected ScriptTemplate asScriptFrom(ScriptTemplate sourceScript, ScriptTemplate startScript,
ScriptTemplate lengthScript)
{
ScriptTemplate lengthScript) {
// basically, transform the script to InternalSqlScriptUtils.[function_name](function_or_field1, function_or_field2,...)
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"),
"substring",
sourceScript.template(),
return new ScriptTemplate(format(Locale.ROOT, formatTemplate("{sql}.%s(%s,%s,%s)"),
"substring",
sourceScript.template(),
startScript.template(),
lengthScript.template()),
paramsBuilder()
@ -107,8 +104,8 @@ public class Substring extends ScalarFunction {
}
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}
@ -126,4 +123,4 @@ public class Substring extends ScalarFunction {
return new Substring(location(), newChildren.get(0), newChildren.get(1), newChildren.get(2));
}
}
}

View File

@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -15,12 +15,12 @@ import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
public class SubstringFunctionPipe extends Pipe {
private final ProcessorDefinition source, start, length;
private final Pipe source, start, length;
public SubstringFunctionProcessorDefinition(Location location, Expression expression, ProcessorDefinition source,
ProcessorDefinition start, ProcessorDefinition length) {
public SubstringFunctionPipe(Location location, Expression expression, Pipe source,
Pipe start, Pipe length) {
super(location, expression, Arrays.asList(source, start, length));
this.source = source;
this.start = start;
@ -28,7 +28,7 @@ public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 3) {
throw new IllegalArgumentException("expected [3] children but received [" + newChildren.size() + "]");
}
@ -36,10 +36,10 @@ public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
ProcessorDefinition newSource = source.resolveAttributes(resolver);
ProcessorDefinition newStart = start.resolveAttributes(resolver);
ProcessorDefinition newLength = length.resolveAttributes(resolver);
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newSource = source.resolveAttributes(resolver);
Pipe newStart = start.resolveAttributes(resolver);
Pipe newLength = length.resolveAttributes(resolver);
if (newSource == source && newStart == start && newLength == length) {
return this;
}
@ -56,9 +56,9 @@ public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
return source.resolved() && start.resolved() && length.resolved();
}
protected ProcessorDefinition replaceChildren(ProcessorDefinition newSource, ProcessorDefinition newStart,
ProcessorDefinition newLength) {
return new SubstringFunctionProcessorDefinition(location(), expression(), newSource, newStart, newLength);
protected Pipe replaceChildren(Pipe newSource, Pipe newStart,
Pipe newLength) {
return new SubstringFunctionPipe(location(), expression(), newSource, newStart, newLength);
}
@Override
@ -69,8 +69,8 @@ public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
}
@Override
protected NodeInfo<SubstringFunctionProcessorDefinition> info() {
return NodeInfo.create(this, SubstringFunctionProcessorDefinition::new, expression(), source, start, length);
protected NodeInfo<SubstringFunctionPipe> info() {
return NodeInfo.create(this, SubstringFunctionPipe::new, expression(), source, start, length);
}
@Override
@ -78,15 +78,15 @@ public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
return new SubstringFunctionProcessor(source.asProcessor(), start.asProcessor(), length.asProcessor());
}
public ProcessorDefinition source() {
public Pipe source() {
return source;
}
public ProcessorDefinition start() {
public Pipe start() {
return start;
}
public ProcessorDefinition length() {
public Pipe length() {
return length;
}
@ -105,7 +105,7 @@ public class SubstringFunctionProcessorDefinition extends ProcessorDefinition {
return false;
}
SubstringFunctionProcessorDefinition other = (SubstringFunctionProcessorDefinition) obj;
SubstringFunctionPipe other = (SubstringFunctionPipe) obj;
return Objects.equals(source, other.source) && Objects.equals(start, other.start) && Objects.equals(length, other.length);
}
}

View File

@ -8,15 +8,16 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import java.io.IOException;
import java.util.Objects;
public class SubstringFunctionProcessor implements Processor {
public static final String NAME = "ssub";
private final Processor source, start, length;
public static final String NAME = "sb";
public SubstringFunctionProcessor(Processor source, Processor start, Processor length) {
this.source = source;
@ -62,7 +63,7 @@ public class SubstringFunctionProcessor implements Processor {
throw new SqlIllegalArgumentException("A positive number is required for [length]; received [{}]", length);
}
return StringFunctionUtils.substring(source instanceof Character ? source.toString() : (String) source,
return StringFunctionUtils.substring(source instanceof Character ? source.toString() : (String) source,
((Number) start).intValue() - 1, // SQL is 1-based when it comes to string manipulation
((Number) length).intValue());
}

View File

@ -6,13 +6,13 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.util.StringUtils;
@ -20,7 +20,7 @@ import java.util.Locale;
import java.util.Objects;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
public abstract class UnaryStringFunction extends UnaryScalarFunction {
@ -49,27 +49,25 @@ public abstract class UnaryStringFunction extends UnaryScalarFunction {
}
@Override
protected final ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this, ProcessorDefinitions.toProcessorDefinition(field()),
new StringProcessor(operation()));
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new StringProcessor(operation()));
}
protected abstract StringOperation operation();
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
public ScriptTemplate scriptWithField(FieldAttribute field) {
//TODO change this to use _source instead of the exact form (aka field.keyword for text fields)
return new ScriptTemplate(formatScript("doc[{}].value"),
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.isInexact() ? field.exactAttribute().name() : field.name()).build(),
dataType());
}
@Override
protected String formatScript(String template) {
// basically, transform the script to InternalSqlScriptUtils.[function_name](other_function_or_field_name)
return super.formatScript(
format(Locale.ROOT, "{sql}.%s(%s)",
StringUtils.underscoreToLowerCamelCase(operation().toString()),
public String processScript(String template) {
return formatTemplate(
format(Locale.ROOT, "{sql}.%s(%s)",
StringUtils.underscoreToLowerCamelCase(operation().name()),
template));
}

View File

@ -6,21 +6,20 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.string;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.FieldAttribute;
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinitions;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.UnaryProcessorDefinition;
import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.expression.function.scalar.string.StringProcessor.StringOperation;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.UnaryPipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.util.StringUtils;
import java.util.Locale;
import java.util.Objects;
import static java.lang.String.format;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.gen.script.ParamsBuilder.paramsBuilder;
/**
* Base unary function for text manipulating SQL functions that receive as parameter a number
@ -52,25 +51,24 @@ public abstract class UnaryStringIntFunction extends UnaryScalarFunction {
}
@Override
protected final ProcessorDefinition makeProcessorDefinition() {
return new UnaryProcessorDefinition(location(), this, ProcessorDefinitions.toProcessorDefinition(field()),
new StringProcessor(operation()));
protected final Pipe makePipe() {
return new UnaryPipe(location(), this, Expressions.pipe(field()), new StringProcessor(operation()));
}
protected abstract StringOperation operation();
@Override
protected ScriptTemplate asScriptFrom(FieldAttribute field) {
return new ScriptTemplate(formatScript("doc[{}].value"),
public ScriptTemplate scriptWithField(FieldAttribute field) {
return new ScriptTemplate(processScript("doc[{}].value"),
paramsBuilder().variable(field.name()).build(),
dataType());
}
@Override
protected String formatScript(String template) {
return super.formatScript(
format(Locale.ROOT, "{sql}.%s(%s)",
StringUtils.underscoreToLowerCamelCase(operation().toString()),
public String processScript(String template) {
return super.processScript(
format(Locale.ROOT, "{sql}.%s(%s)",
operation().toString().toLowerCase(Locale.ROOT),
template));
}

View File

@ -130,4 +130,4 @@ public final class InternalSqlScriptUtils {
public static Number truncate(Number v, Number s) {
return BinaryMathOperation.TRUNCATE.apply(v, s);
}
}
}

View File

@ -3,14 +3,14 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.execution.search.extractor.BucketExtractor;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.BucketExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.ChainingProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.BucketExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.ChainingProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -40,7 +40,7 @@ public class AggExtractorInput extends LeafInput<BucketExtractor> {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public Pipe resolveAttributes(AttributeResolver resolver) {
return this;
}

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.tree.Location;

View File

@ -3,11 +3,11 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.AggRef;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Attribute;
@ -31,7 +31,7 @@ public class AttributeInput extends NonExecutableInput<Attribute> {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public Pipe resolveAttributes(AttributeResolver resolver) {
return new ReferenceInput(location(), expression(), resolver.resolve(context()));
}

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
@ -11,30 +11,31 @@ import org.elasticsearch.xpack.sql.tree.Location;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public abstract class BinaryProcessorDefinition extends ProcessorDefinition {
public abstract class BinaryPipe extends Pipe {
private final ProcessorDefinition left, right;
private final Pipe left, right;
public BinaryProcessorDefinition(Location location, Expression expression, ProcessorDefinition left, ProcessorDefinition right) {
public BinaryPipe(Location location, Expression expression, Pipe left, Pipe right) {
super(location, expression, Arrays.asList(left, right));
this.left = left;
this.right = right;
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 2) {
throw new IllegalArgumentException("expected [2] children but received [" + newChildren.size() + "]");
}
return replaceChildren(newChildren.get(0), newChildren.get(1));
}
public ProcessorDefinition left() {
public Pipe left() {
return left;
}
public ProcessorDefinition right() {
public Pipe right() {
return right;
}
@ -44,9 +45,9 @@ public abstract class BinaryProcessorDefinition extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
ProcessorDefinition newLeft = left.resolveAttributes(resolver);
ProcessorDefinition newRight = right.resolveAttributes(resolver);
public final Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newLeft = left.resolveAttributes(resolver);
Pipe newRight = right.resolveAttributes(resolver);
if (newLeft == left && newRight == right) {
return this;
}
@ -57,7 +58,7 @@ public abstract class BinaryProcessorDefinition extends ProcessorDefinition {
* Build a copy of this object with new left and right children. Used by
* {@link #resolveAttributes(AttributeResolver)}.
*/
protected abstract BinaryProcessorDefinition replaceChildren(ProcessorDefinition left, ProcessorDefinition right);
protected abstract BinaryPipe replaceChildren(Pipe left, Pipe right);
@Override
public boolean resolved() {
@ -69,4 +70,24 @@ public abstract class BinaryProcessorDefinition extends ProcessorDefinition {
left.collectFields(sourceBuilder);
right.collectFields(sourceBuilder);
}
}
@Override
public int hashCode() {
return Objects.hash(left(), right());
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BinaryPipe other = (BinaryPipe) obj;
return Objects.equals(left(), other.left())
&& Objects.equals(right(), other.right());
}
}

View File

@ -3,12 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
/**
@ -26,7 +26,7 @@ abstract class CommonNonExecutableInput<T> extends NonExecutableInput<T> {
}
@Override
public final ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public final Pipe resolveAttributes(AttributeResolver resolver) {
return this;
}

View File

@ -3,12 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.ConstantProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.ConstantProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -34,7 +34,7 @@ public class ConstantInput extends LeafInput<Object> {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public Pipe resolveAttributes(AttributeResolver resolver) {
return this;
}

View File

@ -3,13 +3,13 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.execution.search.extractor.HitExtractor;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -35,7 +35,7 @@ public class HitExtractorInput extends LeafInput<HitExtractor> {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public Pipe resolveAttributes(AttributeResolver resolver) {
return this;
}

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.tree.Location;
@ -13,7 +13,7 @@ import java.util.Objects;
import static java.util.Collections.emptyList;
public abstract class LeafInput<T> extends ProcessorDefinition {
public abstract class LeafInput<T> extends Pipe {
private T context;
@ -23,7 +23,7 @@ public abstract class LeafInput<T> extends ProcessorDefinition {
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
}

View File

@ -3,11 +3,11 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
public abstract class NonExecutableInput<T> extends LeafInput<T> {

View File

@ -3,30 +3,32 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.FieldExtraction;
import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.Node;
import java.util.List;
/**
* Contains the tree for processing a function, so for example, the {@code ProcessorDefinition} of:
* Processing pipe for an expression (tree). Used for local execution of expressions
* on the invoking node.
* For example, the {@code Pipe} of:
*
* ABS(MAX(foo)) + CAST(bar)
*
* Is an {@code Add} Function with left {@code ABS} over an aggregate (MAX), and
* Is an {@code Add} operator with left {@code ABS} over an aggregate (MAX), and
* right being a {@code CAST} function.
*/
public abstract class ProcessorDefinition extends Node<ProcessorDefinition> implements FieldExtraction {
public abstract class Pipe extends Node<Pipe> implements FieldExtraction {
private final Expression expression;
public ProcessorDefinition(Location location, Expression expression, List<ProcessorDefinition> children) {
public Pipe(Location location, Expression expression, List<Pipe> children) {
super(location, children);
this.expression = expression;
}
@ -41,12 +43,13 @@ public abstract class ProcessorDefinition extends Node<ProcessorDefinition> impl
/**
* Resolve {@link Attribute}s which are unprocessable into
* {@link FieldExtraction}s which are processable.
* {@link Pipe}s that are.
*
* @return {@code this} if the resolution doesn't change the
* definition, a new {@link ProcessorDefinition} otherwise
* definition, a new {@link Pipe} otherwise
*/
public abstract ProcessorDefinition resolveAttributes(AttributeResolver resolver);
public abstract Pipe resolveAttributes(AttributeResolver resolver);
public interface AttributeResolver {
FieldExtraction resolve(Attribute attribute);
}

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.FieldExtraction;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
@ -17,7 +17,7 @@ public class ReferenceInput extends NonExecutableInput<FieldExtraction> {
}
@Override
protected NodeInfo<? extends ProcessorDefinition> info() {
protected NodeInfo<? extends Pipe> info() {
return NodeInfo.create(this, ReferenceInput::new, expression(), context());
}
@ -27,7 +27,7 @@ public class ReferenceInput extends NonExecutableInput<FieldExtraction> {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public Pipe resolveAttributes(AttributeResolver resolver) {
return this;
}

View File

@ -3,13 +3,13 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.execution.search.extractor.ScoreExtractor;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.HitExtractorProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -17,18 +17,18 @@ import java.util.List;
import static java.util.Collections.emptyList;
public class ScoreProcessorDefinition extends ProcessorDefinition {
public ScoreProcessorDefinition(Location location, Expression expression) {
public class ScorePipe extends Pipe {
public ScorePipe(Location location, Expression expression) {
super(location, expression, emptyList());
}
@Override
protected NodeInfo<ScoreProcessorDefinition> info() {
return NodeInfo.create(this, ScoreProcessorDefinition::new, expression());
protected NodeInfo<ScorePipe> info() {
return NodeInfo.create(this, ScorePipe::new, expression());
}
@Override
public final ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public final Pipe replaceChildren(List<Pipe> newChildren) {
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
}
@ -48,7 +48,7 @@ public class ScoreProcessorDefinition extends ProcessorDefinition {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
public Pipe resolveAttributes(AttributeResolver resolver) {
return this;
}

View File

@ -3,12 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition;
package org.elasticsearch.xpack.sql.expression.gen.pipeline;
import org.elasticsearch.xpack.sql.execution.search.SqlSourceBuilder;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.ChainingProcessor;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
import org.elasticsearch.xpack.sql.expression.gen.processor.ChainingProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -17,31 +17,31 @@ import java.util.Objects;
import static java.util.Collections.singletonList;
public final class UnaryProcessorDefinition extends ProcessorDefinition {
public final class UnaryPipe extends Pipe {
private final ProcessorDefinition child;
private final Pipe child;
private final Processor action;
public UnaryProcessorDefinition(Location location, Expression expression, ProcessorDefinition child, Processor action) {
public UnaryPipe(Location location, Expression expression, Pipe child, Processor action) {
super(location, expression, singletonList(child));
this.child = child;
this.action = action;
}
@Override
protected NodeInfo<UnaryProcessorDefinition> info() {
return NodeInfo.create(this, UnaryProcessorDefinition::new, expression(), child, action);
protected NodeInfo<UnaryPipe> info() {
return NodeInfo.create(this, UnaryPipe::new, expression(), child, action);
}
@Override
public ProcessorDefinition replaceChildren(List<ProcessorDefinition> newChildren) {
public Pipe replaceChildren(List<Pipe> newChildren) {
if (newChildren.size() != 1) {
throw new IllegalArgumentException("expected [1] child but received [" + newChildren.size() + "]");
}
return new UnaryProcessorDefinition(location(), expression(), newChildren.get(0), action);
return new UnaryPipe(location(), expression(), newChildren.get(0), action);
}
public ProcessorDefinition child() {
public Pipe child() {
return child;
}
@ -65,12 +65,12 @@ public final class UnaryProcessorDefinition extends ProcessorDefinition {
}
@Override
public ProcessorDefinition resolveAttributes(AttributeResolver resolver) {
ProcessorDefinition newChild = child.resolveAttributes(resolver);
public Pipe resolveAttributes(AttributeResolver resolver) {
Pipe newChild = child.resolveAttributes(resolver);
if (newChild == child) {
return this;
}
return new UnaryProcessorDefinition(location(), expression(), newChild, action);
return new UnaryPipe(location(), expression(), newChild, action);
}
@Override
@ -93,7 +93,7 @@ public final class UnaryProcessorDefinition extends ProcessorDefinition {
return false;
}
UnaryProcessorDefinition other = (UnaryProcessorDefinition) obj;
UnaryPipe other = (UnaryPipe) obj;
return Objects.equals(action, other.action)
&& Objects.equals(child, other.child)
&& Objects.equals(expression(), other.expression());

View File

@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime;
package org.elasticsearch.xpack.sql.expression.gen.processor;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

Some files were not shown because too many files have changed in this diff Show More