airflow icon indicating copy to clipboard operation
airflow copied to clipboard

RollupMapper never fires with a BACKWARD window

Open hussein-awala opened this issue 1 week ago • 1 comments

Under which category would you file this issue?

Airflow Core

Apache Airflow version

main (678e3e08d2); the code path is the same in 3.3.x

What happened and how to reproduce it?

A RollupMapper whose window uses direction=Window.Direction.BACKWARD never creates a Dag run, even after every upstream key the window expects has arrived.

PartitionedAssetTimetable(
    assets=hourly_asset,
    default_partition_mapper=RollupMapper(
        upstream_mapper=StartOfDayMapper(),
        window=DayWindow(direction=Window.Direction.BACKWARD),
    ),
)

For downstream key 2024-03-13, RollupMapper.to_upstream expects the hourly keys 2024-03-12T01:00:00 through 2024-03-13T00:00:00. Produce exactly those 24 events: only 2024-03-13T00:00:00 is linked to the 2024-03-13 pending run. The other 23 go to the 2024-03-12 pending run, and neither run is ever satisfied.

Cause: AssetManager._queue_partitioned_dags routes each event with mapper.to_downstream. RollupMapper.to_downstream delegates to the upstream mapper, which routes a key to the period that contains it. A BACKWARD window holds (previous period start, period start], so routing and expected set disagree. The scheduler (_resolve_asset_partition_status) only counts PartitionedAssetKeyLog rows linked to each pending run. This breaks the invariant documented on Window: upstream_key in window.to_upstream(D) ⇔ D in mapper.to_downstream(upstream_key).

The same happens for HourWindow, DayWindow, WeekWindow, MonthWindow, QuarterWindow and YearWindow, each paired with its StartOf*Mapper. FORWARD rollups and BACKWARD fan-outs behave as documented.

What you think should happen instead?

Either rollups honor direction, or a BACKWARD rollup is rejected instead of silently never firing. direction was added for FanOutMapper (#65761, #67475). The rollup claim came later, in the docs (#68889), with no test. A docs PR that references this issue points rollup users to FORWARD until this is decided.

Choosing the fix needs a decision:

  1. Route rollup events by the window's own member set. Add a core-only Window hook that returns the next period start. In RollupMapper.to_downstream, a BACKWARD key that is not in its own period's expected set goes to the next period. This keeps the window maths and fan-out unchanged. A BACKWARD daily rollup for 2024-03-13 would then wait for 2024-03-12T01:00:00 … 2024-03-13T00:00:00, which may not be what users expect.
  2. Give BACKWARD rollups "previous period" semantics. The 2024-03-13 rollup would wait for 2024-03-12T00:00:00 … 2024-03-12T23:00:00, as the old docs described. This needs window maths specific to rollups, or a change to the shared BACKWARD maths. The latter changes fan-out behavior shipped in 3.3.0.
  3. Reject BACKWARD windows in RollupMapper at Dag-parse time. This turns the silent hang into an error, but Dags that parse today would stop parsing.

Operating System

N/A (scheduler logic, reproduced in unit tests)

Deployment

Other

Anything else?

It happens every time. With MinimumCount(1), the run fires as soon as the anchor key arrives, counting events mostly outside the window (worked out from the code, not run).

A DB-backed scheduler test that reproduces this for all six windows is ready on a local branch. It becomes the regression test for whichever option is chosen.

Are you willing to submit PR?

  • [X] Yes I am willing to submit a PR!

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Drafted-by: Claude Code (Opus 5.5); reviewed by @hussein-awala before posting

hussein-awala avatar Nov 27 '26 10:11 hussein-awala

The reproducer points to an inverse-mapping inconsistency: RollupMapper.to_upstream(D) defines a BACKWARD window as the half-open interval (previous_period_start, period_start], but to_downstream(upstream_key) delegates to the upstream mapper and routes the boundary keys into the period that contains them. The two directions therefore disagree.

Proposed solution:

  1. Make RollupMapper.to_downstream() honor the configured window direction and boundary semantics instead of delegating blindly to the upstream mapper.
  2. For BACKWARD windows, map a key at the period boundary to the downstream period whose to_upstream() set contains that key; preserve the existing FORWARD behavior.
  3. Add a parametrized test for Hour/Day/Week/Month/Quarter/Year windows with both directions, asserting the invariant: upstream_key in window.to_upstream(D) == D in mapper.to_downstream(upstream_key).
  4. Add a scheduler-level regression test that produces all expected hourly events and verifies that exactly one pending DAG run is satisfied.

Keeping the invariant test alongside the mapper unit tests should prevent the same boundary error from recurring in another window type.

senapati484 avatar Nov 29 '26 13:11 senapati484