From 8a1716ebc038c5721da364fe339759bb8a55fb35 Mon Sep 17 00:00:00 2001 From: wangfeihuo Date: Fri, 24 Nov 2023 11:49:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=A0=87=E9=A2=98=E3=80=91=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DI8HWKW=E6=89=80=E7=A4=BA=E7=9A=84=E6=96=AD?= =?UTF-8?q?=E8=A8=80=E9=97=AE=E9=A2=98.=20=E3=80=90=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E3=80=91:=20=E4=BF=AE=E5=A4=8DI8HWKW?= =?UTF-8?q?=E6=89=80=E7=A4=BA=E7=9A=84=E6=96=AD=E8=A8=80=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20=E3=80=90=E6=A0=B9=E5=9B=A0=E5=88=86=E6=9E=90=E3=80=91:=20?= =?UTF-8?q?=E5=85=B6=E5=AE=9E=E4=B8=BB=E8=A6=81=E6=98=AF=E5=9B=A0=E4=B8=BA?= =?UTF-8?q?between=20and=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E4=BC=9A=E8=AE=BF=E9=97=AE=E4=B8=A4=E6=AC=A1context->?= =?UTF-8?q?refcount++=E7=9A=84=EF=BC=8C=E4=BD=86=E6=98=AF=E4=BA=8B?= =?UTF-8?q?=E5=AE=9E=E4=B8=8A=E5=8F=AA=E8=AE=BF=E9=97=AE=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=EF=BC=8C=E5=9B=A0=E6=AD=A4=E6=9C=80=E5=90=8Econtext->?= =?UTF-8?q?refcount=3D-1=E8=A7=A6=E5=8F=91=E4=BA=86=E6=96=AD=E8=A8=80?= =?UTF-8?q?=E3=80=82=20=E3=80=90=E5=AE=9E=E7=8E=B0=E6=96=B9=E6=A1=88?= =?UTF-8?q?=E3=80=91:=20=E4=BF=AE=E5=A4=8Dbetween=20and=E6=97=B6=E5=80=99?= =?UTF-8?q?=E5=8F=AA=E8=AE=BF=E9=97=AEcontext->refcount=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82=20=E3=80=90=E5=85=B3?= =?UTF-8?q?=E8=81=94=E9=9C=80=E6=B1=82=E6=88=96issue=E3=80=91:=20https://e?= =?UTF-8?q?.gitee.com/opengaussorg/dashboard=3Fissue=3DI8HWKW?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/parser/parse_expr.cpp | 1 + src/gausskernel/optimizer/plan/subselect.cpp | 6 -- src/test/regress/expected/with_002.out | 81 ++++++++++++++++++++ src/test/regress/parallel_schedule0B | 1 + src/test/regress/sql/with_002.sql | 45 +++++++++++ 5 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 src/test/regress/expected/with_002.out create mode 100644 src/test/regress/sql/with_002.sql diff --git a/src/common/backend/parser/parse_expr.cpp b/src/common/backend/parser/parse_expr.cpp index 4c28146a3..c8a07513f 100644 --- a/src/common/backend/parser/parse_expr.cpp +++ b/src/common/backend/parser/parse_expr.cpp @@ -1393,6 +1393,7 @@ static Node* transformAExprOp(ParseState* pstate, A_Expr* a) static Node* transformAExprAnd(ParseState* pstate, A_Expr* a) { + a->rexpr = (Node *)copyObject(a->rexpr); Node* lexpr = transformExprRecurse(pstate, a->lexpr); Node* rexpr = transformExprRecurse(pstate, a->rexpr); diff --git a/src/gausskernel/optimizer/plan/subselect.cpp b/src/gausskernel/optimizer/plan/subselect.cpp index 74a3cfcb2..80d2ea3f1 100644 --- a/src/gausskernel/optimizer/plan/subselect.cpp +++ b/src/gausskernel/optimizer/plan/subselect.cpp @@ -1925,12 +1925,6 @@ static void inline_cte(PlannerInfo *root, CommonTableExpr *cte) (void) inline_cte_walker((Node*)root->parse, &context); - /* - * We would expect the reference number to be zero after inline, however, the - * reference count of cte are not accurate for re-entry issues at parsing stage - * Until fixed, we only check for non-negative refcnt result. - */ - Assert(context.refcount >= 0); /* Mark this CTE as inlined */ cte->cterefcount = -1; } diff --git a/src/test/regress/expected/with_002.out b/src/test/regress/expected/with_002.out new file mode 100644 index 000000000..8db53b931 --- /dev/null +++ b/src/test/regress/expected/with_002.out @@ -0,0 +1,81 @@ +-- create table +create table t_cte(a int, b int); +insert into t_cte select generate_series(1,5), generate_series(1,5); +create table int8_tbl (q1 int8, q2 int8); +INSERT INTO INT8_TBL VALUES + (' 123 ',' 456'); +-- check case where CTE reference is removed due to optimization +EXPLAIN (VERBOSE, COSTS OFF) +SELECT q1 FROM +( + WITH t_cte AS (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + QUERY PLAN +--------------------------------------------- + Subquery Scan on ss + Output: ss.q1 + -> Seq Scan on public.int8_tbl i8 + Output: i8.q1, (SubPlan 2) + CTE t_cte + -> Seq Scan on public.int8_tbl t + Output: t.q1, t.q2 + SubPlan 2 + -> CTE Scan on t_cte + Output: t_cte.q2 + Filter: (t_cte.q1 = i8.q1) +(11 rows) + +SELECT q1 FROM +( + WITH t_cte AS (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + q1 +----- + 123 +(1 row) + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT q1 FROM +( + WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + QUERY PLAN +--------------------------------------------- + Subquery Scan on ss + Output: ss.q1 + -> Seq Scan on public.int8_tbl i8 + Output: i8.q1, (SubPlan 2) + CTE t_cte + -> Seq Scan on public.int8_tbl t + Output: t.q1, t.q2 + SubPlan 2 + -> CTE Scan on t_cte + Output: t_cte.q2 + Filter: (t_cte.q1 = i8.q1) +(11 rows) + +SELECT q1 FROM +( + WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + q1 +----- + 123 +(1 row) + +SELECT ( WITH table2 AS NOT MATERIALIZED ( SELECT 1 ) SELECT 1 FROM ( SELECT ( SELECT 1 FROM table2 ) BETWEEN 1 AND 1 ) AS alias6 ) ; + ?column? +---------- + 1 +(1 row) + +drop table t_cte; +drop table int8_tbl cascade; diff --git a/src/test/regress/parallel_schedule0B b/src/test/regress/parallel_schedule0B index ca26dd1ef..3c20d4eba 100644 --- a/src/test/regress/parallel_schedule0B +++ b/src/test/regress/parallel_schedule0B @@ -328,6 +328,7 @@ test: hw_pct_type_and_rowtype #test: create_basetype #test: tabletype #test with recursive +test: with_002 test: recursive_ref_recursive #test: recursive_prepare #test: recursive_cte diff --git a/src/test/regress/sql/with_002.sql b/src/test/regress/sql/with_002.sql new file mode 100644 index 000000000..467ecae9d --- /dev/null +++ b/src/test/regress/sql/with_002.sql @@ -0,0 +1,45 @@ +-- create table +create table t_cte(a int, b int); +insert into t_cte select generate_series(1,5), generate_series(1,5); + +create table int8_tbl (q1 int8, q2 int8); +INSERT INTO INT8_TBL VALUES + (' 123 ',' 456'); + + +-- check case where CTE reference is removed due to optimization +EXPLAIN (VERBOSE, COSTS OFF) +SELECT q1 FROM +( + WITH t_cte AS (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + +SELECT q1 FROM +( + WITH t_cte AS (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT q1 FROM +( + WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + +SELECT q1 FROM +( + WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t) + SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub + FROM int8_tbl i8 +) ss; + + +SELECT ( WITH table2 AS NOT MATERIALIZED ( SELECT 1 ) SELECT 1 FROM ( SELECT ( SELECT 1 FROM table2 ) BETWEEN 1 AND 1 ) AS alias6 ) ; + +drop table t_cte; +drop table int8_tbl cascade;