Skip to content

fix(data-warehouse): derive partition count in compact threshold check - #70549

Merged
danielcarletti merged 1 commit into
masterfrom
claude/compact-derive-partition-count
Jul 14, 2026
Merged

fix(data-warehouse): derive partition count in compact threshold check#70549
danielcarletti merged 1 commit into
masterfrom
claude/compact-derive-partition-count

Conversation

@danielcarletti

Copy link
Copy Markdown
Contributor

Problem

compact_if_fragmented decides "fragmented" from files-per-partition, using the partition_count its caller passes. Only md5 partitioning persists a partition_count on the schema — datetime- and numerical-partitioned schemas never get one (append_partition_key_to_table only produces a count for md5 bucketing), so run_pre_write_defensive_compact passes None for them. With None the threshold math treats the table as a single partition, so any datetime-partitioned table with more than 200 total files reads as fragmented and gets a defensive compact plus vacuum at the start of every sync run, even when it is perfectly healthy.

This is the same blind spot #70499 fixed for the CDC post-load path by deriving the count at its call site. This PR moves the derivation into compact_if_fragmented itself, so every run_maintenance caller (the v2 and v3 pre-write defensive path today) gets the correct math without each call site reimplementing it.

Changes

When partition_count is None, compact_if_fragmented derives it from the table's actual layout: the number of distinct file directories in the delta log's file paths (one directory per partition value; unpartitioned tables collapse to the single table root). No extra I/O — it reuses the get_file_uris result the threshold math already fetched.

Note

Behavior change: partitioned tables whose schemas persist no partition_count stop compacting at the start of every sync run and only compact when genuinely past the thresholds (200 files per partition, 5,000 total). The total-files backstop is unaffected.

Checked that no open PR touches this function: #70495 edits the merge-loop region and #70504 the commit-metadata region of the same file, both away from compact_if_fragmented. Once this and #70499 both land, the call-site derivation in pipelines/common/load.py becomes redundant (passing a non-None count simply skips the helper's derivation) and can be dropped in a small cleanup.

How did you test this code?

Extended TestCompactIfFragmented in test_delta_table_helper.py:

  • healthy_partitioned_table_skips: 300 files across 3 partition directories with partition_count=None must not compact — this case fails on the old code (None meant 1 partition, 300 files-per-partition, compact every run), so it pins exactly the regression this PR fixes.
  • fragmented_partitioned_table_fires: 750 files across 3 directories (250 per partition) still compacts — guards the derivation from over-suppressing real fragmentation.
  • The existing threshold cases now use directory-shaped fake URIs so the unpartitioned case keeps meaning "single root directory" under the new derivation.

The derivation lives in the helper, so the coverage lives there too — an extract-level test with a mocked helper cannot observe it, and the existing TestRunPreWriteDefensiveCompact pass-through cases (schema over resource, both-None passes None) remain correct as-is.

Ran locally: pytest .../pipelines/pipeline/test/test_delta_table_helper.py .../pipelines/common/test/test_extract.py (74 passed). hogli ci:preflight --fix clean.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Docs update

None needed, internal pipeline behavior only.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Follow-up flagged while working on #70499, implemented by Claude Code (Claude Fable 5). Skills invoked: /writing-tests. The task allowed either a call-site fix in run_pre_write_defensive_compact or moving the derivation into compact_if_fragmented; I (Claude) chose the helper after verifying the two open PRs touching delta_table_helper.py (#70495, #70504) edit disjoint function regions — one implementation fixes all callers and avoids a second copy of the derivation block. Branched from fresh master, independent of #70499.

Only md5 partitioning persists a partition_count on the schema;
datetime- and numerical-partitioned schemas pass None, which made
compact_if_fragmented treat those tables as one partition, so any
table with more than 200 total files read as fragmented and got a
defensive compact+vacuum at the start of every sync run. Derive the
count from the distinct file directories in the delta log instead —
no extra I/O, and all run_maintenance callers benefit.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@danielcarletti danielcarletti self-assigned this Jul 13, 2026
@trunk-io

trunk-io Bot commented Jul 13, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@danielcarletti
danielcarletti marked this pull request as ready for review July 13, 2026 19:29
@pr-assigner-resolver-posthog
pr-assigner-resolver-posthog Bot requested a review from a team July 13, 2026 19:29
@greptile-apps

greptile-apps Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(data-warehouse): derive partition co..." | Re-trigger Greptile

@danielcarletti
danielcarletti merged commit 7333cfa into master Jul 14, 2026
257 checks passed
@danielcarletti
danielcarletti deleted the claude/compact-derive-partition-count branch July 14, 2026 12:02
@deployment-status-posthog

deployment-status-posthog Bot commented Jul 14, 2026

Copy link
Copy Markdown

Deploy status

Environment Status Deployed At Workflow
dev ✅ Deployed 2026-07-14 12:24 UTC Run
prod-us ✅ Deployed 2026-07-14 12:36 UTC Run
prod-eu ✅ Deployed 2026-07-14 12:36 UTC Run

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