Add support for JSONB datatype (#1089)

This commit is contained in:
Alexander Beedie 2024-01-15 14:46:09 +04:00 committed by GitHub
parent 7cb1654d81
commit 5d66dc5dc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 1 deletions

View file

@ -196,8 +196,10 @@ pub enum DataType {
Timestamp(Option<u64>, TimezoneInfo), Timestamp(Option<u64>, TimezoneInfo),
/// Interval /// Interval
Interval, Interval,
/// JSON type used in BigQuery /// JSON type
JSON, JSON,
/// Binary JSON type
JSONB,
/// Regclass used in postgresql serial /// Regclass used in postgresql serial
Regclass, Regclass,
/// Text /// Text
@ -340,6 +342,7 @@ impl fmt::Display for DataType {
} }
DataType::Interval => write!(f, "INTERVAL"), DataType::Interval => write!(f, "INTERVAL"),
DataType::JSON => write!(f, "JSON"), DataType::JSON => write!(f, "JSON"),
DataType::JSONB => write!(f, "JSONB"),
DataType::Regclass => write!(f, "REGCLASS"), DataType::Regclass => write!(f, "REGCLASS"),
DataType::Text => write!(f, "TEXT"), DataType::Text => write!(f, "TEXT"),
DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false), DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),

View file

@ -366,6 +366,7 @@ define_keywords!(
JAR, JAR,
JOIN, JOIN,
JSON, JSON,
JSONB,
JSONFILE, JSONFILE,
JSON_TABLE, JSON_TABLE,
JULIAN, JULIAN,

View file

@ -5645,6 +5645,7 @@ impl<'a> Parser<'a> {
// parse_interval for a taste. // parse_interval for a taste.
Keyword::INTERVAL => Ok(DataType::Interval), Keyword::INTERVAL => Ok(DataType::Interval),
Keyword::JSON => Ok(DataType::JSON), Keyword::JSON => Ok(DataType::JSON),
Keyword::JSONB => Ok(DataType::JSONB),
Keyword::REGCLASS => Ok(DataType::Regclass), Keyword::REGCLASS => Ok(DataType::Regclass),
Keyword::STRING => Ok(DataType::String(self.parse_optional_precision()?)), Keyword::STRING => Ok(DataType::String(self.parse_optional_precision()?)),
Keyword::TEXT => Ok(DataType::Text), Keyword::TEXT => Ok(DataType::Text),

View file

@ -2172,6 +2172,17 @@ fn parse_cast() {
}, },
expr_from_projection(only(&select.projection)) expr_from_projection(only(&select.projection))
); );
let sql = "SELECT CAST(details AS JSONB) FROM customer";
let select = verified_only_select(sql);
assert_eq!(
&Expr::Cast {
expr: Box::new(Expr::Identifier(Ident::new("details"))),
data_type: DataType::JSONB,
format: None,
},
expr_from_projection(only(&select.projection))
);
} }
#[test] #[test]