Skip to content

Commit 300e28b

Browse files
authored
636 Adjusting VARBINARY and BINARY with optional precision (#637)
1 parent c2d6825 commit 300e28b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/ast/data_type.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ pub enum DataType {
3535
Uuid,
3636
/// Large character object e.g. CLOB(1000)
3737
Clob(u64),
38-
/// Fixed-length binary type e.g. BINARY(10)
39-
Binary(u64),
40-
/// Variable-length binary type e.g. VARBINARY(10)
41-
Varbinary(u64),
38+
/// Fixed-length binary type with optional length e.g. [standard], [MS SQL Server]
39+
///
40+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
41+
/// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
42+
Binary(Option<u64>),
43+
/// Variable-length binary with optional length type e.g. [standard], [MS SQL Server]
44+
///
45+
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-string-type
46+
/// [MS SQL Server]: https://learn.microsoft.com/pt-br/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver16
47+
Varbinary(Option<u64>),
4248
/// Large binary object e.g. BLOB(1000)
4349
Blob(u64),
4450
/// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
@@ -126,8 +132,10 @@ impl fmt::Display for DataType {
126132
}
127133
DataType::Uuid => write!(f, "UUID"),
128134
DataType::Clob(size) => write!(f, "CLOB({})", size),
129-
DataType::Binary(size) => write!(f, "BINARY({})", size),
130-
DataType::Varbinary(size) => write!(f, "VARBINARY({})", size),
135+
DataType::Binary(size) => format_type_with_optional_length(f, "BINARY", size, false),
136+
DataType::Varbinary(size) => {
137+
format_type_with_optional_length(f, "VARBINARY", size, false)
138+
}
131139
DataType::Blob(size) => write!(f, "BLOB({})", size),
132140
DataType::Decimal(precision, scale) => {
133141
if let Some(scale) = scale {

src/parser.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3404,8 +3404,8 @@ impl<'a> Parser<'a> {
34043404
}
34053405
}
34063406
Keyword::CLOB => Ok(DataType::Clob(self.parse_precision()?)),
3407-
Keyword::BINARY => Ok(DataType::Binary(self.parse_precision()?)),
3408-
Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_precision()?)),
3407+
Keyword::BINARY => Ok(DataType::Binary(self.parse_optional_precision()?)),
3408+
Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_optional_precision()?)),
34093409
Keyword::BLOB => Ok(DataType::Blob(self.parse_precision()?)),
34103410
Keyword::UUID => Ok(DataType::Uuid),
34113411
Keyword::DATE => Ok(DataType::Date),
@@ -5260,6 +5260,10 @@ mod tests {
52605260
fn test_parse_data_type() {
52615261
test_parse_data_type("DOUBLE PRECISION", "DOUBLE PRECISION");
52625262
test_parse_data_type("DOUBLE", "DOUBLE");
5263+
test_parse_data_type("VARBINARY", "VARBINARY");
5264+
test_parse_data_type("VARBINARY(20)", "VARBINARY(20)");
5265+
test_parse_data_type("BINARY", "BINARY");
5266+
test_parse_data_type("BINARY(20)", "BINARY(20)");
52635267

52645268
fn test_parse_data_type(input: &str, expected: &str) {
52655269
all_dialects().run_parser_method(input, |parser| {

tests/sqlparser_common.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ fn parse_cast() {
16701670
assert_eq!(
16711671
&Expr::Cast {
16721672
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1673-
data_type: DataType::Binary(50)
1673+
data_type: DataType::Binary(Some(50))
16741674
},
16751675
expr_from_projection(only(&select.projection))
16761676
);
@@ -1680,7 +1680,7 @@ fn parse_cast() {
16801680
assert_eq!(
16811681
&Expr::Cast {
16821682
expr: Box::new(Expr::Identifier(Ident::new("id"))),
1683-
data_type: DataType::Varbinary(50)
1683+
data_type: DataType::Varbinary(Some(50))
16841684
},
16851685
expr_from_projection(only(&select.projection))
16861686
);

0 commit comments

Comments
 (0)