Skip to content

Commit 36a7b0b

Browse files
Only rename when absolutely needed to avoid a name conflict
1 parent f688afc commit 36a7b0b

File tree

1 file changed

+30
-17
lines changed
  • x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/generator/command/pipe

1 file changed

+30
-17
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/generator/command/pipe/LookupJoinGenerator.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public CommandDescription generate(
4040
String lookupIdxName = lookupIdx.idxName();
4141
int joinColumnsCount = randomInt(lookupIdx.keys().size() - 1) + 1; // at least one column must be used for the join
4242
List<LookupIdxColumn> joinColumns = ESTestCase.randomSubsetOf(joinColumnsCount, lookupIdx.keys());
43-
List<String> keyNames = new ArrayList<>();
44-
List<String> joinOn = new ArrayList<>();
43+
List<String> joinOnLeft = new ArrayList<>();
44+
List<String> joinOnRight = new ArrayList<>();
4545
Set<String> usedColumns = new HashSet<>();
4646
for (LookupIdxColumn joinColumn : joinColumns) {
4747
String idxKey = joinColumn.name();
@@ -58,10 +58,10 @@ public CommandDescription generate(
5858
usedColumns.add(key.name());
5959
usedColumns.add(idxKey);
6060
}
61-
keyNames.add(key.name());
62-
joinOn.add(idxKey);
61+
joinOnLeft.add(key.name());
62+
joinOnRight.add(idxKey);
6363
}
64-
if (keyNames.isEmpty()) {
64+
if (joinOnLeft.isEmpty()) {
6565
return EMPTY_DESCRIPTION;
6666
}
6767
StringBuilder stringBuilder = new StringBuilder();
@@ -71,17 +71,17 @@ public CommandDescription generate(
7171

7272
if (useExpressionJoin) {
7373
// Get all right side column names (from lookup index)
74-
Set<String> rightColumnNames = lookupIdx.keys()
74+
Set<String> allRightColumnNames = lookupIdx.keys()
7575
.stream()
7676
.map(LookupIdxColumn::name)
7777
.collect(java.util.stream.Collectors.toSet());
7878

7979
// Generate rename commands for ALL left columns that exist in right side
8080
Set<String> allLeftColumnNames = previousOutput.stream().map(Column::name).collect(java.util.stream.Collectors.toSet());
8181
Map<String, String> leftColumnName2Renamed = new HashMap<>();
82-
// Rename left columns that conflict with right side columns (either in lookup index or used in join)
82+
// Rename due to co1 == col2, the conflict is on col2 name
8383
for (String leftColumnName : allLeftColumnNames) {
84-
if (rightColumnNames.contains(leftColumnName)) {
84+
if (joinOnRight.contains(leftColumnName)) {
8585
String renamedColumn = leftColumnName + "_left";
8686
stringBuilder.append("| rename ");
8787
stringBuilder.append(leftColumnName);
@@ -91,13 +91,26 @@ public CommandDescription generate(
9191
}
9292
}
9393

94+
// Rename due to co1 == col2, the conflict is on col1 name
95+
// but only rename if it wasn't renamed already
96+
for (String rightColumnName : allRightColumnNames) {
97+
if (joinOnLeft.contains(rightColumnName) && leftColumnName2Renamed.containsKey(rightColumnName) == false) {
98+
String renamedColumn = rightColumnName + "_left";
99+
stringBuilder.append("| rename ");
100+
stringBuilder.append(rightColumnName);
101+
stringBuilder.append(" as ");
102+
stringBuilder.append(renamedColumn);
103+
leftColumnName2Renamed.put(rightColumnName, renamedColumn);
104+
}
105+
}
106+
94107
// Generate expression join syntax
95108
stringBuilder.append(" | lookup join ").append(lookupIdxName).append(" on ");
96109

97110
// Add join conditions for all columns
98-
for (int i = 0; i < keyNames.size(); i++) {
99-
String leftColumnName = keyNames.get(i);
100-
String rightColumnName = joinOn.get(i);
111+
for (int i = 0; i < joinOnLeft.size(); i++) {
112+
String leftColumnName = joinOnLeft.get(i);
113+
String rightColumnName = joinOnRight.get(i);
101114

102115
// Only == and != are allowed, as the rest of the operators don's support all types
103116
String[] booleanOperators = { "==", "!=" };
@@ -112,16 +125,16 @@ public CommandDescription generate(
112125
}
113126
} else {
114127
// Generate field-based join (original behavior)
115-
for (int i = 0; i < keyNames.size(); i++) {
128+
for (int i = 0; i < joinOnLeft.size(); i++) {
116129
stringBuilder.append("| rename ");
117-
stringBuilder.append(keyNames.get(i));
130+
stringBuilder.append(joinOnLeft.get(i));
118131
stringBuilder.append(" as ");
119-
stringBuilder.append(joinOn.get(i));
132+
stringBuilder.append(joinOnRight.get(i));
120133
}
121134
stringBuilder.append(" | lookup join ").append(lookupIdxName).append(" on ");
122-
for (int i = 0; i < keyNames.size(); i++) {
123-
stringBuilder.append(joinOn.get(i));
124-
if (i < keyNames.size() - 1) {
135+
for (int i = 0; i < joinOnLeft.size(); i++) {
136+
stringBuilder.append(joinOnRight.get(i));
137+
if (i < joinOnLeft.size() - 1) {
125138
stringBuilder.append(", ");
126139
}
127140
}

0 commit comments

Comments
 (0)