Repository navigation
[core] Do not count a retracted partial-update sum twice #1549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -504,6 +504,49 @@ impl PartialUpdateMergeFunction { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Seeds a merge that starts with a retraction under sequence groups: | ||
| /// required fields take the row's values, required aggregates start empty | ||
| /// when they can, and nullable fields stay NULL. | ||
| #[allow(clippy::too_many_arguments)] | ||
| fn init_from_retraction( | ||
| &self, | ||
| selected_by_col: &mut [Option<(usize, usize)>], | ||
| mut aggregators: Option<&mut [Option<Box<dyn FieldAggregator>>]>, | ||
| group_sequence_rows: &mut [Option<(usize, usize)>], | ||
| row: &MergeRow, | ||
| batch_buffer: &[BufferedBatch], | ||
| source_output_col_indices: &[usize], | ||
| output_schema: &SchemaRef, | ||
| ) -> crate::Result<()> { | ||
| let source = Some((row.batch_idx, row.row_idx)); | ||
| for (index, field) in output_schema.fields().iter().enumerate() { | ||
| let aggregator = aggregators | ||
| .as_mut() | ||
| .and_then(|aggregators| aggregators.get_mut(index)) | ||
| .and_then(Option::as_mut); | ||
| let nullable = field.is_nullable(); | ||
| selected_by_col[index] = if nullable { None } else { source }; | ||
| if let Some(aggregator) = aggregator { | ||
| aggregator.reset(); | ||
| if !nullable && !aggregator.start_empty() { | ||
| aggregator.agg( | ||
| batch_buffer[row.batch_idx] | ||
| .column_for_output(index, source_output_col_indices), | ||
| row.row_idx, | ||
| )?; | ||
| } | ||
| } | ||
| } | ||
| for (group, current) in self.sequence_groups.iter().zip(group_sequence_rows) { | ||
| let required = group | ||
| .sequence_indices | ||
| .iter() | ||
| .all(|&index| !output_schema.field(index).is_nullable()); | ||
| *current = if required { source } else { None }; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Java `updateNonNullFields`: ungrouped fields take the row's non-null | ||
| /// values, or feed their aggregator. | ||
| fn update_non_null_fields( | ||
|
|
@@ -706,6 +749,7 @@ impl MergeFunction for PartialUpdateMergeFunction { | |
| source_output_col_indices, | ||
| output_schema, | ||
| true, | ||
| false, | ||
| ) | ||
| .map(Some) | ||
| } | ||
|
|
@@ -725,12 +769,34 @@ impl PartialUpdateMergeFunction { | |
| source_output_col_indices, | ||
| output_schema, | ||
| false, | ||
| false, | ||
| )? { | ||
| PickedMerge::Result(result) => Ok(result), | ||
| PickedMerge::Picked(_) => unreachable!("picked rows are only returned when requested"), | ||
| } | ||
| } | ||
|
|
||
| /// Fails when the table does not support this retraction. Only the | ||
| /// support is checked, so the row starts from the retracted value and | ||
| /// its retraction cannot overflow a sum. | ||
| pub(super) fn validate_retract( | ||
| &self, | ||
| row: &MergeRow, | ||
| batch_buffer: &[BufferedBatch], | ||
| source_output_col_indices: &[usize], | ||
| output_schema: &SchemaRef, | ||
| ) -> crate::Result<()> { | ||
| self.merge_rows( | ||
| std::slice::from_ref(row), | ||
| batch_buffer, | ||
| source_output_col_indices, | ||
| output_schema, | ||
| false, | ||
| true, | ||
| ) | ||
| .map(|_| ()) | ||
| } | ||
|
|
||
| /// With `pick`, a live row is returned as the input rows its columns come from. | ||
| fn merge_rows( | ||
| &self, | ||
|
|
@@ -739,6 +805,7 @@ impl PartialUpdateMergeFunction { | |
| source_output_col_indices: &[usize], | ||
| output_schema: &SchemaRef, | ||
| pick: bool, | ||
| validate_only: bool, | ||
| ) -> crate::Result<PickedMerge> { | ||
| if rows.is_empty() { | ||
| return Err(Error::UnexpectedError { | ||
|
|
@@ -784,18 +851,34 @@ impl PartialUpdateMergeFunction { | |
| continue; | ||
| } | ||
| if !initialized { | ||
| // Java initializes the row from the first retract before | ||
| // checking ignore-delete or applying sequence-group | ||
| // retractions. Keep that payload for later sparse updates. | ||
| Self::replace_with_source( | ||
| &mut selected_by_col, | ||
| aggregators.as_mut().map(|guard| guard.as_mut_slice()), | ||
| row.batch_idx, | ||
| row.row_idx, | ||
| batch_buffer, | ||
| source_output_col_indices, | ||
| )?; | ||
| group_sequence_rows.fill(Some((row.batch_idx, row.row_idx))); | ||
| if self.sequence_group_enabled && !self.ignore_delete && !validate_only { | ||
| // The retraction is applied to the groups below, so | ||
| // seed only the required fields, with aggregates | ||
| // empty: seeding the retracted value would let the | ||
| // retraction cancel it and keep the old value. | ||
| self.init_from_retraction( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve raw retractions in the postpone writer. On a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @JingsongLi. The postpone writer only needs to know that the retraction is supported, so its check now starts the row from the retracted value as it did before this PR, and |
||
| &mut selected_by_col, | ||
| aggregators.as_mut().map(|guard| guard.as_mut_slice()), | ||
| &mut group_sequence_rows, | ||
| row, | ||
| batch_buffer, | ||
| source_output_col_indices, | ||
| output_schema, | ||
| )?; | ||
| } else { | ||
| // Java initializes the row from the first retract before | ||
| // checking ignore-delete. Keep that payload for later | ||
| // sparse updates. | ||
| Self::replace_with_source( | ||
| &mut selected_by_col, | ||
| aggregators.as_mut().map(|guard| guard.as_mut_slice()), | ||
| row.batch_idx, | ||
| row.row_idx, | ||
| batch_buffer, | ||
| source_output_col_indices, | ||
| )?; | ||
| group_sequence_rows.fill(Some((row.batch_idx, row.row_idx))); | ||
| } | ||
| initialized = true; | ||
| } | ||
| if self.ignore_delete { | ||
|
|
@@ -1032,6 +1115,21 @@ impl PartialUpdateMergeFunction { | |
| })); | ||
| } | ||
|
|
||
| let only_retractions = | ||
| self.sequence_group_enabled && !saw_add && !current_delete_row && !self.ignore_delete; | ||
| // A later merge retracts this result again, so aggregates are written | ||
| // as values to retract. When one has no such value, the accumulated | ||
| // retractions are written as an add instead. | ||
| let retraction_cells = match aggregators.as_ref() { | ||
| Some(aggregators) if only_retractions => retraction_cells(aggregators)?, | ||
| _ => None, | ||
| }; | ||
| let (result_kind, deleted) = | ||
| if only_retractions && aggregators.is_some() && retraction_cells.is_none() { | ||
| (RowKind::Insert, false) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Keep retraction-only keys absent when a checked sum reaches its minimum. I wrote
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @JingsongLi, both reproduce. houlinchn compared a few designs for this on apache/paimon#11274 and asked which one to take, and the raw-records option there is what you describe here. I'll port the one you pick so both sides keep the same rows.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for confirming the reproductions. I retested Please cover both cases in the port. Keep the original record order and row kinds wherever flush or compaction cannot encode the checked intermediate state in one record. The Java experiments also show where widening the accumulator still leaves an unrepresentable on-disk delta. Java comparison |
||
| } else { | ||
| (result_kind, deleted) | ||
| }; | ||
| let output_columns: Vec<ArrayRef> = output_schema | ||
| .fields() | ||
| .iter() | ||
|
|
@@ -1045,7 +1143,13 @@ impl PartialUpdateMergeFunction { | |
| .and_then(|aggregators| aggregators.get(output_col_idx)) | ||
| .and_then(Option::as_ref) | ||
| { | ||
| Some(aggregator) => aggregator.result()?, | ||
| Some(aggregator) => match retraction_cells | ||
| .as_ref() | ||
| .and_then(|cells| cells[output_col_idx].clone()) | ||
| { | ||
| Some(cell) => cell, | ||
| None => aggregator.result()?, | ||
| }, | ||
| None => match selected_by_col[output_col_idx] { | ||
| Some((batch_idx, row_idx)) => batch_buffer[batch_idx] | ||
| .column_for_output(output_col_idx, source_output_col_indices) | ||
|
|
@@ -1085,6 +1189,23 @@ impl PartialUpdateMergeFunction { | |
| } | ||
| } | ||
|
|
||
| /// Each aggregate's value to retract, or `None` when one has none. | ||
| fn retraction_cells( | ||
| aggregators: &[Option<Box<dyn FieldAggregator>>], | ||
| ) -> crate::Result<Option<Vec<Option<ArrayRef>>>> { | ||
| let mut cells = Vec::with_capacity(aggregators.len()); | ||
| for aggregator in aggregators { | ||
| cells.push(match aggregator { | ||
| Some(aggregator) => match aggregator.retraction_result()? { | ||
| Some(cell) => Some(cell), | ||
| None => return Ok(None), | ||
| }, | ||
| None => None, | ||
| }); | ||
| } | ||
| Ok(Some(cells)) | ||
| } | ||
|
|
||
| fn compare_sequence_group_rows( | ||
| incoming_batch_idx: usize, | ||
| incoming_row_idx: usize, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Keep a checked sum writable when the retraction cancels a valid existing value. On a partial-update table with
fields.seq.sequence-group=amount, a requiredBIGINTsum, andfields.amount.sum.fail-on-overflow=true, I committed+I(amount=i64::MIN, seq=1). Committing-U(i64::MIN, 2)and+U(i64::MIN, 3)separately readsi64::MINas expected. Sending the same update pair in one commit on this head fails atprepare_commitwithAggregate function 'sum' overflowed on field 'amount'. Pre-merging the pair starts at zero and computes0 - i64::MINbefore the compensating add. Please keep the original records when that intermediate merge cannot be represented, or represent the intermediate delta exactly, and test both flush layouts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @JingsongLi. The
.so.zstcame from building the Go bindings in the same checkout, sorry about that. It's dropped in 801ff48. The overflow case came up on apache/paimon#11274 today as well, so I'll follow the representation that lands there and update this PR once it's settled.