Skip to content

Commit 2b642b9

Browse files
committed
prohibit * in join patterns
1 parent a5f713f commit 2b642b9

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,14 @@ public PlanFactory visitJoinCommand(EsqlBaseParser.JoinCommandContext ctx) {
523523
}
524524

525525
var target = ctx.joinTarget();
526+
var rightPattern = visitIndexPattern(List.of(target.index));
527+
if (rightPattern.contains(WILDCARD)) {
528+
throw new ParsingException(source(target), "invalid index pattern [{}], * is not allowed in LOOKUP JOIN", rightPattern);
529+
}
530+
526531
UnresolvedRelation right = new UnresolvedRelation(
527532
source(target),
528-
new TableIdentifier(source(target.index), null, visitIndexPattern(List.of(target.index))),
533+
new TableIdentifier(source(target.index), null, rightPattern),
529534
false,
530535
emptyList(),
531536
IndexMode.LOOKUP,

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,8 +2943,8 @@ public void testNamedFunctionArgumentWithUnsupportedNamedParameterTypes() {
29432943
}
29442944

29452945
public void testValidJoinPattern() {
2946-
var basePattern = randomIndexPatterns();
2947-
var joinPattern = randomIndexPattern();
2946+
var basePattern = randomIndexPatterns(true, true);
2947+
var joinPattern = randomIndexPattern(true, false);
29482948
var onField = randomIdentifier();
29492949
var type = randomFrom("", "LOOKUP ");
29502950

@@ -2960,13 +2960,20 @@ public void testValidJoinPattern() {
29602960
assertThat(joinType.coreJoin().joinName(), equalTo("LEFT OUTER"));
29612961
}
29622962

2963-
private static String randomIndexPatterns() {
2964-
return maybeQuote(String.join(",", randomList(1, 5, StatementParserTests::randomIndexPattern)));
2963+
public void testInvalidJoinPatterns() {
2964+
expectError(
2965+
"FROM " + randomIndexPatterns(true, true) + " | JOIN my-index-pattern* ON " + randomIdentifier(),
2966+
"invalid index pattern [my-index-pattern*], * is not allowed in LOOKUP JOIN"
2967+
);
2968+
}
2969+
2970+
private static String randomIndexPatterns(boolean includeRemotes, boolean includePatterns) {
2971+
return maybeQuote(String.join(",", randomList(1, 5, () -> randomIndexPattern(includeRemotes, includePatterns))));
29652972
}
29662973

2967-
private static String randomIndexPattern() {
2968-
String pattern = maybeQuote(randomIndexIdentifier());
2969-
if (randomBoolean()) {
2974+
private static String randomIndexPattern(boolean includeRemotes, boolean includePatterns) {
2975+
String pattern = maybeQuote(randomIndexIdentifier(includePatterns));
2976+
if (includeRemotes && randomBoolean()) {
29702977
var cluster = randomIdentifier();
29712978
pattern = maybeQuote(cluster + ":" + pattern);
29722979
}
@@ -2976,7 +2983,7 @@ private static String randomIndexPattern() {
29762983
/**
29772984
* This generates random valid index, alias or pattern
29782985
*/
2979-
private static String randomIndexIdentifier() {
2986+
private static String randomIndexIdentifier(boolean includePatterns) {
29802987
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#indices-create-api-path-params
29812988
var validFirstCharacters = "abcdefghijklmnopqrstuvwxyz0123456789!'$^&";
29822989
var validCharacters = validFirstCharacters + "+-_.";
@@ -2989,7 +2996,7 @@ private static String randomIndexIdentifier() {
29892996
for (int i = 0; i < randomIntBetween(1, 100); i++) {
29902997
index.append(randomCharacterFrom(validCharacters));
29912998
}
2992-
if (randomBoolean()) {// pattern
2999+
if (includePatterns && randomBoolean()) {// pattern
29933000
index.append('*');
29943001
}
29953002
return index.toString();

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/190_lookup_join.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,16 @@ pattern-multiple:
176176
query: 'FROM test-lookup-1 | LOOKUP JOIN test-lookup-* ON key'
177177
catch: "bad_request"
178178

179-
- match: { error.type: "verification_exception" }
180-
- contains: { error.reason: "Found 1 problem\nline 1:34: invalid [test-lookup-*] resolution in lookup mode to [2] indices" }
179+
- match: { error.type: "parsing_exception" }
180+
- contains: { error.reason: "line 1:34: invalid index pattern [test-lookup-*], * is not allowed in LOOKUP JOIN" }
181181

182182
---
183183
pattern-single:
184184
- do:
185185
esql.query:
186186
body:
187187
query: 'FROM test | SORT key | LOOKUP JOIN test-lookup-1* ON key | LIMIT 3'
188+
catch: "bad_request"
188189

189-
- match: {columns.0.name: "key"}
190-
- match: {columns.0.type: "long"}
191-
- match: {columns.1.name: "color"}
192-
- match: {columns.1.type: "keyword"}
193-
- match: {values.0: [1, "cyan"]}
194-
- match: {values.1: [2, "yellow"]}
190+
- match: { error.type: "parsing_exception" }
191+
- contains: { error.reason: "line 1:36: invalid index pattern [test-lookup-1*], * is not allowed in LOOKUP JOIN" }

0 commit comments

Comments
 (0)