korlov42 commented on a change in pull request #9581:
URL: https://github.com/apache/ignite/pull/9581#discussion_r761769934



##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/schema/TableDescriptorImpl.java
##########
@@ -470,6 +470,31 @@ private IgniteCheckedException 
instantiationException(String typeName, Reflectiv
         return F.t(key, val);
     }
 
+    /** */
+    private <Row> IgniteBiTuple mergeTuple(Row row, List<String> 
updateColList, ExecutionContext<Row> ectx)
+        throws IgniteCheckedException {
+        RowHandler<Row> hnd = ectx.rowHandler();
+
+        int rowColumnsCnt = hnd.columnCount(row);
+
+        if (rowColumnsCnt == descriptors.length)
+            return F.t(insertTuple(row, ectx), null); // Only WHEN NOT MATCHED 
clause in MERGE.
+        else if (rowColumnsCnt == descriptors.length + updateColList.size())
+            return F.t(null, updateTuple(row, updateColList, 0, ectx)); // 
Only WHEN MATCHED clause in MERGE.
+        else {
+            // Both WHEN MATCHED and WHEN NOT MATCHED clauses in MERGE.
+            assert rowColumnsCnt == descriptors.length * 2 + 
updateColList.size() : "Unexpected columns count: " +
+                rowColumnsCnt;
+
+            int updateOffset = descriptors.length; // Offset of fields for 
update statement.
+
+            if (hnd.get(updateOffset + QueryUtils.KEY_COL, row) != null)
+                return F.t(null, updateTuple(row, updateColList, updateOffset, 
ectx));

Review comment:
       Honestly speaking, tuple in tuple freaks me out. I would prefer to 
introduce a more clear abstraction for modify rows. Besides, it would help to 
ease the support  of `WHEN MATCHED THEN DELETE` clause.
   
   ```
   <merge when matched clause> ::=
     WHEN MATCHED [ AND <search condition> ]
         THEN <merge update or delete specification>
   
   <merge update or delete specification> ::=
       <merge update specification>
     | <merge delete specification>
   ...
   <merge delete specification> ::=
     DELETE
   ```

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/PlannerHelper.java
##########
@@ -147,7 +147,10 @@ public static IgniteRel optimize(SqlNode sqlNode, 
IgnitePlanner planner, IgniteL
             if (rel.isDelete())
                 return rel;
 
-            processNode(rel);
+            if (rel.isMerge()) // MERGE operator always contains modified 
table as a source.

Review comment:
       It seems to me we don't need a spool in case when "WHEN NOT MATCHED" 
clause is omitted (effectively it is UPDATE statement), and a table scan were 
used as a source




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to