Skip to content

Commit 8e6c862

Browse files
committed
Fix: Generic supports CREATE TYPE AS ENUM
1 parent 68c41a9 commit 8e6c862

File tree

3 files changed

+21
-54
lines changed

3 files changed

+21
-54
lines changed

src/dialect/postgresql.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
// limitations under the License.
2929
use log::debug;
3030

31-
use crate::ast::{ObjectName, Statement, UserDefinedTypeRepresentation};
3231
use crate::dialect::{Dialect, Precedence};
3332
use crate::keywords::Keyword;
3433
use crate::parser::{Parser, ParserError};
@@ -135,15 +134,6 @@ impl Dialect for PostgreSqlDialect {
135134
}
136135
}
137136

138-
fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
139-
if parser.parse_keyword(Keyword::CREATE) {
140-
parser.prev_token(); // unconsume the CREATE in case we don't end up parsing anything
141-
parse_create(parser)
142-
} else {
143-
None
144-
}
145-
}
146-
147137
fn supports_filter_during_aggregation(&self) -> bool {
148138
true
149139
}
@@ -259,37 +249,3 @@ impl Dialect for PostgreSqlDialect {
259249
true
260250
}
261251
}
262-
263-
pub fn parse_create(parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
264-
let name = parser.maybe_parse(|parser| -> Result<ObjectName, ParserError> {
265-
parser.expect_keyword_is(Keyword::CREATE)?;
266-
parser.expect_keyword_is(Keyword::TYPE)?;
267-
let name = parser.parse_object_name(false)?;
268-
parser.expect_keyword_is(Keyword::AS)?;
269-
parser.expect_keyword_is(Keyword::ENUM)?;
270-
Ok(name)
271-
});
272-
273-
match name {
274-
Ok(name) => name.map(|name| parse_create_type_as_enum(parser, name)),
275-
Err(e) => Some(Err(e)),
276-
}
277-
}
278-
279-
// https://www.postgresql.org/docs/current/sql-createtype.html
280-
pub fn parse_create_type_as_enum(
281-
parser: &mut Parser,
282-
name: ObjectName,
283-
) -> Result<Statement, ParserError> {
284-
if !parser.consume_token(&Token::LParen) {
285-
return parser.expected("'(' after CREATE TYPE AS ENUM", parser.peek_token());
286-
}
287-
288-
let labels = parser.parse_comma_separated0(|p| p.parse_identifier(), Token::RParen)?;
289-
parser.expect_token(&Token::RParen)?;
290-
291-
Ok(Statement::CreateType {
292-
name,
293-
representation: UserDefinedTypeRepresentation::Enum { labels },
294-
})
295-
}

src/parser/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14222,6 +14222,10 @@ impl<'a> Parser<'a> {
1422214222
let name = self.parse_object_name(false)?;
1422314223
self.expect_keyword_is(Keyword::AS)?;
1422414224

14225+
if self.parse_keyword(Keyword::ENUM) {
14226+
return self.parse_create_type_enum(name);
14227+
}
14228+
1422514229
let mut attributes = vec![];
1422614230
if !self.consume_token(&Token::LParen) || self.consume_token(&Token::RParen) {
1422714231
return Ok(Statement::CreateType {
@@ -14258,6 +14262,20 @@ impl<'a> Parser<'a> {
1425814262
})
1425914263
}
1426014264

14265+
/// Parse remainder of `CREATE TYPE AS ENUM` statement (see [Statement::CreateType] and [Self::parse_create_type])
14266+
///
14267+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createtype.html)
14268+
pub fn parse_create_type_enum(&mut self, name: ObjectName) -> Result<Statement, ParserError> {
14269+
self.expect_token(&Token::LParen)?;
14270+
let labels = self.parse_comma_separated0(|p| p.parse_identifier(), Token::RParen)?;
14271+
self.expect_token(&Token::RParen)?;
14272+
14273+
Ok(Statement::CreateType {
14274+
name,
14275+
representation: UserDefinedTypeRepresentation::Enum { labels },
14276+
})
14277+
}
14278+
1426114279
fn parse_parenthesized_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
1426214280
self.expect_token(&Token::LParen)?;
1426314281
let partitions = self.parse_comma_separated(|p| p.parse_identifier())?;

tests/sqlparser_postgres.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5293,15 +5293,8 @@ fn arrow_cast_precedence() {
52935293

52945294
#[test]
52955295
fn parse_create_type_as_enum() {
5296-
let statement = pg().one_statement_parses_to(
5297-
r#"CREATE TYPE public.my_type AS ENUM (
5298-
'label1',
5299-
'label2',
5300-
'label3',
5301-
'label4'
5302-
);"#,
5303-
"CREATE TYPE public.my_type AS ENUM ('label1', 'label2', 'label3', 'label4')",
5304-
);
5296+
let sql = "CREATE TYPE public.my_type AS ENUM ('label1', 'label2', 'label3', 'label4')";
5297+
let statement = pg_and_generic().verified_stmt(sql);
53055298
match statement {
53065299
Statement::CreateType {
53075300
name,
@@ -5316,7 +5309,7 @@ fn parse_create_type_as_enum() {
53165309
labels
53175310
);
53185311
}
5319-
_ => unreachable!(),
5312+
_ => unreachable!("{:?} should parse to Statement::CreateType", sql),
53205313
}
53215314
}
53225315

0 commit comments

Comments
 (0)