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


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/stream/sql/ToAppendTest.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.stream.sql;
+
+import org.apache.flink.table.api.ExplainDetail;
+import org.apache.flink.table.api.TableConfig;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.ResolvedSchema;
+import org.apache.flink.table.planner.utils.TableTestBase;
+import org.apache.flink.table.planner.utils.TableTestUtil;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.apache.flink.core.testutils.FlinkAssertions.anyCauseMatches;
+import static 
org.apache.flink.table.types.logical.utils.LogicalTypeChecks.isProctimeAttribute;
+import static 
org.apache.flink.table.types.logical.utils.LogicalTypeChecks.isRowtimeAttribute;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Plan tests for the TO_APPEND built-in process table function. */
+class ToAppendTest extends TableTestBase {
+
+    private static final List<ExplainDetail> CHANGELOG_MODE = 
List.of(ExplainDetail.CHANGELOG_MODE);
+
+    private TableTestUtil util;
+
+    @BeforeEach
+    void setup() {
+        util = streamTestUtil(TableConfig.getDefault());
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE upsert_source ("
+                                + "  id INT PRIMARY KEY NOT ENFORCED,"
+                                + "  name STRING"
+                                + ") WITH ("
+                                + "  'connector' = 'values',"
+                                + "  'changelog-mode' = 'I,UA,D',"
+                                + "  'source.produces-delete-by-key' = 'true'"
+                                + ")");
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE upsert_source_with_full_deletes ("
+                                + "  id INT PRIMARY KEY NOT ENFORCED,"
+                                + "  name STRING"
+                                + ") WITH ("
+                                + "  'connector' = 'values',"
+                                + "  'changelog-mode' = 'I,UA,D'"
+                                + ")");
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE retract_source ("
+                                + "  id INT PRIMARY KEY NOT ENFORCED,"
+                                + "  name STRING"
+                                + ") WITH ("
+                                + "  'connector' = 'values',"
+                                + "  'changelog-mode' = 'I,UB,UA,D'"
+                                + ")");
+    }
+
+    @Test
+    void testUpsertSourceWithPartialDeletes() {
+        util.verifyRelPlan("SELECT * FROM TO_APPEND(input => TABLE 
upsert_source)", CHANGELOG_MODE);
+    }
+
+    @Test
+    void testUpsertSourceWithFullDeletes() {
+        util.verifyRelPlan(
+                "SELECT * FROM TO_APPEND(input => TABLE 
upsert_source_with_full_deletes)",
+                CHANGELOG_MODE);
+    }
+
+    @Test
+    void testRetractSource() {
+        util.verifyRelPlan(
+                "SELECT * FROM TO_APPEND(input => TABLE retract_source)", 
CHANGELOG_MODE);
+    }
+
+    @Test
+    void testInputWithoutUpsertKey() {
+        util.tableEnv().executeSql("CREATE VIEW names AS SELECT name FROM 
upsert_source");
+        util.verifyRelPlan("SELECT * FROM TO_APPEND(input => TABLE names)", 
CHANGELOG_MODE);
+    }
+
+    @Test
+    void testOutputTypesMatchInput() {
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE typed_source ("
+                                + "  id INT NOT NULL PRIMARY KEY NOT ENFORCED,"
+                                + "  name STRING,"
+                                + "  amount DECIMAL(10, 2) NOT NULL,"
+                                + "  tags ARRAY<STRING>,"
+                                + "  address ROW<city STRING, zip INT NOT 
NULL>,"
+                                + "  ts TIMESTAMP(3),"
+                                + "  pt AS PROCTIME(),"
+                                + "  WATERMARK FOR ts AS ts"
+                                + ") WITH ("
+                                + "  'connector' = 'values',"
+                                + "  'changelog-mode' = 'I,UA,D'"
+                                + ")");
+        final ResolvedSchema input = 
util.tableEnv().from("typed_source").getResolvedSchema();
+        final ResolvedSchema output =
+                util.tableEnv()
+                        .sqlQuery("SELECT * FROM TO_APPEND(input => TABLE 
typed_source)")
+                        .getResolvedSchema();
+        assertThat(output.getColumnNames()).isEqualTo(input.getColumnNames());
+        
assertThat(output.getColumnDataTypes()).isEqualTo(input.getColumnDataTypes());
+        assertThat(isRowtimeAttribute(logicalType(output, "ts"))).isTrue();
+        assertThat(isProctimeAttribute(logicalType(output, "pt"))).isTrue();
+    }
+
+    @Test
+    void testOnTimeNotSupported() {

Review Comment:
   can we remove this check? we support on_time, we should avoid error where we 
can. if somebody likes a unified on_time call for all PTFs, we should not 
disallow it.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/SystemTypeInference.java:
##########
@@ -405,6 +406,9 @@ public Optional<DataType> inferType(CallContext 
callContext) {
                                 // this whole topic is kind of vendor specific 
already
                                 
fields.addAll(derivePassThroughFields(callContext, resolvedArgs));
                                 
fields.addAll(deriveFunctionOutputFields(functionDataType));
+                                if (!preservesTimeAttributes(resolvedArgs)) {

Review Comment:
   let's move this to `deriveFunctionOutputFields`, pass through columns should 
be fine as they are highly specialized



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/StaticArgumentTrait.java:
##########
@@ -47,6 +47,13 @@ public enum StaticArgumentTrait {
     SUPPORT_UPDATES(TABLE),
     REQUIRE_ON_TIME(TABLE),
 
+    /**
+     * Defines that the function emits the rows of this table argument 
unchanged and without
+     * reordering them across watermarks, so the time attributes of the copied 
columns stay valid.

Review Comment:
   ```suggestion
        * Specifies that the function emits rows of this table argument in 
accordance with the watermark strategy, so that the time attributes in the 
output columns remain valid.
   ```



##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/calcite/RelTimeIndicatorConverter.java:
##########
@@ -441,6 +445,23 @@ private RelNode visitCorrelate(FlinkLogicalCorrelate 
correlate) {
                 correlate.getJoinType());
     }
 
+    private RelNode visitTableFunctionScan(FlinkLogicalTableFunctionScan scan) 
{
+        final RelNode newScan = visitSimpleRel(scan);
+        final FunctionDefinition definition =
+                ShortcutUtils.unwrapFunctionDefinition(scan.getCall());
+        if (definition == null
+                || definition.getKind() != FunctionKind.PROCESS_TABLE
+                || !gatherProcTimeIndices(scan).isEmpty()) {
+            return newScan;
+        }
+        // Proctime is only computed on access, so a PTF that does not forward 
it needs its value
+        final List<RelNode> newInputs =
+                newScan.getInputs().stream()
+                        .map(this::materializeProcTime)
+                        .collect(Collectors.toList());
+        return newScan.copy(newScan.getTraitSet(), newInputs);

Review Comment:
   I'm not sure if this fully fixes the problem. Usually you also have to 
update the consumer and all its references. In this case the RexTableArg 
(`TABLE()`) needs an update, otherwise there is a type mismatch.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/SystemTypeInference.java:
##########
@@ -479,6 +483,18 @@ private List<Field> deriveFunctionOutputFields(DataType 
functionDataType) {
                     .collect(Collectors.toList());
         }
 
+        /** A PTF might delay or reorder rows, which would invalidate 
forwarded time attributes. */
+        private boolean preservesTimeAttributes(List<StaticArgument> 
staticArgs) {
+            return functionKind != FunctionKind.PROCESS_TABLE
+                    || staticArgs.stream()
+                            .anyMatch(arg -> 
arg.is(StaticArgumentTrait.PRESERVE_TIME_ATTRIBUTES));

Review Comment:
   allMatch on table attributes



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