Skip to content

Commit efb2e9f

Browse files
authored
Merge pull request #118 from cvng/fix/define-stmt
fix: support `create type`
2 parents 1cdf1da + b1ab67e commit efb2e9f

File tree

9 files changed

+584
-130
lines changed

9 files changed

+584
-130
lines changed

crates/codegen/src/get_node_properties.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ fn custom_handlers(node: &Node) -> TokenStream {
630630
} }
631631
// if its a list, we handle it in the handler for `List`
632632
},
633+
protobuf::ObjectType::ObjectType => {
634+
tokens.push(TokenProperty::from(Token::TypeP));
635+
},
633636
_ => panic!("Unknown DefineStmt {:#?}", n.kind()),
634637
}
635638
},
@@ -814,6 +817,7 @@ fn custom_handlers(node: &Node) -> TokenStream {
814817
"CompositeTypeStmt" => quote! {
815818
tokens.push(TokenProperty::from(Token::Create));
816819
tokens.push(TokenProperty::from(Token::TypeP));
820+
tokens.push(TokenProperty::from(Token::As));
817821
},
818822
"CreatedbStmt" => quote! {
819823
tokens.push(TokenProperty::from(Token::Create));

crates/parser/src/codegen.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,26 @@ mod tests {
284284

285285
#[test]
286286
fn test_create_type() {
287+
test_get_node_properties(
288+
"create type type1",
289+
SyntaxKind::DefineStmt,
290+
vec![
291+
TokenProperty::from(SyntaxKind::Create),
292+
TokenProperty::from(SyntaxKind::TypeP),
293+
TokenProperty::from("type1".to_string()),
294+
],
295+
)
296+
}
297+
298+
#[test]
299+
fn test_create_composite_type() {
287300
test_get_node_properties(
288301
"create type type1 as (attr1 int4, attr2 bool);",
289302
SyntaxKind::CompositeTypeStmt,
290303
vec![
291304
TokenProperty::from(SyntaxKind::Create),
292305
TokenProperty::from(SyntaxKind::TypeP),
306+
TokenProperty::from(SyntaxKind::As),
293307
],
294308
)
295309
}

crates/parser/src/parse/statement_start.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -191,25 +191,57 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
191191
],
192192
));
193193

194-
// CREATE [ OR REPLACE ] OPERATOR
194+
// CREATE OPERATOR
195195
m.push((
196196
SyntaxKind::DefineStmt,
197197
&[
198198
SyntaxToken::Required(SyntaxKind::Create),
199-
SyntaxToken::Optional(SyntaxKind::Or),
200-
SyntaxToken::Optional(SyntaxKind::Replace),
201199
SyntaxToken::Required(SyntaxKind::Operator),
202200
],
203201
));
204202

205-
// CREATE [ OR REPLACE ] TYPE
203+
// CREATE TYPE name
206204
m.push((
207205
SyntaxKind::DefineStmt,
208206
&[
209207
SyntaxToken::Required(SyntaxKind::Create),
210-
SyntaxToken::Optional(SyntaxKind::Or),
211-
SyntaxToken::Optional(SyntaxKind::Replace),
212208
SyntaxToken::Required(SyntaxKind::TypeP),
209+
SyntaxToken::Required(SyntaxKind::Ident),
210+
],
211+
));
212+
213+
// CREATE TYPE name AS
214+
m.push((
215+
SyntaxKind::CompositeTypeStmt,
216+
&[
217+
SyntaxToken::Required(SyntaxKind::Create),
218+
SyntaxToken::Required(SyntaxKind::TypeP),
219+
SyntaxToken::Required(SyntaxKind::Ident),
220+
SyntaxToken::Required(SyntaxKind::As),
221+
],
222+
));
223+
224+
// CREATE TYPE name AS ENUM
225+
m.push((
226+
SyntaxKind::CreateEnumStmt,
227+
&[
228+
SyntaxToken::Required(SyntaxKind::Create),
229+
SyntaxToken::Required(SyntaxKind::TypeP),
230+
SyntaxToken::Required(SyntaxKind::Ident),
231+
SyntaxToken::Required(SyntaxKind::As),
232+
SyntaxToken::Required(SyntaxKind::EnumP),
233+
],
234+
));
235+
236+
// CREATE TYPE name AS RANGE
237+
m.push((
238+
SyntaxKind::CreateRangeStmt,
239+
&[
240+
SyntaxToken::Required(SyntaxKind::Create),
241+
SyntaxToken::Required(SyntaxKind::TypeP),
242+
SyntaxToken::Required(SyntaxKind::Ident),
243+
SyntaxToken::Required(SyntaxKind::As),
244+
SyntaxToken::Required(SyntaxKind::Range),
213245
],
214246
));
215247

