Skip to content

Commit 6d4188d

Browse files
authored
Support BIT column types (#1577)
1 parent e16b246 commit 6d4188d

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/ast/data_type.rs

+14
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,16 @@ pub enum DataType {
307307
FixedString(u64),
308308
/// Bytea
309309
Bytea,
310+
/// Bit string, e.g. [Postgres], [MySQL], or [MSSQL]
311+
///
312+
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
313+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/bit-type.html
314+
/// [MSSQL]: https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
315+
Bit(Option<u64>),
316+
/// Variable-length bit string e.g. [Postgres]
317+
///
318+
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
319+
BitVarying(Option<u64>),
310320
/// Custom type such as enums
311321
Custom(ObjectName, Vec<String>),
312322
/// Arrays
@@ -518,6 +528,10 @@ impl fmt::Display for DataType {
518528
DataType::LongText => write!(f, "LONGTEXT"),
519529
DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),
520530
DataType::Bytea => write!(f, "BYTEA"),
531+
DataType::Bit(size) => format_type_with_optional_length(f, "BIT", size, false),
532+
DataType::BitVarying(size) => {
533+
format_type_with_optional_length(f, "BIT VARYING", size, false)
534+
}
521535
DataType::Array(ty) => match ty {
522536
ArrayElemTypeDef::None => write!(f, "ARRAY"),
523537
ArrayElemTypeDef::SquareBracket(t, None) => write!(f, "{t}[]"),

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ define_keywords!(
126126
BIGNUMERIC,
127127
BINARY,
128128
BINDING,
129+
BIT,
129130
BLOB,
130131
BLOOMFILTER,
131132
BOOL,

src/parser/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -8134,6 +8134,13 @@ impl<'a> Parser<'a> {
81348134
Keyword::MEDIUMBLOB => Ok(DataType::MediumBlob),
81358135
Keyword::LONGBLOB => Ok(DataType::LongBlob),
81368136
Keyword::BYTES => Ok(DataType::Bytes(self.parse_optional_precision()?)),
8137+
Keyword::BIT => {
8138+
if self.parse_keyword(Keyword::VARYING) {
8139+
Ok(DataType::BitVarying(self.parse_optional_precision()?))
8140+
} else {
8141+
Ok(DataType::Bit(self.parse_optional_precision()?))
8142+
}
8143+
}
81378144
Keyword::UUID => Ok(DataType::Uuid),
81388145
Keyword::DATE => Ok(DataType::Date),
81398146
Keyword::DATE32 => Ok(DataType::Date32),

tests/sqlparser_common.rs

+19
Original file line numberDiff line numberDiff line change
@@ -12440,3 +12440,22 @@ fn test_reserved_keywords_for_identifiers() {
1244012440
let sql = "SELECT MAX(interval) FROM tbl";
1244112441
dialects.parse_sql_statements(sql).unwrap();
1244212442
}
12443+
12444+
#[test]
12445+
fn parse_create_table_with_bit_types() {
12446+
let sql = "CREATE TABLE t (a BIT, b BIT VARYING, c BIT(42), d BIT VARYING(43))";
12447+
match verified_stmt(sql) {
12448+
Statement::CreateTable(CreateTable { columns, .. }) => {
12449+
assert_eq!(columns.len(), 4);
12450+
assert_eq!(columns[0].data_type, DataType::Bit(None));
12451+
assert_eq!(columns[0].to_string(), "a BIT");
12452+
assert_eq!(columns[1].data_type, DataType::BitVarying(None));
12453+
assert_eq!(columns[1].to_string(), "b BIT VARYING");
12454+
assert_eq!(columns[2].data_type, DataType::Bit(Some(42)));
12455+
assert_eq!(columns[2].to_string(), "c BIT(42)");
12456+
assert_eq!(columns[3].data_type, DataType::BitVarying(Some(43)));
12457+
assert_eq!(columns[3].to_string(), "d BIT VARYING(43)");
12458+
}
12459+
_ => unreachable!(),
12460+
}
12461+
}

0 commit comments

Comments
 (0)