vldpyatkov commented on code in PR #13479: URL: https://github.com/apache/ignite/pull/13479#discussion_r3926523469
########## modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/RecursiveCtePlannerTest.java: ########## @@ -0,0 +1,184 @@ +/* + * 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.planner; + +import java.util.List; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Exchange; +import org.apache.calcite.rel.core.Spool; +import org.apache.ignite.internal.processors.query.calcite.rel.IgniteIndexScan; +import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRecursiveTableScan; +import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel; +import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRepeatUnion; +import org.apache.ignite.internal.processors.query.calcite.rel.IgniteValues; +import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema; +import org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution; +import org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions; +import org.junit.Test; + +/** Planner tests for recursive common table expressions. */ +public class RecursiveCtePlannerTest extends AbstractPlannerTest { + /** Employee hierarchy query used for distribution and index planning checks. */ + private static final String EMPLOYEE_HIERARCHY_QUERY = + "WITH RECURSIVE employee_hierarchy (id, manager_id, depth) AS (" + + "SELECT id, manager_id, 0 FROM employee WHERE manager_id IS NULL " + + "UNION ALL " + + "SELECT e.id, e.manager_id, h.depth + 1 " + + "FROM employee e " + + "JOIN employee_hierarchy h ON e.manager_id = h.id" + + ") " + + "SELECT id, manager_id, depth FROM employee_hierarchy"; + + /** Employee hierarchy query that requests indexed correlated lookups in the recursive term. */ + private static final String INDEXED_EMPLOYEE_HIERARCHY_QUERY = + EMPLOYEE_HIERARCHY_QUERY + .replace("SELECT e.id", "SELECT /*+ CNL_JOIN */ e.id") + .replace("FROM employee e", "FROM employee /*+ FORCE_INDEX(EMPLOYEE_MANAGER_IDX) */ e"); + + /** Checks the physical operators used to maintain the recursive delta. */ + @Test + public void testRecursiveDeltaPlan() throws Exception { + IgniteSchema schema = new IgniteSchema(DEFAULT_SCHEMA); + + IgniteRel plan = physicalPlan( + "WITH RECURSIVE numbers(n) AS (" + + "SELECT 1 " + + "UNION ALL " + + "SELECT n + 1 FROM numbers WHERE n < 3" + + ") " + + "SELECT n FROM numbers", + schema + ); + + assertRecursivePlan(plan); + IgniteRepeatUnion repeatUnion = findFirstNode(plan, byClass(IgniteRepeatUnion.class)); + + assertTrue(planDescription(plan), repeatUnion.getLeft() instanceof IgniteValues); + assertEquals(1, findNodes(plan, byClass(IgniteRecursiveTableScan.class)).size()); + + checkSplitAndSerialization(plan, schema); + } + + /** A replicated source can be read on the coordinator without an exchange. */ + @Test + public void testRecursiveCteWithReplicatedTable() throws Exception { + IgniteSchema schema = hierarchySchema(IgniteDistributions.broadcast(), false); + + IgniteRel plan = physicalPlan(EMPLOYEE_HIERARCHY_QUERY, schema); + + assertRecursivePlan(plan); + assertTrue(planDescription(plan), findNodes(plan, byClass(Exchange.class)).isEmpty()); + + checkSplitAndSerialization(plan, schema); + } + + /** A partitioned source has to be transferred to the coordinator-side recursive plan. */ + @Test + public void testRecursiveCteWithPartitionedTable() throws Exception { + IgniteDistribution distribution = IgniteDistributions.affinity(0, "EMPLOYEE", "hash"); + IgniteSchema schema = hierarchySchema(distribution, false); + + IgniteRel plan = physicalPlan(EMPLOYEE_HIERARCHY_QUERY, schema); + + assertRecursivePlan(plan); + assertFalse(planDescription(plan), findNodes(plan, byClass(Exchange.class)).isEmpty()); + + IgniteRepeatUnion repeatUnion = findFirstNode(plan, byClass(IgniteRepeatUnion.class)); + Spool spool = findFirstNode(repeatUnion.getRight(), byClass(Spool.class)); + + assertNotNull(planDescription(plan), spool); + assertFalse(planDescription(plan), findNodes(spool.getInput(), byClass(Exchange.class)).isEmpty()); + + checkSplitAndSerialization(plan, schema); + } + + /** A replicated indexed input can be rewound without materialization. */ + @Test + public void testRecursiveCteWithReplicatedIndexedTable() throws Exception { + IgniteSchema schema = hierarchySchema(IgniteDistributions.broadcast(), true); + + IgniteRel plan = physicalPlan(INDEXED_EMPLOYEE_HIERARCHY_QUERY, schema); + + assertRecursivePlan(plan); + + IgniteRepeatUnion repeatUnion = findFirstNode(plan, byClass(IgniteRepeatUnion.class)); + RelNode iterative = repeatUnion.getRight(); + + assertFalse(planDescription(plan), findNodes(iterative, byClass(IgniteIndexScan.class)).isEmpty()); + assertTrue(planDescription(plan), findNodes(iterative, byClass(Spool.class)).isEmpty()); + + checkSplitAndSerialization(plan, schema); + } + + /** Calcite places multiple non-recursive branches into the seed input of RepeatUnion. */ + @Test + public void testRecursiveCteWithMultipleSeedBranches() throws Exception { + IgniteSchema schema = new IgniteSchema(DEFAULT_SCHEMA); + + IgniteRel plan = physicalPlan( + "WITH RECURSIVE numbers(n) AS (" + + "SELECT 1 " + Review Comment: The current test proves the resulting seed values but does not prove that a multi-branch seed is preserved. I used table scans as suggested and asserted that IgniteUnionAll is the left input of IgniteRepeatUnion. -- 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]
