Skip to content

Fix cross-fact aggregation before joining metric inputs - #2754

Open
Miskho wants to merge 2 commits into
DataJunction:mainfrom
Miskho:codex/dj-fix-cross-fact-aggregation
Open

Miskho wants to merge 2 commits into
DataJunction:mainfrom
Miskho:codex/dj-fix-cross-fact-aggregation

Conversation

@Miskho

@Miskho Miskho commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

Combining a daily download total with distinct visitor metrics can inflate the download total. This PR aggregates each safely aggregatable input to the requested output dimensions before joining facts.

For example, request downloads, distinct visitors, and distinct mobile visitors grouped by day. The following simplified intermediate inputs come from the regression fixture (without its optional NULL-account row).

A — downloads aggregated by day

day downloads
1 43

B — visitor membership retained for exact distinct counts

day account_id platform
1 101 mobile
1 102 mobile
1 103 tv

B retains account identities because the metrics count distinct accounts. Two raw visits by account 101 have already collapsed into one membership row. Joining A to B on day still repeats A's total:

day A.downloads B.account_id
1 43 101
1 43 102
1 43 103

A final SUM(downloads) then returns 43 × 3 = 129. The visitor counts remain correct in this example, which can make the inflated download total easy to miss.

Daily metric Before After (expected)
Downloads 129 43
Distinct visitors 3 3
Distinct mobile visitors 2 2

Collapse safely aggregatable inputs to the requested output grain before joining facts. Preserve each component's merge function, exact DISTINCT operator, selected dimensions, and AVG operands. Reject incompatible DISTINCT operators and unsafe cross-fact SQL-window shapes instead of returning inflated results; safe DISTINCT/MIN/MAX windows and fixed-grain broadcasts remain supported.

DISTINCT operator and merge-metadata lookup is scoped to the owning fact group, including base metrics of derived requests. Same-named columns in separate facts do not cause false conflicts; incompatible operators sharing one wrapper remain rejected.

Test Plan

  • Current integration run with all three compiler fixes and Greptile follow-ups: 1,950 compiler, cube API, and SQL instrumentation tests passed; 14 expected failures, zero failures. Combined Ruff, format and mypy checks passed.
  • Current focused module: 63 passed, covering metric order, duplicate/NULL identities, mixed components, selected/filter-only dimensions, windows, safety errors, separate-fact DISTINCT operators, derived metrics, and metadata isolation. Before the follow-up fix, the new checks produced 13 failures and one passing control.
  • Follow-up related compiler/window/decomposition/reaggregation/cube checks: 554 passed, one expected failure. Ruff, formatting, mypy and whitespace checks passed.
  • Review replay: 1,972 related compiler, decomposition, reaggregation, and cube API tests passed; 14 expected failures.
  • Earlier full server/doctest run: 7,030 passed, 823 skipped, 26 expected failures. Full-run coverage plus the final focused replay passed the 100% statement/branch gate; the full suite was not repeated after a small AST-parent guard cleanup.
  • Changed-file hooks, Ruff, mypy, and live authenticated HTTP/native DuckDB restart checks passed.

Local execution used Python 3.13 and DuckDB. Native Spark/Trino, other Python CI versions, and optional integration stacks were not run locally.

Deployment Plan

No schema migration. Queries with unsafe cross-fact SQL-window fanout now return a validation error rather than an incorrect result.

Companion compiler fixes, validated together: #2755 (output-dimension filters) and #2756 (CTE identity). These PRs share compiler files; later PRs may need rebasing after the first merge.

@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

👷 Deploy request for thriving-cassata-78ae72 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 0dae84f

@Miskho
Miskho marked this pull request as ready for review November 26, 2026 09:14
@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The reviewed changes appear safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge.

Summary

The PR aggregates fact inputs to the requested dimensions before joining them, preventing repeated rows from inflating totals.

  • Metric inputs collapse to the requested dimensions before they join.
  • Unsafe cross-fact window requests now get a clear validation error.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Fact components] --> B[Aggregate each fact to output dimensions]
  B --> C[Join fact inputs]
  C --> D[Compute requested metrics]
  E[Each group's base metrics] --> B
Loading

Reviews (2) · Last reviewed commit: "Scope DISTINCT operators and merge metad..." · Reviewed by Greptile

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.

1 participant