Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions compliance/wide_table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since #741 and #747, each case that returns rows has an expected.txt, a query.ossie.sql with the Layer 2 query of its lowering, and an entry in cases/README.md. a22 and a23 still have expected.json and none of the three. Could the rebase convert them (a08 has a two-branch lowering to start from)?

data.

The key words MUST, MUST NOT, SHOULD, SHOULD NOT, and MAY are to be
interpreted as described in BCP 14
Expand All @@ -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/<case>/query.json` | Query under test, in the JSON binding of Section 3.7 |
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions compliance/wide_table/cases/a22_store_productivity/expected.json
Original file line number Diff line number Diff line change
@@ -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]
]
}
3 changes: 3 additions & 0 deletions compliance/wide_table/cases/a22_store_productivity/query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"metrics": ["store_productivity"]
}
Original file line number Diff line number Diff line change
@@ -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.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Each state has one store, so SUM(DISTINCT headcount) also gives 5 and still passes here — CA would be 325 / 5 = 65, the right answer. a22 is the case that catches that plan. Worth a clause in behavior saying so, since the README sentence this PR adds ("Cases a22 and a23 check that the model-scoped metric store_productivity counts it once") reads as though both cases cover both wrong plans.

"spec": ["5.2", "5.3", "5.4", "5.5", "B.6"],
"columns": ["state", "store_productivity"],
"rows": [
["CA", 65],
["NY", 18]
Comment on lines +9 to +10

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

store_productivity is CAST(... AS DOUBLE) / ... with datatype: Float, so this is an approximate numeric. Written without a fraction it declares an exact one: README.md:139 in this directory says a JSON number with no fraction or exponent is exact, and ../../query/README.md:132 then requires exact numerics to be equal, with the 1e-9 relative tolerance applying only to approximate ones.

As written, a23 gets zero tolerance on a floating-point division, and an engine one ULP off 65.0 fails a case that is semantically correct. a22 in this PR has it right with 41.5, as does a08 with 162.5.
Maybe

     ["CA", 65.0],
     ["NY", 18.0]

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dimensions": ["store.state"],
"metrics": ["store_productivity"]
}
21 changes: 12 additions & 9 deletions compliance/wide_table/stores/stores.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-- ============================================================================

Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions compliance/wide_table/stores/stores_ambiguous_model.ossie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
12 changes: 12 additions & 0 deletions compliance/wide_table/stores/stores_model.ossie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
17 changes: 3 additions & 14 deletions core-spec/wide_table_semantics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|
Expand All @@ -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 (
Expand All @@ -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.
Loading