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


##########
flink-python/pyflink/table/table.py:
##########
@@ -1300,6 +1301,174 @@ def from_changelog(self, *arguments: Expression) -> 
'Table':
         """
         return 
Table(self._j_table.fromChangelog(to_expression_jarray(arguments)), self._t_env)
 
+    def partition_by(self, *fields: Expression) -> 'PartitionedTable':
+        """
+        Partitions the table by a set of partition keys.
+
+        Currently, partitioned table objects are intended for table arguments 
of process table
+        functions (PTFs) that take table arguments with set semantics.
+
+        Example:
+        ::
+
+            >>> from pyflink.table.expressions import col
+            >>> table.partition_by(col("key")).to_changelog().execute()
+
+        :param fields: Expressions that define the partition keys.
+        :return: A :class:`~pyflink.table.PartitionedTable`.
+        """
+        return 
PartitionedTable(self._j_table.partitionBy(to_expression_jarray(fields)),
+                                self._t_env)
+
+
+@PublicEvolving()
+class PartitionedTable(object):
+    """
+    A table that has been partitioned by a set of partition keys.
+
+    Currently, partitioned table objects are intended for table arguments of 
process table
+    functions (PTFs) that take table arguments with set semantics.
+    """
+
+    def __init__(self, j_partitioned_table, t_env):
+        self._j_partitioned_table = j_partitioned_table
+        self._t_env = t_env
+
+    def order_by(self, *fields: Expression) -> 'PartitionedTable':
+        """
+        Sorts the partitioned table within each partition.
+
+        This is useful for process table functions that need ordered input 
within each set
+        scoped by the key.
+
+        Note: The first sorting column must be a time attribute column in 
ascending order for
+        which a watermark has been declared. The watermarked column must be 
forwarded without any
+        modification to ensure event-time sorting. Secondary ordering columns 
can differ and are
+        used for deterministic ordering within the same timestamp.
+
+        Example:
+        ::
+
+            >>> from pyflink.table.expressions import col
+            >>> # Primary ordering by watermarked timestamp, secondary by score
+            >>> result = table.partition_by(col("key")).order_by(
+            ...     col("ts").asc, col("score").desc
+            ... ).from_changelog()
+
+        :param fields: Expressions for ordering (e.g., ``col("ts").asc, 
col("score").desc``).
+        :return: A partitioned and ordered 
:class:`~pyflink.table.PartitionedTable`.
+        """
+        return 
PartitionedTable(self._j_partitioned_table.orderBy(to_expression_jarray(fields)),
+                                self._t_env)
+
+    def to_changelog(self, *arguments: Expression) -> 'Table':
+        """
+        Converts this partitioned dynamic table into an append-only table with 
an explicit
+        operation code column using the built-in ``TO_CHANGELOG`` process 
table function with set
+        semantics.
+
+        Each input row - regardless of its original change operation - is 
emitted as an
+        INSERT-only row with a string ``op`` column indicating the original 
operation (INSERT,
+        UPDATE_AFTER, DELETE, etc.). With set semantics, rows for the same 
partition key are
+        co-located in the same parallel operator instance.
+
+        For row semantics (each row processed independently), use
+        :func:`~pyflink.table.Table.to_changelog` on the unpartitioned table.
+
+        Example:
+        ::
+
+            >>> from pyflink.table.expressions import col, descriptor, lit, 
map_
+            >>> # Default: adds 'op' column and supports all changelog modes
+            >>> result = table.partition_by(col("id")).to_changelog()
+            >>> # Custom op column name and mapping
+            >>> result = table.partition_by(col("id")).to_changelog(
+            ...     descriptor("op_code").as_argument("op"),
+            ...     map_("INSERT", "I", "UPDATE_AFTER", 
"U").as_argument("op_mapping")
+            ... )
+            >>> # Deletion flag pattern: comma-separated keys map multiple 
change operations to
+            >>> # the same code
+            >>> result = table.partition_by(col("id")).to_changelog(
+            ...     descriptor("deleted").as_argument("op"),
+            ...     map_("INSERT, UPDATE_AFTER", "false",
+            ...          "DELETE", "true").as_argument("op_mapping")
+            ... )
+            >>> # Opt out of full-delete semantics. When `True` (default), 
DELETE rows carry the
+            >>> # full pre-image. When `False`, only the identifying key 
columns are preserved and
+            >>> # the rest are nulled. See in documentation for more details.

Review Comment:
   nit: "See in documentation for more details" reads a bit odd. It comes from 
the Java docs, so we could fix both.



##########
flink-python/pyflink/table/tests/test_partitioned_table.py:
##########
@@ -0,0 +1,70 @@
+################################################################################
+#  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.
+################################################################################
+
+from pyflink.table.expressions import col
+from pyflink.testing.test_case_utils import PyFlinkStreamTableTestCase
+
+
+class PartitionedTableTests(PyFlinkStreamTableTestCase):
+    """
+    Tests built-in process table function calls through the Python Table API.
+    """
+
+    def test_partition_by_to_changelog(self):

Review Comment:
   Could we add one or two sanity tests that pass named arguments? Right now 
none of the tests do.



##########
flink-python/pyflink/table/table.py:
##########
@@ -1300,6 +1301,174 @@ def from_changelog(self, *arguments: Expression) -> 
'Table':
         """
         return 
Table(self._j_table.fromChangelog(to_expression_jarray(arguments)), self._t_env)

Review Comment:
   nit: the Java docs for `toChangelog`/`fromChangelog` point to `partitionBy`. 
Could we do the same here?



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