snuyanzin commented on code in PR #27382: URL: https://github.com/apache/flink/pull/27382#discussion_r2678596030
########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/LogicalCorrelateToJoinFromTemporalTableFunctionRule.java: ########## @@ -0,0 +1,361 @@ +/* + * 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.ValidationException; +import org.apache.flink.table.expressions.Expression; +import org.apache.flink.table.expressions.FieldReferenceExpression; +import org.apache.flink.table.functions.FunctionDefinition; +import org.apache.flink.table.functions.TemporalTableFunction; +import org.apache.flink.table.functions.TemporalTableFunctionImpl; +import org.apache.flink.table.operations.QueryOperation; +import org.apache.flink.table.planner.calcite.FlinkRelBuilder; +import org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction; +import org.apache.flink.table.planner.functions.utils.TableSqlFunction; +import org.apache.flink.table.planner.plan.nodes.exec.stream.StreamExecTemporalJoin; +import org.apache.flink.table.planner.plan.optimize.program.FlinkOptimizeContext; +import org.apache.flink.table.planner.plan.utils.ExpandTableScanShuttle; +import org.apache.flink.table.planner.plan.utils.RexDefaultVisitor; +import org.apache.flink.table.planner.plan.utils.TemporalJoinUtil; +import org.apache.flink.table.planner.utils.ShortcutUtils; +import org.apache.flink.table.types.logical.LogicalTypeRoot; + +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rel.core.TableFunctionScan; +import org.apache.calcite.rel.logical.LogicalCorrelate; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexFieldAccess; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexVisitorImpl; +import org.apache.calcite.sql.SqlOperator; +import org.immutables.value.Value; + +import java.util.Optional; + +import static org.apache.flink.table.types.logical.utils.LogicalTypeChecks.isProctimeAttribute; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * The initial temporal TableFunction join (LATERAL TemporalTableFunction(o.proctime)) is a + * correlate. Rewrite it into a Join with a special temporal join condition wraps time attribute and + * primary key information. The join will be translated into {@link StreamExecTemporalJoin} in + * physical. + */ [email protected] +public class LogicalCorrelateToJoinFromTemporalTableFunctionRule + extends RelRule< + LogicalCorrelateToJoinFromTemporalTableFunctionRule + .LogicalCorrelateToJoinFromTemporalTableFunctionRuleConfig> { + + public static final LogicalCorrelateToJoinFromTemporalTableFunctionRule INSTANCE = + LogicalCorrelateToJoinFromTemporalTableFunctionRule + .LogicalCorrelateToJoinFromTemporalTableFunctionRuleConfig.DEFAULT + .toRule(); + + private LogicalCorrelateToJoinFromTemporalTableFunctionRule( + LogicalCorrelateToJoinFromTemporalTableFunctionRuleConfig config) { + super(config); + } + + private String extractNameFromTimeAttribute(Expression timeAttribute) { + if (timeAttribute instanceof FieldReferenceExpression) { + FieldReferenceExpression f = (FieldReferenceExpression) timeAttribute; + if (f.getOutputDataType() + .getLogicalType() + .isAnyOf( + LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE, + LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE)) { + return f.getName(); + } + } + throw new ValidationException( + "Invalid timeAttribute [" + timeAttribute + "] in TemporalTableFunction"); + } + + private boolean isProctimeReference(TemporalTableFunctionImpl temporalTableFunction) { + FieldReferenceExpression fieldRef = + (FieldReferenceExpression) temporalTableFunction.getTimeAttribute(); + return isProctimeAttribute(fieldRef.getOutputDataType().getLogicalType()); + } + + private String extractNameFromPrimaryKeyAttribute(Expression expression) { + if (expression instanceof FieldReferenceExpression) { + FieldReferenceExpression f = (FieldReferenceExpression) expression; + return f.getName(); + } + throw new ValidationException( + "Unsupported expression [" + + expression + + "] as primary key. " + + "Only top-level (not nested) field references are supported."); + } + + @Override + public void onMatch(RelOptRuleCall call) { + LogicalCorrelate logicalCorrelate = call.rel(0); + RelNode leftNode = call.rel(1); + TableFunctionScan rightTableFunctionScan = call.rel(2); + + RelOptCluster cluster = logicalCorrelate.getCluster(); + + Optional<TemporalTableFunctionCall> temporalTableFunctionCall = + new GetTemporalTableFunctionCall(cluster.getRexBuilder(), leftNode) + .visit(rightTableFunctionScan.getCall()); + + if (temporalTableFunctionCall.isPresent() + && temporalTableFunctionCall.get().getTemporalTableFunction() + instanceof TemporalTableFunctionImpl) { + TemporalTableFunctionImpl rightTemporalTableFunction = Review Comment: can we do rather something like ```java if (!(temporalTableFunctionCall.isPresent() && temporalTableFunctionCall.get().getTemporalTableFunction() instanceof TemporalTableFunctionImpl)) { return; } ``` instead of having it at the very end? It would simplify code reading and understanding imho -- 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]
