apache / cloudberry

One advanced and mature open-source MPP (Massively Parallel Processing) database. Open source alternative to Greenplum Database.
https://cloudberry.apache.org
Apache License 2.0
433 stars 104 forks source link

CREATE FOREIGN TABLE LIKE. #554

Closed avamingli closed 3 months ago

avamingli commented 3 months ago

Allow user to use LIKE to create foreign tables, ex:

CREATE FOREIGN TABLE ft(LIKE t1);

Foreign table ft will use t1's column definitions as its own.

In CBDB, we usually use foreign tables to import data from outside data source. Each target table usually has an associated foreign table. This is very useful if target table has a lot of columns, making it easy to create a foreign table without defining columns one by one, and avoid mistake.

Distribution policy is not inherited for this case because it should be defined with options by users.

CREATE FOREIGN DATA WRAPPER dummy;
CREATE SERVER s0 FOREIGN DATA WRAPPER dummy;
CREATE TABLE ft_source_table(a INT, b INT, c INT) DISTRIBUTED BY (b);
CREATE FOREIGN TABLE ft_like (LIKE ft_source_table) SERVER s0; 
\d+ ft_like
                                     Foreign table "public.ft_like"
 Column |  Type   | Collation | Nullable | Default | FDW options | Storage | Stats target | Description 
--------+---------+-----------+----------+---------+-------------+---------+--------------+-------------
 a      | integer |           |          |         |             | plain   |              | 
 b      | integer |           |          |         |             | plain   |              | 
 c      | integer |           |          |         |             | plain   |              | 
Server: s0

Authored-by: Zhang Mingli avamingli@gmail.com

fix #ISSUE_Number


Change logs

Describe your change clearly, including what problem is being solved or what feature is being added.

If it has some breaking backward or forward compatibility, please clary.

Why are the changes needed?

Describe why the changes are necessary.

Does this PR introduce any user-facing change?

If yes, please clarify the previous behavior and the change this PR proposes.

How was this patch tested?

Please detail how the changes were tested, including manual tests and any relevant unit or integration tests.

Contributor's Checklist

Here are some reminders and checklists before/when submitting your pull request, please check them: