Skip to content

fix: support create type #118

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 2 commits into from
Feb 8, 2024
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
4 changes: 4 additions & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,9 @@ fn custom_handlers(node: &Node) -> TokenStream {
} }
// if its a list, we handle it in the handler for `List`
},
protobuf::ObjectType::ObjectType => {
tokens.push(TokenProperty::from(Token::TypeP));
},
_ => panic!("Unknown DefineStmt {:#?}", n.kind()),
}
},
Expand Down Expand Up @@ -814,6 +817,7 @@ fn custom_handlers(node: &Node) -> TokenStream {
"CompositeTypeStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
tokens.push(TokenProperty::from(Token::TypeP));
tokens.push(TokenProperty::from(Token::As));
},
"CreatedbStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
Expand Down
14 changes: 14 additions & 0 deletions crates/parser/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,26 @@ mod tests {

#[test]
fn test_create_type() {
test_get_node_properties(
"create type type1",
SyntaxKind::DefineStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::TypeP),
TokenProperty::from("type1".to_string()),
],
)
}

#[test]
fn test_create_composite_type() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: create type as constitute a composite type

test_get_node_properties(
"create type type1 as (attr1 int4, attr2 bool);",
SyntaxKind::CompositeTypeStmt,
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::TypeP),
TokenProperty::from(SyntaxKind::As),
],
)
}
Expand Down
67 changes: 38 additions & 29 deletions crates/parser/src/parse/statement_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,57 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
],
));

// CREATE [ OR REPLACE ] OPERATOR
// CREATE OPERATOR
m.push((
SyntaxKind::DefineStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Optional(SyntaxKind::Or),
SyntaxToken::Optional(SyntaxKind::Replace),
SyntaxToken::Required(SyntaxKind::Operator),
],
));

// CREATE [ OR REPLACE ] TYPE
// CREATE TYPE name
m.push((
SyntaxKind::DefineStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Optional(SyntaxKind::Or),
SyntaxToken::Optional(SyntaxKind::Replace),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
Copy link
Contributor Author

@cvng cvng Jan 30, 2024

Choose a reason for hiding this comment

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

the fix part for 6f8418b is here: DefineStmt was missing SyntaxKind::Ident

otherwise I have removed some illegal tokens for some statements (eg. or replace) based on the docs

],
));

// CREATE TYPE name AS
m.push((
SyntaxKind::CompositeTypeStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
],
));

// CREATE TYPE name AS ENUM
m.push((
SyntaxKind::CreateEnumStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::EnumP),
],
));

// CREATE TYPE name AS RANGE
m.push((
SyntaxKind::CreateRangeStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::Range),
],
));

Expand Down Expand Up @@ -622,28 +654,6 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
],
));

m.push((
SyntaxKind::CreateEnumStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::EnumP),
],
));

m.push((
SyntaxKind::CreateRangeStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::Range),
],
));

m.push((
SyntaxKind::CreateFdwStmt,
&[
Expand Down Expand Up @@ -956,7 +966,6 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
// AlterObjectDependsStmt,
// AlterObjectSchemaStmt,
// AlterOwnerStmt,
// CompositeTypeStmt,
// AlterEnumStmt,
// AlterTsdictionaryStmt,
// AlterTsconfigurationStmt,
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/tests/data/statements/valid/0046.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TYPE type1;
CREATE TYPE type1 AS (attr1 int4, attr2 bool);
CREATE TYPE type1 AS (attr1 int4 COLLATE collation1, attr2 bool);
/* TODO: CREATE TYPE type1 AS (attr1 int4 COLLATE collation1, attr2 bool); */ SELECT 1;
Copy link
Contributor Author

@cvng cvng Jan 30, 2024

Choose a reason for hiding this comment

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

can't get COLLATE to work here, I have tried CollateClause ColumnDef CompositeTypeStmt with no luck

it is obfuscated in the macro stack but we have a hint that it panics in codegen::get_location_internal

CREATE TYPE type1 AS ENUM ('value1', 'value2', 'value3');
CREATE TYPE type1 AS RANGE (subtype = int4);
CREATE TYPE type1 AS RANGE (subtype = int4, receive = receive_func, passedbyvalue);
Expand Down
40 changes: 33 additions & 7 deletions crates/parser/tests/snapshots/statements/valid/0046@1.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@ description: CREATE TYPE type1;
---
Parse {
cst: SourceFile@0..18
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
Ident@12..17 "type1"
Ascii59@17..18 ";"
DefineStmt@0..18
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
Ident@12..17 "type1"
Ascii59@17..18 ";"
,
errors: [],
stmts: [],
stmts: [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🎉

RawStmt {
stmt: DefineStmt(
DefineStmt {
kind: ObjectType,
oldstyle: false,
defnames: [
Node {
node: Some(
String(
String {
sval: "type1",
},
),
),
},
],
args: [],
definition: [],
if_not_exists: false,
replace: false,
},
),
range: 0..17,
},
],
}
161 changes: 141 additions & 20 deletions crates/parser/tests/snapshots/statements/valid/0046@2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,147 @@ description: "CREATE TYPE type1 AS (attr1 int4, attr2 bool);"
---
Parse {
cst: SourceFile@0..46
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
Ident@12..17 "type1"
Whitespace@17..18 " "
As@18..20 "AS"
Whitespace@20..21 " "
Ascii40@21..22 "("
Ident@22..27 "attr1"
Whitespace@27..28 " "
Ident@28..32 "int4"
Ascii44@32..33 ","
Whitespace@33..34 " "
Ident@34..39 "attr2"
Whitespace@39..40 " "
Ident@40..44 "bool"
Ascii41@44..45 ")"
Ascii59@45..46 ";"
CompositeTypeStmt@0..46
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
RangeVar@12..17
Ident@12..17 "type1"
Whitespace@17..18 " "
As@18..20 "AS"
Whitespace@20..21 " "
Ascii40@21..22 "("
ColumnDef@22..32
Ident@22..27 "attr1"
Whitespace@27..28 " "
TypeName@28..32
Ident@28..32 "int4"
Ascii44@32..33 ","
Whitespace@33..34 " "
ColumnDef@34..44
Ident@34..39 "attr2"
Whitespace@39..40 " "
TypeName@40..44
Ident@40..44 "bool"
Ascii41@44..45 ")"
Ascii59@45..46 ";"
,
errors: [],
stmts: [],
stmts: [
RawStmt {
stmt: CompositeTypeStmt(
CompositeTypeStmt {
typevar: Some(
RangeVar {
catalogname: "",
schemaname: "",
relname: "type1",
inh: false,
relpersistence: "p",
alias: None,
location: 12,
},
),
coldeflist: [
Node {
node: Some(
ColumnDef(
ColumnDef {
colname: "attr1",
type_name: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "int4",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 28,
},
),
compression: "",
inhcount: 0,
is_local: true,
is_not_null: false,
is_from_type: false,
storage: "",
raw_default: None,
cooked_default: None,
identity: "",
identity_sequence: None,
generated: "",
coll_clause: None,
coll_oid: 0,
constraints: [],
fdwoptions: [],
location: 22,
},
),
),
},
Node {
node: Some(
ColumnDef(
ColumnDef {
colname: "attr2",
type_name: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "bool",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 40,
},
),
compression: "",
inhcount: 0,
is_local: true,
is_not_null: false,
is_from_type: false,
storage: "",
raw_default: None,
cooked_default: None,
identity: "",
identity_sequence: None,
generated: "",
coll_clause: None,
coll_oid: 0,
constraints: [],
fdwoptions: [],
location: 34,
},
),
),
},
],
},
),
range: 0..45,
},
],
}
Loading