Skip to content

Commit

Permalink
Add a query check for expressions containing aggregates in it
Browse files Browse the repository at this point in the history
IMMV is not supported for expresion which contains aggragate function 
in targetlist. Previously, There are cases in which column value of
IMMV is incorrect, bacause it was not checked  if it used the above
expression.

GitHub issue #96
  • Loading branch information
yugo-n authored May 14, 2020
1 parent f634713 commit d77ed8e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
10 changes: 8 additions & 2 deletions doc/src/sgml/ref/create_materialized_view.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ a, (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) b WHERE a.i = b.i;
<itemizedlist>
<listitem>
<para>
Aggregations other than count, sum, avg, min and max.
Aggregations other than built-in count, sum, avg, min and max.
</para>
</listitem>
<listitem>
Expand Down Expand Up @@ -277,11 +277,17 @@ ERROR: functions in IMMV must be marked IMMUTABLE
</para>
</listitem>

<listitem>
<para>
IMMVs including expressions which contains aggregates in it
</para>
</listitem>

<listitem>
<para>
IMMVs not supported by logical replication.
</para>
</listitem>
</listitem>

</itemizedlist>

Expand Down
8 changes: 7 additions & 1 deletion doc/src/sgml/rules.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,13 @@ Time: 16386.245 ms (00:16.386)

<listitem>
<para>
targetlist cannot contain hidden columns which name start with <literal>__ivm_</literal>.
targetlist cannot contain hidden columns whose name start with <literal>__ivm_</literal>.
</para>
</listitem>

<listitem>
<para>
targetlist cannot contain expressions which contain an aggregate in it.
</para>
</listitem>

Expand Down
5 changes: 5 additions & 0 deletions src/backend/commands/createas.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "nodes/nodeFuncs.h"
#include "parser/parse_clause.h"
#include "rewrite/rewriteHandler.h"
#include "rewrite/rewriteManip.h"
#include "storage/smgr.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
Expand Down Expand Up @@ -1119,6 +1120,10 @@ check_ivm_restriction_walker(Node *node, check_ivm_restriction_context *ctx, int
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("column name %s is not supported on incrementally maintainable materialized view", tle->resname)));
if (ctx->has_agg && !IsA(tle->expr, Aggref) && contain_aggs_of_level((Node *) tle->expr, 0))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("expression containing an aggregate in it is not supported on incrementally maintainable materialized view")));
check_ivm_restriction_walker((Node *) tle->expr, ctx, depth);
}

Expand Down
9 changes: 7 additions & 2 deletions src/test/regress/expected/incremental_matview.out
Original file line number Diff line number Diff line change
Expand Up @@ -5640,7 +5640,7 @@ CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm19 AS SELECT array_agg(j ORDER BY i
ERROR: aggregate function with ORDER clause is not supported on incrementally maintainable materialized view
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm20 AS SELECT i,SUM(j) FROM mv_base_a GROUP BY GROUPING SETS((i),());
ERROR: GROUPING SETS, ROLLUP, or CUBE clauses is not supported on incrementally maintainable materialized view
-- inheritance parent is not supported with IVM"
-- inheritance parent is not supported
BEGIN;
CREATE TABLE parent (i int, v int);
CREATE TABLE child_a(options text) INHERITS(parent);
Expand All @@ -5667,7 +5667,12 @@ ERROR: column name __ivm_count__ is not supported on incrementally maintainable
-- expressions specified in GROUP BY must appear in the target list.
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm29 AS SELECT COUNT(i) FROM mv_base_a GROUP BY i;
ERROR: GROUP BY expression not appeared in select list is not supported on incrementally maintainable materialized view
-- base table has row level security
-- experssions containing an aggregate is not supported
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm30 AS SELECT sum(i)*0.5 FROM mv_base_a;
ERROR: expression containing an aggregate in it is not supported on incrementally maintainable materialized view
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm31 AS SELECT sum(i)/sum(j) FROM mv_base_a;
ERROR: expression containing an aggregate in it is not supported on incrementally maintainable materialized view
-- base table which has row level security
DROP USER IF EXISTS ivm_admin;
NOTICE: role "ivm_admin" does not exist, skipping
DROP USER IF EXISTS ivm_user;
Expand Down
8 changes: 6 additions & 2 deletions src/test/regress/sql/incremental_matview.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm18 AS SELECT COUNT(DISTINCT i) FROM
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm19 AS SELECT array_agg(j ORDER BY i DESC) FROM mv_base_a;
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm20 AS SELECT i,SUM(j) FROM mv_base_a GROUP BY GROUPING SETS((i),());

-- inheritance parent is not supported with IVM"
-- inheritance parent is not supported
BEGIN;
CREATE TABLE parent (i int, v int);
CREATE TABLE child_a(options text) INHERITS(parent);
Expand All @@ -1655,7 +1655,11 @@ CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm28 AS SELECT i AS "__ivm_count__" FR
-- expressions specified in GROUP BY must appear in the target list.
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm29 AS SELECT COUNT(i) FROM mv_base_a GROUP BY i;

-- base table has row level security
-- experssions containing an aggregate is not supported
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm30 AS SELECT sum(i)*0.5 FROM mv_base_a;
CREATE INCREMENTAL MATERIALIZED VIEW mv_ivm31 AS SELECT sum(i)/sum(j) FROM mv_base_a;

-- base table which has row level security
DROP USER IF EXISTS ivm_admin;
DROP USER IF EXISTS ivm_user;
CREATE USER ivm_admin;
Expand Down

0 comments on commit d77ed8e

Please sign in to comment.