Skip to content

Commit 15515a6

Browse files
authored
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
1 parent a2c9418 commit 15515a6

File tree

207 files changed

+3048
-2735
lines changed

Some content is hidden

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

207 files changed

+3048
-2735
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.elasticsearch.xpack.sql.expression.function.Functions;
3030
import org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction;
3131
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
32-
import org.elasticsearch.xpack.sql.expression.function.scalar.arithmetic.ArithmeticFunction;
32+
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.ArithmeticOperation;
3333
import org.elasticsearch.xpack.sql.plan.TableIdentifier;
3434
import org.elasticsearch.xpack.sql.plan.logical.Aggregate;
3535
import org.elasticsearch.xpack.sql.plan.logical.EsRelation;
@@ -1007,8 +1007,8 @@ private Expression implicitCast(Expression e) {
10071007
// BinaryOperations are ignored as they are pushed down to ES
10081008
// and casting (and thus Aliasing when folding) gets in the way
10091009

1010-
if (e instanceof ArithmeticFunction) {
1011-
ArithmeticFunction f = (ArithmeticFunction) e;
1010+
if (e instanceof ArithmeticOperation) {
1011+
ArithmeticOperation f = (ArithmeticOperation) e;
10121012
left = f.left();
10131013
right = f.right();
10141014
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import org.elasticsearch.xpack.sql.execution.search.extractor.FieldHitExtractor;
3333
import org.elasticsearch.xpack.sql.execution.search.extractor.HitExtractor;
3434
import org.elasticsearch.xpack.sql.execution.search.extractor.MetricAggExtractor;
35-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.AggExtractorInput;
36-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.AggPathInput;
37-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.HitExtractorInput;
38-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
39-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ReferenceInput;
35+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AggExtractorInput;
36+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.AggPathInput;
37+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.HitExtractorInput;
38+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
39+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.ReferenceInput;
4040
import org.elasticsearch.xpack.sql.querydsl.agg.Aggs;
4141
import org.elasticsearch.xpack.sql.querydsl.container.ComputedRef;
4242
import org.elasticsearch.xpack.sql.querydsl.container.GlobalCountRef;
@@ -275,7 +275,7 @@ private BucketExtractor createExtractor(FieldExtraction ref, BucketExtractor tot
275275
}
276276

277277
if (ref instanceof ComputedRef) {
278-
ProcessorDefinition proc = ((ComputedRef) ref).processor();
278+
Pipe proc = ((ComputedRef) ref).processor();
279279

280280
// wrap only agg inputs
281281
proc = proc.transformDown(l -> {
@@ -351,7 +351,7 @@ private HitExtractor createExtractor(FieldExtraction ref) {
351351
}
352352

353353
if (ref instanceof ComputedRef) {
354-
ProcessorDefinition proc = ((ComputedRef) ref).processor();
354+
Pipe proc = ((ComputedRef) ref).processor();
355355
// collect hitNames
356356
Set<String> hitNames = new LinkedHashSet<>();
357357
proc = proc.transformDown(l -> {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import org.elasticsearch.common.io.stream.StreamOutput;
1010
import org.elasticsearch.search.SearchHit;
1111
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
12-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.HitExtractorProcessor;
13-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.runtime.Processor;
12+
import org.elasticsearch.xpack.sql.expression.gen.processor.HitExtractorProcessor;
13+
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
1414

1515
import java.io.IOException;
1616
import java.util.Objects;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
*/
66
package org.elasticsearch.xpack.sql.expression;
77

8+
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
9+
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
810
import org.elasticsearch.xpack.sql.tree.Location;
911
import org.elasticsearch.xpack.sql.tree.NodeInfo;
1012
import org.elasticsearch.xpack.sql.type.DataType;
1113
import org.elasticsearch.xpack.sql.type.EsField;
1214

13-
import static java.util.Collections.singletonList;
14-
1515
import java.util.Collections;
1616
import java.util.List;
1717

18+
import static java.util.Collections.singletonList;
19+
1820
/**
1921
* An {@code Alias} is a {@code NamedExpression} that gets renamed to something else through the Alias.
2022
*
@@ -91,6 +93,11 @@ public Attribute toAttribute() {
9193
return lazyAttribute;
9294
}
9395

96+
@Override
97+
public ScriptTemplate asScript() {
98+
throw new SqlIllegalArgumentException("Encountered a bug; an alias should never be scripted");
99+
}
100+
94101
private Attribute createAttribute() {
95102
if (resolved()) {
96103
Expression c = child();
@@ -114,4 +121,4 @@ private Attribute createAttribute() {
114121
public String toString() {
115122
return child + " AS " + name() + "#" + id();
116123
}
117-
}
124+
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66
package org.elasticsearch.xpack.sql.expression;
77

8+
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
9+
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
810
import org.elasticsearch.xpack.sql.tree.Location;
11+
import org.elasticsearch.xpack.sql.tree.NodeInfo;
912

1013
import java.util.List;
1114
import java.util.Objects;
@@ -60,6 +63,11 @@ public final Expression replaceChildren(List<Expression> newChildren) {
6063
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
6164
}
6265

66+
@Override
67+
public ScriptTemplate asScript() {
68+
throw new SqlIllegalArgumentException("Encountered a bug - an attribute should never be scripted");
69+
}
70+
6371
public String qualifier() {
6472
return qualifier;
6573
}
@@ -103,6 +111,11 @@ public int semanticHash() {
103111
return id().hashCode();
104112
}
105113

114+
@Override
115+
protected NodeInfo<? extends Expression> info() {
116+
return null;
117+
}
118+
106119
@Override
107120
public boolean semanticEquals(Expression other) {
108121
return other instanceof Attribute ? id().equals(((Attribute) other).id()) : false;
@@ -130,4 +143,4 @@ public String toString() {
130143
}
131144

132145
protected abstract String label();
133-
}
146+
}

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

Lines changed: 0 additions & 82 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ public boolean resolved() {
132132
public String toString() {
133133
return nodeName() + "[" + propertiesToString(false) + "]";
134134
}
135-
}
135+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66
package org.elasticsearch.xpack.sql.expression;
77

8+
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
89
import org.elasticsearch.xpack.sql.expression.Expression.TypeResolution;
10+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
911

1012
import java.util.ArrayList;
1113
import java.util.Collection;
@@ -112,6 +114,13 @@ public static boolean equalsAsAttribute(Expression left, Expression right) {
112114
return true;
113115
}
114116

117+
public static Pipe pipe(Expression e) {
118+
if (e instanceof NamedExpression) {
119+
return ((NamedExpression) e).asPipe();
120+
}
121+
throw new SqlIllegalArgumentException("Cannot create pipe for {}", e);
122+
}
123+
115124
public static TypeResolution typeMustBe(Expression e, Predicate<Expression> predicate, String message) {
116125
return predicate.test(e) ? TypeResolution.TYPE_RESOLVED : new TypeResolution(message);
117126
}

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

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

88
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
9+
import org.elasticsearch.xpack.sql.expression.gen.script.Params;
10+
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
911
import org.elasticsearch.xpack.sql.tree.Location;
1012
import org.elasticsearch.xpack.sql.tree.NodeInfo;
1113
import org.elasticsearch.xpack.sql.type.DataType;
@@ -77,6 +79,11 @@ public Attribute toAttribute() {
7779
return new LiteralAttribute(location(), name(), null, false, id(), false, dataType, this);
7880
}
7981

82+
@Override
83+
public ScriptTemplate asScript() {
84+
return new ScriptTemplate(String.valueOf(value), Params.EMPTY, dataType);
85+
}
86+
8087
@Override
8188
public Expression replaceChildren(List<Expression> newChildren) {
8289
throw new UnsupportedOperationException("this type of node doesn't have any children to replace");
@@ -87,9 +94,15 @@ public AttributeSet references() {
8794
return AttributeSet.EMPTY;
8895
}
8996

97+
@Override
98+
protected Expression canonicalize() {
99+
String s = String.valueOf(value);
100+
return name().equals(s) ? this : Literal.of(location(), value);
101+
}
102+
90103
@Override
91104
public int hashCode() {
92-
return Objects.hash(name(), value, dataType);
105+
return Objects.hash(value, dataType);
93106
}
94107

95108
@Override
@@ -102,8 +115,7 @@ public boolean equals(Object obj) {
102115
}
103116

104117
Literal other = (Literal) obj;
105-
return Objects.equals(name(), other.name())
106-
&& Objects.equals(value, other.value)
118+
return Objects.equals(value, other.value)
107119
&& Objects.equals(dataType, other.dataType);
108120
}
109121

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.sql.expression;
77

8-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ConstantInput;
9-
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
8+
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
109
import org.elasticsearch.xpack.sql.tree.Location;
1110
import org.elasticsearch.xpack.sql.tree.NodeInfo;
1211
import org.elasticsearch.xpack.sql.type.DataType;
@@ -33,12 +32,13 @@ protected LiteralAttribute clone(Location location, String name, String qualifie
3332
return new LiteralAttribute(location, name, qualifier, nullable, id, synthetic, dataType(), literal);
3433
}
3534

36-
public ProcessorDefinition asProcessorDefinition() {
37-
return new ConstantInput(location(), literal, literal.value());
38-
}
39-
4035
@Override
4136
protected String label() {
4237
return "c";
4338
}
39+
40+
@Override
41+
public Pipe asPipe() {
42+
return literal.asPipe();
43+
}
4444
}

0 commit comments

Comments
 (0)