Skip to content

[core] Support more key-time types in native merge_map_with_keytime - #1378

Open
PyRSA wants to merge 6 commits into
apache:mainfrom
PyRSA:feat/merge-map-keytime-types
Open

PyRSA wants to merge 6 commits into
apache:mainfrom
PyRSA:feat/merge-map-keytime-types

Conversation

@PyRSA

@PyRSA PyRSA commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Purpose

Extend Rust Native merge_map_with_keytime to support additional key-time types and align its comparison and merge semantics with existing implementations.

Brief change log

  • Support CHAR/VARCHAR, integer, FLOAT/DOUBLE, DATE, TIME, DECIMAL, TIMESTAMP, and TIMESTAMP_LTZ key-time types.
  • Use UTF-16 code unit ordering for string comparisons to match Java String.compareTo.
  • Align floating-point comparisons with Python semantics, including NaN and signed zero handling.
  • Compare timestamp values using their underlying ticks.
  • Support overwrite-on-equal-ts to control whether equal key-times replace existing values.
  • Share key-time configuration resolution and type validation between table validation and aggregator construction.
  • Reject unsupported key-time types during table creation.
  • Preserve existing NULL key-time, NULL ROW tombstone, and regular merge_map behavior.

Tests

  • spec::aggregation::tests: 23 passed.
  • merge_map::tests: 16 passed.
  • cargo fmt --check passed.
  • git diff --check passed.

API and Format

No public API or file format changes.

Documentation

Update SQL documentation with supported key-time types, comparison semantics, and overwrite-on-equal-ts behavior.

AuroraVoyage and others added 2 commits November 12, 2026 14:17
…semantics

