This is an automated email from the ASF dual-hosted git repository.

dmsysolyatin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new eee63b24d1 [CALCITE-7276] SqlToRelConverter throws exception for 
UPDATE if identifier expansion disabled
eee63b24d1 is described below

commit eee63b24d1aa17b0d8a1d80fa3dd4aaa20c42723
Author: Dmitry Sysolyatin <[email protected]>
AuthorDate: Fri Nov 7 11:17:36 2025 +0200

    [CALCITE-7276] SqlToRelConverter throws exception for UPDATE if identifier 
expansion disabled
---
 .../org/apache/calcite/sql2rel/SqlToRelConverter.java   | 14 +++++++-------
 .../org/apache/calcite/test/SqlToRelConverterTest.java  | 17 +++++++++++++++++
 .../org/apache/calcite/test/SqlToRelConverterTest.xml   | 12 ++++++++++++
 3 files changed, 36 insertions(+), 7 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java 
b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
index e621ead198..392fcdda10 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
@@ -4470,17 +4470,17 @@ private RelNode convertUpdate(SqlUpdate call) {
       targetColumnNameList.add(field.getName());
     }
 
-    // `sourceSelect` should contain target columns values plus source 
expressions
-    if (sourceSelect.getSelectList().size()
+    RelNode sourceRel = convertSelect(sourceSelect, false);
+    bb.setRoot(sourceRel, false);
+
+    // `sourceRel` should contain target columns values plus source expressions
+    if (sourceRel.getRowType().getFieldCount()
         != targetTable.getRowType().getFieldCount() + 
call.getSourceExpressionList().size()) {
       throw new AssertionError(
-          "Unexpected select list size. Select list should contain both target 
table columns and "
-              + "set expressions");
+          "Unexpected source select row type. Select row type should contain 
both target table "
+              + "columns and set expressions");
     }
 
-    RelNode sourceRel = convertSelect(sourceSelect, false);
-    bb.setRoot(sourceRel, false);
-
     // sourceRel already contains all source expressions. Only create 
references to those fields.
     List<RexNode> rexExpressionList =
         Util.transform(
diff --git 
a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
index 6a3fe210d9..e0477c6501 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
@@ -3404,6 +3404,23 @@ void checkCorrelatedMapSubQuery(boolean expand) {
     sql(sql).ok();
   }
 
+  /**
+   * Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7276";>[CALCITE-7276]
+   * SqlToRelConverter throws exception for UPDATE if identifier expansion 
disabled</a>.
+   */
+  @Test void testUpdateWithIdentifierExpansionDisabled() {
+    final String sql = "update emp set empno = empno + 1";
+    sql(sql)
+        .withFactory(f ->
+            f.withValidator((opTab, catalogReader, typeFactory, config)
+                -> SqlValidatorUtil.newValidator(opTab, catalogReader,
+                typeFactory, config.withIdentifierExpansion(false))))
+        .withTrim(false)
+        .ok();
+  }
+
+
   @Test void testUpdateSubQuery() {
     final String sql = "update emp\n"
         + "set empno = (\n"
diff --git 
a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml 
b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
index 88a93e3d1b..b640d7ddde 100644
--- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml
@@ -9725,6 +9725,18 @@ LogicalTableModify(table=[[CATALOG, SALES, EMP]], 
operation=[UPDATE], updateColu
 LogicalTableModify(table=[[CATALOG, STRUCT, T]], operation=[UPDATE], 
updateColumnList=[["F0"."C0"]], sourceExpressionList=[[$9]], flattened=[true])
   LogicalProject("K0"=[$0], "C1"=[$1], "F1"."A0"=[$2], "F2"."A0"=[$3], 
"F0"."C0"=[$4], "F1"."C0"=[$5], "F0"."C1"=[$6], "F1"."C2"=[$7], "F2"."C3"=[$8], 
EXPR$0=[+($4, 1)])
     LogicalTableScan(table=[[CATALOG, STRUCT, T]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testUpdateWithIdentifierExpansionDisabled">
+    <Resource name="sql">
+      <![CDATA[update emp set empno = empno + 1]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[
+LogicalTableModify(table=[[CATALOG, SALES, EMP]], operation=[UPDATE], 
updateColumnList=[[EMPNO]], sourceExpressionList=[[$9]], flattened=[true])
+  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], 
SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[+($0, 1)])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
 ]]>
     </Resource>
   </TestCase>

Reply via email to