Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

SQL: Internal refactoring of operators as functions #34097

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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;
Expand Down Expand Up @@ -1007,8 +1007,8 @@ private Expression implicitCast(Expression e) {
// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
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;
Expand Down Expand Up @@ -275,7 +275,7 @@ private BucketExtractor createExtractor(FieldExtraction ref, BucketExtractor tot
}

if (ref instanceof ComputedRef) {
ProcessorDefinition proc = ((ComputedRef) ref).processor();
Pipe proc = ((ComputedRef) ref).processor();

// wrap only agg inputs
proc = proc.transformDown(l -> {
Expand Down Expand Up @@ -351,7 +351,7 @@ private HitExtractor createExtractor(FieldExtraction ref) {
}

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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -91,6 +93,11 @@ public Attribute toAttribute() {
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();
Expand All @@ -114,4 +121,4 @@ private Attribute createAttribute() {
public String toString() {
return child + " AS " + name() + "#" + id();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +63,11 @@ public final Expression replaceChildren(List<Expression> newChildren) {
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;
}
Expand Down Expand Up @@ -103,6 +111,11 @@ public int semanticHash() {
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;
Expand Down Expand Up @@ -130,4 +143,4 @@ public String toString() {
}

protected abstract String label();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ public boolean resolved() {
public String toString() {
return nodeName() + "[" + propertiesToString(false) + "]";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,6 +114,13 @@ public static boolean equalsAsAttribute(Expression left, Expression right) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +79,11 @@ public Attribute toAttribute() {
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");
Expand All @@ -87,9 +94,15 @@ public AttributeSet references() {
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
Expand All @@ -102,8 +115,7 @@ public boolean equals(Object obj) {
}

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,12 +32,13 @@ protected LiteralAttribute clone(Location location, String name, String qualifie
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -41,6 +51,20 @@ public boolean synthetic() {

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);
Expand All @@ -67,4 +91,4 @@ public boolean equals(Object obj) {
&& Objects.equals(name, other.name)
&& Objects.equals(children(), other.children());
}
}
}
Loading