Support BIT column types (#1577)

This commit is contained in:
Michael Victor Zink 2024-12-03 16:47:12 -08:00 committed by GitHub
parent e16b24679a
commit 6d4188de53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 0 deletions

View file

@ -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
@ -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}[]"),

View file

@ -126,6 +126,7 @@ define_keywords!(
BIGNUMERIC,
BINARY,
BINDING,
BIT,
BLOB,
BLOOMFILTER,
BOOL,

View file

@ -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),

View file

@ -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!(),
}
}