Fix some comments and error messages

This commit is contained in:
hoshiai 2023-07-26 06:57:15 +09:00
parent c0f8a22fa1
commit 01bc0111f7
4 changed files with 31 additions and 15 deletions

View file

@ -895,8 +895,9 @@ check_ivm_restriction_walker(Node *node, check_ivm_restriction_context *context)
/* /*
* additional restriction checks for exists subquery * additional restriction checks for exists subquery
* *
* When contain EXISTS clauses, and it has a column refernces * If the query has any EXISTS clauses and columns in them refer to
* a table outside, its column must be included by target list. * columns in tables in the output query, those columns must be
* included in the target list.
*/ */
if (context->exists_qual_vars != NIL && context->sublevels_up == 0) if (context->exists_qual_vars != NIL && context->sublevels_up == 0)
{ {
@ -1019,7 +1020,8 @@ check_ivm_restriction_walker(Node *node, check_ivm_restriction_context *context)
case T_Var: case T_Var:
{ {
Var *variable = (Var *) node; Var *variable = (Var *) node;
/* Currently, only EXISTS clause is allowed here. /*
* Currently, only EXISTS clause is allowed here.
* If EXISTS subquery refers to vars of the upper query, collect these vars. * If EXISTS subquery refers to vars of the upper query, collect these vars.
*/ */
if (variable->varlevelsup > 0 && context->in_exists_subquery) if (variable->varlevelsup > 0 && context->in_exists_subquery)
@ -1028,18 +1030,18 @@ check_ivm_restriction_walker(Node *node, check_ivm_restriction_context *context)
} }
case T_SubLink: case T_SubLink:
{ {
/* Now, EXISTS clause is supported only */ /* Currently, EXISTS clause is supported only */
Query *subselect; Query *subselect;
SubLink *sublink = (SubLink *) node; SubLink *sublink = (SubLink *) node;
if (sublink->subLinkType != EXISTS_SUBLINK) if (sublink->subLinkType != EXISTS_SUBLINK)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("this query is not allowed on incrementally maintainable materialized view"), errmsg("this query is not allowed on incrementally maintainable materialized view"),
errhint("subquery in WHERE clause only supports subquery with EXISTS clause"))); errhint("sublink only supports subquery with EXISTS clause in WHERE clause")));
if (context->sublevels_up > 0) if (context->sublevels_up > 0)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("nested subquery is not supported on incrementally maintainable materialized view"))); errmsg("nested sublink is not supported on incrementally maintainable materialized view")));
subselect = (Query *)sublink->subselect; subselect = (Query *)sublink->subselect;
/* raise ERROR if the sublink has CTE */ /* raise ERROR if the sublink has CTE */

View file

@ -827,22 +827,24 @@ SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a, (SELECT
ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view ERROR: DISTINCT clause in nested query are not supported on incrementally maintainable materialized view
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j FROM mv_base_a WHERE i IN (SELECT i FROM mv_base_b WHERE k < 103 )'); SELECT create_immv('mv_ivm_subquery', 'SELECT i,j FROM mv_base_a WHERE i IN (SELECT i FROM mv_base_b WHERE k < 103 )');
ERROR: this query is not allowed on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: subquery in WHERE clause only supports subquery with EXISTS clause HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) FROM mv_base_a a'); SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) FROM mv_base_a a');
ERROR: this query is not allowed on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: subquery in WHERE clause only supports subquery with EXISTS clause HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) + 1 FROM mv_base_a a'); SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMIT 1) + 1 FROM mv_base_a a');
ERROR: this query is not allowed on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: subquery in WHERE clause only supports subquery with EXISTS clause HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v'); SELECT create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v');
ERROR: this query is not allowed on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: subquery in WHERE clause only supports subquery with EXISTS clause HINT: sublink only supports subquery with EXISTS clause in WHERE clause
SELECT create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)'); SELECT create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
ERROR: this query is not allowed on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: targetlist must contain vars that are referred to in EXISTS subquery HINT: targetlist must contain vars that are referred to in EXISTS subquery
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) OR a.i > 2'); SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) OR a.i > 2');
ERROR: this query is not allowed on incrementally maintainable materialized view ERROR: this query is not allowed on incrementally maintainable materialized view
HINT: OR or NOT conditions and EXISTS condition are not used together HINT: OR or NOT conditions and EXISTS condition are not used together
SELECT create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_a a2 WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a2.i = b.i))');
ERROR: nested sublink is not supported on incrementally maintainable materialized view
-- support join subquery in FROM clause -- support join subquery in FROM clause
BEGIN; BEGIN;
SELECT create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) tmp'); SELECT create_immv('mv_ivm_join_subquery', 'SELECT i, j, k FROM ( SELECT i, a.j, b.k FROM mv_base_b b INNER JOIN mv_base_a a USING(i)) tmp');

