Skip to content

Commit 40a30c6

Browse files
authored
SQL: Preserve original source for each expression (#36912)
Improve parsing to save the source for each token alongside the location of each Node/Expression for accurate reproducibility of an expression name and source Fix #36894
1 parent 7686ee7 commit 40a30c6

File tree

337 files changed

+1984
-1896
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

337 files changed

+1984
-1896
lines changed

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/SslConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ private KeyManager[] loadKeyManagers() throws GeneralSecurityException, IOExcept
121121
}
122122

123123

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

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

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

139139
}
@@ -174,6 +174,7 @@ public boolean equals(Object obj) {
174174
&& Objects.equals(truststoreType, other.truststoreType);
175175
}
176176

177+
@Override
177178
public int hashCode() {
178179
return getClass().hashCode();
179180
}

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/AnalysisException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public AnalysisException(Node<?> source, String message, Object... args) {
2121
super(message, args);
2222

2323
Location loc = Location.EMPTY;
24-
if (source != null && source.location() != null) {
25-
loc = source.location();
24+
if (source != null && source.source() != null) {
25+
loc = source.source().source();
2626
}
2727
this.line = loc.getLineNumber();
2828
this.column = loc.getColumnNumber();
@@ -32,8 +32,8 @@ public AnalysisException(Node<?> source, String message, Throwable cause) {
3232
super(message, cause);
3333

3434
Location loc = Location.EMPTY;
35-
if (source != null && source.location() != null) {
36-
loc = source.location();
35+
if (source != null && source.source() != null) {
36+
loc = source.source().source();
3737
}
3838
this.line = loc.getLineNumber();
3939
this.column = loc.getColumnNumber();

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java

Lines changed: 41 additions & 41 deletions
Large diffs are not rendered by default.

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerificationException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
*/
66
package org.elasticsearch.xpack.sql.analysis.analyzer;
77

8-
import java.util.Collection;
9-
import java.util.stream.Collectors;
10-
118
import org.elasticsearch.xpack.sql.analysis.AnalysisException;
129
import org.elasticsearch.xpack.sql.analysis.analyzer.Verifier.Failure;
1310
import org.elasticsearch.xpack.sql.tree.Location;
1411
import org.elasticsearch.xpack.sql.util.StringUtils;
1512

13+
import java.util.Collection;
14+
import java.util.stream.Collectors;
15+
1616

1717
public class VerificationException extends AnalysisException {
1818

@@ -27,7 +27,7 @@ protected VerificationException(Collection<Failure> sources) {
2727
public String getMessage() {
2828
return failures.stream()
2929
.map(f -> {
30-
Location l = f.source().location();
30+
Location l = f.source().source().source();
3131
return "line " + l.getLineNumber() + ":" + l.getColumnNumber() + ": " + f.message();
3232
})
3333
.collect(Collectors.joining(StringUtils.NEW_LINE, "Found " + failures.size() + " problem(s)\n", StringUtils.EMPTY));

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/Querier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private BucketExtractor createExtractor(FieldExtraction ref, BucketExtractor tot
281281
// wrap only agg inputs
282282
proc = proc.transformDown(l -> {
283283
BucketExtractor be = createExtractor(l.context(), totalCount);
284-
return new AggExtractorInput(l.location(), l.expression(), l.action(), be);
284+
return new AggExtractorInput(l.source(), l.expression(), l.action(), be);
285285
}, AggPathInput.class);
286286

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

367-
return new HitExtractorInput(l.location(), l.expression(), he);
367+
return new HitExtractorInput(l.source(), l.expression(), he);
368368
}, ReferenceInput.class);
369369
String hitName = null;
370370
if (hitNames.size() == 1) {

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/Alias.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
99
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
10-
import org.elasticsearch.xpack.sql.tree.Location;
10+
import org.elasticsearch.xpack.sql.tree.Source;
1111
import org.elasticsearch.xpack.sql.tree.NodeInfo;
1212
import org.elasticsearch.xpack.sql.type.DataType;
1313
import org.elasticsearch.xpack.sql.type.EsField;
@@ -36,20 +36,20 @@ public class Alias extends NamedExpression {
3636
*/
3737
private Attribute lazyAttribute;
3838

39-
public Alias(Location location, String name, Expression child) {
40-
this(location, name, null, child, null);
39+
public Alias(Source source, String name, Expression child) {
40+
this(source, name, null, child, null);
4141
}
4242

43-
public Alias(Location location, String name, String qualifier, Expression child) {
44-
this(location, name, qualifier, child, null);
43+
public Alias(Source source, String name, String qualifier, Expression child) {
44+
this(source, name, qualifier, child, null);
4545
}
4646

47-
public Alias(Location location, String name, String qualifier, Expression child, ExpressionId id) {
48-
this(location, name, qualifier, child, id, false);
47+
public Alias(Source source, String name, String qualifier, Expression child, ExpressionId id) {
48+
this(source, name, qualifier, child, id, false);
4949
}
5050

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

7070
public Expression child() {
@@ -104,17 +104,17 @@ private Attribute createAttribute() {
104104

105105
Attribute attr = Expressions.attribute(c);
106106
if (attr != null) {
107-
return attr.clone(location(), name(), qualifier, child.nullable(), id(), synthetic());
107+
return attr.clone(source(), name(), qualifier, child.nullable(), id(), synthetic());
108108
}
109109
else {
110110
// TODO: WE need to fix this fake Field
111-
return new FieldAttribute(location(), null, name(),
111+
return new FieldAttribute(source(), null, name(),
112112
new EsField(name(), child.dataType(), Collections.emptyMap(), true),
113113
qualifier, child.nullable(), id(), synthetic());
114114
}
115115
}
116116

117-
return new UnresolvedAttribute(location(), name(), qualifier);
117+
return new UnresolvedAttribute(source(), name(), qualifier);
118118
}
119119

120120
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/Attribute.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
99
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
10-
import org.elasticsearch.xpack.sql.tree.Location;
10+
import org.elasticsearch.xpack.sql.tree.Source;
1111
import org.elasticsearch.xpack.sql.tree.NodeInfo;
1212

1313
import java.util.List;
@@ -44,16 +44,16 @@ public abstract class Attribute extends NamedExpression {
4444
// can the attr be null - typically used in JOINs
4545
private final boolean nullable;
4646

47-
public Attribute(Location location, String name, String qualifier, ExpressionId id) {
48-
this(location, name, qualifier, true, id);
47+
public Attribute(Source source, String name, String qualifier, ExpressionId id) {
48+
this(source, name, qualifier, true, id);
4949
}
5050

51-
public Attribute(Location location, String name, String qualifier, boolean nullable, ExpressionId id) {
52-
this(location, name, qualifier, nullable, id, false);
51+
public Attribute(Source source, String name, String qualifier, boolean nullable, ExpressionId id) {
52+
this(source, name, qualifier, nullable, id, false);
5353
}
5454

55-
public Attribute(Location location, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
56-
super(location, name, emptyList(), id, synthetic);
55+
public Attribute(Source source, String name, String qualifier, boolean nullable, ExpressionId id, boolean synthetic) {
56+
super(source, name, emptyList(), id, synthetic);
5757
this.qualifier = qualifier;
5858
this.nullable = nullable;
5959
}
@@ -86,19 +86,19 @@ public AttributeSet references() {
8686
return new AttributeSet(this);
8787
}
8888

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

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

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

101-
protected abstract Attribute clone(Location location, String name, String qualifier, boolean nullable, ExpressionId id,
101+
protected abstract Attribute clone(Source source, String name, String qualifier, boolean nullable, ExpressionId id,
102102
boolean synthetic);
103103

104104
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/Exists.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
package org.elasticsearch.xpack.sql.expression;
77

88
import org.elasticsearch.xpack.sql.plan.logical.LogicalPlan;
9-
import org.elasticsearch.xpack.sql.tree.Location;
9+
import org.elasticsearch.xpack.sql.tree.Source;
1010
import org.elasticsearch.xpack.sql.tree.NodeInfo;
1111
import org.elasticsearch.xpack.sql.type.DataType;
1212

1313
public class Exists extends SubQueryExpression {
1414

15-
public Exists(Location location, LogicalPlan query) {
16-
this(location, query, null);
15+
public Exists(Source source, LogicalPlan query) {
16+
this(source, query, null);
1717
}
1818

19-
public Exists(Location location, LogicalPlan query, ExpressionId id) {
20-
super(location, query, id);
19+
public Exists(Source source, LogicalPlan query, ExpressionId id) {
20+
super(source, query, id);
2121
}
2222

2323
@Override
@@ -27,7 +27,7 @@ protected NodeInfo<Exists> info() {
2727

2828
@Override
2929
protected SubQueryExpression clone(LogicalPlan newQuery) {
30-
return new Exists(location(), newQuery);
30+
return new Exists(source(), newQuery);
3131
}
3232

3333
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/Expression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
99
import org.elasticsearch.xpack.sql.capabilities.Resolvable;
1010
import org.elasticsearch.xpack.sql.capabilities.Resolvables;
11-
import org.elasticsearch.xpack.sql.tree.Location;
11+
import org.elasticsearch.xpack.sql.tree.Source;
1212
import org.elasticsearch.xpack.sql.tree.Node;
1313
import org.elasticsearch.xpack.sql.type.DataType;
1414
import org.elasticsearch.xpack.sql.util.StringUtils;
@@ -65,8 +65,8 @@ public String message() {
6565
private Boolean lazyChildrenResolved = null;
6666
private Expression lazyCanonical = null;
6767

68-
public Expression(Location location, List<Expression> children) {
69-
super(location, children);
68+
public Expression(Source source, List<Expression> children) {
69+
super(source, children);
7070
}
7171

7272
// whether the expression can be evaluated statically (folded) or not

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/Expressions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public enum ParamOrdinal {
3535
private Expressions() {}
3636

3737
public static NamedExpression wrapAsNamed(Expression exp) {
38-
return exp instanceof NamedExpression ? (NamedExpression) exp : new Alias(exp.location(), exp.nodeName(), exp);
38+
return exp instanceof NamedExpression ? (NamedExpression) exp : new Alias(exp.source(), exp.nodeName(), exp);
3939
}
4040

4141
public static List<Attribute> asAttributes(List<? extends NamedExpression> named) {

0 commit comments

Comments
 (0)