Skip to content

Commit

Permalink
Merge dfadc85 into 6afd194
Browse files Browse the repository at this point in the history
  • Loading branch information
ding-young committed Sep 28, 2022
2 parents 6afd194 + dfadc85 commit 0051a05
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -3603,6 +3607,13 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_precision(&mut self) -> Result<u64, ParserError> {
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<Option<u64>, ParserError> {
if self.consume_token(&Token::LParen) {
let n = self.parse_literal_uint()?;
Expand Down
40 changes: 40 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0051a05

Please sign in to comment.