Repository navigation
Fix inventory turnover sales attribution and reconciliation - #21
Open
WiktorWisniewski wants to merge 1 commit into
Open
WiktorWisniewski wants to merge 1 commit into
WiktorWisniewski wants to merge 1 commit into
Conversation
WiktorWisniewski
requested review from
sfc-gh-daxu,
sfc-gh-dhuang and
sfc-gh-jreini
as code owners
November 17, 2026 15:56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.mdspecify joining order lines to orders onorder_idand summingquantity_orderedper(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:
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:
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
orders.warehouse_idthrough the reference's fulfilled/windowed order CTEs; aggregate sales by that warehouse without joining inventory snapshots.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:
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:
Full validation ran
solution/solve.sh, followed bypyteston the task'stests/test_outputs.py, insidedata-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.