View file

@ -1587,11 +1587,6 @@ rewrite_query_for_distinct_and_aggregates(Query *query, ParseState *pstate)
return query; return query;
} }
/*
* rewrite_query_for_exists_subquery
*
* Rewrite EXISTS sublink in WHERE to LATERAL subquery
*/
static Query * static Query *
rewrite_exists_subquery_walker(Query *query, Node *node, int *count) rewrite_exists_subquery_walker(Query *query, Node *node, int *count)
{ {
@ -1725,6 +1720,21 @@ rewrite_exists_subquery_walker(Query *query, Node *node, int *count)
return query; return query;
} }
/*
* rewrite_query_for_exists_subquery
*
* Rewrite EXISTS sublink in WHERE to LATERAL subquery
* For example, rewrite
* SELECT t1.* FROM t1
* WHERE EXISTS(SELECT 1 FROM t2 WHERE t1.key = t2.key)
* to
* SELECT t1.*, ex.__ivm_exists_count_0__
* FROM t1, LATERAL(
* SELECT 1, COUNT(*) AS __ivm_exists_count_0__
* FROM t2
* WHERE t1.key = t2.key
* HAVING __ivm_exists_count_0__ > 0) AS ex
*/
Query * Query *
rewrite_query_for_exists_subquery(Query *query) rewrite_query_for_exists_subquery(Query *query)
{ {

View file

@ -270,6 +270,7 @@ SELECT create_immv('mv_ivm_subquery', 'SELECT i,j, (SELECT k FROM mv_base_b LIMI
SELECT create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v'); SELECT create_immv('mv_ivm_subquery', 'SELECT * FROM generate_series(1, (SELECT k FROM mv_base_b LIMIT 1)) AS v');
SELECT create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)'); SELECT create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i)');
SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) OR a.i > 2'); SELECT create_immv('mv_ivm_subquery', 'SELECT a.i,a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a.i = b.i) OR a.i > 2');
SELECT create_immv('mv_ivm_subquery', 'SELECT a.j FROM mv_base_a a WHERE EXISTS(SELECT 1 FROM mv_base_a a2 WHERE EXISTS(SELECT 1 FROM mv_base_b b WHERE a2.i = b.i))');
-- support join subquery in FROM clause -- support join subquery in FROM clause
BEGIN; BEGIN;
@ -379,6 +380,7 @@ ROLLBACK;
--- disallow not-simple CTE --- disallow not-simple CTE
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT i, COUNT(*) FROM mv_base_b GROUP BY i) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT DISTINCT i FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i'); SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT DISTINCT i FROM mv_base_b) SELECT a.i,a.j FROM mv_base_a a, b WHERE a.i = b.i');
SELECT create_immv('mv_cte_fail', 'WITH a AS (SELECT i, j FROM mv_base_a) SELECT a.i,a.j FROM a WHERE EXISTS(WITH b AS (SELECT i FROM mv_base_b) SELECT 1 FROM b WHERE a.i = b.i)');
-- unreferenced CTE -- unreferenced CTE
SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT * FROM mv_base_b) SELECT * FROM mv_base_a a'); SELECT create_immv('mv_cte_fail', 'WITH b AS (SELECT * FROM mv_base_b) SELECT * FROM mv_base_a a');