PyRSA commented on code in PR #11031:
URL: https://github.com/apache/paimon/pull/11031#discussion_r4571746701


##########
paimon-python/pypaimon/execution/native/read/table_read.py:
##########
@@ -40,6 +40,91 @@
 _NATIVE_READ_FILE_SUFFIXES = tuple('.%s' % file_format for file_format in 
_NATIVE_READ_FILE_FORMATS)
 _NATIVE_BLOB_FILE_SUFFIXES = ('.blob', '.video')
 
+# Must stay aligned with Rust StringArray downcast and Python
+# ``_KEYTIME_STRING_TYPES``. Kept local so Native does not import the
+# Python execution backend.
+_NATIVE_KEYTIME_STRING_TYPES = frozenset({"CHAR", "VARCHAR", "STRING"})
+_MERGE_MAP_WITH_KEYTIME = "merge_map_with_keytime"
+
+
+def _atomic_type_base_name(field_type):
+    from pypaimon.schema.data_types import AtomicType
+    if not isinstance(field_type, AtomicType):
+        return None
+    raw = field_type.type
+    return raw.split('(', 1)[0].split(' ', 1)[0].upper()
+
+
+def _field_agg_func_name(field_name, primary_keys, options_map, 
sequence_fields):
+    # Same precedence as Python ``resolve_agg_func_name`` / Java
+    # ``AggregateMergeFunction.getAggFuncName``, inlined to avoid a
+    # Native → Python execution import.
+    if field_name in sequence_fields:
+        return "last_value"
+    if field_name in primary_keys:
+        return "primary_key"
+    return (
+        options_map.get("fields.{}.aggregate-function".format(field_name))
+        or options_map.get("fields.default-aggregate-function")
+        or "last_non_null_value"
+    )
+
+
+def _native_merge_map_keytime_timestamp_supported(table) -> bool:
+    """Whether Native can run ``merge_map_with_keytime`` for this table.
+
+    Rust resolves the timestamp column at merge time by downcasting to
+    ``StringArray`` and raises ``DataInvalid`` (``requires STRING``,
+    ``source: None``) for any other Arrow type. Only CHAR / VARCHAR /
+    STRING clear that check.
+
+    The failure happens while iterating batches after setup, which
+    bypasses the usual setup-time fallback. Typed timestamps that
+    Python already accepts (TIMESTAMP, DOUBLE, ...) must take the
+    Python aggregator path before Native is opened.
+
+    A non-atomic / unresolvable timestamp type returns ``False`` so
+    Python can raise the precise schema validation error; that is not
+    treated as "unsupported but known".
+    """
+    if not getattr(table, 'is_primary_key_table', False):
+        return True
+    options = table.options
+    if options.merge_engine() != MergeEngine.AGGREGATE:
+        return True
+
+    from pypaimon.schema.data_types import MapType, RowType
+
+    options_map = options.options.to_map()
+    pk_set = set(table.primary_keys)
+    sequence_fields = set(options.sequence_field())
+    for field in table.fields:
+        if _field_agg_func_name(
+                field.name, pk_set, options_map, sequence_fields
+        ) != _MERGE_MAP_WITH_KEYTIME:
+            continue
+        field_type = field.type
+        if (not isinstance(field_type, MapType)
+                or not isinstance(field_type.value, RowType)
+                or not field_type.value.fields):
+            return False
+        row_type = field_type.value
+        ts_field_name = options.field_merge_map_ts_field(field.name)
+        if ts_field_name is None:
+            ts_type = row_type.fields[-1].type
+        else:
+            try:
+                ts_index = row_type.get_field_index(ts_field_name)
+            except ValueError:
+                return False
+            if ts_index < 0:
+                return False
+            ts_type = row_type.fields[ts_index].type
+        # None => type parse / non-atomic failure; not a supported native type.
+        if _atomic_type_base_name(ts_type) not in _NATIVE_KEYTIME_STRING_TYPES:

Review Comment:
   Addressed.
   
   Disabled the Native reader path for `merge_map_with_keytime` and routed 
reads through the Python implementation to avoid backend-dependent comparison 
results.
   
   The Python implementation preserves Java-compatible UTF-16 string ordering. 
Added a regression test using U+E000 and U+10000 to verify that the Python 
reader selects the expected value for persisted versions.
   
   Native support can be reconsidered once its string comparison semantics are 
aligned with Java/Python.



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