From 6c8f31c367ac75ea29be030bad244c0c6a2fafee Mon Sep 17 00:00:00 2001 From: ding-young Date: Wed, 28 Sep 2022 20:02:30 +0900 Subject: [PATCH] Fix parsing CLOB, BINARY, VARBINARY, BLOB data type (#618) * fix(parser): parse clob, binary, varbinary, blob data type * feat(tests): parse_cast tests for LOB, BINARY datatype --- src/parser.rs | 11 +++++++++++ tests/sqlparser_common.rs | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/parser.rs b/src/parser.rs index e1f0c87a..76ffa166 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3390,6 +3390,10 @@ impl<'a> Parser<'a> { Ok(DataType::Char(self.parse_optional_precision()?)) } } + Keyword::CLOB => Ok(DataType::Clob(self.parse_precision()?)), + Keyword::BINARY => Ok(DataType::Binary(self.parse_precision()?)), + Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_precision()?)), + Keyword::BLOB => Ok(DataType::Blob(self.parse_precision()?)), Keyword::UUID => Ok(DataType::Uuid), Keyword::DATE => Ok(DataType::Date), Keyword::DATETIME => Ok(DataType::Datetime), @@ -3603,6 +3607,13 @@ impl<'a> Parser<'a> { } } + pub fn parse_precision(&mut self) -> Result { + self.expect_token(&Token::LParen)?; + let n = self.parse_literal_uint()?; + self.expect_token(&Token::RParen)?; + Ok(n) + } + pub fn parse_optional_precision(&mut self) -> Result, ParserError> { if self.consume_token(&Token::LParen) { let n = self.parse_literal_uint()?; diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index e304f38c..048e3517 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -1649,6 +1649,46 @@ fn parse_cast() { }, expr_from_projection(only(&select.projection)) ); + + let sql = "SELECT CAST(id AS CLOB(50)) FROM customer"; + let select = verified_only_select(sql); + assert_eq!( + &Expr::Cast { + expr: Box::new(Expr::Identifier(Ident::new("id"))), + data_type: DataType::Clob(50) + }, + expr_from_projection(only(&select.projection)) + ); + + let sql = "SELECT CAST(id AS BINARY(50)) FROM customer"; + let select = verified_only_select(sql); + assert_eq!( + &Expr::Cast { + expr: Box::new(Expr::Identifier(Ident::new("id"))), + data_type: DataType::Binary(50) + }, + expr_from_projection(only(&select.projection)) + ); + + let sql = "SELECT CAST(id AS VARBINARY(50)) FROM customer"; + let select = verified_only_select(sql); + assert_eq!( + &Expr::Cast { + expr: Box::new(Expr::Identifier(Ident::new("id"))), + data_type: DataType::Varbinary(50) + }, + expr_from_projection(only(&select.projection)) + ); + + let sql = "SELECT CAST(id AS BLOB(50)) FROM customer"; + let select = verified_only_select(sql); + assert_eq!( + &Expr::Cast { + expr: Box::new(Expr::Identifier(Ident::new("id"))), + data_type: DataType::Blob(50) + }, + expr_from_projection(only(&select.projection)) + ); } #[test]