Skip to content

Commit

Permalink
feat: add CreateMode for CTAS in WriteRel (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
scgkiran authored Nov 5, 2024
1 parent a0a10bb commit 2e13d0b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 10 additions & 0 deletions proto/substrait/algebra.proto
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ message WriteRel {
// occur in case of DELETE to ensure multi-engine plans are unequivocal.
Rel input = 5;

CreateMode create_mode = 8; // Used with CTAS to determine what to do if the table already exists

// Output mode determines what is the output of executing this rel
OutputMode output = 6;
RelCommon common = 7;
Expand All @@ -641,6 +643,14 @@ message WriteRel {
WRITE_OP_CTAS = 4;
}

enum CreateMode {
CREATE_MODE_UNSPECIFIED = 0;
CREATE_MODE_APPEND_IF_EXISTS = 1; // Append the data to the table if it already exists
CREATE_MODE_REPLACE_IF_EXISTS = 2; // Replace the table if it already exists ("OR REPLACE")
CREATE_MODE_IGNORE_IF_EXISTS = 3; // Ignore the request if the table already exists ("IF NOT EXISTS")
CREATE_MODE_ERROR_IF_EXISTS = 4; // Throw an error if the table already exists (default behavior)
}

enum OutputMode {
OUTPUT_MODE_UNSPECIFIED = 0;
// return no records at all
Expand Down
15 changes: 8 additions & 7 deletions site/docs/relations/logical_relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,14 @@ The write operator is an operator that consumes one input and writes it to stora
### Write Properties


| Property | Description | Required |
|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------|
| Write Type | Definition of which object we are operating on (e.g., a fully-qualified table name). | Required |
| CTAS Schema | The names of all the columns and their type for a CREATE TABLE AS. | Required only for CTAS |
| Write Operator | Which type of operation we are performing (INSERT/DELETE/UPDATE/CTAS). | Required |
| Rel Input | The Rel representing which records we will be operating on (e.g., VALUES for an INSERT, or which records to DELETE, or records and after-image of their values for UPDATE). | Required |
| Output Mode | For views that modify a DB it is important to control which records to "return". Common default is NO_OUTPUT where we return nothing. Alternatively, we can return MODIFIED_RECORDS, that can be further manipulated by layering more rels ontop of this WriteRel (e.g., to "count how many records were updated"). This also allows to return the after-image of the change. To return before-image (or both) one can use the reference mechanisms and have multiple return values. | Required for VIEW CREATE/CREATE_OR_REPLACE/ALTER |
| Property | Description | Required |
|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------|
| Write Type | Definition of which object we are operating on (e.g., a fully-qualified table name). | Required |
| CTAS Schema | The names of all the columns and their type for a CREATE TABLE AS. | Required only for CTAS |
| Write Operator | Which type of operation we are performing (INSERT/DELETE/UPDATE/CTAS). | Required |
| Rel Input | The Rel representing which records we will be operating on (e.g., VALUES for an INSERT, or which records to DELETE, or records and after-image of their values for UPDATE). | Required |
| Create Mode | This determines what should happen if the table already exists (ERROR/REPLACE/IGNORE) | Required only for CTAS |
| Output Mode | For views that modify a DB it is important to control which records to "return". Common default is NO_OUTPUT where we return nothing. Alternatively, we can return MODIFIED_RECORDS, that can be further manipulated by layering more rels ontop of this WriteRel (e.g., to "count how many records were updated"). This also allows to return the after-image of the change. To return before-image (or both) one can use the reference mechanisms and have multiple return values. | Required for VIEW CREATE/CREATE_OR_REPLACE/ALTER |


### Write Definition Types
Expand Down

0 comments on commit 2e13d0b

Please sign in to comment.