Clarify that aggReversed(acc, input) equals agg(input, acc), and extend
CHAR/DECIMAL/integer keytime coverage so Native docs match the allow-list.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional key-time types have end-to-end value, but this head is not ready for production. All 272 existing relevant tests pass (121 aggregator, 23 configuration, 107 PK compaction and 21 row-kind). Independent public write/commit/read/compact probes pass for DATE, DECIMAL, timestamp precisions, TIMESTAMP_LTZ and basic FLOAT/DOUBLE behavior, but expose the flush-dependent NaN result and existing string-order regression below. Public schema evolution probes also reproduce the new equal-timestamp option being lost on rename and retained after drop.

} else if new_v < old_v {
KeytimeCmp::Less
} else {
KeytimeCmp::Unordered

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Make floating key-time reduction independent of flush grouping

Treating NaN as unordered against every value makes this reducer non-associative. I reproduced it with a real aggregation table, one map key, and arrival rows (payload=11,ts=0), (22,NaN), (33,1). Three separate writer flush/commits read back 33 and retain 33 after compaction; committing the first row separately but buffering the last two together reads back 11. The latter buffer first reduces NaN and 1 to NaN, losing the update that would have won against 0. This silently changes persisted results based only on batching for newly accepted DOUBLE/FLOAT key-times. Define a merge-safe NaN policy or reject NaNs before accepting this type; matching Python's two-value comparison is insufficient for LSM partial aggregation.

KeytimeKind::Utf8 => {
let new_v = downcast::<StringArray>(new_ts, field_name)?.value(0);
let old_v = downcast::<StringArray>(old_ts, field_name)?.value(0);
Ok(ordering_to_cmp(utf16_code_unit_cmp(new_v, old_v)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve BinaryString ordering for existing character key-times

Java's key-time aggregator calls InternalRowUtils.compare, whose CHAR/VARCHAR branch compares BinaryString values, not java.lang.String values. BinaryString.compareTo orders unsigned UTF-8 bytes. Changing the previous Rust byte order to UTF-16 reverses supplementary-character comparisons and chooses a different row on existing tables. Public write/read probe: first (payload=11, ts=U+E000), then (22,U+10000); this head returns 11, while the exact base returns 22 and Java's BinaryString ordering also selects 22. Keep the existing UTF-8 comparison and correct the new test/docs oracle. Reference: https://github.com/apache/paimon/blob/f289b0a2829f8b0c28ae92743f11893893ce1116/paimon-common/src/main/java/org/apache/paimon/utils/InternalRowUtils.java#L487-L489 and https://github.com/apache/paimon/blob/f289b0a2829f8b0c28ae92743f11893893ce1116/paimon-common/src/main/java/org/apache/paimon/data/BinaryString.java#L103-L126 .

let (timestamp_index, keytime_kind) =
resolve_keytime_config(field_name, data_type, options)?;
let overwrite_on_equal_ts = options
.get(&format!("fields.{field_name}.overwrite-on-equal-ts"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Register the new field option with schema evolution

The new suffix is absent from parse_field_scoped_option_key and FIELD_SCOPED_RENAMEABLE_SUFFIXES. A valid table with fields.m.aggregate-function=merge_map_with_keytime and fields.m.overwrite-on-equal-ts=true (using the default last ts field) can successfully rename m to renamed. apply_changes moves the aggregate-function but leaves this option under fields.m, so the renamed aggregator silently reverts to false and selects a different winner for equal timestamps. Dropping m also leaves the option behind, allowing it to reactivate if the name is reused. Both public TableSchema::apply_changes probes fail on this head. Include the option in recognition/rename/drop handling and cover its policy through schema evolution.

…equal-ts evolution

Reject FLOAT/DOUBLE NaN before key-time aggregation so results stay associative across flush groups; restore BinaryString UTF-8 ordering for CHAR/VARCHAR; migrate/clear overwrite-on-equal-ts on rename/drop/recreate.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional key-time types have end-to-end value. On e59048c6, 295 focused unit tests and 136 existing integration tests passed. Additional public write/read/compact checks passed for DATE, DECIMAL, TIME, nanosecond TIMESTAMP, and TIMESTAMP_LTZ. Five local regression probes reproduced the data and schema behaviors in the inline comments. The Java type restrictions were reproduced against the production 2.2 snapshot factory.

// entry order and any duplicate keys. Still reject NaN key-times
// so they never become accumulator state.
if self.timestamp_index.is_some() {
self.reject_nan_in_entries(&incoming)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Reject NaN before the singleton write can publish it

This check runs only when the field aggregator runs. The singleton PK fast paths in kv_file_writer.rs can bypass it. I wrote one unique primary key with a NaN time to separate FLOAT and DOUBLE tables. Both committed snapshot 1 and could read back the row. A finite update for the same key then committed snapshot 2, after which the scan failed with rejects NaN. The current write_or_merge_err helper also accepts a scan failure after a successful commit. Validate configured key-time cells on the normal ingestion path before the singleton shortcut, and assert that rejection leaves the published snapshot unchanged.

let cmp = compare_keytime(kind, new_ts, old_ts, &self.field_name)?;
Ok(match cmp {
KeytimeCmp::Greater => true,
KeytimeCmp::Equal => self.overwrite_on_equal_ts,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve the winning sequence for equal key-times across flushes

With INT key-time, sequence.field=seq, and overwrite-on-equal-ts=true, I wrote the same three rows for one PK and map key in both cases. The (seq, payload, key-time) values were (1,11,5), (3,33,0), and (2,22,5). A single flush returns payload 22. Flushing the first two together and the last separately returns 11. The first file retains payload 11 with the group's sequence 3, so it wins an equal-time comparison against sequence 2. Preserve the winning entry's sequence across partial reductions or reject this option combination until it can be represented.

Comment thread docs/src/sql.md Outdated
rejected and never enter aggregation. `TIMESTAMP` / `TIMESTAMP_LTZ` compare
by ticks under a unified time semantics (timezone metadata does not affect
ordering). A NULL key-time ignores the incoming update and leaves any
existing key unchanged. A NULL ROW value removes that key. Rust Native

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve a key tombstone across partial aggregation

I used a newly supported INT key-time and wrote three versions of the same key: payload 11 at time 9, payload 22 at time 1, then a NULL ROW for that map key. Flushing all three together leaves the map empty. Flushing the old version separately and grouping the lower-time update with the tombstone leaves payload 11 readable. The second partial aggregate persists an empty map and loses the deletion marker, so it cannot remove the older key. Preserve that deletion information across materialization or restrict this newly supported path to a safe merge.

IGNORE_RETRACT_SUFFIX,
DISTINCT_SUFFIX,
LIST_AGG_DELIMITER_SUFFIX,
OVERWRITE_ON_EQUAL_TS_SUFFIX,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Migrate the explicit time-field selector with the map column

The new equal-time option follows the map through rename, but fields.<column>.ts-field still stays under the old name. I created a table with a non-last INT field payload as the explicit key-time, wrote (payload=10,ts=1), renamed the map through the filesystem catalog, then appended (payload=1,ts=2). ALTER and both writes succeed, and the scan changes the winner to 1 because the renamed field falls back to the trailing ts. Rename and clean up .ts-field alongside .overwrite-on-equal-ts, and cover a non-default selector in the catalog round trip.

resolve_keytime_config(field_name, data_type, options)?;
let overwrite_on_equal_ts = options
.get(&format!("fields.{field_name}.overwrite-on-equal-ts"))
.is_some_and(|value| value.eq_ignore_ascii_case("true"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reject invalid values for the new boolean option

Rust CREATE accepts fields.updates.overwrite-on-equal-ts=treu and silently interprets it as false. Java Paimon 2.2 snapshot 20261118.220748-71 rejects this same option when constructing the aggregator, while true and false both work. An accepted schema with a typo can therefore select the wrong tie winner in Rust and fail to initialize in Java. Parse and validate the boolean in the shared schema/runtime configuration code.

Comment thread docs/src/sql.md Outdated
supports the key-time types listed above, including `FLOAT`/`DOUBLE`. The
Python Native eligibility gate allows the same Rust-supported set (and
falls back only for types Rust cannot compare). Java still rejects
`FLOAT`/`DOUBLE` key-times.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Document the actual Java key-time type gate

The Java caveat needs to include DECIMAL. I ran the production Java factory in Paimon 2.2 snapshot 20261118.220748-71. It accepts INT, TIME(3), and TIMESTAMP(9), and rejects DECIMAL(10,2), FLOAT, and DOUBLE. I also wrote, scanned, and compacted a DECIMAL table using this Rust head successfully. Readers of mixed-engine tables need the version-specific Java type restriction before enabling the new types.

Reject unsafe key-time inputs and configurations, and preserve
field-scoped key-time options across schema evolution.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native support for additional key-time types has end-to-end value. The aggregator and configuration suites and all eight checked-in table tests passed on 61741baf. Three additional table tests reproduced failures around default tie handling, partial-update validation, and existing VARCHAR tables.

let ts_field = &value_type.fields()[timestamp_index];
let keytime_kind = keytime_kind_for_type(field_name, ts_field.name(), ts_field.data_type())?;
let overwrite_on_equal_ts = parse_overwrite_on_equal_ts(field_name, options)?;
if overwrite_on_equal_ts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve default equal-keytime winners across flush groups

The new guard covers overwrite-on-equal-ts=true. The default false policy also loses per-entry sequence provenance. With INT key-time and sequence.field=seq, I committed (seq, payload, keytime) values (1,11,5), (3,33,0), and (2,22,5) for the same primary key and map key. One flush returned 11. Flushing the first two together and then the last returned 22. The first aggregate stores payload 11 with group sequence 3, so sequence 2 appears earlier on read and wins the false tie.

Please preserve the sequence of the winning map entry or reject the newly supported configuration for both tie policies until it is deterministic.

) -> Result<(RecordBatch, Arc<dyn Array>)> {
// Shared write-entry checks for merge_map_with_keytime: must run before
// the singleton shortcut so NaN / NULL ROW cannot bypass aggregation.
self.reject_merge_map_with_keytime_write_inputs(batch)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Validate key-time values before the partial-update singleton path

The check runs only in merge_aggregation_rows. A table with merge-engine=partial-update, fields.seq.sequence-group=updates, and fields.updates.aggregate-function=merge_map_with_keytime accepts FLOAT key-time. I wrote a singleton map row with a NaN key-time under this configuration, and commit returned success. The partial-update singleton path bypasses the aggregator, so the invalid value can be materialized and later rejected by a merged read.

Apply the input validation before both flush paths take their singleton shortcuts, or reject the new type on partial-update until that path can enforce the contract.

});
}
for entry_idx in start..end {
if values.is_null(entry_idx) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve key deletion for existing VARCHAR key-time tables

On ea90bef2, I wrote a VARCHAR key-time map entry, then committed a NULL ROW for that map key in a separate write. The resulting map was empty. The same two commits on 61741baf reject the second write during prepare_commit. This changes an existing Rust write contract, including callers that submit one update per key per commit.

Please preserve delete markers through partial aggregation, or scope the new rejection to newly enabled configurations and provide a compatibility path for existing VARCHAR tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants