Skip to content

Commit cad4923

Browse files
authored
Parse Postgres VARBIT datatype (#1703)
1 parent 86abbd6 commit cad4923

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/ast/data_type.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,15 @@ pub enum DataType {
328328
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/bit-type.html
329329
/// [MSSQL]: https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
330330
Bit(Option<u64>),
331-
/// Variable-length bit string e.g. [Postgres]
331+
/// `BIT VARYING(n)`: Variable-length bit string e.g. [Postgres]
332332
///
333333
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
334334
BitVarying(Option<u64>),
335+
/// `VARBIT(n)`: Variable-length bit string. [Postgres] alias for `BIT VARYING`
336+
///
337+
/// [Postgres]: https://www.postgresql.org/docs/current/datatype.html
338+
VarBit(Option<u64>),
339+
///
335340
/// Custom type such as enums
336341
Custom(ObjectName, Vec<String>),
337342
/// Arrays
@@ -550,6 +555,7 @@ impl fmt::Display for DataType {
550555
DataType::BitVarying(size) => {
551556
format_type_with_optional_length(f, "BIT VARYING", size, false)
552557
}
558+
DataType::VarBit(size) => format_type_with_optional_length(f, "VARBIT", size, false),
553559
DataType::Array(ty) => match ty {
554560
ArrayElemTypeDef::None => write!(f, "ARRAY"),
555561
ArrayElemTypeDef::SquareBracket(t, None) => write!(f, "{t}[]"),

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ define_keywords!(
925925
VALUES,
926926
VALUE_OF,
927927
VARBINARY,
928+
VARBIT,
928929
VARCHAR,
929930
VARIABLES,
930931
VARYING,

src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8779,6 +8779,7 @@ impl<'a> Parser<'a> {
87798779
Ok(DataType::Bit(self.parse_optional_precision()?))
87808780
}
87818781
}
8782+
Keyword::VARBIT => Ok(DataType::VarBit(self.parse_optional_precision()?)),
87828783
Keyword::UUID => Ok(DataType::Uuid),
87838784
Keyword::DATE => Ok(DataType::Date),
87848785
Keyword::DATE32 => Ok(DataType::Date32),

tests/sqlparser_postgres.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,3 +5327,29 @@ fn parse_bitstring_literal() {
53275327
))]
53285328
);
53295329
}
5330+
5331+
#[test]
5332+
fn parse_varbit_datatype() {
5333+
match pg_and_generic().verified_stmt("CREATE TABLE foo (x VARBIT, y VARBIT(42))") {
5334+
Statement::CreateTable(CreateTable { columns, .. }) => {
5335+
assert_eq!(
5336+
columns,
5337+
vec![
5338+
ColumnDef {
5339+
name: "x".into(),
5340+
data_type: DataType::VarBit(None),
5341+
collation: None,
5342+
options: vec![],
5343+
},
5344+
ColumnDef {
5345+
name: "y".into(),
5346+
data_type: DataType::VarBit(Some(42)),
5347+
collation: None,
5348+
options: vec![],
5349+
}
5350+
]
5351+
);
5352+
}
5353+
_ => unreachable!(),
5354+
}
5355+
}

0 commit comments

Comments
 (0)