Repository navigation
Fix non-sequential _id gaps on mixed upsert batches - #23
Merged
Merged
Conversation
merge_sql() numbered every incoming row with a single ROW_NUMBER() OVER() spanning the whole batch, but only unmatched (new) rows ever consumed that number for their _id (MAX(_id) + _rn). A batch mixing already-present unchanged rows with new rows left gaps: unchanged rows' _rn slots were never inserted anywhere. Partition the window on whether the row's primary key is found in the target table (via a self-join probe) so _rn is numbered 1..k within just the unmatched rows, independent of their position among matched rows in the same batch. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Member
|
LGTM |
Member
|
@luccasmmg i merged this but i guess we also need to fix on the aircan DAG isn't it ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_idvalues for the newly inserted rows were non-sequential, with permanent gaps.merge_sql()'sUSINGsubquery numbered every incoming row with a singleROW_NUMBER() OVER ()spanning the whole batch, but_id = MAX(_id) + _rnis only ever applied onWHEN NOT MATCHED. Unchanged matched rows (no-op'd via theIS DISTINCT FROMdiff guard) still consumed_rnslots that nothing ever inserted, so subsequent new rows jumped ahead by however many unchanged rows preceded them in that chunk.ROW_NUMBER()window on whether each row's primary key is already present in the target table (via aLEFT JOINself-probe on the PK), so_rnrestarts at 1 within the "genuinely new" partition — independent of how many unchanged rows are interleaved in the same batch._idassignment forWHEN NOT MATCHEDrows staysMAX(_id) + _rn, now gap-free.WHEN MATCHEDsemantics (diff-gated update,_updated_atonly bumps on real changes) are unchanged.Test plan
test_merge_sql_renders_typed_extractors_on_match_update_no_match_insertto assert the new partitionedROW_NUMBER()+ self-join shape.test_merge_sql_numbers_unmatched_rows_within_their_own_partitionas a regression test documenting the gap-avoidance intent.python3 -m pytest tests/engines/bigquery/test_tables.py— 88 passed.python3 -m pytest— 496 passed.Note: adds one extra self-join (target table probed on the PK) per upsert chunk on top of the existing
MERGE's own join — worth checking PK-column clustering/partitioning on large resource tables if this shows up in BigQuery cost/latency.🤖 Generated with Claude Code