This is an automated email from the ASF dual-hosted git repository.

avamingli pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit c70f0a8ade256b3707b7d65bd4d07a1a747c9899
Author: Huansong Fu <[email protected]>
AuthorDate: Thu Sep 29 08:12:30 2022 -0700

    Support ALTER TABLE SET DISTRIBUTED BY for external tables
    
    The external tables have distribution policy but it does not dictate
    actual data distribution. It is only used when unloading data, to
    compare with source table's distribution policy.
    
    Therefore, when supporting ALTER TABLE SET DISTRIBUTED BY for external
    tables, we don't really need to re-organize the table but just need
    to make sure the catalog change happens.
---
 src/backend/commands/tablecmds.c              | 11 +++++++++--
 src/test/regress/input/external_table.source  |  5 +----
 src/test/regress/output/external_table.source | 18 +++++-------------
 3 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 6030e7d8f0..a6706848eb 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5563,7 +5563,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
                                pass = AT_PASS_MISC;
                                break;
                        }
-                       ATSimplePermissions(rel, ATT_TABLE | 
ATT_DIRECTORY_TABLE);
+                       ATSimplePermissions(rel, ATT_TABLE | 
ATT_DIRECTORY_TABLE | ATT_FOREIGN_TABLE);
 
                        if (!recursing) /* MPP-5772, MPP-5784 */
                        {
@@ -18620,6 +18620,12 @@ ATExecSetDistributedBy(Relation rel, Node *node, 
AlterTableCmd *cmd)
 
                        lwith = nlist;
                }
+               /* External tables cannot really be re-organized. Error out if 
we are instructed to do so.*/
+               if (force_reorg && rel_is_external_table(RelationGetRelid(rel)))
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                        errmsg("cannot reorganize external 
table \"%s\"",
+                                                       
RelationGetRelationName(rel))));
 
                if (ldistro)
                        change_policy = true;
@@ -18853,7 +18859,8 @@ ATExecSetDistributedBy(Relation rel, Node *node, 
AlterTableCmd *cmd)
                {
                        need_reorg = true;
                }
-               else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+               else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ||
+                       rel_is_external_table(RelationGetRelid(rel)))
                        need_reorg = false;
                else
                        elog(ERROR, "unexpected relkind '%c'", 
rel->rd_rel->relkind);
diff --git a/src/test/regress/input/external_table.source 
b/src/test/regress/input/external_table.source
index 58c2153421..806dec1d08 100644
--- a/src/test/regress/input/external_table.source
+++ b/src/test/regress/input/external_table.source
@@ -3614,9 +3614,6 @@ ALTER TABLE ext_w_dist SET DISTRIBUTED BY (b);
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'ext_w_dist'::regclass;
 ALTER TABLE ext_w_dist SET DISTRIBUTED RANDOMLY;
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'ext_w_dist'::regclass;
-CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' 
FORMAT 'TEXT' DISTRIBUTED BY (a);
-CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' 
FORMAT 'TEXT';
-ALTER TABLE ext_r_dist SET DISTRIBUTED BY (a); -- should error out altering 
readable external tables' distribution policy
 
 -- Testing external table as the partition child.
 CREATE TABLE part_root(a int) PARTITION BY RANGE(a);
@@ -3629,7 +3626,7 @@ ALTER TABLE part_root ATTACH PARTITION part_ext_w FOR 
VALUES FROM (10) TO (20);
 ALTER TABLE part_root ADD COLUMN b int;
 INSERT INTO part_root SELECT i,i FROM generate_series(1,19)i;
 
--- altering distribution policy should work fine
+-- altering distribution policy should work fine 
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'part_ext_w'::regclass;
 ALTER TABLE part_root SET DISTRIBUTED BY (b);
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'part_ext_w'::regclass;
diff --git a/src/test/regress/output/external_table.source 
b/src/test/regress/output/external_table.source
index 9d538cf3a0..f18f80a08d 100644
--- a/src/test/regress/output/external_table.source
+++ b/src/test/regress/output/external_table.source
@@ -4916,7 +4916,7 @@ DROP TABLE test_part_integrity;
 -- Testing altering the distribution policy of external tables.
 CREATE WRITABLE EXTERNAL WEB TABLE ext_w_dist(a int, b int) EXECUTE 'cat > 
@abs_srcdir@/data/ext_w_dist.tbl' FORMAT 'TEXT' (DELIMITER AS '|' NULL AS 
'null' ESCAPE AS ' ') DISTRIBUTED BY (a);
 ALTER TABLE ext_w_dist SET WITH (reorganize=true); -- should error out if 
forcing reorganize
-ERROR:  "ext_w_dist" is not a table or directory table
+ERROR:  cannot reorganize external table "ext_w_dist"
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'ext_w_dist'::regclass;
  policytype | distkey 
 ------------+---------
@@ -4924,26 +4924,19 @@ SELECT policytype, distkey FROM gp_distribution_policy 
WHERE localoid = 'ext_w_d
 (1 row)
 
 ALTER TABLE ext_w_dist SET DISTRIBUTED BY (b);
-ERROR:  "ext_w_dist" is not a table or directory table
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'ext_w_dist'::regclass;
  policytype | distkey 
 ------------+---------
- p          | 1
+ p          | 2
 (1 row)
 
 ALTER TABLE ext_w_dist SET DISTRIBUTED RANDOMLY;
-ERROR:  "ext_w_dist" is not a table or directory table
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'ext_w_dist'::regclass;
  policytype | distkey 
 ------------+---------
- p          | 1
+ p          | 
 (1 row)
 
-CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' 
FORMAT 'TEXT' DISTRIBUTED BY (a);
-ERROR:  readable external tables can't specify a DISTRIBUTED BY clause
-CREATE EXTERNAL WEB TABLE ext_r_dist(a int) EXECUTE 'printf ${GP_SEGMENT_ID}' 
FORMAT 'TEXT';
-ALTER TABLE ext_r_dist SET DISTRIBUTED BY (a); -- should error out altering 
readable external tables' distribution policy
-ERROR:  "ext_r_dist" is not a table or directory table
 -- Testing external table as the partition child.
 CREATE TABLE part_root(a int) PARTITION BY RANGE(a);
 CREATE TABLE part_child (LIKE part_root);
@@ -4953,7 +4946,7 @@ ALTER TABLE part_root ATTACH PARTITION part_ext_w FOR 
VALUES FROM (10) TO (20);
 -- Adding column should work fine
 ALTER TABLE part_root ADD COLUMN b int;
 INSERT INTO part_root SELECT i,i FROM generate_series(1,19)i;
--- altering distribution policy should work fine
+-- altering distribution policy should work fine 
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'part_ext_w'::regclass;
  policytype | distkey 
 ------------+---------
@@ -4961,11 +4954,10 @@ SELECT policytype, distkey FROM gp_distribution_policy 
WHERE localoid = 'part_ex
 (1 row)
 
 ALTER TABLE part_root SET DISTRIBUTED BY (b);
-ERROR:  "part_ext_w" is not a table or directory table
 SELECT policytype, distkey FROM gp_distribution_policy WHERE localoid = 
'part_ext_w'::regclass;
  policytype | distkey 
 ------------+---------
- p          | 1
+ p          | 2
 (1 row)
 
 DROP TABLE part_root;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to