Skip to content
Merged
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
37 changes: 20 additions & 17 deletions tests/semantic_conformance/test_complete_filters_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,32 @@ 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")
expected = [("b", 0.375, None, 0.375, None, 2)]
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)")
Expand All @@ -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"])
Expand Down
17 changes: 16 additions & 1 deletion tests/semantic_conformance/test_rollup_reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
)
Expand All @@ -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):
Expand Down
Loading