xiedeyantu commented on code in PR #4709: URL: https://github.com/apache/calcite/pull/4709#discussion_r2649188687
########## core/src/main/java/org/apache/calcite/rel/rules/UnnestDecorrelateRule.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.calcite.rel.rules; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Correlate; +import org.apache.calcite.rel.core.CorrelationId; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.core.Uncollect; +import org.apache.calcite.rel.logical.LogicalValues; +import org.apache.calcite.rex.RexCorrelVariable; +import org.apache.calcite.rex.RexFieldAccess; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.ImmutableBitSet; + +import org.immutables.value.Value; + +import java.util.List; + +/** Convert representations of a projected Unnest that use LogicalCorrelate into + * simple Unnest representations. + * + * <p>Original plan: + * LogicalProject // only uses rightmost columns of correlate, outerProject + * LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{...}]) + * LeftSubquery + * LogicalProject (optional; innerProject) + * Uncollect + * LogicalProject(COL=[$cor0.ARRAY]) + * LogicalValues(tuples=[[{ 0 }]]) + * + * <p>is converted to + * + * <p>Resulting plan: + * LogicalProject + * LogicalProject (optional) + * Uncollect + * LogicalProject + * LeftSubquery + */ [email protected] +public class UnnestDecorrelateRule extends RelRule<UnnestDecorrelateRule.Config> + implements TransformationRule { + + protected UnnestDecorrelateRule(UnnestDecorrelateRule.Config config) { + super(config); + } + + @Override public void onMatch(RelOptRuleCall call) { + Project outerProject = call.rel(0); + Correlate cor = call.rel(1); + CorrelationId corId = cor.getCorrelationId(); + + RelNode left = call.rel(2); + int leftCount = left.getRowType().getFieldCount(); + ImmutableBitSet used = RelOptUtil.InputFinder.bits(outerProject.getProjects(), null); + for (int ref : used) { + if (ref < leftCount) { + return; + } + } + + int uncollectIndex = 3; + Project innerProject = null; + if (call.rel(uncollectIndex) instanceof Project) { + innerProject = call.rel(3); + uncollectIndex = 4; + } + + Uncollect uncollect = call.rel(uncollectIndex); + Project project = call.rel(uncollectIndex + 1); + + List<RexNode> projects = project.getProjects(); + if (projects.size() != 1) { + return; + } + + RexNode projected = projects.get(0); + if (projected instanceof RexFieldAccess) { + RexFieldAccess fa = (RexFieldAccess) projected; + RexNode referenceExpr = fa.getReferenceExpr(); + if (referenceExpr instanceof RexCorrelVariable) { + RexCorrelVariable cv = (RexCorrelVariable) referenceExpr; + if (cv.id != corId) { + return; + } + + RelBuilder builder = call.builder(); + builder.push(left); + + RexInputRef field = builder.field(fa.getField().getName()); + builder.project(field); + builder.uncollect(uncollect.getItemAliases(), uncollect.withOrdinality); + if (innerProject != null) { + builder.project(innerProject.getProjects()); + } + List<RexNode> shifted = RexUtil.shift(outerProject.getProjects(), -leftCount); + builder.project(shifted); + RelNode result = builder.build(); Review Comment: A small suggestion. It seems that all the operations related to the builder can be linked together in a point-like manner. ########## core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java: ########## @@ -673,6 +673,46 @@ private HepProgram createHypergraphProgram() { .checkUnchanged(); } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-7196">[CALCITE-7196] + * Create an optimization pass which can convert some cases of Correlate + Unnest + * to Unnest</a>. */ + @Test void testUnnestDecorrelate() { + final String sql = "WITH t1 AS (SELECT ARRAY[1, 2, 3] as arr)\n" + + "SELECT array_element.id\n" + + "FROM t1, UNNEST(t1.arr) AS array_element(id)"; + sql(sql) + .withRule(CoreRules.UNNEST_PROJECT_DECORRELATE) + .check(); + } + + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-7196">[CALCITE-7196] + * Create an optimization pass which can convert some cases of Correlate + Unnest + * to Unnest</a>. */ + @Test void testUnnestDecorrelate2() { + final String sql = "WITH t1 AS (SELECT ARRAY[1, 2, 3] as arr)\n" + + "SELECT array_element.id, array_element.ord\n" + + "FROM t1, UNNEST(t1.arr) WITH ORDINALITY AS array_element(id, ord)"; + sql(sql) + .withRule(CoreRules.UNNEST_PROJECT_DECORRELATE) + .check(); + } + + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-7196">[CALCITE-7196] + * Create an optimization pass which can convert some cases of Correlate + Unnest + * to Unnest</a>. */ + @Test void testUnnestDecorrelate3() { + final String sql = "WITH t1 AS (SELECT ARRAY[1, 2, 3] as arr)\n" + + "SELECT array_element.id\n" + + "FROM t1, UNNEST(t1.arr) AS array_element(id)"; + sql(sql) + .withPreRule(CoreRules.PROJECT_REMOVE) + .withRule(CoreRules.UNNEST_DECORRELATE) + .check(); + } + Review Comment: I think we can add a test case constructed in a non-Values manner. Like the one below, I tested your code and it looks good. ``` @Test void testUnnestDecorrelate4() { final String sql = "select t2.ename\n" + "from DEPT_NESTED as t1,\n" + "unnest(t1.employees) as t2"; sql(sql) .withPreRule(CoreRules.PROJECT_REMOVE) .withRule(CoreRules.UNNEST_DECORRELATE) .check(); } ``` Xml result: ``` <TestCase name="testUnnestDecorrelate4"> <Resource name="sql"> <![CDATA[select t2.ename from DEPT_NESTED as t1, unnest(t1.employees) as t2]]> </Resource> <Resource name="planBefore"> <![CDATA[ LogicalProject(ENAME=[$5]) LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{3}]) LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]]) Uncollect LogicalProject(EMPLOYEES=[$cor0.EMPLOYEES]) LogicalValues(tuples=[[{ 0 }]]) ]]> </Resource> <Resource name="planAfter"> <![CDATA[ LogicalProject(ENAME=[$1]) Uncollect LogicalProject(EMPLOYEES=[$3]) LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]]) ]]> </Resource> </TestCase> ``` ########## core/src/main/java/org/apache/calcite/rel/rules/UnnestDecorrelateRule.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.calcite.rel.rules; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.Correlate; +import org.apache.calcite.rel.core.CorrelationId; +import org.apache.calcite.rel.core.Project; +import org.apache.calcite.rel.core.Uncollect; +import org.apache.calcite.rel.logical.LogicalValues; +import org.apache.calcite.rex.RexCorrelVariable; +import org.apache.calcite.rex.RexFieldAccess; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.ImmutableBitSet; + +import org.immutables.value.Value; + +import java.util.List; + +/** Convert representations of a projected Unnest that use LogicalCorrelate into + * simple Unnest representations. + * + * <p>Original plan: + * LogicalProject // only uses rightmost columns of correlate, outerProject + * LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{...}]) + * LeftSubquery + * LogicalProject (optional; innerProject) + * Uncollect + * LogicalProject(COL=[$cor0.ARRAY]) + * LogicalValues(tuples=[[{ 0 }]]) + * + * <p>is converted to + * + * <p>Resulting plan: + * LogicalProject + * LogicalProject (optional) + * Uncollect + * LogicalProject + * LeftSubquery + */ [email protected] +public class UnnestDecorrelateRule extends RelRule<UnnestDecorrelateRule.Config> + implements TransformationRule { + + protected UnnestDecorrelateRule(UnnestDecorrelateRule.Config config) { + super(config); + } + + @Override public void onMatch(RelOptRuleCall call) { + Project outerProject = call.rel(0); + Correlate cor = call.rel(1); + CorrelationId corId = cor.getCorrelationId(); + + RelNode left = call.rel(2); + int leftCount = left.getRowType().getFieldCount(); + ImmutableBitSet used = RelOptUtil.InputFinder.bits(outerProject.getProjects(), null); + for (int ref : used) { Review Comment: ``` int firstUsed = used.nextSetBit(0); if (firstUsed != -1 && firstUsed < leftCount) { return; } ``` Would this be better? -- 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]
