Skip to content

Commit

Permalink
Support PostgreSQL CREATE TABLE .. OF type
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 13, 2024
1 parent c8a697f commit e2e394b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
30 changes: 21 additions & 9 deletions src/cst/CreateTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export type AllCreateTableNodes =
| CreateTableTablespaceClause
| CreateTableUsingAccessMethodClause
| CreateTableWithClause
| CreateTableWithoutOidsClause;
| CreateTableWithoutOidsClause
| CreateTableOfTypeClause;

// CREATE TABLE
export interface CreateTableStmt extends BaseNode {
Expand All @@ -63,6 +64,7 @@ export interface CreateTableStmt extends BaseNode {
ifNotExistsKw?: [Keyword<"IF">, Keyword<"NOT">, Keyword<"EXISTS">];
name: EntityName;
partitionOf?: CreateTablePartitionOfClause;
ofType?: CreateTableOfTypeClause;
columns?: ParenExpr<
ListExpr<ColumnDefinition | TableConstraint | Constraint<TableConstraint>>
>;
Expand Down Expand Up @@ -218,14 +220,6 @@ export interface CreateTablePartitionByClause extends BaseNode {
columns: ParenExpr<ListExpr<Expr>>;
}

// PostgreSQL (we're not including this to PostgresqlCreateTableClause because
// it comes right after table name, unlike other clauses that come after the column definitions)
export interface CreateTablePartitionOfClause extends BaseNode {
type: "create_table_partition_of_clause";
partitionOfKw: [Keyword<"PARTITION">, Keyword<"OF">];
table: EntityName;
}

export interface CreateTablePartitionBoundClause extends BaseNode {
type: "create_table_partition_bound_clause";
forValuesKw: [Keyword<"FOR">, Keyword<"VALUES">];
Expand Down Expand Up @@ -318,3 +312,21 @@ export interface CreateTableWithoutOidsClause extends BaseNode {
type: "create_table_without_oids_clause";
withoutOidsKw: [Keyword<"WITHOUT">, Keyword<"OIDS">];
}

// PostgreSQL
//
// We're not including `PARTITION OF` and `OF type` clauses
// PostgresqlCreateTableClause because it comes right after table name,
// unlike other clauses that come after the column definitions.

export interface CreateTablePartitionOfClause extends BaseNode {
type: "create_table_partition_of_clause";
partitionOfKw: [Keyword<"PARTITION">, Keyword<"OF">];
table: EntityName;
}

export interface CreateTableOfTypeClause extends BaseNode {
type: "create_table_of_type_clause";
ofKw: Keyword<"OF">;
typeName: EntityName;
}
13 changes: 12 additions & 1 deletion src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,7 @@ create_table_stmt
ifKw:(__ if_not_exists)?
name:(__ entity_name)
partitionOf:(__ create_table_partition_of_clause)?
ofType:(__ create_table_of_type_clause)?
columns:(__ paren$list$create_definition)?
options:(__ table_options)?
clauses:(__ create_table_clause)*
Expand All @@ -1950,6 +1951,7 @@ create_table_stmt
ifNotExistsKw: read(ifKw),
name: read(name),
partitionOf: read(partitionOf),
ofType: read(ofType),
columns: read(columns),
options: read(options),
clauses: clauses.map(read),
Expand Down Expand Up @@ -1993,14 +1995,23 @@ column_definition
}

create_table_partition_of_clause
= kw:(PARTITION __ OF __) table:entity_name {
= kw:(PARTITION __ OF __) table:entity_name &postgres {
return loc({
type: "create_table_partition_of_clause",
partitionOfKw: read(kw),
table,
});
}

create_table_of_type_clause
= kw:(OF __) typeName:entity_name &postgres {
return loc({
type: "create_table_of_type_clause",
ofKw: read(kw),
typeName,
});
}

create_table_clause
= as_clause$compound_select_stmt
/ (&bigquery / &mysql) x:create_table_like_clause { return x; }
Expand Down
2 changes: 2 additions & 0 deletions src/showNode/create_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const createTableMap: FullTransformMap<string, AllCreateTableNodes> = {
node.ifNotExistsKw,
node.name,
node.partitionOf,
node.ofType,
node.columns,
node.options,
node.clauses,
Expand Down Expand Up @@ -62,4 +63,5 @@ export const createTableMap: FullTransformMap<string, AllCreateTableNodes> = {
show([node.usingKw, node.method]),
create_table_with_clause: (node) => show([node.withKw, node.options]),
create_table_without_oids_clause: (node) => show(node.withoutOidsKw),
create_table_of_type_clause: (node) => show([node.ofKw, node.typeName]),
};
5 changes: 5 additions & 0 deletions test/ddl/create_table_postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ describe("create table (PostgreSQL)", () => {
});
});

it("supports CREATE TABLE .. OF type", () => {
testWc("CREATE TABLE client OF client_type");
testWc("CREATE TABLE foo OF schm.my_type");
});

it("supports ON COMMIT clause", () => {
testClauseWc(`ON COMMIT PRESERVE ROWS`);
testClauseWc(`ON COMMIT DELETE ROWS`);
Expand Down

0 comments on commit e2e394b

Please sign in to comment.