yiyutian1 commented on code in PR #29939:
URL: https://github.com/apache/flink/pull/29939#discussion_r4582389152


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/batch/AsyncCalcBatchRestoreTest.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.table.planner.plan.nodes.exec.stream.AsyncCalcTestPrograms;
+import 
org.apache.flink.table.planner.plan.nodes.exec.testutils.BatchRestoreTestBase;
+import org.apache.flink.table.test.program.TableTestProgram;
+
+import java.util.Arrays;
+import java.util.List;
+
+/** Batch compiled plan tests for {@link BatchExecAsyncCalc}. */
+public class AsyncCalcBatchRestoreTest extends BatchRestoreTestBase {
+
+    public AsyncCalcBatchRestoreTest() {
+        super(BatchExecAsyncCalc.class);
+    }
+
+    @Override
+    public List<TableTestProgram> programs() {
+        return Arrays.asList(

Review Comment:
   Done.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/batch/sql/AsyncCalcITCase.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.runtime.batch.sql;
+
+import org.apache.flink.table.api.config.ExecutionConfigOptions;
+import org.apache.flink.table.functions.AsyncScalarFunction;
+import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.functions.TableFunction;
+import org.apache.flink.table.planner.factories.TestValuesTableFactory;
+import org.apache.flink.table.planner.runtime.utils.BatchTestBase;
+import org.apache.flink.types.Row;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** IT cases for {@link AsyncScalarFunction} in batch mode. */
+class AsyncCalcITCase extends BatchTestBase {
+
+    private static final int NUM_ROWS = 1000;
+    private static final int SMALL_NUM_ROWS = 100;
+    private static final int RETRY_NUM_ROWS = 10;
+    private static final int MAX_CONCURRENT_OPERATIONS = 50;
+    private static final int ADDEND = 10;
+
+    @BeforeEach
+    @Override
+    public void before() throws Exception {
+        super.before();
+        tEnv().getConfig()
+                .set(
+                        
ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_MAX_CONCURRENT_OPERATIONS,
+                        MAX_CONCURRENT_OPERATIONS);
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM,
 1);
+        tEnv().createTemporarySystemFunction("delayedAdd", 
DelayedAddTen.class);
+        tEnv().createTemporarySystemFunction("failingAdd", 
FailingAddTen.class);
+        tEnv().createTemporarySystemFunction("twice", Twice.class);
+        FailingAddTen.FAILURES.set(0);
+        FailingAddTen.ATTEMPTS.set(0);
+    }
+
+    private void createTables(int numRows, int sinkParallelism) {
+        final List<Row> data = new ArrayList<>();
+        for (int i = 0; i < numRows; i++) {
+            data.add(Row.of(i));
+        }
+        tEnv().executeSql(
+                        "CREATE TABLE source_t (a INT) WITH ("
+                                + "'connector' = 'values', "
+                                + "'bounded' = 'true', "
+                                + "'data-id' = '"
+                                + TestValuesTableFactory.registerData(data)
+                                + "')");
+        tEnv().executeSql(
+                        "CREATE TABLE sink_t (a INT, b INT) WITH ("
+                                + "'connector' = 'values', "
+                                + "'sink-insert-only' = 'true', "
+                                + "'sink.parallelism' = '"
+                                + sinkParallelism
+                                + "')");
+    }
+
+    @Test
+    void testAsyncCalc() throws Exception {
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query = "SELECT a, delayedAdd(a) FROM source_t";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");

Review Comment:
   Removed. The plans are verified in `AsyncCalcTest` and 
`ForwardHashExchangeTest`.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCalcTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.planner.plan.rules.logical.AsyncCalcSplitRuleTest;
+import 
org.apache.flink.table.planner.runtime.utils.JavaUserDefinedScalarFunctions.AsyncPythonScalarFunction;
+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;
+
+/** Plan tests for async scalar functions in a batch calc ({@code 
BatchPhysicalAsyncCalc}). */
+class AsyncCalcTest extends TableTestBase {
+
+    private BatchTableTestUtil util;
+
+    @BeforeEach
+    void setup() {
+        util = batchTestUtil(TableConfig.getDefault());
+        for (String table : new String[] {"MyTable", "MyTable2"}) {
+            util.getTableEnv()
+                    .executeSql(
+                            "CREATE TABLE "
+                                    + table
+                                    + " (\n"
+                                    + "  a INT,\n"
+                                    + "  b BIGINT,\n"
+                                    + "  c STRING\n"
+                                    + ") WITH (\n"
+                                    + "  'connector' = 
'test-simple-table-source'\n"
+                                    + ")");
+        }
+        util.addTemporarySystemFunction("func1", new 
AsyncCalcSplitRuleTest.Func1());

Review Comment:
   Removed `MyTable2`, now a self-join.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCalcTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.planner.plan.rules.logical.AsyncCalcSplitRuleTest;
+import 
org.apache.flink.table.planner.runtime.utils.JavaUserDefinedScalarFunctions.AsyncPythonScalarFunction;
+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;
+
+/** Plan tests for async scalar functions in a batch calc ({@code 
BatchPhysicalAsyncCalc}). */
+class AsyncCalcTest extends TableTestBase {
+
+    private BatchTableTestUtil util;
+
+    @BeforeEach
+    void setup() {
+        util = batchTestUtil(TableConfig.getDefault());
+        for (String table : new String[] {"MyTable", "MyTable2"}) {
+            util.getTableEnv()
+                    .executeSql(
+                            "CREATE TABLE "
+                                    + table
+                                    + " (\n"
+                                    + "  a INT,\n"
+                                    + "  b BIGINT,\n"
+                                    + "  c STRING\n"
+                                    + ") WITH (\n"
+                                    + "  'connector' = 
'test-simple-table-source'\n"
+                                    + ")");
+        }
+        util.addTemporarySystemFunction("func1", new 
AsyncCalcSplitRuleTest.Func1());
+        util.addTemporarySystemFunction("func2", new 
AsyncCalcSplitRuleTest.Func2());
+        util.addTemporarySystemFunction(
+                "tableFunc", new AsyncCalcSplitRuleTest.RandomTableFunction());
+        util.addTemporarySystemFunction("pya", new 
AsyncPythonScalarFunction("pya"));
+    }
+
+    @Test
+    void testAsyncCalc() {
+        util.verifyExecPlan("SELECT func1(a) FROM MyTable");
+    }
+
+    @Test
+    void testAsyncCalcInFilterIsSplit() {
+        util.verifyExecPlan("SELECT a FROM MyTable WHERE func1(a) > 0");
+    }
+
+    @Test
+    void testTwoAsyncCallsTwoCalcs() {
+        util.verifyExecPlan("SELECT func1(a), func2(a) FROM MyTable");
+    }
+
+    @Test
+    void testNestedAsyncCalls() {
+        util.verifyExecPlan("SELECT func1(func1(a)) FROM MyTable");
+    }
+
+    @Test
+    void testAsyncCalcNextToJoin() {
+        util.verifyExecPlan(
+                "SELECT func1(t1.a), t2.b FROM MyTable t1 JOIN MyTable2 t2 ON 
t1.a = t2.a");
+    }
+
+    @Test
+    void testAsyncCalcIsNotMergedIntoMultipleInput() {
+        util.verifyExecPlan(
+                "SELECT t1.a, t1.f, t2.b FROM "
+                        + "(SELECT a, func1(a) AS f FROM (SELECT a FROM 
MyTable GROUP BY a)) t1 "
+                        + "JOIN MyTable2 t2 ON t1.a = t2.a");
+    }
+
+    @Test
+    void testAsyncCallInCorrelateArgument() {
+        util.verifyExecPlan("SELECT * FROM MyTable, LATERAL 
TABLE(tableFunc(func1(a)))");

Review Comment:
   Not needed, `LATERAL tableFunc(...)` works. Removed.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCalcTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.planner.plan.rules.logical.AsyncCalcSplitRuleTest;
+import 
org.apache.flink.table.planner.runtime.utils.JavaUserDefinedScalarFunctions.AsyncPythonScalarFunction;
+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;
+
+/** Plan tests for async scalar functions in a batch calc ({@code 
BatchPhysicalAsyncCalc}). */
+class AsyncCalcTest extends TableTestBase {
+
+    private BatchTableTestUtil util;
+
+    @BeforeEach
+    void setup() {
+        util = batchTestUtil(TableConfig.getDefault());
+        for (String table : new String[] {"MyTable", "MyTable2"}) {
+            util.getTableEnv()
+                    .executeSql(
+                            "CREATE TABLE "
+                                    + table
+                                    + " (\n"
+                                    + "  a INT,\n"
+                                    + "  b BIGINT,\n"
+                                    + "  c STRING\n"
+                                    + ") WITH (\n"
+                                    + "  'connector' = 
'test-simple-table-source'\n"
+                                    + ")");
+        }
+        util.addTemporarySystemFunction("func1", new 
AsyncCalcSplitRuleTest.Func1());
+        util.addTemporarySystemFunction("func2", new 
AsyncCalcSplitRuleTest.Func2());
+        util.addTemporarySystemFunction(
+                "tableFunc", new AsyncCalcSplitRuleTest.RandomTableFunction());
+        util.addTemporarySystemFunction("pya", new 
AsyncPythonScalarFunction("pya"));
+    }
+
+    @Test
+    void testAsyncCalc() {
+        util.verifyExecPlan("SELECT func1(a) FROM MyTable");
+    }
+
+    @Test
+    void testAsyncCalcInFilterIsSplit() {
+        util.verifyExecPlan("SELECT a FROM MyTable WHERE func1(a) > 0");
+    }
+
+    @Test
+    void testTwoAsyncCallsTwoCalcs() {
+        util.verifyExecPlan("SELECT func1(a), func2(a) FROM MyTable");
+    }
+
+    @Test
+    void testNestedAsyncCalls() {
+        util.verifyExecPlan("SELECT func1(func1(a)) FROM MyTable");
+    }
+
+    @Test
+    void testAsyncCalcNextToJoin() {
+        util.verifyExecPlan(
+                "SELECT func1(t1.a), t2.b FROM MyTable t1 JOIN MyTable2 t2 ON 
t1.a = t2.a");
+    }
+
+    @Test
+    void testAsyncCalcIsNotMergedIntoMultipleInput() {
+        util.verifyExecPlan(
+                "SELECT t1.a, t1.f, t2.b FROM "
+                        + "(SELECT a, func1(a) AS f FROM (SELECT a FROM 
MyTable GROUP BY a)) t1 "
+                        + "JOIN MyTable2 t2 ON t1.a = t2.a");
+    }
+
+    @Test
+    void testAsyncCallInCorrelateArgument() {
+        util.verifyExecPlan("SELECT * FROM MyTable, LATERAL 
TABLE(tableFunc(func1(a)))");
+    }
+
+    @Test
+    void testNestedAsyncCallInCorrelateArgument() {
+        util.verifyExecPlan("SELECT * FROM MyTable, LATERAL 
TABLE(tableFunc(ABS(func1(a))))");

Review Comment:
   Removed.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCalcTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.planner.plan.rules.logical.AsyncCalcSplitRuleTest;
+import 
org.apache.flink.table.planner.runtime.utils.JavaUserDefinedScalarFunctions.AsyncPythonScalarFunction;
+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;
+
+/** Plan tests for async scalar functions in a batch calc ({@code 
BatchPhysicalAsyncCalc}). */
+class AsyncCalcTest extends TableTestBase {
+
+    private BatchTableTestUtil util;
+
+    @BeforeEach
+    void setup() {
+        util = batchTestUtil(TableConfig.getDefault());
+        for (String table : new String[] {"MyTable", "MyTable2"}) {
+            util.getTableEnv()
+                    .executeSql(
+                            "CREATE TABLE "
+                                    + table
+                                    + " (\n"
+                                    + "  a INT,\n"
+                                    + "  b BIGINT,\n"
+                                    + "  c STRING\n"
+                                    + ") WITH (\n"
+                                    + "  'connector' = 
'test-simple-table-source'\n"
+                                    + ")");
+        }
+        util.addTemporarySystemFunction("func1", new 
AsyncCalcSplitRuleTest.Func1());
+        util.addTemporarySystemFunction("func2", new 
AsyncCalcSplitRuleTest.Func2());
+        util.addTemporarySystemFunction(
+                "tableFunc", new AsyncCalcSplitRuleTest.RandomTableFunction());
+        util.addTemporarySystemFunction("pya", new 
AsyncPythonScalarFunction("pya"));
+    }
+
+    @Test
+    void testAsyncCalc() {
+        util.verifyExecPlan("SELECT func1(a) FROM MyTable");
+    }
+
+    @Test
+    void testAsyncCalcInFilterIsSplit() {
+        util.verifyExecPlan("SELECT a FROM MyTable WHERE func1(a) > 0");
+    }
+
+    @Test
+    void testTwoAsyncCallsTwoCalcs() {
+        util.verifyExecPlan("SELECT func1(a), func2(a) FROM MyTable");
+    }
+
+    @Test
+    void testNestedAsyncCalls() {
+        util.verifyExecPlan("SELECT func1(func1(a)) FROM MyTable");
+    }
+
+    @Test
+    void testAsyncCalcNextToJoin() {
+        util.verifyExecPlan(
+                "SELECT func1(t1.a), t2.b FROM MyTable t1 JOIN MyTable2 t2 ON 
t1.a = t2.a");
+    }
+
+    @Test
+    void testAsyncCalcIsNotMergedIntoMultipleInput() {
+        util.verifyExecPlan(
+                "SELECT t1.a, t1.f, t2.b FROM "
+                        + "(SELECT a, func1(a) AS f FROM (SELECT a FROM 
MyTable GROUP BY a)) t1 "
+                        + "JOIN MyTable2 t2 ON t1.a = t2.a");
+    }
+
+    @Test
+    void testAsyncCallInCorrelateArgument() {
+        util.verifyExecPlan("SELECT * FROM MyTable, LATERAL 
TABLE(tableFunc(func1(a)))");
+    }
+
+    @Test
+    void testNestedAsyncCallInCorrelateArgument() {
+        util.verifyExecPlan("SELECT * FROM MyTable, LATERAL 
TABLE(tableFunc(ABS(func1(a))))");
+    }
+
+    @Test
+    void testPythonAsyncAndJavaAsyncInProjection() {
+        util.verifyExecPlan("SELECT pya(a, a), func1(a) FROM MyTable");
+    }
+
+    @Test
+    void testJavaAsyncOnPythonAsync() {
+        util.verifyExecPlan("SELECT func1(pya(a, a)) FROM MyTable");
+    }
+
+    @Test
+    void testPythonAsyncOnJavaAsync() {
+        util.verifyExecPlan("SELECT pya(func1(a), a) FROM MyTable");
+    }
+
+    @Test
+    void testPythonAsyncInCorrelateArgument() {
+        util.verifyExecPlan("SELECT * FROM MyTable, LATERAL 
TABLE(tableFunc(pya(a, a)))");

Review Comment:
   Removed.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/batch/sql/AsyncCalcITCase.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.runtime.batch.sql;
+
+import org.apache.flink.table.api.config.ExecutionConfigOptions;
+import org.apache.flink.table.functions.AsyncScalarFunction;
+import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.functions.TableFunction;
+import org.apache.flink.table.planner.factories.TestValuesTableFactory;
+import org.apache.flink.table.planner.runtime.utils.BatchTestBase;
+import org.apache.flink.types.Row;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** IT cases for {@link AsyncScalarFunction} in batch mode. */
+class AsyncCalcITCase extends BatchTestBase {
+
+    private static final int NUM_ROWS = 1000;
+    private static final int SMALL_NUM_ROWS = 100;
+    private static final int RETRY_NUM_ROWS = 10;
+    private static final int MAX_CONCURRENT_OPERATIONS = 50;
+    private static final int ADDEND = 10;
+
+    @BeforeEach
+    @Override
+    public void before() throws Exception {
+        super.before();
+        tEnv().getConfig()
+                .set(
+                        
ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_MAX_CONCURRENT_OPERATIONS,
+                        MAX_CONCURRENT_OPERATIONS);
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM,
 1);
+        tEnv().createTemporarySystemFunction("delayedAdd", 
DelayedAddTen.class);
+        tEnv().createTemporarySystemFunction("failingAdd", 
FailingAddTen.class);
+        tEnv().createTemporarySystemFunction("twice", Twice.class);
+        FailingAddTen.FAILURES.set(0);
+        FailingAddTen.ATTEMPTS.set(0);
+    }
+
+    private void createTables(int numRows, int sinkParallelism) {
+        final List<Row> data = new ArrayList<>();
+        for (int i = 0; i < numRows; i++) {
+            data.add(Row.of(i));
+        }
+        tEnv().executeSql(
+                        "CREATE TABLE source_t (a INT) WITH ("
+                                + "'connector' = 'values', "
+                                + "'bounded' = 'true', "
+                                + "'data-id' = '"
+                                + TestValuesTableFactory.registerData(data)
+                                + "')");
+        tEnv().executeSql(
+                        "CREATE TABLE sink_t (a INT, b INT) WITH ("
+                                + "'connector' = 'values', "
+                                + "'sink-insert-only' = 'true', "
+                                + "'sink.parallelism' = '"
+                                + sinkParallelism
+                                + "')");
+    }
+
+    @Test
+    void testAsyncCalc() throws Exception {
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query = "SELECT a, delayedAdd(a) FROM source_t";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected(SMALL_NUM_ROWS));
+    }
+
+    @Test
+    void testInFlightRowsDrainedAtEndOfInput() throws Exception {
+        createTables(NUM_ROWS, 2);
+        tEnv().executeSql("INSERT INTO sink_t SELECT a, delayedAdd(a) FROM 
source_t").await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected(NUM_ROWS));
+    }
+
+    @Test
+    void testOutputKeepsInputOrder() throws Exception {
+        createTables(NUM_ROWS, 1);
+        tEnv().executeSql("INSERT INTO sink_t SELECT a, delayedAdd(a) FROM 
source_t").await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyElementsOf(expected(NUM_ROWS));
+    }
+
+    @Test
+    void testAsyncCallInFilter() throws Exception {
+        createTables(NUM_ROWS, 2);
+        final String query = "SELECT a, a FROM source_t WHERE delayedAdd(a) % 
2 = 0";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < NUM_ROWS; i += 2) {
+            expected.add("+I[" + i + ", " + i + "]");
+        }
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected);
+    }
+
+    @Test
+    void testAsyncCallInCorrelateArgument() throws Exception {
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query =
+                "SELECT a, b FROM source_t, LATERAL 
TABLE(twice(delayedAdd(a))) AS T(b)";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < SMALL_NUM_ROWS; i++) {
+            expected.add("+I[" + i + ", " + (i + ADDEND) + "]");
+            expected.add("+I[" + i + ", " + (i + ADDEND) + "]");
+        }
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected);
+    }
+
+    @Test
+    void testAggregateOnAsyncCalcOnHashJoin() throws Exception {
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM,
 4);
+        tEnv().getConfig()
+                .set(
+                        ExecutionConfigOptions.TABLE_EXEC_DISABLED_OPERATORS,
+                        "SortMergeJoin,NestedLoopJoin,SortAgg");
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query =
+                "SELECT t1.a, SUM(delayedAdd(t1.a + t2.a)) FROM source_t t1 "
+                        + "JOIN source_t t2 ON t1.a = t2.a GROUP BY t1.a";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc", 
"keep_input_as_is");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < SMALL_NUM_ROWS; i++) {
+            expected.add("+I[" + i + ", " + (2 * i + ADDEND) + "]");
+        }
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected);
+    }
+
+    @Test
+    void testPendingRetryIsTriedOnceAtEndOfInput() throws Exception {
+        configureRetries();
+        FailingAddTen.FAILURES.set(1);
+        createTables(RETRY_NUM_ROWS, 1);
+        tEnv().executeSql("INSERT INTO sink_t SELECT a, failingAdd(a) FROM 
source_t").await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyElementsOf(expected(RETRY_NUM_ROWS));
+        assertThat(FailingAddTen.ATTEMPTS.get()).isEqualTo(2);
+    }
+
+    @Test
+    void testPendingRetryIsNotRetriedAgainAtEndOfInput() {
+        configureRetries();
+        FailingAddTen.FAILURES.set(2);
+        createTables(RETRY_NUM_ROWS, 1);
+        assertThatThrownBy(
+                        () ->
+                                tEnv().executeSql(
+                                                "INSERT INTO sink_t SELECT a, 
failingAdd(a) FROM source_t")
+                                        .await())
+                .satisfies(anyCauseMatches(FailingAddTen.MESSAGE + 2));
+        assertThat(FailingAddTen.ATTEMPTS.get()).isEqualTo(2);
+    }
+
+    private void configureRetries() {
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_MAX_ATTEMPTS,
 5);
+        tEnv().getConfig()
+                .set(
+                        
ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_RETRY_DELAY,
+                        Duration.ofMinutes(10));
+    }
+
+    private static List<String> expected(int numRows) {
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < numRows; i++) {
+            expected.add("+I[" + i + ", " + (i + ADDEND) + "]");
+        }
+        return expected;
+    }
+
+    /** Adds ten, completing from another thread after a random delay. */
+    public static class DelayedAddTen extends AsyncScalarFunction {

Review Comment:
   The batch programs now reuse `AsyncJavaFunc0`, `AsyncUdfWithOpen` and 
`AsyncFuncAdd10`, plus `StringSplit` from `JavaUserDefinedTableFunctions`. No 
new test functions.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/runtime/batch/sql/AsyncCalcITCase.java:
##########
@@ -0,0 +1,271 @@
+/*
+ * 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.runtime.batch.sql;
+
+import org.apache.flink.table.api.config.ExecutionConfigOptions;
+import org.apache.flink.table.functions.AsyncScalarFunction;
+import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.functions.TableFunction;
+import org.apache.flink.table.planner.factories.TestValuesTableFactory;
+import org.apache.flink.table.planner.runtime.utils.BatchTestBase;
+import org.apache.flink.types.Row;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** IT cases for {@link AsyncScalarFunction} in batch mode. */
+class AsyncCalcITCase extends BatchTestBase {
+
+    private static final int NUM_ROWS = 1000;
+    private static final int SMALL_NUM_ROWS = 100;
+    private static final int RETRY_NUM_ROWS = 10;
+    private static final int MAX_CONCURRENT_OPERATIONS = 50;
+    private static final int ADDEND = 10;
+
+    @BeforeEach
+    @Override
+    public void before() throws Exception {
+        super.before();
+        tEnv().getConfig()
+                .set(
+                        
ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_MAX_CONCURRENT_OPERATIONS,
+                        MAX_CONCURRENT_OPERATIONS);
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM,
 1);
+        tEnv().createTemporarySystemFunction("delayedAdd", 
DelayedAddTen.class);
+        tEnv().createTemporarySystemFunction("failingAdd", 
FailingAddTen.class);
+        tEnv().createTemporarySystemFunction("twice", Twice.class);
+        FailingAddTen.FAILURES.set(0);
+        FailingAddTen.ATTEMPTS.set(0);
+    }
+
+    private void createTables(int numRows, int sinkParallelism) {
+        final List<Row> data = new ArrayList<>();
+        for (int i = 0; i < numRows; i++) {
+            data.add(Row.of(i));
+        }
+        tEnv().executeSql(
+                        "CREATE TABLE source_t (a INT) WITH ("
+                                + "'connector' = 'values', "
+                                + "'bounded' = 'true', "
+                                + "'data-id' = '"
+                                + TestValuesTableFactory.registerData(data)
+                                + "')");
+        tEnv().executeSql(
+                        "CREATE TABLE sink_t (a INT, b INT) WITH ("
+                                + "'connector' = 'values', "
+                                + "'sink-insert-only' = 'true', "
+                                + "'sink.parallelism' = '"
+                                + sinkParallelism
+                                + "')");
+    }
+
+    @Test
+    void testAsyncCalc() throws Exception {
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query = "SELECT a, delayedAdd(a) FROM source_t";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected(SMALL_NUM_ROWS));
+    }
+
+    @Test
+    void testInFlightRowsDrainedAtEndOfInput() throws Exception {
+        createTables(NUM_ROWS, 2);
+        tEnv().executeSql("INSERT INTO sink_t SELECT a, delayedAdd(a) FROM 
source_t").await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected(NUM_ROWS));
+    }
+
+    @Test
+    void testOutputKeepsInputOrder() throws Exception {
+        createTables(NUM_ROWS, 1);
+        tEnv().executeSql("INSERT INTO sink_t SELECT a, delayedAdd(a) FROM 
source_t").await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyElementsOf(expected(NUM_ROWS));
+    }
+
+    @Test
+    void testAsyncCallInFilter() throws Exception {
+        createTables(NUM_ROWS, 2);
+        final String query = "SELECT a, a FROM source_t WHERE delayedAdd(a) % 
2 = 0";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < NUM_ROWS; i += 2) {
+            expected.add("+I[" + i + ", " + i + "]");
+        }
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected);
+    }
+
+    @Test
+    void testAsyncCallInCorrelateArgument() throws Exception {
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query =
+                "SELECT a, b FROM source_t, LATERAL 
TABLE(twice(delayedAdd(a))) AS T(b)";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < SMALL_NUM_ROWS; i++) {
+            expected.add("+I[" + i + ", " + (i + ADDEND) + "]");
+            expected.add("+I[" + i + ", " + (i + ADDEND) + "]");
+        }
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected);
+    }
+
+    @Test
+    void testAggregateOnAsyncCalcOnHashJoin() throws Exception {
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM,
 4);
+        tEnv().getConfig()
+                .set(
+                        ExecutionConfigOptions.TABLE_EXEC_DISABLED_OPERATORS,
+                        "SortMergeJoin,NestedLoopJoin,SortAgg");
+        createTables(SMALL_NUM_ROWS, 2);
+        final String query =
+                "SELECT t1.a, SUM(delayedAdd(t1.a + t2.a)) FROM source_t t1 "
+                        + "JOIN source_t t2 ON t1.a = t2.a GROUP BY t1.a";
+        assertThat(tEnv().explainSql(query)).contains("AsyncCalc", 
"keep_input_as_is");
+        tEnv().executeSql("INSERT INTO sink_t " + query).await();
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < SMALL_NUM_ROWS; i++) {
+            expected.add("+I[" + i + ", " + (2 * i + ADDEND) + "]");
+        }
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyInAnyOrderElementsOf(expected);
+    }
+
+    @Test
+    void testPendingRetryIsTriedOnceAtEndOfInput() throws Exception {
+        configureRetries();
+        FailingAddTen.FAILURES.set(1);
+        createTables(RETRY_NUM_ROWS, 1);
+        tEnv().executeSql("INSERT INTO sink_t SELECT a, failingAdd(a) FROM 
source_t").await();
+        assertThat(TestValuesTableFactory.getResultsAsStrings("sink_t"))
+                .containsExactlyElementsOf(expected(RETRY_NUM_ROWS));
+        assertThat(FailingAddTen.ATTEMPTS.get()).isEqualTo(2);
+    }
+
+    @Test
+    void testPendingRetryIsNotRetriedAgainAtEndOfInput() {
+        configureRetries();
+        FailingAddTen.FAILURES.set(2);
+        createTables(RETRY_NUM_ROWS, 1);
+        assertThatThrownBy(
+                        () ->
+                                tEnv().executeSql(
+                                                "INSERT INTO sink_t SELECT a, 
failingAdd(a) FROM source_t")
+                                        .await())
+                .satisfies(anyCauseMatches(FailingAddTen.MESSAGE + 2));
+        assertThat(FailingAddTen.ATTEMPTS.get()).isEqualTo(2);
+    }
+
+    private void configureRetries() {
+        
tEnv().getConfig().set(ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_MAX_ATTEMPTS,
 5);
+        tEnv().getConfig()
+                .set(
+                        
ExecutionConfigOptions.TABLE_EXEC_ASYNC_SCALAR_RETRY_DELAY,
+                        Duration.ofMinutes(10));
+    }
+
+    private static List<String> expected(int numRows) {
+        final List<String> expected = new ArrayList<>();
+        for (int i = 0; i < numRows; i++) {
+            expected.add("+I[" + i + ", " + (i + ADDEND) + "]");
+        }
+        return expected;
+    }
+
+    /** Adds ten, completing from another thread after a random delay. */
+    public static class DelayedAddTen extends AsyncScalarFunction {
+
+        private static final int THREADS = 4;
+        private static final int MAX_DELAY_MS = 20;
+
+        private transient ExecutorService executor;
+
+        @Override
+        public void open(FunctionContext context) {
+            executor = Executors.newFixedThreadPool(THREADS);
+        }
+
+        public void eval(CompletableFuture<Integer> future, Integer a) {
+            executor.execute(
+                    () -> {
+                        try {
+                            
Thread.sleep(ThreadLocalRandom.current().nextInt(MAX_DELAY_MS + 1));
+                        } catch (InterruptedException e) {
+                            Thread.currentThread().interrupt();
+                            future.completeExceptionally(e);
+                            return;
+                        }
+                        future.complete(a + ADDEND);
+                    });
+        }
+
+        @Override
+        public void close() {
+            if (executor != null) {
+                executor.shutdownNow();
+            }
+        }
+    }
+
+    /** Adds ten, failing the first {@link #FAILURES} attempts for the first 
row. */
+    public static class FailingAddTen extends AsyncScalarFunction {
+
+        static final String MESSAGE = "Failed attempt ";
+        static final AtomicInteger FAILURES = new AtomicInteger();
+        static final AtomicInteger ATTEMPTS = new AtomicInteger();

Review Comment:
   Removed together with the retry tests. The end-of-input retry behaviour 
belongs to `AsyncWaitOperator` and isn't changed by this PR, so I documented it 
in udfs.md instead.



-- 
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]

Reply via email to