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
2 changes: 1 addition & 1 deletion src/backend/optimizer/README.cbdb.aqumv
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Below are not supported now:
Window Functions
CTE
Distinct (on view_query)
Distinct On
Distinct On (on view_query)
UNION/INTERSECT/EXCEPT
FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, FOR KEY SHARE
Scatter By
Expand Down
2 changes: 1 addition & 1 deletion src/backend/optimizer/plan/aqumv.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ answer_query_using_materialized_views(PlannerInfo *root,
(parse->cteList != NIL) ||
(parse->setOperations != NULL) ||
parse->hasWindowFuncs ||
parse->hasDistinctOn ||
parse->hasModifyingCTE ||
(parse->parentStmtType == PARENTSTMTTYPE_REFRESH_MATVIEW) ||
(parse->parentStmtType == PARENTSTMTTYPE_CTAS) ||
Expand Down Expand Up @@ -328,6 +327,7 @@ answer_query_using_materialized_views(PlannerInfo *root,
* could be computed from viewQuery.
*/
viewQuery->hasAggs = parse->hasAggs;
viewQuery->hasDistinctOn = parse->hasDistinctOn;
/*
* For HAVING quals have aggregations, we have already processed them in
* Aggrefs during aqumv_process_targetlist().
Expand Down
78 changes: 78 additions & 0 deletions src/test/regress/expected/aqumv.out
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,84 @@ select c1, c2, c3, sum(c4) from aqumv_t6 where c1 > 97 group by distinct rollup(
98 | NULL | 100 | 202
(13 rows)

-- DISTINCT ON
set local enable_answer_query_using_materialized_views = off;
explain(costs off, verbose)
select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
QUERY PLAN
-----------------------------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
Output: c1, c2, ((c1 - 1))
Merge Key: ((c1 - 1)), c2
-> Unique
Output: c1, c2, ((c1 - 1))
Group Key: ((c1 - 1))
-> Sort
Output: c1, c2, ((c1 - 1))
Sort Key: ((aqumv_t6.c1 - 1)), aqumv_t6.c2 NULLS FIRST
-> Redistribute Motion 3:3 (slice2; segments: 3)
Output: c1, c2, ((c1 - 1))
Hash Key: ((c1 - 1))
-> Seq Scan on public.aqumv_t6
Output: c1, c2, (c1 - 1)
Filter: (aqumv_t6.c1 > 90)
Settings: enable_answer_query_using_materialized_views = 'off', optimizer = 'off'
Optimizer: Postgres query optimizer
(17 rows)

select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
c1 | c2
-----+------
91 | NULL
92 | 93
93 | 94
94 | 95
95 | 96
96 | 97
97 | 98
98 | 99
99 | 100
100 | 101
(10 rows)

set local enable_answer_query_using_materialized_views = on;
explain(costs off, verbose)
select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
QUERY PLAN
----------------------------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
Output: mc1, mc2, ((mc1 - 1))
Merge Key: ((mc1 - 1)), mc2
-> Unique
Output: mc1, mc2, ((mc1 - 1))
Group Key: ((mc1 - 1))
-> Sort
Output: mc1, mc2, ((mc1 - 1))
Sort Key: ((aqumv_mvt6_0.mc1 - 1)), aqumv_mvt6_0.mc2 NULLS FIRST
-> Redistribute Motion 3:3 (slice2; segments: 3)
Output: mc1, mc2, ((mc1 - 1))
Hash Key: ((mc1 - 1))
-> Seq Scan on public.aqumv_mvt6_0
Output: mc1, mc2, (mc1 - 1)
Settings: enable_answer_query_using_materialized_views = 'on', optimizer = 'off'
Optimizer: Postgres query optimizer
(16 rows)

select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
c1 | c2
-----+------
91 | NULL
92 | 93
93 | 94
94 | 95
95 | 96
96 | 97
97 | 98
98 | 99
99 | 100
100 | 101
(10 rows)

\pset null ''
abort;
reset optimizer;
Expand Down
10 changes: 10 additions & 0 deletions src/test/regress/sql/aqumv.sql
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,16 @@ explain(costs off, verbose)
select c1, c2, c3, sum(c4) from aqumv_t6 where c1 > 97 group by distinct rollup(c1, c2), rollup(c1, c3);
select c1, c2, c3, sum(c4) from aqumv_t6 where c1 > 97 group by distinct rollup(c1, c2), rollup(c1, c3);

-- DISTINCT ON
set local enable_answer_query_using_materialized_views = off;
explain(costs off, verbose)
select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
set local enable_answer_query_using_materialized_views = on;
explain(costs off, verbose)
select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;
select distinct on(c1 - 1) c1, c2 from aqumv_t6 where c1 > 90 order by c1 - 1, c2 nulls first;

\pset null ''
abort;

Expand Down