Skip to content

Commit

Permalink
Adds support for JSONB datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jan 12, 2024
1 parent ce49886 commit e2bc1af
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ pub enum DataType {
Timestamp(Option<u64>, TimezoneInfo),
/// Interval
Interval,
/// JSON type used in BigQuery
/// JSON type
JSON,
/// Binary JSON type
JSONB,
/// Regclass used in postgresql serial
Regclass,
/// Text
Expand Down Expand Up @@ -340,6 +342,7 @@ impl fmt::Display for DataType {
}
DataType::Interval => write!(f, "INTERVAL"),
DataType::JSON => write!(f, "JSON"),
DataType::JSONB => write!(f, "JSONB"),
DataType::Regclass => write!(f, "REGCLASS"),
DataType::Text => write!(f, "TEXT"),
DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ define_keywords!(
JAR,
JOIN,
JSON,
JSONB,
JSONFILE,
JSON_TABLE,
JULIAN,
Expand Down
1 change: 1 addition & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5641,6 +5641,7 @@ impl<'a> Parser<'a> {
// parse_interval for a taste.
Keyword::INTERVAL => Ok(DataType::Interval),
Keyword::JSON => Ok(DataType::JSON),
Keyword::JSONB => Ok(DataType::JSONB),
Keyword::REGCLASS => Ok(DataType::Regclass),
Keyword::STRING => Ok(DataType::String(self.parse_optional_precision()?)),
Keyword::TEXT => Ok(DataType::Text),
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,16 @@ fn parse_cast() {
},
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 {

Check failure on line 2179 in tests/sqlparser_common.rs

View workflow job for this annotation

GitHub Actions / compile

missing field `format` in initializer of `sqlparser::ast::Expr`

Check failure on line 2179 in tests/sqlparser_common.rs

View workflow job for this annotation

GitHub Actions / lint

missing field `format` in initializer of `sqlparser::ast::Expr`

Check failure on line 2179 in tests/sqlparser_common.rs

View workflow job for this annotation

GitHub Actions / test (stable)

missing field `format` in initializer of `sqlparser::ast::Expr`

Check failure on line 2179 in tests/sqlparser_common.rs

View workflow job for this annotation

GitHub Actions / test (beta)

missing field `format` in initializer of `sqlparser::ast::Expr`

Check failure on line 2179 in tests/sqlparser_common.rs

View workflow job for this annotation

GitHub Actions / test (nightly)

missing field `format` in initializer of `sqlparser::ast::Expr`
expr: Box::new(Expr::Identifier(Ident::new("details"))),
data_type: DataType::JSONB,
},
expr_from_projection(only(&select.projection))
);
}

#[test]
Expand Down

0 comments on commit e2bc1af

Please sign in to comment.