snuyanzin commented on code in PR #28664: URL: https://github.com/apache/flink/pull/28664#discussion_r3549528400
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/LogicalJoinToLateralSnapshotJoinRule.java: ########## @@ -0,0 +1,507 @@ +/* + * 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.logical; + +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.planner.calcite.FlinkTypeFactory; +import org.apache.flink.table.planner.calcite.RexTableArgCall; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalJoin; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalLateralSnapshotJoin; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan; +import org.apache.flink.table.planner.plan.utils.FlinkRexUtil; +import org.apache.flink.table.planner.plan.utils.LateralSnapshotJoinUtil; +import org.apache.flink.table.types.inference.strategies.LateralSnapshotTypeStrategy; + +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.plan.hep.HepRelVertex; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.JoinInfo; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexExecutor; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexProgram; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.sql.SqlKind; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.List; + +/** + * Rewrites a {@link FlinkLogicalJoin} whose right side is a {@link FlinkLogicalTableFunctionScan} + * backed by the built-in {@code SNAPSHOT} function into a dedicated {@link + * FlinkLogicalLateralSnapshotJoin}. The right-side input becomes the actual TABLE argument of the + * SNAPSHOT call. The SNAPSHOT-specific arguments (load_completed_time, load_completed_idle_timeout, + * state_ttl) are carried as fields on the new node. + * + * <p>By the time this rule fires, Calcite's decorrelator has already converted the original {@code + * LogicalCorrelate} into a {@code LogicalJoin} (because SNAPSHOT does not actually reference any + * field of the outer input). The rule therefore matches the join shape directly. + */ [email protected] +public class LogicalJoinToLateralSnapshotJoinRule + extends RelRule< + LogicalJoinToLateralSnapshotJoinRule.LogicalJoinToLateralSnapshotJoinRuleConfig> { + + public static final LogicalJoinToLateralSnapshotJoinRule INSTANCE = + LogicalJoinToLateralSnapshotJoinRule.LogicalJoinToLateralSnapshotJoinRuleConfig.DEFAULT + .toRule(); + + private LogicalJoinToLateralSnapshotJoinRule( + LogicalJoinToLateralSnapshotJoinRuleConfig config) { + super(config); + } + + @Override + public boolean matches(RelOptRuleCall call) { + final FlinkLogicalJoin join = call.rel(0); + // the rule replaces FlinkLogicalJoin, so it won't fire on its output. + return findSnapshotScan(join.getRight()) != null; + } + + @Override + public void onMatch(RelOptRuleCall call) { + final FlinkLogicalJoin join = call.rel(0); + final RelNode leftNode = join.getLeft(); + final FlinkLogicalTableFunctionScan scan = findSnapshotScan(join.getRight()); Review Comment: do we need to call this second time? we already know it is not null since we have such condition in `onMatch` or did I miss anything? -- 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]
