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 all commits
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 @@ -121,19 +121,19 @@ private KeyManager[] loadKeyManagers() throws GeneralSecurityException, IOExcept
}


private KeyStore loadKeyStore(String location, char[] pass, String keyStoreType) throws GeneralSecurityException, IOException {
private KeyStore loadKeyStore(String source, char[] pass, String keyStoreType) throws GeneralSecurityException, IOException {
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
Path path = Paths.get(location);
Path path = Paths.get(source);

if (!Files.exists(path)) {
throw new ClientException(
"Expected to find keystore file at [" + location + "] but was unable to. Make sure you have specified a valid URI.");
"Expected to find keystore file at [" + source + "] but was unable to. Make sure you have specified a valid URI.");
}

try (InputStream in = Files.newInputStream(Paths.get(location), StandardOpenOption.READ)) {
try (InputStream in = Files.newInputStream(Paths.get(source), StandardOpenOption.READ)) {
keyStore.load(in, pass);
} catch (Exception ex) {
throw new ClientException("Cannot open keystore [" + location + "] - " + ex.getMessage(), ex);
throw new ClientException("Cannot open keystore [" + source + "] - " + ex.getMessage(), ex);
} finally {

}
Expand Down Expand Up @@ -174,6 +174,7 @@ public boolean equals(Object obj) {
&& Objects.equals(truststoreType, other.truststoreType);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
Expand Down
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().source();
}
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().source();
}
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().source();
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,20 +36,20 @@ public class Alias extends NamedExpression {
*/
private Attribute lazyAttribute;

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

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

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

public Alias(Location location, String name, String qualifier, Expression child, ExpressionId id, boolean synthetic) {
super(location, name, singletonList(child), id, synthetic);
public Alias(Source source, String name, String qualifier, Expression child, ExpressionId id, boolean synthetic) {
super(source, 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,16 +44,16 @@ 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) {
this(location, name, qualifier, true, id);
public Attribute(Source source, String name, String qualifier, ExpressionId id) {
this(source, name, qualifier, true, id);
}

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

public Attribute(Location location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
super(location, name, emptyList(), id, synthetic);
public Attribute(Source source, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
super(source, 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 source) {
return Objects.equals(source(), source) ? this : clone(source, 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 source, String name, String qualifier, boolean nullable, ExpressionId id,
boolean synthetic);

@Override
Expand Down
Expand Up @@ -6,18 +6,18 @@
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) {
this(location, query, null);
public Exists(Source source, LogicalPlan query) {
this(source, query, null);
}

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

@Override
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,8 +65,8 @@ public String message() {
private Boolean lazyChildrenResolved = null;
private Expression lazyCanonical = null;

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

// whether the expression can be evaluated statically (folded) or not
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,17 +29,17 @@ public class FieldAttribute extends TypedAttribute {
private final String path;
private final EsField field;

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

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

public FieldAttribute(Location location, FieldAttribute parent, String name, EsField field, String qualifier,
public FieldAttribute(Source source, FieldAttribute parent, String name, EsField field, String qualifier,
boolean nullable, ExpressionId id, boolean synthetic) {
super(location, name, field.getDataType(), qualifier, nullable, id, synthetic);
super(source, name, field.getDataType(), qualifier, nullable, id, synthetic);
this.path = parent != null ? parent.name() : StringUtils.EMPTY;
this.parent = parent;
this.field = field;
Expand Down Expand Up @@ -93,18 +93,18 @@ 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 source, 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);
return new FieldAttribute(source, qualifiedParent, name, field, qualifier, nullable, id, synthetic);
}

@Override
Expand Down
Expand Up @@ -5,16 +5,16 @@
*/
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) {
super(location, emptyList());
protected LeafExpression(Source source) {
super(source, emptyList());
}

@Override
Expand Down