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: Preserve original source for each expression #36912

Merged
merged 8 commits into from Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Expand Up @@ -21,8 +21,8 @@ public AnalysisException(Node<?> source, String message, Object... args) {
super(message, args);

Location loc = Location.EMPTY;
if (source != null && source.location() != null) {
loc = source.location();
if (source != null && source.source() != null) {
loc = source.source().location();
}
this.line = loc.getLineNumber();
this.column = loc.getColumnNumber();
Expand All @@ -32,8 +32,8 @@ public AnalysisException(Node<?> source, String message, Throwable cause) {
super(message, cause);

Location loc = Location.EMPTY;
if (source != null && source.location() != null) {
loc = source.location();
if (source != null && source.source() != null) {
loc = source.source().location();
}
this.line = loc.getLineNumber();
this.column = loc.getColumnNumber();
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -5,14 +5,14 @@
*/
package org.elasticsearch.xpack.sql.analysis.analyzer;

import java.util.Collection;
import java.util.stream.Collectors;

import org.elasticsearch.xpack.sql.analysis.AnalysisException;
import org.elasticsearch.xpack.sql.analysis.analyzer.Verifier.Failure;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.util.StringUtils;

import java.util.Collection;
import java.util.stream.Collectors;


public class VerificationException extends AnalysisException {

Expand All @@ -27,7 +27,7 @@ protected VerificationException(Collection<Failure> sources) {
public String getMessage() {
return failures.stream()
.map(f -> {
Location l = f.source().location();
Location l = f.source().source().location();
return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message();
})
.collect(Collectors.joining(StringUtils.NEW_LINE, "Found " + failures.size() + " problem(s)\n", StringUtils.EMPTY));
Expand Down
Expand Up @@ -281,7 +281,7 @@ private BucketExtractor createExtractor(FieldExtraction ref, BucketExtractor tot
// wrap only agg inputs
proc = proc.transformDown(l -> {
BucketExtractor be = createExtractor(l.context(), totalCount);
return new AggExtractorInput(l.location(), l.expression(), l.action(), be);
return new AggExtractorInput(l.source(), l.expression(), l.action(), be);
}, AggPathInput.class);

return new ComputingExtractor(proc.asProcessor());
Expand Down Expand Up @@ -364,7 +364,7 @@ private HitExtractor createExtractor(FieldExtraction ref) {
throw new SqlIllegalArgumentException("Multi-level nested fields [{}] not supported yet", hitNames);
}

return new HitExtractorInput(l.location(), l.expression(), he);
return new HitExtractorInput(l.source(), l.expression(), he);
}, ReferenceInput.class);
String hitName = null;
if (hitNames.size() == 1) {
Expand Down
Expand Up @@ -7,7 +7,7 @@

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.Source;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.EsField;
Expand Down Expand Up @@ -36,19 +36,19 @@ public class Alias extends NamedExpression {
*/
private Attribute lazyAttribute;

public Alias(Location location, String name, Expression child) {
public Alias(Source location, String name, Expression child) {
this(location, name, null, child, null);
}

public Alias(Location location, String name, String qualifier, Expression child) {
public Alias(Source location, String name, String qualifier, Expression child) {
this(location, name, qualifier, child, null);
}

public Alias(Location location, String name, String qualifier, Expression child, ExpressionId id) {
public Alias(Source location, String name, String qualifier, Expression child, ExpressionId id) {
this(location, name, qualifier, child, id, false);
}

public Alias(Location location, String name, String qualifier, Expression child, ExpressionId id, boolean synthetic) {
public Alias(Source location, String name, String qualifier, Expression child, ExpressionId id, boolean synthetic) {
super(location, name, singletonList(child), id, synthetic);
this.child = child;
this.qualifier = qualifier;
Expand All @@ -64,7 +64,7 @@ public Expression replaceChildren(List<Expression> newChildren) {
if (newChildren.size() != 1) {
throw new IllegalArgumentException("expected [1] child but received [" + newChildren.size() + "]");
}
return new Alias(location(), name(), qualifier, newChildren.get(0), id(), synthetic());
return new Alias(source(), name(), qualifier, newChildren.get(0), id(), synthetic());
}

public Expression child() {
Expand Down Expand Up @@ -104,17 +104,17 @@ private Attribute createAttribute() {

Attribute attr = Expressions.attribute(c);
if (attr != null) {
return attr.clone(location(), name(), qualifier, child.nullable(), id(), synthetic());
return attr.clone(source(), name(), qualifier, child.nullable(), id(), synthetic());
}
else {
// TODO: WE need to fix this fake Field
return new FieldAttribute(location(), null, name(),
return new FieldAttribute(source(), null, name(),
new EsField(name(), child.dataType(), Collections.emptyMap(), true),
qualifier, child.nullable(), id(), synthetic());
}
}

return new UnresolvedAttribute(location(), name(), qualifier);
return new UnresolvedAttribute(source(), name(), qualifier);
}

@Override
Expand Down
Expand Up @@ -7,7 +7,7 @@

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.Source;
import org.elasticsearch.xpack.sql.tree.NodeInfo;

import java.util.List;
Expand Down Expand Up @@ -44,15 +44,15 @@ public abstract class Attribute extends NamedExpression {
// can the attr be null - typically used in JOINs
private final boolean nullable;

public Attribute(Location location, String name, String qualifier, ExpressionId id) {
public Attribute(Source location, String name, String qualifier, ExpressionId id) {
this(location, name, qualifier, true, id);
}

public Attribute(Location location, String name, String qualifier, boolean nullable, ExpressionId id) {
public Attribute(Source location, String name, String qualifier, boolean nullable, ExpressionId id) {
this(location, name, qualifier, nullable, id, false);
}

public Attribute(Location location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
public Attribute(Source location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
super(location, name, emptyList(), id, synthetic);
this.qualifier = qualifier;
this.nullable = nullable;
Expand Down Expand Up @@ -86,19 +86,19 @@ public AttributeSet references() {
return new AttributeSet(this);
}

public Attribute withLocation(Location location) {
return Objects.equals(location(), location) ? this : clone(location, name(), qualifier(), nullable(), id(), synthetic());
public Attribute withLocation(Source location) {
return Objects.equals(source(), location) ? this : clone(location, name(), qualifier(), nullable(), id(), synthetic());
}

public Attribute withQualifier(String qualifier) {
return Objects.equals(qualifier(), qualifier) ? this : clone(location(), name(), qualifier, nullable(), id(), synthetic());
return Objects.equals(qualifier(), qualifier) ? this : clone(source(), name(), qualifier, nullable(), id(), synthetic());
}

public Attribute withNullability(boolean nullable) {
return Objects.equals(nullable(), nullable) ? this : clone(location(), name(), qualifier(), nullable, id(), synthetic());
return Objects.equals(nullable(), nullable) ? this : clone(source(), name(), qualifier(), nullable, id(), synthetic());
}

protected abstract Attribute clone(Location location, String name, String qualifier, boolean nullable, ExpressionId id,
protected abstract Attribute clone(Source location, String name, String qualifier, boolean nullable, ExpressionId id,
boolean synthetic);

@Override
Expand Down
Expand Up @@ -6,17 +6,17 @@
package org.elasticsearch.xpack.sql.expression;

import org.elasticsearch.xpack.sql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.Source;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;

public class Exists extends SubQueryExpression {

public Exists(Location location, LogicalPlan query) {
public Exists(Source location, LogicalPlan query) {
this(location, query, null);
}

public Exists(Location location, LogicalPlan query, ExpressionId id) {
public Exists(Source location, LogicalPlan query, ExpressionId id) {
super(location, query, id);
}

Expand All @@ -27,7 +27,7 @@ protected NodeInfo<Exists> info() {

@Override
protected SubQueryExpression clone(LogicalPlan newQuery) {
return new Exists(location(), newQuery);
return new Exists(source(), newQuery);
}

@Override
Expand Down
Expand Up @@ -8,7 +8,7 @@
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.capabilities.Resolvable;
import org.elasticsearch.xpack.sql.capabilities.Resolvables;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.Source;
import org.elasticsearch.xpack.sql.tree.Node;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.StringUtils;
Expand Down Expand Up @@ -65,7 +65,7 @@ public String message() {
private Boolean lazyChildrenResolved = null;
private Expression lazyCanonical = null;

public Expression(Location location, List<Expression> children) {
public Expression(Source location, List<Expression> children) {
super(location, children);
}

Expand Down
Expand Up @@ -35,7 +35,7 @@ public enum ParamOrdinal {
private Expressions() {}

public static NamedExpression wrapAsNamed(Expression exp) {
return exp instanceof NamedExpression ? (NamedExpression) exp : new Alias(exp.location(), exp.nodeName(), exp);
return exp instanceof NamedExpression ? (NamedExpression) exp : new Alias(exp.source(), exp.nodeName(), exp);
}

public static List<Attribute> asAttributes(List<? extends NamedExpression> named) {
Expand Down
Expand Up @@ -6,7 +6,7 @@
package org.elasticsearch.xpack.sql.expression;

import org.elasticsearch.common.Strings;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.Source;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.EsField;
Expand All @@ -29,15 +29,15 @@ public class FieldAttribute extends TypedAttribute {
private final String path;
private final EsField field;

public FieldAttribute(Location location, String name, EsField field) {
public FieldAttribute(Source location, String name, EsField field) {
this(location, null, name, field);
}

public FieldAttribute(Location location, FieldAttribute parent, String name, EsField field) {
public FieldAttribute(Source location, FieldAttribute parent, String name, EsField field) {
this(location, parent, name, field, null, true, null, false);
}

public FieldAttribute(Location location, FieldAttribute parent, String name, EsField field, String qualifier,
public FieldAttribute(Source location, FieldAttribute parent, String name, EsField field, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic) {
super(location, name, field.getDataType(), qualifier, nullable, id, synthetic);
this.path = parent != null ? parent.name() : StringUtils.EMPTY;
Expand Down Expand Up @@ -93,16 +93,16 @@ public FieldAttribute exactAttribute() {
}

private FieldAttribute innerField(EsField type) {
return new FieldAttribute(location(), this, name() + "." + type.getName(), type, qualifier(), nullable(), id(), synthetic());
return new FieldAttribute(source(), this, name() + "." + type.getName(), type, qualifier(), nullable(), id(), synthetic());
}

@Override
protected Expression canonicalize() {
return new FieldAttribute(location(), null, "<none>", field, null, true, id(), false);
return new FieldAttribute(source(), null, "<none>", field, null, true, id(), false);
}

@Override
protected Attribute clone(Location location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
protected Attribute clone(Source location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
FieldAttribute qualifiedParent = parent != null ? (FieldAttribute) parent.withQualifier(qualifier) : null;
return new FieldAttribute(location, qualifiedParent, name, field, qualifier, nullable, id, synthetic);
}
Expand Down
Expand Up @@ -5,15 +5,15 @@
*/
package org.elasticsearch.xpack.sql.expression;

import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.Source;

import java.util.List;

import static java.util.Collections.emptyList;

public abstract class LeafExpression extends Expression {

protected LeafExpression(Location location) {
protected LeafExpression(Source location) {
super(location, emptyList());
}

Expand Down
Expand Up @@ -8,7 +8,7 @@
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.Source;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.DataTypeConversion;
Expand All @@ -24,18 +24,18 @@
*/
public class Literal extends NamedExpression {

public static final Literal TRUE = Literal.of(Location.EMPTY, Boolean.TRUE);
public static final Literal FALSE = Literal.of(Location.EMPTY, Boolean.FALSE);
public static final Literal NULL = Literal.of(Location.EMPTY, null);
public static final Literal TRUE = Literal.of(Source.EMPTY, Boolean.TRUE);
public static final Literal FALSE = Literal.of(Source.EMPTY, Boolean.FALSE);
public static final Literal NULL = Literal.of(Source.EMPTY, null);

private final Object value;
private final DataType dataType;

public Literal(Location location, Object value, DataType dataType) {
public Literal(Source location, Object value, DataType dataType) {
this(location, null, value, dataType);
}

public Literal(Location location, String name, Object value, DataType dataType) {
public Literal(Source location, String name, Object value, DataType dataType) {
super(location, name == null ? String.valueOf(value) : name, emptyList(), null);
this.dataType = dataType;
this.value = DataTypeConversion.convert(value, dataType);
Expand Down Expand Up @@ -77,7 +77,7 @@ public Object fold() {

@Override
public Attribute toAttribute() {
return new LiteralAttribute(location(), name(), null, false, id(), false, dataType, this);
return new LiteralAttribute(source(), name(), null, false, id(), false, dataType, this);
}

@Override
Expand All @@ -98,7 +98,7 @@ public AttributeSet references() {
@Override
protected Expression canonicalize() {
String s = String.valueOf(value);
return name().equals(s) ? this : Literal.of(location(), value);
return name().equals(s) ? this : Literal.of(source(), value);
}

@Override
Expand Down Expand Up @@ -129,11 +129,11 @@ public String toString() {
/**
* Utility method for creating 'in-line' Literals (out of values instead of expressions).
*/
public static Literal of(Location loc, Object value) {
public static Literal of(Source source, Object value) {
if (value instanceof Literal) {
return (Literal) value;
}
return new Literal(loc, value, DataTypes.fromJava(value));
return new Literal(source, value, DataTypes.fromJava(value));
}

/**
Expand Down Expand Up @@ -161,11 +161,11 @@ public static Literal of(String name, Expression foldable) {
if (name == null) {
name = foldable instanceof NamedExpression ? ((NamedExpression) foldable).name() : String.valueOf(fold);
}
return new Literal(foldable.location(), name, fold, foldable.dataType());
return new Literal(foldable.source(), name, fold, foldable.dataType());
}

public static Literal of(Expression source, Object value) {
String name = source instanceof NamedExpression ? ((NamedExpression) source).name() : String.valueOf(value);
return new Literal(source.location(), name, value, source.dataType());
return new Literal(source.source(), name, value, source.dataType());
}
}