Skip to content

Fix DuckDB FIFO allocations without a matching receipt layer - #14

Merged
sfc-gh-dhuang merged 1 commit into
Snowflake-Labs:masterfrom
understudylabs:fix/fifo-null-allocation
Nov 12, 2026
Merged

sfc-gh-dhuang merged 1 commit into
Snowflake-Labs:masterfrom
understudylabs:fix/fifo-null-allocation

Conversation

@lluisinthedesert

@lluisinthedesert lluisinthedesert commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

The DuckDB reference for fifo-inventory-cogs allocates units to picks with no matching receipt layer. Its expected-results file preserves those allocations, so the shipped reference passes while a NULL-safe implementation fails seven expected-value checks.

Root cause

The allocation query LEFT JOINs receipt layers, then computes:

GREATEST(0,
    LEAST(r.cumulative_qty_after, p.consumption_end) -
    GREATEST(r.cumulative_qty_before, p.consumption_start)
)

When no layer matches, both receipt bounds are NULL. DuckDB ignores those NULL arguments, leaving consumption_end - consumption_start: the entire pick quantity. Snowflake propagates NULL, which the reference subsequently converts to zero.

A minimal expression reproduces the difference:

SELECT COALESCE(
    GREATEST(0, LEAST(NULL, 7) - GREATEST(NULL, 3)),
    0
);

DuckDB 1.2.2 returns 4; live Snowflake returns 0.

Effect on the pinned dataset

At a3278ad102829a6084dde086244a0ef665a8011c:

  • Total receipts: 131,306 units.
  • Shipped DuckDB allocation: 187,826 units.
  • 3,392 rows with NULL receipt_id carry 168,315 allocated units.
  • Guarding unmatched receipt rows produces 19,511 allocated units, matching the checked-in Snowflake fulfilled expectation.

The DuckDB and Snowflake verifiers select different expected files. Consequently both shipped references pass their respective suites, despite the DuckDB allocation exceeding all received inventory.

Intended behavior

A pick without a matching receipt layer must receive zero allocation. The sibling dbt-multicurrency-lifo reference already applies this rule: it evaluates the same range-overlap calculation only when p.purchase_id IS NOT NULL, otherwise returning zero. This FIFO guard follows that existing repository pattern.

The checked-in Snowflake expected results also expect 19,511 fulfilled units, matching the corrected DuckDB allocation rather than the shipped DuckDB total of 187,826.

Impact

The shipped DuckDB reference passes all 50 tests despite allocating more units than were received. Correcting the unmatched-receipt allocation while retaining the original verifier produces seven failures. This means the task can penalize correct allocation logic and reward reproducing the reference bug. Phantom consumption also affects fulfillment and ending-inventory metrics, while the separate backend fixtures grade DuckDB and Snowflake against different inventory outcomes. This is a correctness issue for this task, not a claim about the benchmark as a whole.

Change

  • Return zero allocation when r.receipt_id IS NULL.
  • Update the seven affected DuckDB expected values, including downstream ending-inventory values.
  • Add one regression test requiring aggregate allocations not to exceed receipts and prohibiting positive allocations without a receipt.

No input data, ticket wording, or Snowflake expected values change. The guard, affected expectations, and regression cover this single root cause.

Validation

Pairing DuckDB 1.2.2 Live Snowflake
Shipped reference + shipped verifier 50 passed 50 passed
Corrected reference + shipped verifier 43 passed, 7 failed —
Corrected reference + corrected verifier 51 passed 51 passed

Executed the reference and pytest verifier in isolated local projects on macOS arm64, Python 3.12, dbt-core 1.10.23, dbt-duckdb 1.10.0, and dbt-snowflake 1.10.0. Container paths were remapped locally. Snowflake runs used separate database clones with the task's required source inputs from the pinned dataset. These are direct dbt/pytest checks, not Harbor agent runs.

@lluisinthedesert
lluisinthedesert marked this pull request as ready for review October 21, 2026 04:34

@sfc-gh-dhuang sfc-gh-dhuang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirmed DuckDB 1.2.2 returns 4 for GREATEST(0, LEAST(NULL,7) - GREATEST(NULL,3)) where Snowflake returns 0. After this PR the DuckDB expected values equal the Snowflake ones (19,511 fulfilled).

One thing flagged by one of the reviewer agents - since DuckDB fulfillment rate changes from 0.9465 to 0.1093, this needs a new dataset revision and earlier DuckDB results aren't comparable.

@sfc-gh-dhuang
sfc-gh-dhuang merged commit 48185c8 into Snowflake-Labs:master Nov 12, 2026
1 of 2 checks passed
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