Skip to content

feat: add support for create table like #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ fn custom_handlers(node: &Node) -> TokenStream {
tokens.push(TokenProperty::from(Token::Key));
},
protobuf::ConstrType::ConstrForeign => tokens.push(TokenProperty::from(Token::References)),
protobuf::ConstrType::ConstrUnique => tokens.push(TokenProperty::from(Token::Unique)),
_ => panic!("Unknown Constraint {:#?}", n.contype()),
};
if n.options.len() > 0 {
tokens.push(TokenProperty::from(Token::With));
}
},
"PartitionSpec" => quote! {
Expand Down Expand Up @@ -439,6 +443,29 @@ fn custom_handlers(node: &Node) -> TokenStream {
tokens.push(TokenProperty::from(Token::For));
tokens.push(TokenProperty::from(Token::Values));
}
if let Some(n) = &n.relation {
match n.relpersistence.as_str() {
// Unlogged
"u" => tokens.push(TokenProperty::from(Token::Unlogged)),
// Temporary
"t" => tokens.push(TokenProperty::from(Token::Temporary)),
_ => {},
}
if n.inh {
tokens.push(TokenProperty::from(Token::Inherits));
}
}
},
"TableLikeClause" => quote! {
tokens.push(TokenProperty::from(Token::Like));
// CREATE_TABLE_LIKE_ALL
if n.options == 0x7FFFFFFF {
tokens.push(TokenProperty::from(Token::Including));
tokens.push(TokenProperty::from(Token::All));
} else {
tokens.push(TokenProperty::from(Token::Excluding));
tokens.push(TokenProperty::from(Token::All));
}
},
"PartitionBoundSpec" => quote! {
tokens.push(TokenProperty::from(Token::From));
Expand Down
11 changes: 11 additions & 0 deletions crates/parser/tests/data/statements/valid/0043.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE UNLOGGED TABLE cities (name text, population real, altitude double, identifier smallint, postal_code int, foreign_id bigint);
/* TODO: CREATE TABLE IF NOT EXISTS distributors (name varchar(40) DEFAULT 'Luso Films', len interval hour to second(3), name varchar(40) DEFAULT 'Luso Films', did int DEFAULT nextval('distributors_serial'), stamp timestamp DEFAULT now() NOT NULL, stamptz timestamp with time zone, "time" time NOT NULL, timetz time with time zone, CONSTRAINT name_len PRIMARY KEY (name, len)); */ SELECT 1;
/* TODO: CREATE TABLE types (a real, b double precision, c numeric(2, 3), d char(4), e char(5), f varchar(6), g varchar(7)); */ SELECT 1;
Comment on lines +2 to +3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cvng why is this todo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have at least 3 more PRs based on this one and I was trying to keep it smaller

For now each TODO is a failing case with either a parser bug (no panics but produces an incorrect tree) or missing node properties, eg TypeName

The next PRs should be smaller reproduction cases

/* TODO: CREATE TABLE types (a geometry(point) NOT NULL); */ SELECT 1;
CREATE TABLE tablename (colname int NOT NULL DEFAULT nextval('tablename_colname_seq'));
CREATE TABLE capitals (state char(2)) INHERITS (cities);
/* TODO: CREATE TEMPORARY TABLE temp AS SELECT c FROM t; */ SELECT 1;
/* TODO: CREATE TABLE films2 AS SELECT * FROM films; */ SELECT 1;
/* TODO: CREATE TEMPORARY TABLE films_recent ON COMMIT DROP AS SELECT * FROM films WHERE date_prod > $1; */ SELECT 1;
CREATE TABLE like_constraint_rename_cache (LIKE constraint_rename_cache INCLUDING ALL);
CREATE TABLE distributors (did int, name varchar(40), UNIQUE (name) WITH (fillfactor=70)) WITH (fillfactor=70);
412 changes: 412 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0043@1.snap

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions crates/parser/tests/snapshots/statements/valid/0043@10.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
source: crates/parser/tests/statement_parser_test.rs
description: CREATE TABLE like_constraint_rename_cache (LIKE constraint_rename_cache INCLUDING ALL);
---
Parse {
cst: SourceFile@0..87
CreateStmt@0..87
Create@0..6 "CREATE"
Whitespace@6..7 " "
Table@7..12 "TABLE"
Whitespace@12..13 " "
RangeVar@13..41
Ident@13..41 "like_constraint_renam ..."
Whitespace@41..42 " "
Ascii40@42..43 "("
TableLikeClause@43..85
Like@43..47 "LIKE"
Whitespace@47..48 " "
RangeVar@48..71
Ident@48..71 "constraint_rename_cache"
Whitespace@71..72 " "
Including@72..81 "INCLUDING"
Whitespace@81..82 " "
All@82..85 "ALL"
Ascii41@85..86 ")"
Ascii59@86..87 ";"
,
errors: [],
stmts: [
RawStmt {
stmt: CreateStmt(
CreateStmt {
relation: Some(
RangeVar {
catalogname: "",
schemaname: "",
relname: "like_constraint_rename_cache",
inh: true,
relpersistence: "p",
alias: None,
location: 13,
},
),
table_elts: [
Node {
node: Some(
TableLikeClause(
TableLikeClause {
relation: Some(
RangeVar {
catalogname: "",
schemaname: "",
relname: "constraint_rename_cache",
inh: true,
relpersistence: "p",
alias: None,
location: 48,
},
),
options: 2147483647,
relation_oid: 0,
},
),
),
},
],
inh_relations: [],
partbound: None,
partspec: None,
of_typename: None,
constraints: [],
options: [],
oncommit: OncommitNoop,
tablespacename: "",
access_method: "",
if_not_exists: false,
},
),
range: 0..86,
},
],
}
Loading