Skip to content

Commit 060c833

Browse files
authored
ESQL: Simplify TableIdentifier class + rename to IndexPattern (#120797)
This class is confusing: - It contains an **unused** `cluster` attribute - we never separate out the cluster, it remains in the `index` field. Also, in the constructor, this field is called `catalog`, which is a concept entirely absent from ESQL at the moment. - It can refer to multiple indices, even multiple wildcard patterns, but doesn't mention this neither in its name nor javadoc. - It has little to do with tables, which is likely a remnant of this class' usage in SQL, before the `esql.core` split. This PR removes the `cluster` attribute, renames the class to `IndexPattern`, and adds javadoc to clarify that it can also contain stuff like `remote1:idx1,remote-*:idx-*`.
1 parent 10e96bd commit 060c833

File tree

14 files changed

+120
-135
lines changed

14 files changed

+120
-135
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import org.elasticsearch.xpack.esql.index.EsIndex;
6565
import org.elasticsearch.xpack.esql.index.IndexResolution;
6666
import org.elasticsearch.xpack.esql.parser.ParsingException;
67-
import org.elasticsearch.xpack.esql.plan.TableIdentifier;
67+
import org.elasticsearch.xpack.esql.plan.IndexPattern;
6868
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
6969
import org.elasticsearch.xpack.esql.plan.logical.Drop;
7070
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
@@ -202,7 +202,9 @@ private static class ResolveTable extends ParameterizedAnalyzerRule<UnresolvedRe
202202
protected LogicalPlan rule(UnresolvedRelation plan, AnalyzerContext context) {
203203
return resolveIndex(
204204
plan,
205-
plan.indexMode().equals(IndexMode.LOOKUP) ? context.lookupResolution().get(plan.table().index()) : context.indexResolution()
205+
plan.indexMode().equals(IndexMode.LOOKUP)
206+
? context.lookupResolution().get(plan.indexPattern().indexPattern())
207+
: context.indexResolution()
206208
);
207209
}
208210

@@ -213,20 +215,20 @@ private LogicalPlan resolveIndex(UnresolvedRelation plan, IndexResolution indexR
213215
? plan
214216
: new UnresolvedRelation(
215217
plan.source(),
216-
plan.table(),
218+
plan.indexPattern(),
217219
plan.frozen(),
218220
plan.metadataFields(),
219221
plan.indexMode(),
220222
indexResolutionMessage,
221223
plan.commandName()
222224
);
223225
}
224-
TableIdentifier table = plan.table();
225-
if (indexResolution.matches(table.index()) == false) {
226+
IndexPattern table = plan.indexPattern();
227+
if (indexResolution.matches(table.indexPattern()) == false) {
226228
// TODO: fix this (and tests), or drop check (seems SQL-inherited, where's also defective)
227229
new UnresolvedRelation(
228230
plan.source(),
229-
plan.table(),
231+
plan.indexPattern(),
230232
plan.frozen(),
231233
plan.metadataFields(),
232234
plan.indexMode(),

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/PreAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected PreAnalysis doPreAnalyze(LogicalPlan plan) {
5151

5252
plan.forEachUp(UnresolvedRelation.class, p -> {
5353
List<TableInfo> list = p.indexMode() == IndexMode.LOOKUP ? lookupIndices : indices;
54-
list.add(new TableInfo(p.table()));
54+
list.add(new TableInfo(p.indexPattern()));
5555
});
5656
plan.forEachUp(Enrich.class, unresolvedEnriches::add);
5757

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/analysis/TableInfo.java

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

88
package org.elasticsearch.xpack.esql.analysis;
99

10-
import org.elasticsearch.xpack.esql.plan.TableIdentifier;
10+
import org.elasticsearch.xpack.esql.plan.IndexPattern;
1111

1212
public class TableInfo {
1313

14-
private final TableIdentifier id;
14+
private final IndexPattern id;
1515

16-
public TableInfo(TableIdentifier id) {
16+
public TableInfo(IndexPattern id) {
1717
this.id = id;
1818
}
1919

20-
public TableIdentifier id() {
20+
public IndexPattern id() {
2121
return id;
2222
}
2323
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.elasticsearch.xpack.esql.expression.Order;
3535
import org.elasticsearch.xpack.esql.expression.UnresolvedNamePattern;
3636
import org.elasticsearch.xpack.esql.expression.function.UnresolvedFunction;
37-
import org.elasticsearch.xpack.esql.plan.TableIdentifier;
37+
import org.elasticsearch.xpack.esql.plan.IndexPattern;
3838
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
3939
import org.elasticsearch.xpack.esql.plan.logical.Dissect;
4040
import org.elasticsearch.xpack.esql.plan.logical.Drop;
@@ -255,7 +255,7 @@ public LogicalPlan visitRowCommand(EsqlBaseParser.RowCommandContext ctx) {
255255
@Override
256256
public LogicalPlan visitFromCommand(EsqlBaseParser.FromCommandContext ctx) {
257257
Source source = source(ctx);
258-
TableIdentifier table = new TableIdentifier(source, null, visitIndexPattern(ctx.indexPattern()));
258+
IndexPattern table = new IndexPattern(source, visitIndexPattern(ctx.indexPattern()));
259259
Map<String, Attribute> metadataMap = new LinkedHashMap<>();
260260
if (ctx.metadata() != null) {
261261
for (var c : ctx.metadata().UNQUOTED_SOURCE()) {
@@ -468,7 +468,7 @@ public LogicalPlan visitMetricsCommand(EsqlBaseParser.MetricsCommandContext ctx)
468468
throw new IllegalArgumentException("METRICS command currently requires a snapshot build");
469469
}
470470
Source source = source(ctx);
471-
TableIdentifier table = new TableIdentifier(source, null, visitIndexPattern(ctx.indexPattern()));
471+
IndexPattern table = new IndexPattern(source, visitIndexPattern(ctx.indexPattern()));
472472

473473
if (ctx.aggregates == null && ctx.grouping == null) {
474474
return new UnresolvedRelation(source, table, false, List.of(), IndexMode.STANDARD, null, "METRICS");
@@ -530,7 +530,7 @@ public PlanFactory visitJoinCommand(EsqlBaseParser.JoinCommandContext ctx) {
530530

531531
UnresolvedRelation right = new UnresolvedRelation(
532532
source(target),
533-
new TableIdentifier(source(target.index), null, rightPattern),
533+
new IndexPattern(source(target.index), rightPattern),
534534
false,
535535
emptyList(),
536536
IndexMode.LOOKUP,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
package org.elasticsearch.xpack.esql.plan;
8+
9+
import org.elasticsearch.xpack.esql.core.tree.Source;
10+
11+
import java.util.Objects;
12+
13+
/**
14+
* Contains an index pattern together with its {@link Source}. Can also be a comma-separated list, like {@code idx-*,remote:other-idx*}.
15+
*/
16+
public class IndexPattern {
17+
18+
private final Source source;
19+
private final String indexPattern;
20+
21+
public IndexPattern(Source source, String indexPattern) {
22+
this.source = source;
23+
this.indexPattern = indexPattern;
24+
}
25+
26+
public String indexPattern() {
27+
return indexPattern;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Objects.hash(indexPattern);
33+
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (this == obj) {
38+
return true;
39+
}
40+
41+
if (obj == null || getClass() != obj.getClass()) {
42+
return false;
43+
}
44+
45+
IndexPattern other = (IndexPattern) obj;
46+
return Objects.equals(indexPattern, other.indexPattern);
47+
}
48+
49+
public Source source() {
50+
return source;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return indexPattern;
56+
}
57+
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/TableIdentifier.java

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

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/UnresolvedRelation.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.elasticsearch.xpack.esql.core.expression.Attribute;
1313
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1414
import org.elasticsearch.xpack.esql.core.tree.Source;
15-
import org.elasticsearch.xpack.esql.plan.TableIdentifier;
15+
import org.elasticsearch.xpack.esql.plan.IndexPattern;
1616

1717
import java.util.Collections;
1818
import java.util.List;
@@ -22,7 +22,7 @@
2222

2323
public class UnresolvedRelation extends LeafPlan implements Unresolvable {
2424

25-
private final TableIdentifier table;
25+
private final IndexPattern indexPattern;
2626
private final boolean frozen;
2727
private final List<Attribute> metadataFields;
2828
/*
@@ -40,19 +40,19 @@ public class UnresolvedRelation extends LeafPlan implements Unresolvable {
4040

4141
public UnresolvedRelation(
4242
Source source,
43-
TableIdentifier table,
43+
IndexPattern indexPattern,
4444
boolean frozen,
4545
List<Attribute> metadataFields,
4646
IndexMode indexMode,
4747
String unresolvedMessage,
4848
String commandName
4949
) {
5050
super(source);
51-
this.table = table;
51+
this.indexPattern = indexPattern;
5252
this.frozen = frozen;
5353
this.metadataFields = metadataFields;
5454
this.indexMode = indexMode;
55-
this.unresolvedMsg = unresolvedMessage == null ? "Unknown index [" + table.index() + "]" : unresolvedMessage;
55+
this.unresolvedMsg = unresolvedMessage == null ? "Unknown index [" + indexPattern.indexPattern() + "]" : unresolvedMessage;
5656
this.commandName = commandName;
5757
}
5858

@@ -68,11 +68,11 @@ public String getWriteableName() {
6868

6969
@Override
7070
protected NodeInfo<UnresolvedRelation> info() {
71-
return NodeInfo.create(this, UnresolvedRelation::new, table, frozen, metadataFields, indexMode, unresolvedMsg, commandName);
71+
return NodeInfo.create(this, UnresolvedRelation::new, indexPattern, frozen, metadataFields, indexMode, unresolvedMsg, commandName);
7272
}
7373

74-
public TableIdentifier table() {
75-
return table;
74+
public IndexPattern indexPattern() {
75+
return indexPattern;
7676
}
7777

7878
public boolean frozen() {
@@ -124,7 +124,7 @@ public String unresolvedMessage() {
124124

125125
@Override
126126
public int hashCode() {
127-
return Objects.hash(source(), table, metadataFields, indexMode, unresolvedMsg);
127+
return Objects.hash(source(), indexPattern, metadataFields, indexMode, unresolvedMsg);
128128
}
129129

130130
@Override
@@ -138,7 +138,7 @@ public boolean equals(Object obj) {
138138
}
139139

140140
UnresolvedRelation other = (UnresolvedRelation) obj;
141-
return Objects.equals(table, other.table)
141+
return Objects.equals(indexPattern, other.indexPattern)
142142
&& Objects.equals(frozen, other.frozen)
143143
&& Objects.equals(metadataFields, other.metadataFields)
144144
&& indexMode == other.indexMode
@@ -147,11 +147,11 @@ public boolean equals(Object obj) {
147147

148148
@Override
149149
public List<Object> nodeProperties() {
150-
return singletonList(table);
150+
return singletonList(indexPattern);
151151
}
152152

153153
@Override
154154
public String toString() {
155-
return UNRESOLVED_PREFIX + table.index();
155+
return UNRESOLVED_PREFIX + indexPattern.indexPattern();
156156
}
157157
}

0 commit comments

Comments
 (0)