alex-plekhanov commented on code in PR #12593: URL: https://github.com/apache/ignite/pull/12593#discussion_r2648049812
########## 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: Review Comment: Added one test. ########## modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/TestTable.java: ########## @@ -314,4 +330,171 @@ public TestTable addIndex(String name, int... keys) { @Override public void authorize(Operation op) { // No-op. } + + /** {@inheritDoc} */ + @Override public <C> C unwrap(Class<C> cls) { + if (cls.isInstance(this)) + return cls.cast(this); + + if (cls.isInstance(desc)) + return cls.cast(desc); + + return null; + } + + /** */ + static class TestTableDescriptor extends NullInitializerExpressionFactory implements CacheTableDescriptor { + /** */ + private final Supplier<IgniteDistribution> distributionSupp; + + /** */ + private final RelDataType rowType; + + /** */ + private final GridCacheContextInfo<?, ?> cacheInfo; + + /** */ + public TestTableDescriptor(Supplier<IgniteDistribution> distribution, RelDataType rowType) { + distributionSupp = distribution; + this.rowType = rowType; + cacheInfo = Mockito.mock(GridCacheContextInfo.class); + + CacheConfiguration cfg = Mockito.mock(CacheConfiguration.class); + Mockito.when(cfg.isEagerTtl()).thenReturn(true); + + Mockito.when(cacheInfo.cacheId()).thenReturn(CU.cacheId("TEST")); + Mockito.when(cacheInfo.config()).thenReturn(cfg); + } + + /** {@inheritDoc} */ + @Override public GridCacheContextInfo<?, ?> cacheInfo() { + return cacheInfo; + } + + /** {@inheritDoc} */ + @Override public GridCacheContext<?, ?> cacheContext() { + throw new AssertionError(); + } + + /** {@inheritDoc} */ + @Override public IgniteDistribution distribution() { + return distributionSupp.get(); + } + + /** {@inheritDoc} */ + @Override public ColocationGroup colocationGroup(MappingQueryContext ctx) { + throw new AssertionError(); + } + + /** {@inheritDoc} */ + @Override public RelDataType rowType(IgniteTypeFactory factory, ImmutableBitSet usedColumns) { + if (usedColumns == null) + return rowType; + else { + RelDataTypeFactory.Builder b = new RelDataTypeFactory.Builder(factory); + + for (int i = usedColumns.nextSetBit(0); i != -1; i = usedColumns.nextSetBit(i + 1)) + b.add(rowType.getFieldList().get(i)); + + return b.build(); + } + 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]
