Skip to content

Commit 65cbccc

Browse files
authored
ESQL: Fix some more usages of field attribute names in LOOKUP JOIN (#129355)
* Add a test that demonstrates that LOOKUP JOIN works with union types. * Fix usage of FieldAttribute#name where we should use FieldAttribute#fieldName in LOOKUP JOIN. * Refactor LookupJoinTypesIT This doesn't fix any bugs per se, but will avoid bwc problems once we propagate union type field attributes into Join LogicalPlan nodes.
1 parent e879299 commit 65cbccc

File tree

10 files changed

+362
-155
lines changed

10 files changed

+362
-155
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/FieldAttribute.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
*
3535
* To adequately represent e.g. union types, the name of the attribute can be altered because we may have multiple synthetic field
3636
* attributes that really belong to the same underlying field. For instance, if a multi-typed field is used both as {@code field::string}
37-
* and {@code field::ip}, we'll generate 2 field attributes called {@code $$field$converted_to$string} and {@code $$field$converted_to$ip}
38-
* but still referring to the same underlying field.
37+
* and {@code field::ip}, we'll generate 2 field attributes called {@code $$field$converted_to$keyword} and {@code $$field$converted_to$ip}
38+
* which still refer to the same underlying index field.
3939
*/
4040
public class FieldAttribute extends TypedAttribute {
4141

@@ -163,6 +163,15 @@ public FieldName fieldName() {
163163
return lazyFieldName;
164164
}
165165

166+
/**
167+
* The name of the attribute. Can deviate from the field name e.g. in case of union types. For the physical field name, use
168+
* {@link FieldAttribute#fieldName()}.
169+
*/
170+
@Override
171+
public String name() {
172+
return super.name();
173+
}
174+
166175
public EsField.Exact getExactInfo() {
167176
return field.getExactInfo();
168177
}
@@ -176,7 +185,7 @@ public FieldAttribute exactAttribute() {
176185
}
177186

178187
private FieldAttribute innerField(EsField type) {
179-
return new FieldAttribute(source(), name(), name() + "." + type.getName(), type, nullable(), id(), synthetic());
188+
return new FieldAttribute(source(), fieldName().string, name() + "." + type.getName(), type, nullable(), id(), synthetic());
180189
}
181190

182191
@Override

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,28 @@ language.id:integer | language.name:text | language.name.keyword:keyword | langu
732732
2 | French | French | FR
733733
;
734734

735+
###############################################
736+
# union type behavior
737+
###############################################
738+
739+
joinOnMultiTypedMatchFieldCastToInteger
740+
required_capability: join_lookup_v12
741+
742+
FROM apps, apps_short METADATA _index
743+
| EVAL language_code = id::integer
744+
| KEEP _index, language_code
745+
| WHERE language_code < 3
746+
| LOOKUP JOIN languages_lookup ON language_code
747+
| SORT _index ASC, language_code ASC
748+
;
749+
750+
_index:keyword | language_code:integer | language_name:keyword
751+
apps | 1 | English
752+
apps | 2 | French
753+
apps_short | 1 | English
754+
apps_short | 2 | French
755+
;
756+
735757
###############################################
736758
# Tests with clientips_lookup index
737759
###############################################

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/LookupFromIndexIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.threadpool.ThreadPool;
5555
import org.elasticsearch.xpack.core.async.AsyncExecutionId;
5656
import org.elasticsearch.xpack.esql.core.expression.Alias;
57+
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
5758
import org.elasticsearch.xpack.esql.core.expression.ReferenceAttribute;
5859
import org.elasticsearch.xpack.esql.core.tree.Source;
5960
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -229,7 +230,7 @@ private void runLookup(DataType keyType, PopulateIndices populateIndices) throws
229230
keyType,
230231
"lookup",
231232
"lookup",
232-
"key",
233+
new FieldAttribute.FieldName("key"),
233234
List.of(new Alias(Source.EMPTY, "l", new ReferenceAttribute(Source.EMPTY, "l", DataType.LONG))),
234235
Source.EMPTY
235236
);

0 commit comments

Comments
 (0)