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


##########
docs/content.zh/docs/sql/built-in-functions/process-table-functions.md:
##########
@@ -29,6 +29,7 @@ The built-in process table functions (PTFs) take a table as 
input and return a t
 
 | Function                                                | Description        
                                                                                
                                                                                
                                                                                
                                            |
 
|---------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| `TO_APPEND(input => TABLE t)`                           | Converts an 
updating table into an append-only table: new rows and updates are appended, 
deletes are dropped. See Changelog Conversion for the full list of arguments, 
semantics, and usage.                                                           
                                                        |

Review Comment:
   Fixed. The zh docs were missing the Changelog Conversion page, so I copied 
the English one and linked it like in the English table.



##########
docs/content/docs/sql/reference/queries/changelog.md:
##########
@@ -30,9 +30,101 @@ Flink SQL provides built-in process table functions (PTFs) 
for working with chan
 
 | Function | Description |
 |:---------|:------------|
+| [TO_APPEND](#to_append) | Converts an updating table into an append-only 
table: new rows and updates are appended, deletes are dropped |
 | [FROM_CHANGELOG](#from_changelog) | Converts an append-only table with 
operation codes into a (potentially updating) dynamic table |
 | [TO_CHANGELOG](#to_changelog) | Converts a dynamic table into an append-only 
table with explicit operation codes |
 
+## TO_APPEND
+
+The `TO_APPEND` PTF converts an updating table into an append-only table. 
Every new row and every update is appended as a new row, and deletes are 
dropped. The output has the same columns as the input.
+
+### Syntax
+
+```sql
+SELECT * FROM TO_APPEND(input => TABLE source_table)
+```
+
+### Examples
+
+#### A table that changes
+
+`customers` is a table with primary key `id`. A row is inserted, updated and 
finally deleted:
+
+| Change   | `customers`         |
+|:---------|:--------------------|
+| Inserted | `(1, Berlin)`       |
+| Updated  | `(1, Munich)`       |
+| Deleted  | *(empty)*           |
+
+`TO_APPEND` keeps every inserted and updated version as a new row and ignores 
deletes:
+
+```sql
+SELECT * FROM TO_APPEND(input => TABLE customers)
+```
+
+| Result        |
+|:--------------|
+| `(1, Berlin)` |
+| `(1, Munich)` |
+
+{{< hint info >}}
+Flink represents these changes as a changelog: an insert (`+I`), an update 
(`+U`, optionally preceded by `-U` with the previous version) and a delete 
(`-D`). `TO_APPEND` emits inserts and updates as inserts and drops `-U` and 
`-D`. To keep the change operation, use [TO_CHANGELOG](#to_changelog).
+{{< /hint >}}
+
+#### From a changing table to stream processing
+
+`TO_APPEND` turns a table that changes into a stream of its changes. On that 
stream, you can use stream processing operations that only accept append-only 
tables, such as windows, `OVER` aggregations, and interval joins. For example, 
count the order changes per hour:

Review Comment:
   Done.



##########
docs/content/docs/sql/reference/queries/changelog.md:
##########
@@ -30,9 +30,101 @@ Flink SQL provides built-in process table functions (PTFs) 
for working with chan
 
 | Function | Description |
 |:---------|:------------|
+| [TO_APPEND](#to_append) | Converts an updating table into an append-only 
table: new rows and updates are appended, deletes are dropped |
 | [FROM_CHANGELOG](#from_changelog) | Converts an append-only table with 
operation codes into a (potentially updating) dynamic table |
 | [TO_CHANGELOG](#to_changelog) | Converts a dynamic table into an append-only 
table with explicit operation codes |
 
+## TO_APPEND
+
+The `TO_APPEND` PTF converts an updating table into an append-only table. 
Every new row and every update is appended as a new row, and deletes are 
dropped. The output has the same columns as the input.
+
+### Syntax
+
+```sql
+SELECT * FROM TO_APPEND(input => TABLE source_table)
+```
+
+### Examples
+
+#### A table that changes
+
+`customers` is a table with primary key `id`. A row is inserted, updated and 
finally deleted:
+
+| Change   | `customers`         |
+|:---------|:--------------------|
+| Inserted | `(1, Berlin)`       |
+| Updated  | `(1, Munich)`       |
+| Deleted  | *(empty)*           |
+
+`TO_APPEND` keeps every inserted and updated version as a new row and ignores 
deletes:
+
+```sql
+SELECT * FROM TO_APPEND(input => TABLE customers)
+```
+
+| Result        |
+|:--------------|
+| `(1, Berlin)` |
+| `(1, Munich)` |
+
+{{< hint info >}}
+Flink represents these changes as a changelog: an insert (`+I`), an update 
(`+U`, optionally preceded by `-U` with the previous version) and a delete 
(`-D`). `TO_APPEND` emits inserts and updates as inserts and drops `-U` and 
`-D`. To keep the change operation, use [TO_CHANGELOG](#to_changelog).
+{{< /hint >}}
+
+#### From a changing table to stream processing
+
+`TO_APPEND` turns a table that changes into a stream of its changes. On that 
stream, you can use stream processing operations that only accept append-only 
tables, such as windows, `OVER` aggregations, and interval joins. For example, 
count the order changes per hour:
+
+| Change   | `orders` (with event-time attribute `ts`) |
+|:---------|:------------------------------------------|
+| Inserted | `(1, CREATED, 10:05)`                     |
+| Inserted | `(2, CREATED, 10:20)`                     |
+| Updated  | `(1, PAID, 10:40)`                        |
+| Updated  | `(1, SHIPPED, 11:10)`                     |
+
+```sql
+WITH order_changes AS (
+  SELECT * FROM TO_APPEND(input => TABLE orders)
+)
+SELECT window_start, window_end, COUNT(*) AS changes
+FROM TUMBLE(TABLE order_changes, DESCRIPTOR(ts), INTERVAL '1' HOUR)
+GROUP BY window_start, window_end
+```
+
+| Result                |
+|:----------------------|
+| `(10:00, 11:00, 3)`   |
+| `(11:00, 12:00, 1)`   |
+
+A window emits each result once, when the window closes, and then frees its 
state.
+
+#### Marking a query as append-only
+
+Some queries are declared updating although they never change a row they have 
emitted, for example a deduplication over all columns. Here `TO_APPEND` does 
not change the result: it only marks the query as append-only, so its result 
can be used where an append-only table is required:

Review Comment:
   Done.



##########
docs/content/docs/sql/reference/queries/changelog.md:
##########
@@ -30,9 +30,101 @@ Flink SQL provides built-in process table functions (PTFs) 
for working with chan
 
 | Function | Description |
 |:---------|:------------|
+| [TO_APPEND](#to_append) | Converts an updating table into an append-only 
table: new rows and updates are appended, deletes are dropped |
 | [FROM_CHANGELOG](#from_changelog) | Converts an append-only table with 
operation codes into a (potentially updating) dynamic table |
 | [TO_CHANGELOG](#to_changelog) | Converts a dynamic table into an append-only 
table with explicit operation codes |
 
+## TO_APPEND
+
+The `TO_APPEND` PTF converts an updating table into an append-only table. 
Every new row and every update is appended as a new row, and deletes are 
dropped. The output has the same columns as the input.
+
+### Syntax
+
+```sql
+SELECT * FROM TO_APPEND(input => TABLE source_table)
+```

Review Comment:
   Added a warning after the syntax: deletes are dropped, so a consumer cannot 
tell that a row was deleted. It points to an upsert sink or `TO_CHANGELOG` to 
keep deletes.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/ToAppendTestPrograms.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.connector.ChangelogMode;
+import org.apache.flink.table.test.program.SinkTestStep;
+import org.apache.flink.table.test.program.SourceTestStep;
+import org.apache.flink.table.test.program.TableTestProgram;
+import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
+
+import java.time.LocalDateTime;
+
+/** {@link TableTestProgram} definitions for testing the built-in TO_APPEND 
PTF. */
+public class ToAppendTestPrograms {
+
+    private static final SourceTestStep UPSERT_SOURCE =
+            SourceTestStep.newBuilder("source_table")
+                    .addSchema("id INT PRIMARY KEY NOT ENFORCED", "v STRING")
+                    .addMode(ChangelogMode.upsert(true))
+                    .producedValues(
+                            Row.ofKind(RowKind.INSERT, 1, "a"),
+                            Row.ofKind(RowKind.UPDATE_AFTER, 1, "b"),
+                            Row.ofKind(RowKind.DELETE, 1, null))
+                    .build();
+
+    public static final TableTestProgram UPSERT =
+            TableTestProgram.of(
+                            "to-append-upsert", "upserts become inserts and 
the delete is dropped")
+                    .setupTableSource(UPSERT_SOURCE)
+                    .setupTableSink(appendOnlySink("+I[1, a]", "+I[1, b]"))
+                    .runSql("INSERT INTO sink SELECT * FROM TO_APPEND(input => 
TABLE source_table)")
+                    .build();
+
+    public static final TableTestProgram RETRACT =
+            TableTestProgram.of(
+                            "to-append-retract",
+                            "inserts and update-afters become inserts, the 
rest is dropped")
+                    .setupTableSource(
+                            SourceTestStep.newBuilder("source_table")
+                                    .addSchema("id INT", "v STRING")
+                                    .addMode(ChangelogMode.all())
+                                    .producedValues(
+                                            Row.ofKind(RowKind.INSERT, 1, "a"),
+                                            Row.ofKind(RowKind.UPDATE_BEFORE, 
1, "a"),
+                                            Row.ofKind(RowKind.UPDATE_AFTER, 
1, "b"),
+                                            Row.ofKind(RowKind.DELETE, 1, "b"))
+                                    .build())
+                    .setupTableSink(appendOnlySink("+I[1, a]", "+I[1, b]"))
+                    .runSql("INSERT INTO sink SELECT * FROM TO_APPEND(input => 
TABLE source_table)")
+                    .build();
+
+    public static final TableTestProgram WINDOW =
+            TableTestProgram.of(
+                            "to-append-window",
+                            "Tumbling window over the changes of an upsert 
table")
+                    .setupTableSource(
+                            SourceTestStep.newBuilder("orders")
+                                    .addSchema(
+                                            "order_id INT",
+                                            "status STRING",
+                                            "ts TIMESTAMP(3)",
+                                            "PRIMARY KEY (order_id) NOT 
ENFORCED",
+                                            "WATERMARK FOR ts AS ts")
+                                    .addMode(ChangelogMode.upsert())
+                                    .producedValues(
+                                            Row.ofKind(
+                                                    RowKind.INSERT,
+                                                    1,
+                                                    "CREATED",
+                                                    
LocalDateTime.parse("2024-01-01T10:05")),
+                                            Row.ofKind(
+                                                    RowKind.INSERT,
+                                                    2,
+                                                    "CREATED",
+                                                    
LocalDateTime.parse("2024-01-01T10:20")),
+                                            Row.ofKind(
+                                                    RowKind.UPDATE_AFTER,
+                                                    1,
+                                                    "PAID",
+                                                    
LocalDateTime.parse("2024-01-01T10:40")),
+                                            Row.ofKind(
+                                                    RowKind.UPDATE_AFTER,
+                                                    1,
+                                                    "SHIPPED",
+                                                    
LocalDateTime.parse("2024-01-01T11:10")))
+                                    .build())
+                    .setupTableSink(
+                            SinkTestStep.newBuilder("sink")
+                                    .addSchema(
+                                            "window_start TIMESTAMP(3)",
+                                            "window_end TIMESTAMP(3)",
+                                            "changes BIGINT")
+                                    .addMode(ChangelogMode.insertOnly())
+                                    .consumedValues(
+                                            "+I[2024-01-01T10:00, 
2024-01-01T11:00, 3]",
+                                            "+I[2024-01-01T11:00, 
2024-01-01T12:00, 1]")
+                                    .build())
+                    .runSql(
+                            "INSERT INTO sink "
+                                    + "WITH order_changes AS ("
+                                    + "SELECT * FROM TO_APPEND(input => TABLE 
orders)) "
+                                    + "SELECT window_start, window_end, 
COUNT(*) AS changes "
+                                    + "FROM TUMBLE(TABLE order_changes, 
DESCRIPTOR(ts), INTERVAL '1' HOUR) "

Review Comment:
   As discussed, I added an internal `PRESERVE_TIME_ATTRIBUTES` trait. By 
default PTFs now drop all time attributes from their output (only the `on_time` 
rowtime stays) and materialize proctime before the PTF. `TO_APPEND` and 
`ML_PREDICT` declare the trait because they emit input rows unchanged. 
`on_time` together with the trait is rejected.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/ptf/ToAppendFunction.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.runtime.functions.ptf;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.SpecializedFunction.SpecializedContext;
+import org.apache.flink.types.RowKind;
+
+/**
+ * Runtime implementation of {@link BuiltInFunctionDefinitions#TO_APPEND}.
+ *
+ * <p>Emits INSERT and UPDATE_AFTER rows as INSERT rows and drops 
UPDATE_BEFORE and DELETE rows.
+ */
+@Internal
+public class ToAppendFunction extends BuiltInProcessTableFunction<RowData> {
+
+    private static final long serialVersionUID = 1L;
+
+    public ToAppendFunction(final SpecializedContext context) {
+        super(BuiltInFunctionDefinitions.TO_APPEND, context);
+    }
+
+    public void eval(final Context ctx, final RowData input) {

Review Comment:
   Done.



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