alex-plekhanov commented on code in PR #12593:
URL: https://github.com/apache/ignite/pull/12593#discussion_r2648051661


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/TableModifyDistributedConverterRule.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.rule;
+
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.PhysicalNode;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelDistribution;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.logical.LogicalTableModify;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableIntList;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteTableModify;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteTable;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.RewindabilityTrait;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Converts LogicalTableModify to distributed IgniteTableModify (Perform table 
modify on remote nodes,
+ * aggregate affected rows count and send result to the initiator node).
+ */
+public class TableModifyDistributedConverterRule extends 
AbstractIgniteConverterRule<LogicalTableModify> {
+    /** */
+    public static final RelOptRule INSTANCE = new 
TableModifyDistributedConverterRule();
+
+    /**
+     * Creates a ConverterRule.
+     */
+    public TableModifyDistributedConverterRule() {
+        super(LogicalTableModify.class, 
TableModifyDistributedConverterRule.class.getSimpleName());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected @Nullable PhysicalNode convert(
+        RelOptPlanner planner,
+        RelMetadataQuery mq,
+        LogicalTableModify rel
+    ) {
+        RelOptCluster cluster = rel.getCluster();
+
+        // If transaction is explicitly started it's only allowed to perform 
table modify on initiator node.
+        if (Commons.queryTransactionVersion(planner.getContext()) != null)
+            return null;
+
+        RelDataType rowType = rel.getRowType();
+        IgniteTable table = rel.getTable().unwrap(IgniteTable.class);
+        IgniteDistribution inputDistribution = table.distribution();
+        boolean affectsSrc = false;
+
+        // Single distribution table modify is prefered in this case.
+        if (inputDistribution == IgniteDistributions.single())
+            return null;
+
+        switch (rel.getOperation()) {
+            case MERGE:
+                // Merge contains insert fields as well as update fields, it's 
impossible to check input distribution
+                // over these two fields sets in common case, only corner 
cases can be implemented, skip it for now.
+                return null;
+
+            case INSERT:
+                affectsSrc = 
RelOptUtil.findTables(rel).contains(rel.getTable());
+
+                if (inputDistribution.getType() != 
RelDistribution.Type.HASH_DISTRIBUTED) {
+                    if (affectsSrc)
+                        return null;
+                    else
+                        inputDistribution = 
IgniteDistributions.hash(ImmutableIntList.range(0, rowType.getFieldCount()));
+                }
+
+                break;
+
+            case UPDATE:
+                if (inputDistribution.getType() != 
RelDistribution.Type.HASH_DISTRIBUTED)
+                    inputDistribution = 
IgniteDistributions.hash(ImmutableIntList.of(0));
+
+                break;
+
+            case DELETE:
+                inputDistribution = 
IgniteDistributions.hash(ImmutableIntList.of(0));
+
+                break;
+
+            default:
+                throw new IllegalStateException("Unknown operation type: " + 
rel.getOperation());
+        }
+
+        // Create distributed table modify.
+        RelBuilder relBuilder = relBuilderFactory.create(rel.getCluster(), 
null);

Review Comment:
   Fixed



-- 
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