From 58216c410d04dde28a7f5a9c8e55dc07577411b8 Mon Sep 17 00:00:00 2001 From: Nico Ritschel Date: Mon, 9 Nov 2026 17:56:30 -0800 Subject: [PATCH] Update PostgreSQL conformance rows for fact-anchored joins Two PostgreSQL-executed tests still expected a dimension member without metric rows as a group. They run only with SIDEMANTIC_TEST_POSTGRES_DSN, so the anchoring change did not reach them locally. Both now expect the rows of the equivalent fact-anchored SQL and keep the zero-count member behind an explicit dimension anchor. The join rollup case also gains an order with an unmatched key and one with a NULL key, so PostgreSQL executes the NULL group and its ordering. --- .../test_complete_filters_postgres.py | 37 ++++++++++--------- .../test_rollup_reconstruction.py | 17 ++++++++- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/tests/semantic_conformance/test_complete_filters_postgres.py b/tests/semantic_conformance/test_complete_filters_postgres.py index 4f3cb5693..4a1f532d6 100644 --- a/tests/semantic_conformance/test_complete_filters_postgres.py +++ b/tests/semantic_conformance/test_complete_filters_postgres.py @@ -90,10 +90,11 @@ def test_postgres_filtered_float_fanout_average_preserves_values(having): ) ) filters = ["items.category IS NOT NULL"] + # Orders own the measures, so the category whose only item names a missing + # order is not one of their groups. expected = [ ("a", pytest.approx(5 / 24), 0.125, 0.625, 0.25, 4), ("b", 0.375, None, 0.375, None, 2), - ("orphan", None, None, None, None, 0), ] if having == "average": filters.append("orders.average > 0.3") @@ -101,22 +102,20 @@ def test_postgres_filtered_float_fanout_average_preserves_values(having): elif having == "sum": filters.append("orders.total > 0.5") expected = [("a", pytest.approx(5 / 24), 0.125, 0.625, 0.25, 4)] - sql = compile_semantic_input( - graph, - { - "metrics": [ - "orders.average", - "orders.first_average", - "orders.total", - "orders.first_total", - "orders.row_count", - ], - "dimensions": ["items.category"], - "filters": filters, - "order_by": ["items.category"], - "dialect": "postgres", - }, - ) + query = { + "metrics": [ + "orders.average", + "orders.first_average", + "orders.total", + "orders.first_total", + "orders.row_count", + ], + "dimensions": ["items.category"], + "filters": filters, + "order_by": ["items.category"], + "dialect": "postgres", + } + sql = compile_semantic_input(graph, query) with psycopg.connect(dsn, autocommit=True) as connection: connection.execute('create temporary table float_orders(id integer, "Amount" double precision, paid boolean)') connection.execute("insert into float_orders values (1,0.125,true),(2,0.375,true),(3,null,true),(7,0.125,true)") @@ -125,6 +124,10 @@ def test_postgres_filtered_float_fanout_average_preserves_values(having): "insert into float_items values (1,1,'a'),(2,1,'a'),(3,1,'a'),(4,2,'a'),(5,3,'a'),(6,7,'a'),(7,7,'a'),(8,2,'b'),(9,2,'b'),(10,3,'b'),(11,999,'orphan')" ) assert connection.execute(sql).fetchall() == expected + if having == "none": + # Anchored at the dimension model, that category is kept as a zero group. + anchored = compile_semantic_input(graph, {**query, "consumption_base_model": "items"}) + assert connection.execute(anchored).fetchall() == [*expected, ("orphan", None, None, None, None, 0)] @pytest.mark.parametrize("population", ["grouped", "no_matches", "empty"]) diff --git a/tests/semantic_conformance/test_rollup_reconstruction.py b/tests/semantic_conformance/test_rollup_reconstruction.py index fc14c820d..b5eebd784 100644 --- a/tests/semantic_conformance/test_rollup_reconstruction.py +++ b/tests/semantic_conformance/test_rollup_reconstruction.py @@ -526,6 +526,11 @@ def test_postgres_rollup_reconstruction_executes(mode): connection.execute( "insert into pg_rollup_orders values (1,1,'paid',2,0,'2026-01-01 01:00:00'),(2,1,'paid',3,1,'2026-01-02 01:00:00'),(3,2,'paid',null,0,'2026-01-02 02:00:00'),(4,2,'open',100,10,'2026-01-02 15:00:00')" ) + if mode == "join": + # One order names a customer that does not exist and one has no customer. + connection.execute( + "insert into pg_rollup_orders values (5,9,'paid',7,0,'2026-01-03 01:00:00'),(6,null,'paid',8,1,'2026-01-03 02:00:00')" + ) connection.execute( f"create temporary table orders_preagg_daily as {model.pre_aggregations[0].generate_materialization_sql(model)}" ) @@ -536,11 +541,21 @@ def test_postgres_rollup_reconstruction_executes(mode): elif mode == "join": connection.execute("create temporary table pg_rollup_customers(id integer,name text)") connection.execute("insert into pg_rollup_customers values (1,'a'),(2,'b'),(3,'empty')") - expected = [("a", 2.5, 4, 2.5, 2), ("b", 100.0, 90, 100.0, 1), ("empty", None, None, None, 0)] + # The rollup is the preserved side: both orders without a customer + # row form the NULL group and the customer without orders has none. + expected = [(None, 7.5, 14, 7.5, 2), ("a", 2.5, 4, 2.5, 2), ("b", 100.0, 90, 100.0, 1)] rows = connection.execute(sql).fetchall() assert [ tuple(float(value) if isinstance(value, Decimal) else value for value in row) for row in rows ] == expected + if mode == "join": + # Anchored at the dimension model, every customer is listed and the + # one without orders counts zero. That anchor reads raw rows. + anchored = compile_semantic_input(graph, {**query, "consumption_base_model": "customers"}) + assert "orders_preagg_daily" not in anchored + rows = connection.execute(anchored).fetchall() + assert [row[0] for row in rows] == ["a", "b", "empty"] + assert rows[-1] == ("empty", None, None, None, 0) def test_average_chooses_compatible_count_by_definition_not_name(layer):