Timm0 commented on code in PR #30053:
URL: https://github.com/apache/flink/pull/30053#discussion_r4680416989
##########
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:
Done, I added a more explanatory section to the JavaDoc/DocString and
reduced the example code comment.
##########
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:
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]