Skip to content

Commit

Permalink
Fix crash with ORCA for partition tables with rewritten rules
Browse files Browse the repository at this point in the history
Earlier, the check for identifying if a table is randomly distributed relied
on the policy to be not null, however if we have rewritten rules on child
partition table, the table storage is marked as RELSTORAGE_VIRTUAL which can
lead to null policy.

Should the table be marked with virtual storage, that's a different issue though.
  • Loading branch information
bhuvnesh2703 committed Dec 5, 2018
1 parent 46dadf4 commit 0483e2a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/backend/utils/cache/lsyscache.c
Expand Up @@ -4084,7 +4084,7 @@ child_distribution_mismatch(Relation rel)
GpPolicy *rootPolicy = rel->rd_cdbpolicy;
Assert(NULL != rootPolicy && "Partitioned tables cannot be master-only");

if (POLICYTYPE_PARTITIONED == rootPolicy->ptype && 0 == rootPolicy->nattrs)
if (GpPolicyIsRandomly(rootPolicy))
{
/* root partition policy already marked as Random, no mismatch possible as
* all children must be random as well */
Expand All @@ -4101,7 +4101,8 @@ child_distribution_mismatch(Relation rel)
Assert(NULL != relChild);

GpPolicy *childPolicy = relChild->rd_cdbpolicy;
if (POLICYTYPE_PARTITIONED == childPolicy->ptype && 0 == childPolicy->nattrs)

if (GpPolicyIsRandomly(childPolicy))
{
/* child partition is Random, and parent is not */
RelationClose(relChild);
Expand Down
14 changes: 14 additions & 0 deletions src/test/regress/expected/gporca.out
Expand Up @@ -10588,3 +10588,17 @@ WITH abc AS (SELECT onetimefilter1.a, onetimefilter1.b FROM onetimefilter1, onet
1 | 0 | 2
(10 rows)

-- queries on partition table with rewritten rule should not crash
-- github issue #5913
create table rewrite_rules (id int, stamp date, amount int)
with (appendonly=false) distributed by (id)
partition by range (stamp) (
start ('2018-01-01') end ('2019-01-01') every(interval '1 year'),
default partition extra
);
NOTICE: CREATE TABLE will create partition "rewrite_rules_1_prt_extra" for table "rewrite_rules"
NOTICE: CREATE TABLE will create partition "rewrite_rules_1_prt_2" for table "rewrite_rules"
create rule "_RETURN" as on select to rewrite_rules_1_prt_2 do instead
select 1 as id, current_date as stamp, 1 as amount;
select * from rewrite_rules;
ERROR: plan contains range table with relstorage='v' (allpaths.c:346)
14 changes: 14 additions & 0 deletions src/test/regress/expected/gporca_optimizer.out
Expand Up @@ -10655,3 +10655,17 @@ WITH abc AS (SELECT onetimefilter1.a, onetimefilter1.b FROM onetimefilter1, onet
1 | 0 | 2
(10 rows)

-- queries on partition table with rewritten rule should not crash
-- github issue #5913
create table rewrite_rules (id int, stamp date, amount int)
with (appendonly=false) distributed by (id)
partition by range (stamp) (
start ('2018-01-01') end ('2019-01-01') every(interval '1 year'),
default partition extra
);
NOTICE: CREATE TABLE will create partition "rewrite_rules_1_prt_extra" for table "rewrite_rules"
NOTICE: CREATE TABLE will create partition "rewrite_rules_1_prt_2" for table "rewrite_rules"
create rule "_RETURN" as on select to rewrite_rules_1_prt_2 do instead
select 1 as id, current_date as stamp, 1 as amount;
select * from rewrite_rules;
ERROR: undefined table type for storage format: v (execScan.c:414)
12 changes: 12 additions & 0 deletions src/test/regress/sql/gporca.sql
Expand Up @@ -1623,6 +1623,18 @@ ANALYZE onetimefilter2;
EXPLAIN WITH abc AS (SELECT onetimefilter1.a, onetimefilter1.b FROM onetimefilter1, onetimefilter2 WHERE onetimefilter1.a=onetimefilter2.a) SELECT (SELECT 1 FROM abc WHERE f1.b = f2.b LIMIT 1), COALESCE((SELECT 2 FROM abc WHERE f1.a=random() AND f1.a=2), 0), (SELECT b FROM abc WHERE b=f1.b) FROM onetimefilter1 f1, onetimefilter2 f2 WHERE f1.b = f2.b;
WITH abc AS (SELECT onetimefilter1.a, onetimefilter1.b FROM onetimefilter1, onetimefilter2 WHERE onetimefilter1.a=onetimefilter2.a) SELECT (SELECT 1 FROM abc WHERE f1.b = f2.b LIMIT 1), COALESCE((SELECT 2 FROM abc WHERE f1.a=random() AND f1.a=2), 0), (SELECT b FROM abc WHERE b=f1.b) FROM onetimefilter1 f1, onetimefilter2 f2 WHERE f1.b = f2.b;

-- queries on partition table with rewritten rule should not crash
-- github issue #5913
create table rewrite_rules (id int, stamp date, amount int)
with (appendonly=false) distributed by (id)
partition by range (stamp) (
start ('2018-01-01') end ('2019-01-01') every(interval '1 year'),
default partition extra
);
create rule "_RETURN" as on select to rewrite_rules_1_prt_2 do instead
select 1 as id, current_date as stamp, 1 as amount;
select * from rewrite_rules;

-- start_ignore
DROP SCHEMA orca CASCADE;
-- end_ignore

0 comments on commit 0483e2a

Please sign in to comment.