AlanConfluent commented on code in PR #29939:
URL: https://github.com/apache/flink/pull/29939#discussion_r4776957913
##########
docs/content/docs/dev/table/functions/udfs.md:
##########
@@ -1119,6 +1121,8 @@ While calls to an `AsyncScalarFunction` may be completed
out of the original inp
#### Error Handling
The primary way for a user to indicate an error is to call
`CompletableFuture.completeExceptionally(Throwable)`. Similarly, if an
exception is encountered by the system when invoking `eval`, that will also
result in an error. When an error occurs, the system will consider the retry
strategy, configured by [`table.exec.async-scalar.retry-strategy`]({{< ref
"docs/dev/table/config#table-exec-async-scalar-retry-strategy" >}}). If this is
`NO_RETRY`, the job is failed. If it is set to `FIXED_DELAY`, a period of
[`table.exec.async-scalar.retry-delay`]({{< ref
"docs/dev/table/config#table-exec-async-scalar-retry-delay" >}}) will be
waited, and the function call will be retried. If there have been
[`table.exec.async-scalar.max-attempts`]({{< ref
"docs/dev/table/config#table-exec-async-scalar-max-attempts" >}}) failed
attempts or if the timeout [`table.exec.async-scalar.timeout`]({{< ref
"docs/dev/table/config#table-exec-async-scalar-timeout" >}}) expires (including
all retry attempts), the
job will fail.
+When a bounded input ends, every call that is waiting for a delayed retry is
retried once immediately and no further retries happen. Any attempt that fails
after the end of the input fails the job, even if fewer than
`table.exec.async-scalar.max-attempts` attempts have been made.
Review Comment:
Failing the job for any failure after the end of input might not be great
with a flaky backend and with batch, you will always hit an end to input,
unlike unbounded streams. Could be worth making a follow up to extend it, but
this is just core `AsyncWaitOperator` so it's not specific to your change.
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/common/AsyncCalcTestPrograms.java:
##########
@@ -16,12 +16,16 @@
* limitations under the License.
*/
-package org.apache.flink.table.planner.plan.nodes.exec.stream;
+package org.apache.flink.table.planner.plan.nodes.exec.common;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.data.TimestampData;
import org.apache.flink.table.functions.AsyncScalarFunction;
import org.apache.flink.table.functions.FunctionContext;
+import org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecAsyncCalc;
+import
org.apache.flink.table.planner.plan.nodes.exec.stream.StreamExecAsyncCalc;
+import
org.apache.flink.table.planner.runtime.stream.table.AsyncCalcITCase.AsyncFuncAdd10;
Review Comment:
Nit: You could move this function to JavaUserDefinedScalarFunctions so that
it's not in a stream specific package.
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecAsyncCalc.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.AsyncScalarFunction;
+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.CommonExecAsyncCalc;
+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.RexNode;
+
+import java.util.Collections;
+import java.util.List;
+
+/** Batch {@link ExecNode} for {@link AsyncScalarFunction}. */
+@ExecNodeMetadata(
+ name = "batch-exec-async-calc",
+ version = 1,
+ producedTransformations =
CommonExecAsyncCalc.ASYNC_CALC_TRANSFORMATION,
+ minPlanVersion = FlinkVersion.v2_5,
+ minStateVersion = FlinkVersion.v2_5,
+ consumedOptions = {
+ "table.exec.async-scalar.max-concurrent-operations",
Review Comment:
For what it's worth, I've seen situations where it's better not to have
these as part of the plan. Namely if you want to set some runtime options
later that are meant to override these values, the values stored with a
compiled plan will clobber that. In other words, these are more about execution
than the plan. I think for streaming async correlate, we deliberately didn't
include these options. And it might be useful to skip them here too unless
there is a reason.
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCalcTest.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.nodes.physical.batch.BatchPhysicalAsyncCalc;
+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;
+
+/** Tests for {@link BatchPhysicalAsyncCalc}. */
+class AsyncCalcTest extends TableTestBase {
Review Comment:
This is essentially the same as AsyncCalcSplitRuleTest. Could you factor
the tests out to a shared class (e.g. `AsyncCalcSplitTestBase`) and just have
subclasses set up the tests with their stream/batch `TableTestUtil` and
separate xml plan files? That would allow you to share way more cases and
ensure some amount of parity with what is covered.
##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/processor/MultipleInputNodeCreationProcessor.java:
##########
@@ -195,6 +196,10 @@ private boolean
canBeMultipleInputNodeMember(ExecNodeWrapper wrapper) {
// exchange cannot be a member of multiple input node
return false;
}
+ if (wrapper.execNode instanceof BatchExecAsyncCalc) {
Review Comment:
The exclusion seems reasonable to me. The cost is that, in the
async-calc-join-input test, the async calc and the join run as separate tasks.
The async calc's output gets written out and read back by the join instead of
passed along in memory. For an async UDF, the remote calls should matter more
than that.
The same NPE likely applies to other async nodes: async lookup join,
ML_PREDICT and VECTOR_SEARCH. If so, can you extend the exclusion or file a
follow-up?
##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/FlinkBatchRuleSets.scala:
##########
@@ -388,6 +388,8 @@ object FlinkBatchRuleSets {
PythonCorrelateSplitFunctionKindRule.INSTANCE,
// merge calc after calc transpose
FlinkCalcMergeRule.INSTANCE,
+ // Extract after calc merging to preserve shared results, but before
Python calc normalization.
+ AsyncCorrelateSplitRule.CORRELATE_SPLIT_ASYNC_SCALAR,
Review Comment:
Are all of the the other split rules are already there? It seems like most
have been added already to support async python, so I would guess so, but might
want to check to be sure.
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/AsyncCalcTest.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.nodes.physical.batch.BatchPhysicalAsyncCalc;
+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;
+
+/** Tests for {@link BatchPhysicalAsyncCalc}. */
+class AsyncCalcTest extends TableTestBase {
+
+ private BatchTableTestUtil util;
+
+ @BeforeEach
+ void setup() {
+ util = batchTestUtil(TableConfig.getDefault());
+ util.getTableEnv()
+ .executeSql(
+ "CREATE TABLE MyTable (\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 MyTable 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 MyTable t2 ON t1.a = t2.a");
+ }
+
+ @Test
+ void testPythonAsyncCalcIsMergedIntoMultipleInput() {
+ util.verifyExecPlan(
+ "SELECT t1.a, t1.f, t2.b FROM "
+ + "(SELECT a, pya(a, a) AS f FROM (SELECT a FROM
MyTable GROUP BY a)) t1 "
+ + "JOIN MyTable t2 ON t1.a = t2.a");
+ }
+
+ @Test
+ void testAsyncCallInCorrelateArgument() {
+ util.verifyExecPlan("SELECT * FROM MyTable, LATERAL
tableFunc(func1(a))");
+ }
+
+ @Test
+ void testNestedAsyncCallInCorrelateArgument() {
+ util.verifyExecPlan("SELECT * FROM MyTable, LATERAL
tableFunc(ABS(func1(a)))");
Review Comment:
How about an async call in a condition on the output of a correlate? This
might also affect streaming too. If it fails and is missing a split rule, can
you file a bug?
##########
docs/content/docs/dev/table/functions/udfs.md:
##########
@@ -1030,6 +1030,8 @@ Naively accessing data in the external database, for
example using a `ScalarFunc
To address this inefficiency, there is an `AsyncScalarFunction`. Asynchronous
interaction with the database 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.
+`AsyncScalarFunction` is supported in both streaming and batch mode.
Review Comment:
Do you have to update docs/content.zh/docs/dev/table/functions/udfs.md too?
--
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]