Skip to content

Commit cfae068

Browse files
Change Parser to allow AND of expressions
1 parent 0fd03d7 commit cfae068

File tree

17 files changed

+897
-554
lines changed

17 files changed

+897
-554
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/lookup/QueryList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,5 +461,5 @@ private IntFunction<Query> shapeQuery() {
461461
}
462462
}
463463

464-
protected record OnlySingleValueParams(Warnings warnings, String multiValueWarningMessage) {}
464+
public record OnlySingleValueParams(Warnings warnings, String multiValueWarningMessage) {}
465465
}

x-pack/plugin/esql/qa/testFixtures/src/main/resources/lookup-join-expression.csv-spec

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

x-pack/plugin/esql/src/main/antlr/parser/Expression.g4

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ matchBooleanExpression
3030

3131
valueExpression
3232
: operatorExpression #valueExpressionDefault
33-
| left=operatorExpression comparisonOperator right=operatorExpression #comparison
33+
| comparisonExpression #comparison
34+
;
35+
36+
comparisonExpression
37+
: left=operatorExpression comparisonOperator right=operatorExpression
3438
;
3539

3640
operatorExpression

x-pack/plugin/esql/src/main/antlr/parser/Join.g4

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ joinTarget
1818
;
1919

2020
joinCondition
21-
: ON joinPredicate (COMMA joinPredicate)*
22-
;
23-
24-
joinPredicate
25-
: valueExpression
21+
: ON qualifiedName (COMMA qualifiedName)* #fieldBasedLookupJoin
22+
| ON comparisonExpression (AND comparisonExpression)* #expressionBasedLookupJoin
2623
;
2724

2825

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/BinaryComparisonQueryList.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,16 @@ public BinaryComparisonQueryList(
3939
Block block,
4040
EsqlBinaryComparison binaryComparison,
4141
ClusterService clusterService,
42-
AliasFilter aliasFilter
42+
AliasFilter aliasFilter,
43+
Warnings warnings
4344
) {
44-
super(field, searchExecutionContext, aliasFilter, block, null);
45+
super(
46+
field,
47+
searchExecutionContext,
48+
aliasFilter,
49+
block,
50+
new OnlySingleValueParams(warnings, "LOOKUP JOIN encountered multi-value")
51+
);
4552
// swap left and right if the field is on the right
4653
// We get a filter in the form left_expr >= right_expr
4754
// here we will swap it to right_expr <= left_expr
@@ -62,9 +69,6 @@ public QueryList onlySingleValues(Warnings warnings, String multiValueWarningMes
6269

6370
@Override
6471
public Query doGetQuery(int position, int firstValueIndex, int valueCount) {
65-
if (valueCount == 0) {
66-
return null;
67-
}
6872
Object value = blockValueReader.apply(firstValueIndex);
6973
// create a new comparison with the value from the block as a literal
7074
EsqlBinaryComparison comparison = binaryComparison.getFunctionType()

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/ExpressionQueryList.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.lucene.search.Query;
1515
import org.elasticsearch.cluster.service.ClusterService;
1616
import org.elasticsearch.compute.data.Block;
17+
import org.elasticsearch.compute.operator.Warnings;
1718
import org.elasticsearch.compute.operator.lookup.LookupEnrichQueryGenerator;
1819
import org.elasticsearch.compute.operator.lookup.QueryList;
1920
import org.elasticsearch.index.mapper.MappedFieldType;
@@ -58,20 +59,21 @@ public ExpressionQueryList(
5859
PhysicalPlan rightPreJoinPlan,
5960
ClusterService clusterService,
6061
LookupFromIndexService.TransportRequest request,
61-
AliasFilter aliasFilter
62+
AliasFilter aliasFilter,
63+
Warnings warnings
6264
) {
6365
if (queryLists.size() < 2 && (rightPreJoinPlan instanceof FilterExec == false) && request.getJoinOnConditions() == null) {
6466
throw new IllegalArgumentException("ExpressionQueryList must have at least two QueryLists or a pre-join filter");
6567
}
6668
this.queryLists = queryLists;
6769
this.context = context;
6870
this.aliasFilter = aliasFilter;
69-
buildJoinOnConditions(request, clusterService);
71+
buildJoinOnConditions(request, clusterService, warnings);
7072
buildPreJoinFilter(rightPreJoinPlan, clusterService);
7173

7274
}
7375

74-
private void buildJoinOnConditions(LookupFromIndexService.TransportRequest request, ClusterService clusterService) {
76+
private void buildJoinOnConditions(LookupFromIndexService.TransportRequest request, ClusterService clusterService, Warnings warnings) {
7577
// we support 2 modes of operation:
7678
// Join on fields
7779
// Join on AND of binary comparisons
@@ -110,7 +112,8 @@ private void buildJoinOnConditions(LookupFromIndexService.TransportRequest reque
110112
block,
111113
binaryComparison,
112114
clusterService,
113-
aliasFilter
115+
aliasFilter,
116+
warnings
114117
)
115118
);
116119
matched = true;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/enrich/LookupFromIndexService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected LookupEnrichQueryGenerator queryList(
128128
if (queryLists.size() == 1 && physicalPlan instanceof FilterExec == false && request.joinOnConditions == null) {
129129
return queryLists.getFirst();
130130
}
131-
return new ExpressionQueryList(queryLists, context, physicalPlan, clusterService, request, aliasFilter);
131+
return new ExpressionQueryList(queryLists, context, physicalPlan, clusterService, request, aliasFilter, warnings);
132132

133133
}
134134

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/EsqlBaseParser.interp

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)