Skip to content

Commit

Permalink
Support BIT column types (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvzink authored Dec 4, 2024
1 parent e16b246 commit 6d4188d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ pub enum DataType {
FixedString(u64),
/// Bytea
Bytea,
/// Bit string, e.g. [Postgres], [MySQL], or [MSSQL]
///
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/bit-type.html
/// [MSSQL]: https://learn.microsoft.com/en-us/sql/t-sql/data-types/bit-transact-sql?view=sql-server-ver16
Bit(Option<u64>),
/// Variable-length bit string e.g. [Postgres]
///
/// [Postgres]: https://www.postgresql.org/docs/current/datatype-bit.html
BitVarying(Option<u64>),
/// Custom type such as enums
Custom(ObjectName, Vec<String>),
/// Arrays
Expand Down Expand Up @@ -518,6 +528,10 @@ impl fmt::Display for DataType {
DataType::LongText => write!(f, "LONGTEXT"),
DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),
DataType::Bytea => write!(f, "BYTEA"),
DataType::Bit(size) => format_type_with_optional_length(f, "BIT", size, false),
DataType::BitVarying(size) => {
format_type_with_optional_length(f, "BIT VARYING", size, false)
}
DataType::Array(ty) => match ty {
ArrayElemTypeDef::None => write!(f, "ARRAY"),
ArrayElemTypeDef::SquareBracket(t, None) => write!(f, "{t}[]"),
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ define_keywords!(
BIGNUMERIC,
BINARY,
BINDING,
BIT,
BLOB,
BLOOMFILTER,
BOOL,
Expand Down
7 changes: 7 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8134,6 +8134,13 @@ impl<'a> Parser<'a> {
Keyword::MEDIUMBLOB => Ok(DataType::MediumBlob),
Keyword::LONGBLOB => Ok(DataType::LongBlob),
Keyword::BYTES => Ok(DataType::Bytes(self.parse_optional_precision()?)),
Keyword::BIT => {
if self.parse_keyword(Keyword::VARYING) {
Ok(DataType::BitVarying(self.parse_optional_precision()?))
} else {
Ok(DataType::Bit(self.parse_optional_precision()?))
}
}
Keyword::UUID => Ok(DataType::Uuid),
Keyword::DATE => Ok(DataType::Date),
Keyword::DATE32 => Ok(DataType::Date32),
Expand Down
19 changes: 19 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12440,3 +12440,22 @@ fn test_reserved_keywords_for_identifiers() {
let sql = "SELECT MAX(interval) FROM tbl";
dialects.parse_sql_statements(sql).unwrap();
}

#[test]
fn parse_create_table_with_bit_types() {
let sql = "CREATE TABLE t (a BIT, b BIT VARYING, c BIT(42), d BIT VARYING(43))";
match verified_stmt(sql) {
Statement::CreateTable(CreateTable { columns, .. }) => {
assert_eq!(columns.len(), 4);
assert_eq!(columns[0].data_type, DataType::Bit(None));
assert_eq!(columns[0].to_string(), "a BIT");
assert_eq!(columns[1].data_type, DataType::BitVarying(None));
assert_eq!(columns[1].to_string(), "b BIT VARYING");
assert_eq!(columns[2].data_type, DataType::Bit(Some(42)));
assert_eq!(columns[2].to_string(), "c BIT(42)");
assert_eq!(columns[3].data_type, DataType::BitVarying(Some(43)));
assert_eq!(columns[3].to_string(), "d BIT VARYING(43)");
}
_ => unreachable!(),
}
}

0 comments on commit 6d4188d

Please sign in to comment.