Skip to content

Commit 6c8f31c

Browse files
authored
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
1 parent 6afd194 commit 6c8f31c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/parser.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3390,6 +3390,10 @@ impl<'a> Parser<'a> {
33903390
Ok(DataType::Char(self.parse_optional_precision()?))
33913391
}
33923392
}
3393+
Keyword::CLOB => Ok(DataType::Clob(self.parse_precision()?)),
3394+
Keyword::BINARY => Ok(DataType::Binary(self.parse_precision()?)),
3395+
Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_precision()?)),
3396+
Keyword::BLOB => Ok(DataType::Blob(self.parse_precision()?)),
33933397
Keyword::UUID => Ok(DataType::Uuid),
33943398
Keyword::DATE => Ok(DataType::Date),
33953399
Keyword::DATETIME => Ok(DataType::Datetime),
@@ -3603,6 +3607,13 @@ impl<'a> Parser<'a> {
36033607
}
36043608
}
36053609

3610+
pub fn parse_precision(&mut self) -> Result<u64, ParserError> {
3611+
self.expect_token(&Token::LParen)?;
3612+
let n = self.parse_literal_uint()?;
3613+
self.expect_token(&Token::RParen)?;
3614+
Ok(n)
3615+
}
3616+
36063617
pub fn parse_optional_precision(&mut self) -> Result<Option<u64>, ParserError> {
36073618
if self.consume_token(&Token::LParen) {
36083619
let n = self.parse_literal_uint()?;

tests/sqlparser_common.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,46 @@ fn parse_cast() {
16491649
},
16501650
expr_from_projection(only(&select.projection))
16511651
);
1652+
1653+
let sql = "SELECT CAST(id AS CLOB(50)) FROM customer";
1654+
let select = verified_only_select(sql);
1655+
assert_eq!(
1656+
&Expr::Cast {
1657+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1658+
data_type: DataType::Clob(50)
1659+
},
1660+
expr_from_projection(only(&select.projection))
1661+
);
1662+
1663+
let sql = "SELECT CAST(id AS BINARY(50)) FROM customer";
1664+
let select = verified_only_select(sql);
1665+
assert_eq!(
1666+
&Expr::Cast {
1667+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1668+
data_type: DataType::Binary(50)
1669+
},
1670+
expr_from_projection(only(&select.projection))
1671+
);
1672+
1673+
let sql = "SELECT CAST(id AS VARBINARY(50)) FROM customer";
1674+
let select = verified_only_select(sql);
1675+
assert_eq!(
1676+
&Expr::Cast {
1677+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1678+
data_type: DataType::Varbinary(50)
1679+
},
1680+
expr_from_projection(only(&select.projection))
1681+
);
1682+
1683+
let sql = "SELECT CAST(id AS BLOB(50)) FROM customer";
1684+
let select = verified_only_select(sql);
1685+
assert_eq!(
1686+
&Expr::Cast {
1687+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1688+
data_type: DataType::Blob(50)
1689+
},
1690+
expr_from_projection(only(&select.projection))
1691+
);
16521692
}
16531693

16541694
#[test]

0 commit comments

Comments
 (0)