vldpyatkov commented on code in PR #13479: URL: https://github.com/apache/ignite/pull/13479#discussion_r3853009540
########## modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/RecursiveCteUtils.java: ########## @@ -0,0 +1,195 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; +import org.apache.calcite.plan.RelOptPlanner; +import org.apache.calcite.plan.RelOptTable; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.volcano.RelSubset; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Aggregate; +import org.apache.calcite.rel.core.AggregateCall; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexSubQuery; +import org.apache.calcite.schema.TransientTable; +import org.apache.calcite.sql.validate.SqlUserDefinedFunction; +import org.apache.ignite.internal.processors.query.calcite.exec.exp.IgniteScalarFunction; +import org.apache.ignite.internal.processors.query.calcite.prepare.BaseQueryContext; +import org.apache.ignite.internal.processors.query.calcite.rel.logical.IgniteLogicalRecursiveStaticSpool; + +/** Utilities shared by recursive CTE converter rules. */ +final class RecursiveCteUtils { + /** */ + private RecursiveCteUtils() { + // No-op. + } + + /** Returns whether the table is Calcite's query-local transient table. */ + static boolean isTransient(RelOptTable table) { + return table != null && table.unwrap(TransientTable.class) != null; + } + + /** Stable identifier preserved in the serialized physical plan. */ + static String stateId(RelOptPlanner planner, RelOptTable table) { + BaseQueryContext ctx = planner.getContext().unwrap(BaseQueryContext.class); + + assert ctx != null; + + return ctx.recursiveCteStateId(table); + } + + /** Counts scans of the recursive transient table. */ + static int referenceCount(RelNode rel, RelOptTable table) { + rel = original(rel); + + int cnt = isRecursiveScan(rel, table) ? 1 : 0; + + for (RelNode input : rel.getInputs()) + cnt += referenceCount(input, table); + + return cnt; + } + + /** Materializes maximal iteration subtrees that do not depend on the current delta. */ + static RelNode materializeStaticInputs(RelNode rel, RelOptTable table) { + rel = original(rel); + + if (isRecursiveScan(rel, table)) + return rel; + + List<RelNode> inputs = rel.getInputs(); + + if (inputs.isEmpty()) + return rel; + + List<RelNode> newInputs = new ArrayList<>(inputs.size()); + + for (RelNode input : inputs) { + if (referenceCount(input, table) == 0 && isInvariant(input)) + newInputs.add(new IgniteLogicalRecursiveStaticSpool(input)); + else + newInputs.add(materializeStaticInputs(input, table)); + } + + return rel.copy(rel.getTraitSet(), newInputs); + } + + /** Returns whether the subtree produces the same result on every recursive iteration. */ + private static boolean isInvariant(RelNode rel) { + rel = original(rel); + + if (!RelOptUtil.getVariablesUsed(rel).isEmpty()) + return false; + + DeterminismChecker checker = new DeterminismChecker(); + + rel.accept(checker); Review Comment: This comment is definitely relevant. We use _RexShuttle_ to determine whether a subtree of the recursive term is invariant and can be materialized only once. Without an accept implementation, expressions owned by these nodes are not visited, so a subtree containing non-deterministic expressions could be incorrectly treated as invariant and not re-evaluated on subsequent iterations. I implemented accept for _ProjectableFilterableTableScan_, _IgniteHashIndexSpool_, and _IgniteSortedIndexSpool_, covering projections, conditions, and search rows. This behavior is covered by the following tests in RecursiveCteIntegrationTest: - testIndependentNonDeterministicSubtreeIsEvaluatedForEveryIteration - testNonDeterministicTableScanIsNotMaterialized - testNonDeterministicExpressionsInIndexSpoolsAreDetected — covers both hash and sorted index spools. -- 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]
