Skip to content

Commit 03ba900

Browse files
wt0530githubgxll
authored andcommitted
[fix][core] Fixed the issue of aliases being empty when multiple table updates
1 parent 414b1ea commit 03ba900

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

core/src/main/java/org/apache/calcite/sql/SqlUpdate.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ public SqlNode getTargetTable() {
152152
return targetTable;
153153
}
154154

155+
public void setTargetTable(SqlNode targetTable) {
156+
this.targetTable = targetTable;
157+
}
158+
155159
/** Returns the alias for the target table of this UPDATE. */
156160
@Pure
157161
public @Nullable SqlIdentifier getAlias() {
@@ -186,6 +190,10 @@ public SqlNodeList getAliases() {
186190
return aliases;
187191
}
188192

193+
public void setAliases(SqlNodeList aliases) {
194+
this.aliases = aliases;
195+
}
196+
189197
public boolean singleTable() {
190198
return getSourceTables().size() <= 1;
191199
}
@@ -228,11 +236,25 @@ public void setSourceSelect(SqlSelect sourceSelect) {
228236
writer.startList(SqlWriter.FrameTypeEnum.SELECT, "UPDATE", "");
229237
final int opLeft = getOperator().getLeftPrec();
230238
final int opRight = getOperator().getRightPrec();
231-
targetTable.unparse(writer, opLeft, opRight);
232-
SqlIdentifier alias = this.alias;
233-
if (alias != null) {
234-
writer.keyword("AS");
235-
alias.unparse(writer, opLeft, opRight);
239+
if (singleTable()) {
240+
targetTable.unparse(writer, opLeft, opRight);
241+
if (alias != null) {
242+
writer.keyword("AS");
243+
alias.unparse(writer, opLeft, opRight);
244+
}
245+
} else {
246+
for (SqlNode table : sourceTables) {
247+
if (table instanceof SqlIdentifier) {
248+
((SqlIdentifier) table).unparse(writer, opLeft, opRight);
249+
} else {
250+
table.unparse(writer, opLeft, opRight);
251+
}
252+
writer.keyword("AS");
253+
SqlIdentifier alias = (SqlIdentifier) aliases.get(sourceTables.indexOf(table));
254+
alias.unparse(writer, opLeft, opRight);
255+
writer.sep(",");
256+
}
257+
// targetTable.unparse(writer, opLeft, opRight);
236258
}
237259
final SqlWriter.Frame setFrame =
238260
writer.startList(SqlWriter.FrameTypeEnum.UPDATE_SET_LIST, "SET", "");

core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,32 @@ protected SqlSelect createSourceSelectForUpdate(SqlUpdate call) {
17281728
sourceTable,
17291729
alias.getLastName());
17301730
}
1731+
if (!call.singleTable() && call.getAliases() != null) {
1732+
if (sourceTable.getKind() == SqlKind.JOIN) {
1733+
SqlNodeList aliases = new SqlNodeList(call.getAliases().getParserPosition());
1734+
SqlJoin join = (SqlJoin) sourceTable;
1735+
SqlNode left = join.getLeft();
1736+
SqlNode right = join.getRight();
1737+
SqlNode newLeft = SqlValidatorUtil.addAlias(
1738+
left,
1739+
((SqlIdentifier) call.getAliases().get(0)).getLastName());
1740+
SqlNode newRight = SqlValidatorUtil.addAlias(
1741+
right,
1742+
((SqlIdentifier) call.getAliases().get(1)).getLastName());
1743+
sourceTable = new SqlJoin(
1744+
join.getParserPosition(),
1745+
newLeft,
1746+
join.isNaturalNode(),
1747+
join.getJoinTypeNode(),
1748+
newRight,
1749+
join.getConditionTypeNode(),
1750+
join.getCondition());
1751+
aliases.add(newLeft);
1752+
aliases.add(newRight);
1753+
call.setTargetTable(sourceTable);
1754+
call.setAliases(aliases);
1755+
}
1756+
}
17311757
return new SqlSelect(SqlParserPos.ZERO, null, selectList, sourceTable,
17321758
call.getCondition(), null, null, null, null, null, null, null);
17331759
}

0 commit comments

Comments
 (0)