yiyutian1 commented on code in PR #29980: URL: https://github.com/apache/flink/pull/29980#discussion_r4794109548
########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCorrelateTest.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.batch.sql; + +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalAsyncCorrelate; +import org.apache.flink.table.planner.runtime.utils.JavaUserDefinedTableFunctions; +import org.apache.flink.table.planner.utils.BatchTableTestUtil; +import org.apache.flink.table.planner.utils.TableTestBase; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for {@link BatchPhysicalAsyncCorrelate}. */ +class AsyncCorrelateTest extends TableTestBase { Review Comment: Moved the shared cases into `AsyncCorrelateTestBase` under plan/common, following `CalcMergeTestBase`, so the stream and batch tests now only provide their `TableTestUtil`. The three cases with an async scalar function in the table function argument stay in the stream test for now. Batch can't split them until `CORRELATE_SPLIT_ASYNC_SCALAR` and the batch async calc rule from #29939 are both in, and I'll move them into the base then. ########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCorrelateTest.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.batch.sql; + +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalAsyncCorrelate; +import org.apache.flink.table.planner.runtime.utils.JavaUserDefinedTableFunctions; +import org.apache.flink.table.planner.utils.BatchTableTestUtil; +import org.apache.flink.table.planner.utils.TableTestBase; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** Tests for {@link BatchPhysicalAsyncCorrelate}. */ +class AsyncCorrelateTest extends TableTestBase { + + private BatchTableTestUtil util; + + @BeforeEach + void setup() { + util = batchTestUtil(TableConfig.getDefault()); + util.getTableEnv() + .executeSql( + "CREATE TABLE MyTable (\n" + + " a INT NOT NULL,\n" + + " b BIGINT,\n" + + " c STRING\n" + + ") WITH (\n" + + " 'connector' = 'test-simple-table-source'\n" + + ")"); + util.addTemporarySystemFunction( + "asyncTableFunc", new JavaUserDefinedTableFunctions.AsyncTestTableFunction()); + util.addTemporarySystemFunction( + "mySum", new JavaUserDefinedTableFunctions.SumScalarFunction()); + } + + @Test + void testAsyncCorrelate() { + util.verifyExecPlan("SELECT * FROM MyTable, LATERAL asyncTableFunc(a)"); + } + + @Test + void testAsyncCorrelateLeftJoin() { + util.verifyExecPlan("SELECT * FROM MyTable LEFT JOIN LATERAL asyncTableFunc(a) ON TRUE"); Review Comment: Agreed, the runner ignores the join type and drops the left row when the function returns nothing. I removed the LEFT JOIN cases from the batch plan and restore tests, and LEFT support can come with #26817. ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecAsyncCorrelate.java: ########## @@ -0,0 +1,97 @@ +/* + * 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.nodes.exec.batch; + +import org.apache.flink.FlinkVersion; +import org.apache.flink.configuration.ReadableConfig; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.functions.AsyncTableFunction; +import org.apache.flink.table.planner.plan.nodes.exec.ExecNode; +import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeContext; +import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeMetadata; +import org.apache.flink.table.planner.plan.nodes.exec.InputProperty; +import org.apache.flink.table.planner.plan.nodes.exec.common.CommonExecAsyncCorrelate; +import org.apache.flink.table.runtime.operators.join.FlinkJoinType; +import org.apache.flink.table.types.logical.RowType; + +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator; +import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty; + +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexNode; + +import java.util.Collections; +import java.util.List; + +/** Batch {@link ExecNode} for {@link AsyncTableFunction}. */ +@ExecNodeMetadata( + name = "batch-exec-async-correlate", + version = 1, + producedTransformations = CommonExecAsyncCorrelate.ASYNC_CORRELATE_TRANSFORMATION, + minPlanVersion = FlinkVersion.v2_5, + minStateVersion = FlinkVersion.v2_5, + consumedOptions = { + "table.exec.async-table.max-concurrent-operations", Review Comment: Removed, matching `StreamExecAsyncCorrelate`, and regenerated the batch restore plans. ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/common/CommonPhysicalAsyncCorrelateRule.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.common; + +import org.apache.flink.table.functions.AsyncTableFunction; +import org.apache.flink.table.functions.FunctionKind; +import org.apache.flink.table.planner.plan.nodes.FlinkConvention; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCalc; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalCorrelate; +import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan; +import org.apache.flink.table.planner.plan.rules.physical.stream.StreamPhysicalCorrelateRule; +import org.apache.flink.table.planner.plan.utils.AsyncUtil; + +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.plan.volcano.RelSubset; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.convert.ConverterRule; +import org.apache.calcite.rel.core.JoinRelType; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Base rule for converting logical correlates containing {@link AsyncTableFunction} calls to a + * physical async correlate. Subclasses only create the physical node of their convention. + */ +public abstract class CommonPhysicalAsyncCorrelateRule extends ConverterRule { + + protected CommonPhysicalAsyncCorrelateRule(Config config) { + super(config); + } + + // find only calc and table function + private boolean findAsyncTableFunction(RelNode node) { + if (node instanceof FlinkLogicalTableFunctionScan) { + FlinkLogicalTableFunctionScan scan = (FlinkLogicalTableFunctionScan) node; + return AsyncUtil.isAsyncCall(scan.getCall(), FunctionKind.ASYNC_TABLE); + } else if (node instanceof FlinkLogicalCalc) { + FlinkLogicalCalc calc = (FlinkLogicalCalc) node; + RelNode child = ((RelSubset) calc.getInput()).getOriginal(); + return findAsyncTableFunction(child); + } + return false; + } + + @Override + public boolean matches(RelOptRuleCall call) { + FlinkLogicalCorrelate correlate = call.rel(0); + RelNode right = ((RelSubset) correlate.getRight()).getOriginal(); + return findAsyncTableFunction(right); + } + + @Override + public RelNode convert(RelNode rel) { + FlinkLogicalCorrelate correlate = (FlinkLogicalCorrelate) rel; + FlinkConvention convention = (FlinkConvention) getOutConvention(); + RelTraitSet traitSet = rel.getTraitSet().replace(convention); + RelNode convInput = RelOptRule.convert(correlate.getInput(0), convention); + RelNode right = correlate.getInput(1); + return convertToCorrelate( + right, correlate, traitSet, convInput, Optional.empty(), Optional.empty()); + } + + private RelNode convertToCorrelate( + RelNode relNode, + FlinkLogicalCorrelate correlate, + RelTraitSet traitSet, + RelNode convInput, + Optional<List<RexNode>> projections, + Optional<RexNode> condition) { + if (relNode instanceof RelSubset) { + RelSubset rel = (RelSubset) relNode; + return convertToCorrelate( + rel.getRelList().get(0), + correlate, + traitSet, + convInput, + projections, + condition); + } else if (relNode instanceof FlinkLogicalCalc) { + FlinkLogicalCalc calc = (FlinkLogicalCalc) relNode; + RelNode tableScan = StreamPhysicalCorrelateRule.getTableScan(calc); Review Comment: Done. `CommonPhysicalAsyncCorrelateRule` now has its own private helpers and no longer calls `StreamPhysicalCorrelateRule`. ########## docs/content/docs/dev/table/functions/udfs.md: ########## @@ -1286,6 +1286,8 @@ Similar to `AsyncScalarFunction`, there also exists a `AsyncTableFunction` for r Asynchronous interaction with an external system means that a single function instance can handle many requests concurrently and receive the responses concurrently. That way, the waiting time can be overlaid with sending other requests and receiving responses. At the very least, the waiting time is amortized over multiple requests. This leads in most cases to much higher streaming throughput. +`AsyncTableFunction` is supported in both streaming and batch mode. Review Comment: Done, the Chinese docs now cover batch support and the retry behaviour at the end of input. -- 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]
