diff --git a/compliance/wide_table/README.md b/compliance/wide_table/README.md index 6905c8ad..a4f4b0f3 100644 --- a/compliance/wide_table/README.md +++ b/compliance/wide_table/README.md @@ -26,7 +26,8 @@ Compliance tests for the expected results come from the examples of the specification: Section 1.3, Section 5.2, Section 6.1, and Appendix B. Appendix B.3 gives no rows, so the `stores` data is chosen for its cases, and their expected results follow from -it. +it. Cases `a22` and `a23` evaluate the metric of Appendix B.6 on the `stores` +data. The key words MUST, MUST NOT, SHOULD, SHOULD NOT, and MAY are to be interpreted as described in BCP 14 @@ -41,7 +42,7 @@ Layout |------|----------| | [`retail/retail.sql`](retail/retail.sql) | Schema and data: customers, orders, returns, visits, and tickets (Appendix B.1, B.2, B.4, B.5) | | [`retail/retail_model.ossie.yaml`](retail/retail_model.ossie.yaml) | Ossie model over `retail.sql` | -| [`stores/stores.sql`](stores/stores.sql) | Schema and data: store_sales, store_returns, item, date_dim, store, and customers (Appendix B.3; unmatched home rows) | +| [`stores/stores.sql`](stores/stores.sql) | Schema and data: store_sales, store_returns, item, date_dim, store, and customers (Appendix B.3 and B.6; unmatched home rows) | | [`stores/stores_model.ossie.yaml`](stores/stores_model.ossie.yaml) | Ossie model over `stores.sql` with one path from `store_sales` to `date_dim` | | [`stores/stores_ambiguous_model.ossie.yaml`](stores/stores_ambiguous_model.ossie.yaml) | Same as `stores_model`, plus `store` to `date_dim` through `closing_date_id` | | `cases//query.json` | Query under test, in the JSON binding of Section 3.7 | @@ -73,7 +74,10 @@ matches a customer, so no NULL groups arise from unmatched home rows. `stores` matches Appendix B.3 without the closing-date relationship. Sale SS4 has a NULL `customer_id`, and customer C3 has a NULL `region`, so a `LEFT JOIN` places both amounts in the NULL region group (Section 5.4; Section 6, S1). An -`INNER JOIN` would drop SS4. +`INNER JOIN` would drop SS4. Both stores have 5 employees, and S1 has three +sales, so a join from `store_sales` to `store` counts S1's headcount three +times. Cases `a22` and `a23` check that the model-scoped metric +`store_productivity` counts it once (Appendix B.6). `stores_ambiguous` adds `store` to `date_dim` through `closing_date_id`, so `store_sales` has two paths to `date_dim`. Cases `e24` and `e25` use this diff --git a/compliance/wide_table/cases/a22_store_productivity/expected.json b/compliance/wide_table/cases/a22_store_productivity/expected.json new file mode 100644 index 00000000..754bbf92 --- /dev/null +++ b/compliance/wide_table/cases/a22_store_productivity/expected.json @@ -0,0 +1,11 @@ +{ + "fixture": "stores", + "title": "A store's headcount counts once, however many sales it has", + "question": "What is the sales amount per employee?", + "behavior": "store_productivity has a component on store_sales and one on store, each in its own branch: 415 / (5 + 5) = 41.5. Over a join from store_sales to store, S1's headcount counts once per sale (3 x 5 + 2 x 5 = 25) and gives 16.6. Summing distinct headcounts counts the shared value 5 once and gives 83. Appendix B.6.", + "spec": ["5.2", "5.4", "5.5", "B.6"], + "columns": ["store_productivity"], + "rows": [ + [41.5] + ] +} diff --git a/compliance/wide_table/cases/a22_store_productivity/query.json b/compliance/wide_table/cases/a22_store_productivity/query.json new file mode 100644 index 00000000..be60936e --- /dev/null +++ b/compliance/wide_table/cases/a22_store_productivity/query.json @@ -0,0 +1,3 @@ +{ + "metrics": ["store_productivity"] +} diff --git a/compliance/wide_table/cases/a23_store_productivity_by_state/expected.json b/compliance/wide_table/cases/a23_store_productivity_by_state/expected.json new file mode 100644 index 00000000..8428fd00 --- /dev/null +++ b/compliance/wide_table/cases/a23_store_productivity_by_state/expected.json @@ -0,0 +1,12 @@ +{ + "fixture": "stores", + "title": "Each group counts a store's headcount once", + "question": "What is the sales amount per employee by store state?", + "behavior": "Both components reach store, so each branch groups by state. CA has sales of 325 (SS1, SS2, SS4) and 5 employees, NY has 90 (SS3, SS5) and 5. Over a join from store_sales to store, CA's headcount counts three times and gives 325 / 15. Appendix B.6.", + "spec": ["5.2", "5.3", "5.4", "5.5", "B.6"], + "columns": ["state", "store_productivity"], + "rows": [ + ["CA", 65], + ["NY", 18] + ] +} diff --git a/compliance/wide_table/cases/a23_store_productivity_by_state/query.json b/compliance/wide_table/cases/a23_store_productivity_by_state/query.json new file mode 100644 index 00000000..3324af59 --- /dev/null +++ b/compliance/wide_table/cases/a23_store_productivity_by_state/query.json @@ -0,0 +1,4 @@ +{ + "dimensions": ["store.state"], + "metrics": ["store_productivity"] +} diff --git a/compliance/wide_table/stores/stores.sql b/compliance/wide_table/stores/stores.sql index 41b4889e..f802cca4 100644 --- a/compliance/wide_table/stores/stores.sql +++ b/compliance/wide_table/stores/stores.sql @@ -18,14 +18,16 @@ -- ============================================================================ -- Data: store_sales, store_returns, item, date_dim, store, and customers -- --- Shared input for the Appendix B.3 and unmatched-home-row cases under --- ../cases/. Load this file before a stores case runs. Do not change the --- values: the expected results depend on them. +-- Shared input for the Appendix B.3, Appendix B.6, and unmatched-home-row +-- cases under ../cases/. Load this file before a stores case runs. Do not +-- change the values: the expected results depend on them. -- -- Appendix B.3 names the datasets and relationships but gives no rows, so -- the rows here are chosen for the cases. SS4 has no customer, and C3 has a -- NULL region (Section 6, S1). The year column is d_year because YEAR is a --- reserved word in standard SQL; the model names the field year. +-- reserved word in standard SQL; the model names the field year. Both stores +-- have 5 employees, and S1 has three sales, so a join from store_sales to +-- store repeats S1's headcount (Appendix B.6). -- stores_ambiguous_model adds store → date_dim via closing_date_id. -- ============================================================================ @@ -52,14 +54,15 @@ INSERT INTO stores.item VALUES ('I2', 'Beta'); CREATE TABLE stores.store ( - store_id VARCHAR NOT NULL PRIMARY KEY, - state VARCHAR NOT NULL, - closing_date_id VARCHAR NOT NULL + store_id VARCHAR NOT NULL PRIMARY KEY, + state VARCHAR NOT NULL, + closing_date_id VARCHAR NOT NULL, + number_employees INTEGER NOT NULL ); INSERT INTO stores.store VALUES - ('S1', 'CA', 'D2024b'), - ('S2', 'NY', 'D2024a'); + ('S1', 'CA', 'D2024b', 5), + ('S2', 'NY', 'D2024a', 5); CREATE TABLE stores.customers ( customer_id VARCHAR NOT NULL PRIMARY KEY, diff --git a/compliance/wide_table/stores/stores_ambiguous_model.ossie.yaml b/compliance/wide_table/stores/stores_ambiguous_model.ossie.yaml index f9daadcf..8c67f0c8 100644 --- a/compliance/wide_table/stores/stores_ambiguous_model.ossie.yaml +++ b/compliance/wide_table/stores/stores_ambiguous_model.ossie.yaml @@ -66,6 +66,10 @@ datasets: datatype: String expression: sql: closing_date_id + - name: number_employees + datatype: Integer + expression: + sql: number_employees - name: customers source: stores.customers @@ -184,3 +188,10 @@ relationships: to: date_dim from_columns: [closing_date_id] to_columns: [date_id] + +metrics: + - name: store_productivity + description: Sales amount per employee, counting each store row once + datatype: Float + expression: + sql: CAST(SUM(store_sales.sales_amount) AS DOUBLE) / NULLIF(SUM(store.number_employees), 0) diff --git a/compliance/wide_table/stores/stores_model.ossie.yaml b/compliance/wide_table/stores/stores_model.ossie.yaml index 608d6c2b..7fb23fa9 100644 --- a/compliance/wide_table/stores/stores_model.ossie.yaml +++ b/compliance/wide_table/stores/stores_model.ossie.yaml @@ -19,6 +19,7 @@ # # Model over stores.sql (Appendix B.3). store_sales reaches date_dim only # through sale_date_id. See stores_ambiguous_model.ossie.yaml for two paths. +# store_productivity is the metric of Appendix B.6. version: 0.3.0.dev0 name: stores @@ -66,6 +67,10 @@ datasets: datatype: String expression: sql: closing_date_id + - name: number_employees + datatype: Integer + expression: + sql: number_employees - name: customers source: stores.customers @@ -179,3 +184,10 @@ relationships: to: date_dim from_columns: [return_date_id] to_columns: [date_id] + +metrics: + - name: store_productivity + description: Sales amount per employee, counting each store row once + datatype: Float + expression: + sql: CAST(SUM(store_sales.sales_amount) AS DOUBLE) / NULLIF(SUM(store.number_employees), 0) diff --git a/core-spec/wide_table_semantics.md b/core-spec/wide_table_semantics.md index b1f3cffd..e713187c 100644 --- a/core-spec/wide_table_semantics.md +++ b/core-spec/wide_table_semantics.md @@ -502,12 +502,7 @@ Using the data in Appendix B.1: ### B.6 Sales per employee across stores -The [TPC-DS example model](../examples/tpcds_semantic_model.yaml) defines -the model-scoped metric `store_productivity` as -`SUM(store_sales.ss_ext_sales_price) / NULLIF(SUM(store.s_number_employees), 0)`. -Sales are recorded per line item and headcount is recorded per store. The -`store_sales_to_store` relationship joins `ss_store_sk` to the store key -`s_store_sk`. Consider these rows, showing only the columns used in the example: +The [TPC-DS example model](../examples/tpcds_semantic_model.yaml) defines the model-scoped metric `store_productivity` as `SUM(store_sales.ss_ext_sales_price) / NULLIF(SUM(store.s_number_employees), 0)`. Sales are recorded per line item and headcount per `store` row. The `store_sales_to_store` relationship joins `ss_store_sk` to the store key `s_store_sk`. Consider these rows, showing only the columns used in the example: | store_sales.ss_store_sk | ss_ext_sales_price | |---|---| @@ -520,10 +515,7 @@ Sales are recorded per line item and headcount is recorded per store. The | 1 | 10 | | 2 | 10 | -The query `{"metrics": ["store_productivity"]}` returns one row with -`store_productivity = 30`. Sections 5.2, 5.4, and 5.5 put the two aggregates in -separate branches, each over its own home dataset. This SQL computes the same -result over the source rows: +The query `{"metrics": ["store_productivity"]}` returns one row with `store_productivity = 30`. Sections 5.2, 5.4, and 5.5 put the two aggregates in separate branches, each over its own home dataset. This SQL computes the same result over the source rows: ```sql WITH sales AS ( @@ -536,7 +528,4 @@ SELECT CAST(sales.revenue AS DOUBLE PRECISION) / NULLIF(staffing.headcount, 0) FROM sales CROSS JOIN staffing; ``` -Aggregating over a join of the source rows would count store 1's headcount -twice and return 600 / 30 = 20. Summing distinct headcount values would count -both stores' shared headcount of 10 only once and return 600 / 10 = 60. The -staffing branch counts each store row once, giving 600 / 20 = 30. +Aggregating over a join of the source rows would count store 1's headcount twice and return 600 / 30 = 20. Summing distinct headcount values would count both stores' shared headcount of 10 only once and return 600 / 10 = 60. The staffing branch counts each store row once, giving 600 / 20 = 30.