Skip to content

Fix #892: materialize viral-propagation operand and unpivot window once#905

Open
albertohernandez1995 wants to merge 3 commits into
mainfrom
cr-892
Open

Fix #892: materialize viral-propagation operand and unpivot window once#905
albertohernandez1995 wants to merge 3 commits into
mainfrom
cr-892

Conversation

@albertohernandez1995

Copy link
Copy Markdown
Contributor

Summary

Viral-propagation SQL repeated expensive work: hierarchy re-embedded a
derived operand (e.g. an inline filter) once per leaf child, and unpivot
recomputed the dataset-wide viral window (AGG(...) OVER ()) once per measure
arm. Both now route through a single materialized CTE — _op for the hierarchy
operand (referenced by the pivot chain and the viral CTEs) and _uv_src for the
unpivot window (referenced by every measure arm) — so the operand subquery and
the window pass are evaluated once. The hierarchy change is gated on viral
attributes being present, so the measure-only path is byte-for-byte unchanged.

Checklist

  • Code quality checks pass (ruff format, ruff check, mypy)
  • Tests pass (pytest) — 5051 passed, 11 skipped
  • Documentation updated (if applicable)

Fixes #892

@albertohernandez1995
albertohernandez1995 enabled auto-merge (squash) July 14, 2026 11:25

@javihern98 javihern98 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.

I am not really sure this actually gives any performance gain given the duckdb internal optimiser. I believe we should add as well some kind of comparison between the before and after using the reproducible example of the issue, as well as looking into the actual duckdb execution plan

Benchmarks (DS_1 1.2M / DS_2 3M rows, DuckDB threads=4) show the _op
MATERIALIZED CTE regresses hierarchy ~12% by blocking filter pushdown,
with no measurable win — DuckDB already plans the repeated inline operand
efficiently. Revert that part; keep the unpivot _uv_src window dedup
(EXPLAIN: WINDOW/SEQ_SCAN 3->1, scales with measure count).
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.

Viral propagation SQL re-executes the hierarchy operand per leaf child and recomputes the unpivot dataset-wide window per measure

2 participants