From b59af1946226f071c346e0d6361cb3a444570f49 Mon Sep 17 00:00:00 2001 From: Laishenghao Date: Fri, 25 Nov 2022 17:54:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90bugfixed=E3=80=91=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E7=89=A9=E5=8C=96=E8=A7=86=E5=9B=BE=E5=AD=98=E5=9C=A8recursive?= =?UTF-8?q?=20=E8=AF=AD=E5=8F=A5=E5=92=8Clevel=E7=AD=89=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=20=E5=AF=BC=E5=87=BA=E7=9A=84=20sql=20?= =?UTF-8?q?=E4=B8=8D=E5=AF=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/nodes/copyfuncs.cpp | 3 ++- src/common/backend/nodes/equalfuncs.cpp | 3 ++- src/common/backend/nodes/outfuncs.cpp | 3 +++ src/common/backend/nodes/readfuncs.cpp | 3 +++ src/common/backend/parser/parse_startwith.cpp | 1 + src/common/backend/utils/adt/ruleutils.cpp | 2 +- src/common/backend/utils/init/globals.cpp | 3 ++- src/include/miscadmin.h | 1 + src/include/nodes/primnodes.h | 1 + .../regress/expected/sw_by_rownum_level.out | 18 ++++++++++++++++++ src/test/regress/sql/sw_by_rownum_level.sql | 10 ++++++++++ 11 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/common/backend/nodes/copyfuncs.cpp b/src/common/backend/nodes/copyfuncs.cpp index 7b43d2a84..fa4468873 100644 --- a/src/common/backend/nodes/copyfuncs.cpp +++ b/src/common/backend/nodes/copyfuncs.cpp @@ -3117,6 +3117,7 @@ static TargetEntry* _copyTargetEntry(const TargetEntry* from) COPY_SCALAR_FIELD(resorigcol); COPY_SCALAR_FIELD(resjunk); COPY_SCALAR_FIELD(rtindex); + COPY_SCALAR_FIELD(isStartWithPseudo); return newnode; } @@ -6086,7 +6087,7 @@ static CreateRoleStmt* _copyCreateRoleStmt(const CreateRoleStmt* from) COPY_SCALAR_FIELD(stmt_type); COPY_STRING_FIELD(role); - COPY_SCALAR_FIELD(missing_ok); + COPY_SCALAR_FIELD(missing_ok); COPY_NODE_FIELD(options); return newnode; diff --git a/src/common/backend/nodes/equalfuncs.cpp b/src/common/backend/nodes/equalfuncs.cpp index aec9e2cab..26a7dd1c6 100644 --- a/src/common/backend/nodes/equalfuncs.cpp +++ b/src/common/backend/nodes/equalfuncs.cpp @@ -664,6 +664,7 @@ static bool _equalTargetEntry(const TargetEntry* a, const TargetEntry* b) COMPARE_SCALAR_FIELD(resorigcol); COMPARE_SCALAR_FIELD(resjunk); COMPARE_SCALAR_FIELD(rtindex); + COMPARE_SCALAR_FIELD(isStartWithPseudo); return true; } @@ -2177,7 +2178,7 @@ static bool _equalCreatePLangStmt(const CreatePLangStmt* a, const CreatePLangStm static bool _equalCreateRoleStmt(const CreateRoleStmt* a, const CreateRoleStmt* b) { COMPARE_SCALAR_FIELD(stmt_type); - COMPARE_SCALAR_FIELD(missing_ok); + COMPARE_SCALAR_FIELD(missing_ok); COMPARE_STRING_FIELD(role); COMPARE_NODE_FIELD(options); diff --git a/src/common/backend/nodes/outfuncs.cpp b/src/common/backend/nodes/outfuncs.cpp index 7b35bd089..642627b3d 100755 --- a/src/common/backend/nodes/outfuncs.cpp +++ b/src/common/backend/nodes/outfuncs.cpp @@ -2849,6 +2849,9 @@ static void _outTargetEntry(StringInfo str, TargetEntry* node) if (t_thrd.proc->workingVersionNum >= MULTI_MODIFY_VERSION_NUM) { WRITE_UINT_FIELD(rtindex); } + if (t_thrd.proc->workingVersionNum >= MAT_VIEW_RECURSIVE_VERSION_NUM) { + WRITE_BOOL_FIELD(isStartWithPseudo); + } } static void _outPseudoTargetEntry(StringInfo str, PseudoTargetEntry* node) diff --git a/src/common/backend/nodes/readfuncs.cpp b/src/common/backend/nodes/readfuncs.cpp index 790c85865..4a0c37495 100755 --- a/src/common/backend/nodes/readfuncs.cpp +++ b/src/common/backend/nodes/readfuncs.cpp @@ -2811,6 +2811,9 @@ static TargetEntry* _readTargetEntry(void) IF_EXIST(rtindex) { READ_UINT_FIELD(rtindex); } + IF_EXIST(isStartWithPseudo) { + READ_BOOL_FIELD(isStartWithPseudo); + } READ_DONE(); } diff --git a/src/common/backend/parser/parse_startwith.cpp b/src/common/backend/parser/parse_startwith.cpp index 15d8d1eff..f2a2d9489 100644 --- a/src/common/backend/parser/parse_startwith.cpp +++ b/src/common/backend/parser/parse_startwith.cpp @@ -1231,6 +1231,7 @@ void AddStartWithCTEPseudoReturnColumns(CommonTableExpr *cte, /* make pseudo return column's TLE */ tle = makeTargetEntry((Expr *)expr, list_length(ctequery->targetList) + 1, att->colname, false); + tle->isStartWithPseudo = true; /* Add the pseudo return column to CTE's target list */ ctequery->targetList = lappend(ctequery->targetList, tle); diff --git a/src/common/backend/utils/adt/ruleutils.cpp b/src/common/backend/utils/adt/ruleutils.cpp index 1b0849a39..f6ba2c239 100644 --- a/src/common/backend/utils/adt/ruleutils.cpp +++ b/src/common/backend/utils/adt/ruleutils.cpp @@ -6272,7 +6272,7 @@ static void get_target_list(Query* query, List* targetList, deparse_context* con continue; /* ignore junk entries */ /* Ignore junk columns from the targetlist in start with */ - if (query->hasRecursive && IsPseudoReturnColumn(tle->resname)) { + if (query->hasRecursive && tle->isStartWithPseudo) { continue; } diff --git a/src/common/backend/utils/init/globals.cpp b/src/common/backend/utils/init/globals.cpp index 16f7b1ecd..52507c9c9 100644 --- a/src/common/backend/utils/init/globals.cpp +++ b/src/common/backend/utils/init/globals.cpp @@ -59,7 +59,7 @@ bool open_join_children = true; bool will_shutdown = false; /* hard-wired binary version number */ -const uint32 GRAND_VERSION_NUM = 92830; +const uint32 GRAND_VERSION_NUM = 92831; const uint32 DOLPHIN_ENABLE_DROP_NUM = 92830; const uint32 SQL_PATCH_VERSION_NUM = 92675; @@ -159,6 +159,7 @@ const uint32 COMMENT_SUPPORT_VERSION_NUM = 92662; const uint32 ON_UPDATE_TIMESTAMP_VERSION_NUM = 92664; const uint32 STANDBY_STMTHIST_VERSION_NUM = 92827; +const uint32 MAT_VIEW_RECURSIVE_VERSION_NUM = 92831; #ifdef PGXC bool useLocalXid = false; diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index f54d0d3b2..46110efef 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -114,6 +114,7 @@ extern const uint32 PLAN_SELECT_VERSION_NUM; extern const uint32 ON_UPDATE_TIMESTAMP_VERSION_NUM; extern const uint32 STANDBY_STMTHIST_VERSION_NUM; extern const uint32 PG_AUTHID_PASSWORDEXT_VERSION_NUM; +extern const uint32 MAT_VIEW_RECURSIVE_VERSION_NUM; extern void register_backend_version(uint32 backend_version); extern bool contain_backend_version(uint32 version_number); diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index e6fa38812..bc5c8845f 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1254,6 +1254,7 @@ typedef struct TargetEntry { * final target list */ Index rtindex; /* used when multiple modifying. It indicates the resultRelation * to which this TLE belongs. */ + bool isStartWithPseudo; } TargetEntry; /* mainly support Start with */ diff --git a/src/test/regress/expected/sw_by_rownum_level.out b/src/test/regress/expected/sw_by_rownum_level.out index 3ac0ef3ab..3bb615659 100644 --- a/src/test/regress/expected/sw_by_rownum_level.out +++ b/src/test/regress/expected/sw_by_rownum_level.out @@ -178,3 +178,21 @@ select * from test_area CONNECT BY APPLE <= LENGTH('SOME TEXT'); ERROR: column "apple" does not exist LINE 2: select * from test_area CONNECT BY APPLE <= LENGTH('SOME TEX... ^ +-- materialized view with column name 'level' 'connect_by_isleaf' 'connect_by_iscycle' +create table startwith_t(id int, level int, connect_by_isleaf int, connect_by_iscycle int); +create materialized view startwith_mv as + WITH RECURSIVE t(id, level, connect_by_isleaf, connect_by_iscycle) as (select * from startwith_t) + select t.id, t.connect_by_isleaf as level, t.level as connect_by_isleaf from t; +select pg_get_viewdef('startwith_mv', true); + pg_get_viewdef +-------------------------------------------------------------------------- + WITH RECURSIVE t(id, level, connect_by_isleaf, connect_by_iscycle) AS (+ + SELECT * + + FROM startwith_t + + ) + + SELECT t.id, t.connect_by_isleaf AS level, t.level AS connect_by_isleaf+ + FROM t; +(1 row) + +drop materialized view startwith_mv; +drop table startwith_t; diff --git a/src/test/regress/sql/sw_by_rownum_level.sql b/src/test/regress/sql/sw_by_rownum_level.sql index e482f11aa..49146143a 100644 --- a/src/test/regress/sql/sw_by_rownum_level.sql +++ b/src/test/regress/sql/sw_by_rownum_level.sql @@ -34,3 +34,13 @@ explain (costs off) select * from test_area CONNECT BY APPLE < LENGTH('SOME TEXT'); explain (costs off) select * from test_area CONNECT BY APPLE <= LENGTH('SOME TEXT'); + +-- materialized view with column name 'level' 'connect_by_isleaf' 'connect_by_iscycle' +create table startwith_t(id int, level int, connect_by_isleaf int, connect_by_iscycle int); +create materialized view startwith_mv as + WITH RECURSIVE t(id, level, connect_by_isleaf, connect_by_iscycle) as (select * from startwith_t) + select t.id, t.connect_by_isleaf as level, t.level as connect_by_isleaf from t; +select pg_get_viewdef('startwith_mv', true); + +drop materialized view startwith_mv; +drop table startwith_t;