twalthr commented on code in PR #29368:
URL: https://github.com/apache/flink/pull/29368#discussion_r4183080376


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/batch/sql/join/LateralSnapshotJoinTableApiTest.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.join;
+
+import org.apache.flink.table.api.ApiExpression;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableConfig;
+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 java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.table.api.Expressions.call;
+import static org.apache.flink.table.api.Expressions.lit;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Verifies that the Table API surface of the {@code LATERAL SNAPSHOT} join 
produces the same
+ * optimized physical plan as the equivalent SQL in batch mode. In batch the 
join degenerates to a
+ * regular join (the SNAPSHOT-specific arguments are dropped), so {@code 
on_time} is not required.
+ */
+public class LateralSnapshotJoinTableApiTest extends TableTestBase {

Review Comment:
   We have a test base for this already:
   QueryOperationSqlSerializationTest
   we should add a test there



##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/utils/CorrelatedFunctionTableFactory.java:
##########
@@ -91,11 +100,15 @@ private CorrelatedFunctionQueryOperation 
unwrapFromAlias(CallExpression call) {
                                                                                
     + alias)))
                             .collect(toList());
 
-            if (!isFunctionOfKind(children.get(0), FunctionKind.TABLE)) {
+            // SNAPSHOT is PROCESS_TABLE rather than TABLE but is valid in a 
LATERAL join.
+            Expression firstChild = children.get(0);
+            if (!isFunctionOfKind(firstChild, FunctionKind.TABLE)
+                    && !(firstChild instanceof CallExpression
+                            && isSnapshot(((CallExpression) 
firstChild).getFunctionDefinition()))) {

Review Comment:
   ```suggestion
                       && !ApiExpressionUtils.isFunction(firstChild, SNAPSHOT)) 
{
   ```



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/join/LateralSnapshotJoinTest.java:
##########
@@ -203,6 +203,32 @@ void testBuildSideProctimeIsMaterialized() {
                         + "ON probe.pk = s.bk");
     }
 
+    @Test
+    void testBuildSideRowtimeFromMetadataColumn() {

Review Comment:
   this seems unrelated to this PR?



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/LateralSnapshotJoinTableApiSemanticTestPrograms.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.stream;
+
+import org.apache.flink.table.api.ApiExpression;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.Tumble;
+import org.apache.flink.table.test.program.SinkTestStep;
+import org.apache.flink.table.test.program.TableTestProgram;
+import org.apache.flink.types.Row;
+
+import java.util.Arrays;
+
+import static org.apache.flink.table.api.Expressions.$;
+import static org.apache.flink.table.api.Expressions.call;
+import static org.apache.flink.table.api.Expressions.descriptor;
+import static org.apache.flink.table.api.Expressions.lit;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.FLIP_TRIGGER_TS;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.appendBuild;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.defaultBuild;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.defaultProbe;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.keyValueSink;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.throttledProbe;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.ts;
+import static 
org.apache.flink.table.planner.plan.nodes.exec.stream.LateralSnapshotJoinSemanticTestPrograms.withFlipTrigger;
+
+/**
+ * Table API counterparts of {@link LateralSnapshotJoinSemanticTestPrograms}, 
reusing the same
+ * sources, sinks, and flip-trigger determinism tricks but expressing the 
{@code LATERAL SNAPSHOT}
+ * join through {@link Table#joinLateral} / {@link Table#leftOuterJoinLateral} 
instead of SQL.
+ */
+public class LateralSnapshotJoinTableApiSemanticTestPrograms {

Review Comment:
   nit: can we merge all semantic test programs into one class?



##########
docs/content.zh/docs/dev/table/tableApi.md:
##########
@@ -1152,6 +1152,47 @@ val result = orders
 {{< /tab >}}
 {{< /tabs >}}
 
+#### Lateral Snapshot Join
+
+{{< label "Batch" >}} {{< label "Streaming" >}}
+
+Enriches an append-only table with the current state of an updating table by 
calling the built-in `SNAPSHOT` [process table function]({{< ref 
"docs/sql/functions/built-in-functions" >}}) as the build (right) side of a 
`LATERAL` join. Each probe-side row is joined with the build-side state that is 
current when the row is processed. Both inner and left outer joins are 
supported, and the predicate must contain at least one equality condition.
+
+See [LATERAL SNAPSHOT join]({{< ref "docs/sql/reference/queries/joins" 
>}}#lateral-snapshot-join) for the full list of `SNAPSHOT` arguments and the 
join semantics.
+
+{{< tabs "snapshotjoin" >}}
+{{< tab "Java" >}}
+```java
+Table orders = tableEnv.from("Orders");
+Table currencyRates = tableEnv.from("CurrencyRates");
+
+// inner join: enrich every order with the current conversion rate
+Table result = orders
+    .joinLateral(
+        call(
+            "SNAPSHOT",

Review Comment:
   I'm fine with this syntax for now. We are planning to introduce builder 
patterns for PTFs going forward.



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