Skip to content

Fix inventory turnover sales attribution and reconciliation - #21

Open
WiktorWisniewski wants to merge 1 commit into
Snowflake-Labs:masterfrom
WiktorWisniewski:fix/inventory-turnover-sales-join
Open

WiktorWisniewski wants to merge 1 commit into
Snowflake-Labs:masterfrom
WiktorWisniewski:fix/inventory-turnover-sales-join

Conversation

@WiktorWisniewski

Copy link
Copy Markdown

The reference solution and sales verifier join order lines to inventory snapshots before summing sales. This multiplies fulfilled quantities by the number of matching inventory rows and attributes sales to inventory warehouses instead of the order's warehouse. This PR fixes both aggregations and closes a sampling gap that can hide the error.

Baseline: 74d4bcd4192e1959754f683841f9f602b8bc86e1. Pattern follows the evidence-first approach in PR #9.

What the task requires

The sales rules in tasks/dbt-inventory-turnover-analysis/instruction.md specify joining order lines to orders on order_id and summing quantity_ordered per (sku, warehouse_id) within the fulfilled-order window. The reconciliation requirement explicitly calls for direct aggregation from order lines and orders.

Inventory snapshots describe stock. Adding another snapshot must not change the quantity sold.

What the reference and verifier compute

Both currently include:

JOIN main.stg_product__product_variants pv ON ol.variant_id = pv.variant_id
JOIN main.stg_inventory__inventory_levels il ON pv.variant_id = il.variant_id

The reference groups by il.warehouse_id; the verifier filters by it. A fulfilled line with quantity 3 and two matching inventory rows therefore contributes 6. Inventory in another warehouse also receives the sale, even when the order belongs to a different warehouse.

The bundled DuckDB data contains a concrete example:

SKU Order warehouse Direct fulfilled quantity Original reference Corrected reference
SKU00001238 0822043e-bb97-4569-8fd5-ab9778e46823 3 6 3

The quantity comes from order 8aeebd06-935e-4400-9d8f-c0b6199cba7b. Two inventory rows match the product variant. The correction retains the existing variant-to-SKU mapping.

Why the original reference still passes

The verifier samples only rows where current_quantity_on_hand > 0. On the bundled data, the reference produces no such rows, so this reconciliation test returns without checking any sales. Passing the old suite therefore does not establish sales correctness.

The revised sample still prioritizes the highest stock quantities, but includes zero-stock rows and breaks ties deterministically using sales, SKU, and warehouse. It retains the limit of 20 and the existing numeric tolerance.

Changes

  • Carry orders.warehouse_id through the reference's fulfilled/windowed order CTEs; aggregate sales by that warehouse without joining inventory snapshots.
  • Apply the equivalent correction to both DuckDB and Snowflake verifier queries.
  • Include zero-stock output in the reconciliation sample.
  • Add nine small DuckDB regression cases exercising the actual verifier: snapshot independence (0/1/2/5 snapshots), warehouse attribution, rejection of doubled or missing sales, zero-stock sampling, and existing fulfillment/window/fallback behavior.

The task instructions and inventory calculations are unchanged. This addresses sales aggregation and reconciliation coverage; it is not a complete audit of other inventory metrics.

Validation

Executed both reference scripts in separate fresh containers using DuckDB 1.2.2, then ran the complete task verifier against each generated dbt project:

Reference Original verifier Corrected verifier
Original 10 passed 9 passed, 1 failed (sales reconciliation)
Corrected 10 passed 10 passed

The original verifier's passing result on the corrected reference is explained by the empty positive-stock sample above.

Regression tests: 6 failed / 3 passed against the original verifier; 9 passed against the corrected verifier.

Run the focused regression suite from the repository root:

uv run --with pytest --with duckdb python -m pytest tests/test_inventory_turnover_verifier.py -q

Full validation ran solution/solve.sh, followed by pytest on the task's tests/test_outputs.py, inside data-eng-bench-claude-code:2.1.185. No model calls were needed. The Snowflake SQL branch was updated consistently but was not executed against a live Snowflake account.

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