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
9 changes: 4 additions & 5 deletions src/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,10 @@ static ParserExtensionPlanResult PlanMaterializedView(ClientContext &context,
}
CountDistinctExtract cd_extract;
if (ExtractCountDistinctAggregate(local_view_query, candidate_group_columns, output_names, cd_extract)) {
count_distinct_aux_candidate = {
"openivm_aux_" + view_name, SqlUtils::LastIdentifierPart(cd_extract.source),
candidate_group_columns, cd_extract.group_exprs,
cd_extract.distinct_col, cd_extract.distinct_expr,
cd_extract.output_col, cd_extract.filter};
count_distinct_aux_candidate = {"openivm_aux_" + view_name, cd_extract.source,
candidate_group_columns, cd_extract.group_exprs,
cd_extract.distinct_col, cd_extract.distinct_expr,
cd_extract.output_col, cd_extract.filter};
model_input.count_distinct_aux_candidate = &count_distinct_aux_candidate;
} else {
OPENIVM_DEBUG_PRINT("[CREATE MV] COUNT_DISTINCT_INCREMENTAL extractor failed — demoting to "
Expand Down
106 changes: 106 additions & 0 deletions test/sql/count_distinct_aux_schema.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# name: test/sql/count_distinct_aux_schema.test
# description: COUNT(DISTINCT) aux-state MV over a table in a non-default schema
# group: [sql]

require openivm

statement ok
SET openivm_stateful_auxstate = true;

statement ok
CREATE SCHEMA sc;

statement ok
CREATE TABLE sc.a(k INT, v INT);

statement ok
INSERT INTO sc.a VALUES (1, 10), (1, 20);

statement ok
CREATE MATERIALIZED VIEW sc.mv AS SELECT k, COUNT(DISTINCT v) AS d FROM sc.a GROUP BY k;

query II
SELECT * FROM sc.mv;
----
1 2

query T
SELECT refresh_type_name
FROM openivm_compile_with_facts(
'sc.mv',
'{"target_dialect":"duckdb","compile_only":true}'
)
WHERE stmt_kind = 'data'
ORDER BY stmt_order
LIMIT 1;
----
COUNT_DISTINCT_INCREMENTAL

# Same table name in the default schema must not be confused with sc.a.
statement ok
CREATE TABLE a(k INT, v INT);

statement ok
INSERT INTO a VALUES (1, 1), (2, 2), (2, 3);

# Mixed batch: inserts, a duplicate, a delete and a new group.
statement ok
INSERT INTO sc.a VALUES (1, 30), (1, 10), (2, 5);

statement ok
DELETE FROM sc.a WHERE k = 1 AND v = 20;

statement ok
PRAGMA refresh('sc.mv');

query I
SELECT COUNT(*) FROM (
SELECT k, d FROM sc.mv
EXCEPT ALL
SELECT k, COUNT(DISTINCT v) FROM sc.a GROUP BY k
);
----
0

query I
SELECT COUNT(*) FROM (
SELECT k, COUNT(DISTINCT v) FROM sc.a GROUP BY k
EXCEPT ALL
SELECT k, d FROM sc.mv
);
----
0

query II
SELECT k, d FROM sc.mv ORDER BY k;
----
1 2
2 1

# Changes to the default-schema table with the same name are not tracked by sc.mv.
statement ok
INSERT INTO a VALUES (1, 100);

statement ok
PRAGMA refresh('sc.mv');

query I
SELECT COUNT(*) FROM (
SELECT k, d FROM sc.mv
EXCEPT ALL
SELECT k, COUNT(DISTINCT v) FROM sc.a GROUP BY k
);
----
0

query I
SELECT COUNT(*) FROM (
SELECT k, COUNT(DISTINCT v) FROM sc.a GROUP BY k
EXCEPT ALL
SELECT k, d FROM sc.mv
);
----
0

statement ok
SET openivm_stateful_auxstate = false;
Loading