dawidwys commented on code in PR #26780: URL: https://github.com/apache/flink/pull/26780#discussion_r2200487907
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/FlinkMarkChangelogNormaliseProgram.java: ########## @@ -0,0 +1,150 @@ +/* + * 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.flink.table.planner.plan.rules.physical.stream; + +import org.apache.flink.table.planner.calcite.FlinkRexBuilder; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalChangelogNormalize; +import org.apache.flink.table.planner.plan.optimize.program.FlinkOptimizeProgram; +import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; + +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A {@link FlinkOptimizeProgram} that marks changelog normalize nodes using the same source and + * determines common filters if any. + */ +public class FlinkMarkChangelogNormaliseProgram + implements FlinkOptimizeProgram<StreamOptimizeContext> { + @Override + public RelNode optimize(RelNode root, StreamOptimizeContext context) { + final Map<TableScan, List<ChangelogNormalizeContext>> tableScansToChangelogNormalize = + new HashMap<>(); + final FlinkRexBuilder rexBuilder = + new FlinkRexBuilder(context.getFlinkRelBuilder().getTypeFactory()); + for (RelNode relNode : root.getInputs()) { + gatherTableScanToChangelogNormalizeMap( + relNode, tableScansToChangelogNormalize, rexBuilder); + } + + for (Map.Entry<TableScan, List<ChangelogNormalizeContext>> entry : + tableScansToChangelogNormalize.entrySet()) { + if (entry.getValue().size() <= 1) { + continue; + } + + List<ChangelogNormalizeContext> list = entry.getValue(); + list.sort(Comparator.comparingInt(o -> o.rexNodes.size())); + Set<RexNode> common = new HashSet<>(list.get(0).rexNodes); + + for (int i = 1; i < list.size() && !common.isEmpty(); i++) { + common.retainAll(list.get(i).rexNodes); + } + + for (ChangelogNormalizeContext ctx : entry.getValue()) { + ctx.getChangelogNormalize().markSourceReuse(); + if (!common.isEmpty()) { + ctx.getChangelogNormalize().setCommonFilter(common.toArray(new RexNode[0])); + } + } + } + return root; + } + + private void gatherTableScanToChangelogNormalizeMap( + RelNode curRelNode, + Map<TableScan, List<ChangelogNormalizeContext>> map, + FlinkRexBuilder rexBuilder) { + for (RelNode input : curRelNode.getInputs()) { + if (input instanceof StreamPhysicalChangelogNormalize) { + StreamPhysicalChangelogNormalize changelogNormalize = + (StreamPhysicalChangelogNormalize) input; + if (curRelNode instanceof StreamPhysicalCalc) { + StreamPhysicalCalc calc = (StreamPhysicalCalc) curRelNode; + final List<RexNode> list = getConditions(calc, rexBuilder); + gatherTableScanToChangelogNormalizeMap( + input, ChangelogNormalizeContext.of(changelogNormalize, list), map); + } + } else { + gatherTableScanToChangelogNormalizeMap(input, map, rexBuilder); + } + } + } + + private List<RexNode> getConditions(StreamPhysicalCalc calc, RexBuilder rexBuilder) { Review Comment: Could we unify it? We use the same method in https://github.com/apache/flink/blob/1d8aba1a8a5dba661204b23d251160593fc899e2/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/PushCalcPastChangelogNormalizeRule.java#L137 ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/FlinkMarkChangelogNormaliseProgram.java: ########## @@ -0,0 +1,150 @@ +/* + * 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.flink.table.planner.plan.rules.physical.stream; + +import org.apache.flink.table.planner.calcite.FlinkRexBuilder; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalChangelogNormalize; +import org.apache.flink.table.planner.plan.optimize.program.FlinkOptimizeProgram; +import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; + +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A {@link FlinkOptimizeProgram} that marks changelog normalize nodes using the same source and + * determines common filters if any. + */ +public class FlinkMarkChangelogNormaliseProgram + implements FlinkOptimizeProgram<StreamOptimizeContext> { + @Override + public RelNode optimize(RelNode root, StreamOptimizeContext context) { + final Map<TableScan, List<ChangelogNormalizeContext>> tableScansToChangelogNormalize = + new HashMap<>(); + final FlinkRexBuilder rexBuilder = + new FlinkRexBuilder(context.getFlinkRelBuilder().getTypeFactory()); + for (RelNode relNode : root.getInputs()) { + gatherTableScanToChangelogNormalizeMap( + relNode, tableScansToChangelogNormalize, rexBuilder); + } + + for (Map.Entry<TableScan, List<ChangelogNormalizeContext>> entry : + tableScansToChangelogNormalize.entrySet()) { + if (entry.getValue().size() <= 1) { Review Comment: Can you please add a comment why you skip it? I guess it's for a source that is used only once. ########## flink-table/flink-table-planner/src/test/resources/org/apache/flink/table/planner/plan/rules/physical/stream/PushCalcPastChangelogNormalizeRuleTest.xml: ########## @@ -54,7 +135,87 @@ Calc(select=[f0, f1]) +- ChangelogNormalize(key=[f1, f2], condition=[IS NOT NULL(f0)]) +- Exchange(distribution=[hash[f1, f2]]) +- Calc(select=[f0, f1, f2], where=[OR(<(f1, 1), >(f2, 10))]) - +- TableSourceScan(table=[[default_catalog, default_database, T]], fields=[f0, f1, f2]) + +- TableSourceScan(table=[[default_catalog, default_database, T]], fields=[f0, f1, f2, f3, f4, f5, f6, f7]) +]]> + </Resource> + </TestCase> + <TestCase name="testNonPrimaryKeySeveralSameSources"> + <Resource name="sql"> + <![CDATA[SELECT * FROM T WHERE f3 < 1 +UNION SELECT * FROM T WHERE f3 < 3 +INTERSECT SELECT * FROM T WHERE f3 > 0]]> + </Resource> + <Resource name="ast"> + <![CDATA[ +LogicalUnion(all=[false]) +:- LogicalProject(f0=[$0], f1=[$1], f2=[$2], f3=[$3]) +: +- LogicalFilter(condition=[<($3, 1)]) +: +- LogicalTableScan(table=[[default_catalog, default_database, T]]) ++- LogicalIntersect(all=[false]) + :- LogicalProject(f0=[$0], f1=[$1], f2=[$2], f3=[$3]) + : +- LogicalFilter(condition=[<($3, 3)]) + : +- LogicalTableScan(table=[[default_catalog, default_database, T]]) + +- LogicalProject(f0=[$0], f1=[$1], f2=[$2], f3=[$3]) + +- LogicalFilter(condition=[>($3, 0)]) + +- LogicalTableScan(table=[[default_catalog, default_database, T]]) +]]> + </Resource> + <Resource name="optimized exec plan"> + <![CDATA[ +GroupAggregate(groupBy=[f0, f1, f2, f3], select=[f0, f1, f2, f3]) ++- Exchange(distribution=[hash[f0, f1, f2, f3]]) + +- Union(all=[true], union=[f0, f1, f2, f3]) + :- Calc(select=[f0, f1, f2, f3], where=[(f3 < 1)]) + : +- ChangelogNormalize(key=[f1])(reuse_id=[1]) + : +- Exchange(distribution=[hash[f1]]) + : +- TableSourceScan(table=[[default_catalog, default_database, T]], fields=[f0, f1, f2, f3]) + +- Join(joinType=[LeftSemiJoin], where=[(((f0 = f00) OR (f0 IS NULL AND f00 IS NULL)) AND (f1 = f10) AND ((f2 = f20) OR (f2 IS NULL AND f20 IS NULL)) AND (f3 = f30))], select=[f0, f1, f2, f3], leftInputSpec=[JoinKeyContainsUniqueKey], rightInputSpec=[JoinKeyContainsUniqueKey]) + :- Exchange(distribution=[hash[f0, f1, f2, f3]]) + : +- Calc(select=[f0, f1, f2, f3], where=[(f3 < 3)]) + : +- Reused(reference_id=[1]) + +- Exchange(distribution=[hash[f0, f1, f2, f3]]) + +- Calc(select=[f0, f1, f2, f3], where=[(f3 > 0)]) + +- Reused(reference_id=[1]) +]]> + </Resource> + </TestCase> + <TestCase name="testNonPrimaryKeySeveralSameSourcesPartialPushedDown"> + <Resource name="sql"> + <![CDATA[SELECT * FROM T WHERE f3 < 1 AND f3 > 0 + UNION SELECT * FROM T WHERE f3 < 3 AND f3 > 0 + INTERSECT SELECT * FROM T WHERE f3 > 0 AND f3 < 10]]> + </Resource> + <Resource name="ast"> + <![CDATA[ +LogicalUnion(all=[false]) +:- LogicalProject(f0=[$0], f1=[$1], f2=[$2], f3=[$3]) +: +- LogicalFilter(condition=[AND(<($3, 1), >($3, 0))]) +: +- LogicalTableScan(table=[[default_catalog, default_database, T]]) ++- LogicalIntersect(all=[false]) + :- LogicalProject(f0=[$0], f1=[$1], f2=[$2], f3=[$3]) + : +- LogicalFilter(condition=[AND(<($3, 3), >($3, 0))]) + : +- LogicalTableScan(table=[[default_catalog, default_database, T]]) + +- LogicalProject(f0=[$0], f1=[$1], f2=[$2], f3=[$3]) + +- LogicalFilter(condition=[AND(>($3, 0), <($3, 10))]) + +- LogicalTableScan(table=[[default_catalog, default_database, T]]) +]]> + </Resource> + <Resource name="optimized exec plan"> + <![CDATA[ +GroupAggregate(groupBy=[f0, f1, f2, f3], select=[f0, f1, f2, f3]) ++- Exchange(distribution=[hash[f0, f1, f2, f3]]) + +- Union(all=[true], union=[f0, f1, f2, f3]) + :- Calc(select=[f0, f1, f2, f3], where=[(f3 < 1)]) + : +- ChangelogNormalize(key=[f1], condition=[(f3 > 0)])(reuse_id=[1]) Review Comment: Nicely done on the common condition pushdown! 👍 ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/FlinkMarkChangelogNormaliseProgram.java: ########## @@ -0,0 +1,150 @@ +/* + * 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.flink.table.planner.plan.rules.physical.stream; + +import org.apache.flink.table.planner.calcite.FlinkRexBuilder; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalChangelogNormalize; +import org.apache.flink.table.planner.plan.optimize.program.FlinkOptimizeProgram; +import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; + +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A {@link FlinkOptimizeProgram} that marks changelog normalize nodes using the same source and + * determines common filters if any. + */ +public class FlinkMarkChangelogNormaliseProgram + implements FlinkOptimizeProgram<StreamOptimizeContext> { + @Override + public RelNode optimize(RelNode root, StreamOptimizeContext context) { + final Map<TableScan, List<ChangelogNormalizeContext>> tableScansToChangelogNormalize = + new HashMap<>(); + final FlinkRexBuilder rexBuilder = + new FlinkRexBuilder(context.getFlinkRelBuilder().getTypeFactory()); + for (RelNode relNode : root.getInputs()) { + gatherTableScanToChangelogNormalizeMap( + relNode, tableScansToChangelogNormalize, rexBuilder); + } + + for (Map.Entry<TableScan, List<ChangelogNormalizeContext>> entry : + tableScansToChangelogNormalize.entrySet()) { + if (entry.getValue().size() <= 1) { + continue; + } + + List<ChangelogNormalizeContext> list = entry.getValue(); + list.sort(Comparator.comparingInt(o -> o.rexNodes.size())); + Set<RexNode> common = new HashSet<>(list.get(0).rexNodes); + + for (int i = 1; i < list.size() && !common.isEmpty(); i++) { + common.retainAll(list.get(i).rexNodes); + } + + for (ChangelogNormalizeContext ctx : entry.getValue()) { + ctx.getChangelogNormalize().markSourceReuse(); + if (!common.isEmpty()) { + ctx.getChangelogNormalize().setCommonFilter(common.toArray(new RexNode[0])); + } + } + } + return root; + } + + private void gatherTableScanToChangelogNormalizeMap( + RelNode curRelNode, + Map<TableScan, List<ChangelogNormalizeContext>> map, + FlinkRexBuilder rexBuilder) { + for (RelNode input : curRelNode.getInputs()) { + if (input instanceof StreamPhysicalChangelogNormalize) { + StreamPhysicalChangelogNormalize changelogNormalize = + (StreamPhysicalChangelogNormalize) input; + if (curRelNode instanceof StreamPhysicalCalc) { + StreamPhysicalCalc calc = (StreamPhysicalCalc) curRelNode; + final List<RexNode> list = getConditions(calc, rexBuilder); + gatherTableScanToChangelogNormalizeMap( + input, ChangelogNormalizeContext.of(changelogNormalize, list), map); + } + } else { + gatherTableScanToChangelogNormalizeMap(input, map, rexBuilder); + } + } + } + + private List<RexNode> getConditions(StreamPhysicalCalc calc, RexBuilder rexBuilder) { + final RexProgram program = calc.getProgram(); + if (program.getCondition() != null) { + final RexNode rexNode = + RexUtil.toCnf( + rexBuilder, + FlinkRexUtil.expandSearch( + rexBuilder, program.expandLocalRef(program.getCondition()))); + return RelOptUtil.conjunctions(rexNode); + } + return List.of(); + } + + private void gatherTableScanToChangelogNormalizeMap( + RelNode cur, + ChangelogNormalizeContext context, + Map<TableScan, List<ChangelogNormalizeContext>> currentMap) { + if (cur instanceof TableScan) { + currentMap.computeIfAbsent((TableScan) cur, (k) -> new ArrayList<>()).add(context); + } else { + for (RelNode relNode : cur.getInputs()) { + gatherTableScanToChangelogNormalizeMap(relNode, context, currentMap); + } + } + } + + private static class ChangelogNormalizeContext { + private final StreamPhysicalChangelogNormalize changelogNormalize; + private final List<RexNode> rexNodes; Review Comment: Could we use a more descriptive name? Are those conditions, filters? ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/FlinkMarkChangelogNormaliseProgram.java: ########## @@ -0,0 +1,150 @@ +/* + * 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.flink.table.planner.plan.rules.physical.stream; + +import org.apache.flink.table.planner.calcite.FlinkRexBuilder; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalChangelogNormalize; +import org.apache.flink.table.planner.plan.optimize.program.FlinkOptimizeProgram; +import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; + +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A {@link FlinkOptimizeProgram} that marks changelog normalize nodes using the same source and + * determines common filters if any. + */ +public class FlinkMarkChangelogNormaliseProgram + implements FlinkOptimizeProgram<StreamOptimizeContext> { + @Override + public RelNode optimize(RelNode root, StreamOptimizeContext context) { + final Map<TableScan, List<ChangelogNormalizeContext>> tableScansToChangelogNormalize = + new HashMap<>(); + final FlinkRexBuilder rexBuilder = + new FlinkRexBuilder(context.getFlinkRelBuilder().getTypeFactory()); + for (RelNode relNode : root.getInputs()) { + gatherTableScanToChangelogNormalizeMap( + relNode, tableScansToChangelogNormalize, rexBuilder); + } + + for (Map.Entry<TableScan, List<ChangelogNormalizeContext>> entry : + tableScansToChangelogNormalize.entrySet()) { + if (entry.getValue().size() <= 1) { + continue; + } + + List<ChangelogNormalizeContext> list = entry.getValue(); + list.sort(Comparator.comparingInt(o -> o.rexNodes.size())); + Set<RexNode> common = new HashSet<>(list.get(0).rexNodes); + + for (int i = 1; i < list.size() && !common.isEmpty(); i++) { + common.retainAll(list.get(i).rexNodes); + } Review Comment: nit: extract to a separate method e.g. `calculateCommonCondition` ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/FlinkMarkChangelogNormaliseProgram.java: ########## @@ -0,0 +1,150 @@ +/* + * 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.flink.table.planner.plan.rules.physical.stream; + +import org.apache.flink.table.planner.calcite.FlinkRexBuilder; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalCalc; +import org.apache.flink.table.planner.plan.nodes.physical.stream.StreamPhysicalChangelogNormalize; +import org.apache.flink.table.planner.plan.optimize.program.FlinkOptimizeProgram; +import org.apache.flink.table.planner.plan.optimize.program.StreamOptimizeContext; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; + +import org.apache.calcite.plan.RelOptUtil; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.TableScan; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexUtil; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A {@link FlinkOptimizeProgram} that marks changelog normalize nodes using the same source and + * determines common filters if any. + */ +public class FlinkMarkChangelogNormaliseProgram + implements FlinkOptimizeProgram<StreamOptimizeContext> { + @Override + public RelNode optimize(RelNode root, StreamOptimizeContext context) { + final Map<TableScan, List<ChangelogNormalizeContext>> tableScansToChangelogNormalize = + new HashMap<>(); + final FlinkRexBuilder rexBuilder = + new FlinkRexBuilder(context.getFlinkRelBuilder().getTypeFactory()); + for (RelNode relNode : root.getInputs()) { + gatherTableScanToChangelogNormalizeMap( + relNode, tableScansToChangelogNormalize, rexBuilder); + } + + for (Map.Entry<TableScan, List<ChangelogNormalizeContext>> entry : + tableScansToChangelogNormalize.entrySet()) { + if (entry.getValue().size() <= 1) { + continue; + } + + List<ChangelogNormalizeContext> list = entry.getValue(); + list.sort(Comparator.comparingInt(o -> o.rexNodes.size())); + Set<RexNode> common = new HashSet<>(list.get(0).rexNodes); + + for (int i = 1; i < list.size() && !common.isEmpty(); i++) { + common.retainAll(list.get(i).rexNodes); + } + + for (ChangelogNormalizeContext ctx : entry.getValue()) { + ctx.getChangelogNormalize().markSourceReuse(); + if (!common.isEmpty()) { + ctx.getChangelogNormalize().setCommonFilter(common.toArray(new RexNode[0])); + } + } + } + return root; + } + + private void gatherTableScanToChangelogNormalizeMap( + RelNode curRelNode, + Map<TableScan, List<ChangelogNormalizeContext>> map, + FlinkRexBuilder rexBuilder) { + for (RelNode input : curRelNode.getInputs()) { + if (input instanceof StreamPhysicalChangelogNormalize) { + StreamPhysicalChangelogNormalize changelogNormalize = + (StreamPhysicalChangelogNormalize) input; + if (curRelNode instanceof StreamPhysicalCalc) { + StreamPhysicalCalc calc = (StreamPhysicalCalc) curRelNode; + final List<RexNode> list = getConditions(calc, rexBuilder); + gatherTableScanToChangelogNormalizeMap( + input, ChangelogNormalizeContext.of(changelogNormalize, list), map); + } + } else { + gatherTableScanToChangelogNormalizeMap(input, map, rexBuilder); + } + } + } + + private List<RexNode> getConditions(StreamPhysicalCalc calc, RexBuilder rexBuilder) { + final RexProgram program = calc.getProgram(); + if (program.getCondition() != null) { + final RexNode rexNode = + RexUtil.toCnf( + rexBuilder, + FlinkRexUtil.expandSearch( + rexBuilder, program.expandLocalRef(program.getCondition()))); + return RelOptUtil.conjunctions(rexNode); + } + return List.of(); + } + + private void gatherTableScanToChangelogNormalizeMap( + RelNode cur, + ChangelogNormalizeContext context, + Map<TableScan, List<ChangelogNormalizeContext>> currentMap) { + if (cur instanceof TableScan) { + currentMap.computeIfAbsent((TableScan) cur, (k) -> new ArrayList<>()).add(context); + } else { + for (RelNode relNode : cur.getInputs()) { + gatherTableScanToChangelogNormalizeMap(relNode, context, currentMap); + } + } + } + + private static class ChangelogNormalizeContext { + private final StreamPhysicalChangelogNormalize changelogNormalize; + private final List<RexNode> rexNodes; + + public ChangelogNormalizeContext( + StreamPhysicalChangelogNormalize changelogNormalize, List<RexNode> rexNodes) { + this.changelogNormalize = changelogNormalize; + this.rexNodes = rexNodes; + } + + public static ChangelogNormalizeContext of( + StreamPhysicalChangelogNormalize changelogNormalize, List<RexNode> rexNodes) { + return new ChangelogNormalizeContext(changelogNormalize, rexNodes); + } + + public StreamPhysicalChangelogNormalize getChangelogNormalize() { + return changelogNormalize; + } Review Comment: Could you unify the access? Either use getters or direct field access. -- 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]
