From 3b24a56130fdf780160bd778fd744f9359a3894b Mon Sep 17 00:00:00 2001 From: Venkatesh Raghavan Date: Wed, 19 Jul 2017 16:20:43 -0700 Subject: [PATCH] Fix rtable index of FunctionScan when translating GPORCA plan. Arguments to the function scan can themselve have a subquery that can create new rtable entries. Therefore, first translate all arguments of the FunctionScan before setting scanrelid of the FunctionScan. --- .../gpopt/translate/CTranslatorDXLToPlStmt.cpp | 11 +++++------ src/test/regress/expected/gporca.out | 11 +++++++++++ src/test/regress/expected/gporca_optimizer.out | 15 +++++++++++++++ src/test/regress/sql/gporca.sql | 8 ++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp b/src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp index 7573c7d007..9d1b0ab449 100644 --- a/src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp +++ b/src/backend/gpopt/translate/CTranslatorDXLToPlStmt.cpp @@ -1852,14 +1852,8 @@ CTranslatorDXLToPlStmt::PplanFunctionScanFromDXLTVF // translation context for column mappings CDXLTranslateContextBaseTable dxltrctxbt(m_pmp); - // we will add the new range table entry as the last element of the range table - Index iRel = gpdb::UlListLength(m_pctxdxltoplstmt->PlPrte()) + 1; - - dxltrctxbt.SetIdx(iRel); - // create function scan node FunctionScan *pfuncscan = MakeNode(FunctionScan); - pfuncscan->scan.scanrelid = iRel; Plan *pplan = &(pfuncscan->scan.plan); RangeTblEntry *prte = PrteFromDXLTVF(pdxlnTVF, pdxltrctxOut, &dxltrctxbt, pplan); @@ -1868,6 +1862,11 @@ CTranslatorDXLToPlStmt::PplanFunctionScanFromDXLTVF pfuncscan->funcexpr = prte->funcexpr; pfuncscan->funccolnames = prte->eref->colnames; + // we will add the new range table entry as the last element of the range table + Index iRel = gpdb::UlListLength(m_pctxdxltoplstmt->PlPrte()) + 1; + dxltrctxbt.SetIdx(iRel); + pfuncscan->scan.scanrelid = iRel; + m_pctxdxltoplstmt->AddRTE(prte); pplan->plan_node_id = m_pctxdxltoplstmt->UlNextPlanId(); diff --git a/src/test/regress/expected/gporca.out b/src/test/regress/expected/gporca.out index 79abec5caf..1e25c53fa2 100644 --- a/src/test/regress/expected/gporca.out +++ b/src/test/regress/expected/gporca.out @@ -10139,6 +10139,17 @@ LOG: statement: create table foo_ctas(a) as (select generate_series(1,10)) dist reset client_min_messages; LOG: statement: reset client_min_messages; reset optimizer_enable_ctas; +-- TVF with a subplan that generates an RTABLE entry +create table bar(name text); +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'name' as the Greenplum Database data distribution key for this table. +HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. +insert into bar values('person'); +select * from unnest((select string_to_array(name, ',') from bar)) as a; + a +-------- + person +(1 row) + -- clean up drop schema orca cascade; NOTICE: drop cascades to table orca.index_test diff --git a/src/test/regress/expected/gporca_optimizer.out b/src/test/regress/expected/gporca_optimizer.out index 38a692ec06..0ae1438f31 100644 --- a/src/test/regress/expected/gporca_optimizer.out +++ b/src/test/regress/expected/gporca_optimizer.out @@ -10198,6 +10198,21 @@ LOG: Planner produced plan :0 reset client_min_messages; LOG: statement: reset client_min_messages; reset optimizer_enable_ctas; +-- start_ignore +drop table bar; +ERROR: table "bar" does not exist +-- end_ignore +-- TVF with a subplan that generates an RTABLE entry +create table bar(name text); +NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'name' as the Greenplum Database data distribution key for this table. +HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. +insert into bar values('person'); +select * from unnest((select string_to_array(name, ',') from bar)) as a; + a +-------- + person +(1 row) + -- clean up drop schema orca cascade; NOTICE: drop cascades to table orca.index_test diff --git a/src/test/regress/sql/gporca.sql b/src/test/regress/sql/gporca.sql index f578b9e474..138a45b042 100644 --- a/src/test/regress/sql/gporca.sql +++ b/src/test/regress/sql/gporca.sql @@ -1404,6 +1404,14 @@ create table foo_ctas(a) as (select generate_series(1,10)) distributed by (a); reset client_min_messages; reset optimizer_enable_ctas; +-- start_ignore +drop table bar; +-- end_ignore +-- TVF with a subplan that generates an RTABLE entry +create table bar(name text); +insert into bar values('person'); +select * from unnest((select string_to_array(name, ',') from bar)) as a; + -- clean up drop schema orca cascade; reset optimizer_segments; -- GitLab