Skip to content

Commit

Permalink
Disallow SUBPARTITION BY clause on empty partition
Browse files Browse the repository at this point in the history
We should not allow subpartition by clause when creating empty partition
hierarchy with modern syntax (CREATE PARTITION BY ... without creating
any partitions and then later on adding partitions by CREATE TABLE
PARTITION OF...).

Example:
CREATE TABLE foo(i int, j int) PARTITION BY range(i) SUBPARTITION BY
range(j);
Here the info about subpart by j is lost.

This is because the info in the subpartition by clause is currently
lost. Partitions attached henceforth don't obey the partition policy of
the top level. So, ban it outright - it is a bad mixture of upstream and
legacy syntax.
  • Loading branch information
divyeshddv committed Dec 5, 2023
1 parent 507b0b5 commit 4b2de78
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/backend/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -5461,6 +5461,10 @@ OptFirstPartitionSpec: PartitionSpec opt_list_subparts OptTabPartitionSpec
if ($1->gpPartDef)
check_expressions_in_partition_key($1, yyscanner);
$$ = $1;
/* Do not allow SUBPARTITION BY clause for empty partition hierarchy */
if (!$1->gpPartDef && $1->subPartSpec)
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("SUBPARTITION BY clause is not allowed when no partitions specified at depth 1")));

pg_yyget_extra(yyscanner)->tail_partition_magic = true;
}
Expand Down Expand Up @@ -5488,6 +5492,12 @@ OptSecondPartitionSpec:
*/
if (n->gpPartDef)
check_expressions_in_partition_key(n, yyscanner);

/* Do not allow SUBPARTITION BY clause for empty partition hierarchy */
if (!n->gpPartDef && n->subPartSpec)
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("SUBPARTITION BY clause is not allowed when no partitions specified at depth 1")));

$$ = n;

pg_yyget_extra(yyscanner)->tail_partition_magic = false;
Expand Down
15 changes: 15 additions & 0 deletions src/test/regress/expected/partition.out
Original file line number Diff line number Diff line change
Expand Up @@ -6572,3 +6572,18 @@ SELECT level, pg_get_expr(template, relid, true) FROM gp_partition_template WHER
(1 row)

DROP TABLE public.logs_issue_16558;
-- We should not allow subpartition by clause when creating empty partition hierarchy
-- Should error out
CREATE TABLE empty_partition_disallow_subpartition(i int, j int)
PARTITION BY range(i) SUBPARTITION BY range(j);
ERROR: SUBPARTITION BY clause is not allowed when no partitions specified at depth 1
-- Check with other Partition syntax
CREATE TABLE empty_partition_disallow_subpartition_2(i int, j int)
DISTRIBUTED BY (i) PARTITION BY range(i) SUBPARTITION BY range(j);
ERROR: SUBPARTITION BY clause is not allowed when no partitions specified at depth 1
-- Should work fine for empty hierarchy when subpartition is not specified
CREATE TABLE empty_partition(i int, j int) PARTITION BY range(i);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' 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.
-- Check with other Partition syntax
CREATE TABLE empty_partition2(i int, j int) DISTRIBUTED BY (i) PARTITION BY range(i);
15 changes: 15 additions & 0 deletions src/test/regress/expected/partition_optimizer.out
Original file line number Diff line number Diff line change
Expand Up @@ -6543,3 +6543,18 @@ SELECT level, pg_get_expr(template, relid, true) FROM gp_partition_template WHER
(1 row)

DROP TABLE public.logs_issue_16558;
-- We should not allow subpartition by clause when creating empty partition hierarchy
-- Should error out
CREATE TABLE empty_partition_disallow_subpartition(i int, j int)
PARTITION BY range(i) SUBPARTITION BY range(j);
ERROR: SUBPARTITION BY clause is not allowed when no partitions specified at depth 1
-- Check with other Partition syntax
CREATE TABLE empty_partition_disallow_subpartition_2(i int, j int)
DISTRIBUTED BY (i) PARTITION BY range(i) SUBPARTITION BY range(j);
ERROR: SUBPARTITION BY clause is not allowed when no partitions specified at depth 1
-- Should work fine for empty hierarchy when subpartition is not specified
CREATE TABLE empty_partition(i int, j int) PARTITION BY range(i);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' 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.
-- Check with other Partition syntax
CREATE TABLE empty_partition2(i int, j int) DISTRIBUTED BY (i) PARTITION BY range(i);
14 changes: 14 additions & 0 deletions src/test/regress/sql/partition.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4174,3 +4174,17 @@ ALTER TABLE public.logs_issue_16558 SET SUBPARTITION TEMPLATE
SELECT level, pg_get_expr(template, relid, true) FROM gp_partition_template WHERE relid = 'public.logs_issue_16558'::regclass ORDER BY 1 DESC;
DROP TABLE public.logs_issue_16558;

-- We should not allow subpartition by clause when creating empty partition hierarchy
-- Should error out
CREATE TABLE empty_partition_disallow_subpartition(i int, j int)
PARTITION BY range(i) SUBPARTITION BY range(j);

-- Check with other Partition syntax
CREATE TABLE empty_partition_disallow_subpartition_2(i int, j int)
DISTRIBUTED BY (i) PARTITION BY range(i) SUBPARTITION BY range(j);

-- Should work fine for empty hierarchy when subpartition is not specified
CREATE TABLE empty_partition(i int, j int) PARTITION BY range(i);

-- Check with other Partition syntax
CREATE TABLE empty_partition2(i int, j int) DISTRIBUTED BY (i) PARTITION BY range(i);

0 comments on commit 4b2de78

Please sign in to comment.