@@ -622,28 +654,6 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
622654
],
623655
));
624656

625-
m.push((
626-
SyntaxKind::CreateEnumStmt,
627-
&[
628-
SyntaxToken::Required(SyntaxKind::Create),
629-
SyntaxToken::Required(SyntaxKind::TypeP),
630-
SyntaxToken::Required(SyntaxKind::Ident),
631-
SyntaxToken::Required(SyntaxKind::As),
632-
SyntaxToken::Required(SyntaxKind::EnumP),
633-
],
634-
));
635-
636-
m.push((
637-
SyntaxKind::CreateRangeStmt,
638-
&[
639-
SyntaxToken::Required(SyntaxKind::Create),
640-
SyntaxToken::Required(SyntaxKind::TypeP),
641-
SyntaxToken::Required(SyntaxKind::Ident),
642-
SyntaxToken::Required(SyntaxKind::As),
643-
SyntaxToken::Required(SyntaxKind::Range),
644-
],
645-
));
646-
647657
m.push((
648658
SyntaxKind::CreateFdwStmt,
649659
&[
@@ -956,7 +966,6 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
956966
// AlterObjectDependsStmt,
957967
// AlterObjectSchemaStmt,
958968
// AlterOwnerStmt,
959-
// CompositeTypeStmt,
960969
// AlterEnumStmt,
961970
// AlterTsdictionaryStmt,
962971
// AlterTsconfigurationStmt,

crates/parser/tests/data/statements/valid/0046.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CREATE TYPE type1;
22
CREATE TYPE type1 AS (attr1 int4, attr2 bool);
3-
CREATE TYPE type1 AS (attr1 int4 COLLATE collation1, attr2 bool);
3+
/* TODO: CREATE TYPE type1 AS (attr1 int4 COLLATE collation1, attr2 bool); */ SELECT 1;
44
CREATE TYPE type1 AS ENUM ('value1', 'value2', 'value3');
55
CREATE TYPE type1 AS RANGE (subtype = int4);
66
CREATE TYPE type1 AS RANGE (subtype = int4, receive = receive_func, passedbyvalue);

crates/parser/tests/snapshots/statements/valid/0046@1.snap

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,39 @@ description: CREATE TYPE type1;
44
---
55
Parse {
66
cst: SourceFile@0..18
7-
Create@0..6 "CREATE"
8-
Whitespace@6..7 " "
9-
TypeP@7..11 "TYPE"
10-
Whitespace@11..12 " "
11-
Ident@12..17 "type1"
12-
Ascii59@17..18 ";"
7+
DefineStmt@0..18
8+
Create@0..6 "CREATE"
9+
Whitespace@6..7 " "
10+
TypeP@7..11 "TYPE"
11+
Whitespace@11..12 " "
12+
Ident@12..17 "type1"
13+
Ascii59@17..18 ";"
1314
,
1415
errors: [],
15-
stmts: [],
16+
stmts: [
17+
RawStmt {
18+
stmt: DefineStmt(
19+
DefineStmt {
20+
kind: ObjectType,
21+
oldstyle: false,
22+
defnames: [
23+
Node {
24+
node: Some(
25+
String(
26+
String {
27+
sval: "type1",
28+
},
29+
),
30+
),
31+
},
32+
],
33+
args: [],
34+
definition: [],
35+
if_not_exists: false,
36+
replace: false,
37+
},
38+
),
39+
range: 0..17,
40+
},
41+
],
1642
}

crates/parser/tests/snapshots/statements/valid/0046@2.snap

Lines changed: 141 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,147 @@ description: "CREATE TYPE type1 AS (attr1 int4, attr2 bool);"
44
---
55
Parse {
66
cst: SourceFile@0..46
7-
Create@0..6 "CREATE"
8-
Whitespace@6..7 " "
9-
TypeP@7..11 "TYPE"
10-
Whitespace@11..12 " "
11-
Ident@12..17 "type1"
12-
Whitespace@17..18 " "
13-
As@18..20 "AS"
14-
Whitespace@20..21 " "
15-
Ascii40@21..22 "("
16-
Ident@22..27 "attr1"
17-
Whitespace@27..28 " "
18-
Ident@28..32 "int4"
19-
Ascii44@32..33 ","
20-
Whitespace@33..34 " "
21-
Ident@34..39 "attr2"
22-
Whitespace@39..40 " "
23-
Ident@40..44 "bool"
24-
Ascii41@44..45 ")"
25-
Ascii59@45..46 ";"
7+
CompositeTypeStmt@0..46
8+
Create@0..6 "CREATE"
9+
Whitespace@6..7 " "
10+
TypeP@7..11 "TYPE"
11+
Whitespace@11..12 " "
12+
RangeVar@12..17
13+
Ident@12..17 "type1"
14+
Whitespace@17..18 " "
15+
As@18..20 "AS"
16+
Whitespace@20..21 " "
17+
Ascii40@21..22 "("
18+
ColumnDef@22..32
19+
Ident@22..27 "attr1"
20+
Whitespace@27..28 " "
21+
TypeName@28..32
22+
Ident@28..32 "int4"
23+
Ascii44@32..33 ","
24+
Whitespace@33..34 " "
25+
ColumnDef@34..44
26+
Ident@34..39 "attr2"
27+
Whitespace@39..40 " "
28+
TypeName@40..44
29+
Ident@40..44 "bool"
30+
Ascii41@44..45 ")"
31+
Ascii59@45..46 ";"
2632
,
2733
errors: [],
28-
stmts: [],
34+
stmts: [
35+
RawStmt {
36+
stmt: CompositeTypeStmt(
37+
CompositeTypeStmt {
38+
typevar: Some(
39+
RangeVar {
40+
catalogname: "",
41+
schemaname: "",
42+
relname: "type1",
43+
inh: false,
44+
relpersistence: "p",
45+
alias: None,
46+
location: 12,
47+
},
48+
),
49+
coldeflist: [
50+
Node {
51+
node: Some(
52+
ColumnDef(
53+
ColumnDef {
54+
colname: "attr1",
55+
type_name: Some(
56+
TypeName {
57+
names: [
58+
Node {
59+
node: Some(
60+
String(
61+
String {
62+
sval: "int4",
63+
},
64+
),
65+
),
66+
},
67+
],
68+
type_oid: 0,
69+
setof: false,
70+
pct_type: false,
71+
typmods: [],
72+
typemod: -1,
73+
array_bounds: [],
74+
location: 28,
75+
},
76+
),
77+
compression: "",
78+
inhcount: 0,
79+
is_local: true,
80+
is_not_null: false,
81+
is_from_type: false,
82+
storage: "",
83+
raw_default: None,
84+
cooked_default: None,
85+
identity: "",
86+
identity_sequence: None,
87+
generated: "",
88+
coll_clause: None,
89+
coll_oid: 0,
90+
constraints: [],
91+
fdwoptions: [],
92+
location: 22,
93+
},
94+
),
95+
),
96+
},
97+
Node {
98+
node: Some(
99+
ColumnDef(
100+
ColumnDef {
101+
colname: "attr2",
102+
type_name: Some(
103+
TypeName {
104+
names: [
105+
Node {
106+
node: Some(
107+
String(
108+
String {
109+
sval: "bool",
110+
},
111+
),
112+
),
113+
},
114+
],
115+
type_oid: 0,
116+
setof: false,
117+
pct_type: false,
118+
typmods: [],
119+
typemod: -1,
120+
array_bounds: [],
121+
location: 40,
122+
},
123+
),
124+
compression: "",
125+
inhcount: 0,
126+
is_local: true,
127+
is_not_null: false,
128+
is_from_type: false,
129+
storage: "",
130+
raw_default: None,
131+
cooked_default: None,
132+
identity: "",
133+
identity_sequence: None,
134+
generated: "",
135+
coll_clause: None,
136+
coll_oid: 0,
137+
constraints: [],
138+
fdwoptions: [],
139+
location: 34,
140+
},
141+
),
142+
),
143+
},
144+
],
145+
},
146+
),
147+
range: 0..45,
148+
},
149+
],
29150
}

0 commit comments

Comments
 (0)