Skip to content

Commit

Permalink
feat(parser): support "jsonb" as typed string (#13087)
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 authored Oct 30, 2023
1 parent af6b4c6 commit 9a5e2b8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions e2e_test/batch/types/jsonb.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ null
true
NULL

# typed string
query TT
select jsonb 'true', JsonB '{}';
----
true {}

query T
select 'true'::jsonb::bool;
----
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result<DataType> {
"float4" => DataType::Float32,
"float8" => DataType::Float64,
"timestamptz" => DataType::Timestamptz,
"jsonb" => DataType::Jsonb,
"serial" => {
return Err(ErrorCode::NotSupported(
"Column type SERIAL is not supported".into(),
Expand All @@ -654,6 +653,7 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result<DataType> {
}
}
AstDataType::Bytea => DataType::Bytea,
AstDataType::Jsonb => DataType::Jsonb,
AstDataType::Regclass
| AstDataType::Regproc
| AstDataType::Uuid
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ fn data_type_to_alias(data_type: &AstDataType) -> Option<String> {
AstDataType::Regproc => "regproc".to_string(),
AstDataType::Text => "text".to_string(),
AstDataType::Bytea => "bytea".to_string(),
AstDataType::Jsonb => "jsonb".to_string(),
AstDataType::Array(ty) => return data_type_to_alias(ty),
AstDataType::Custom(ty) => format!("{}", ty),
AstDataType::Struct(_) => {
Expand Down
3 changes: 3 additions & 0 deletions src/sqlparser/src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub enum DataType {
Text,
/// Bytea
Bytea,
/// JSONB
Jsonb,
/// Custom type such as enums
Custom(ObjectName),
/// Arrays
Expand Down Expand Up @@ -102,6 +104,7 @@ impl fmt::Display for DataType {
DataType::Regproc => write!(f, "REGPROC"),
DataType::Text => write!(f, "TEXT"),
DataType::Bytea => write!(f, "BYTEA"),
DataType::Jsonb => write!(f, "JSONB"),
DataType::Array(ty) => write!(f, "{}[]", ty),
DataType::Custom(ty) => write!(f, "{}", ty),
DataType::Struct(defs) => {
Expand Down
7 changes: 6 additions & 1 deletion src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,12 @@ impl Parser {
_ => {
self.prev_token();
let type_name = self.parse_object_name()?;
Ok(DataType::Custom(type_name))
// JSONB is not a keyword
if type_name.to_string().eq_ignore_ascii_case("jsonb") {
Ok(DataType::Jsonb)
} else {
Ok(DataType::Custom(type_name))
}
}
},
unexpected => {
Expand Down

0 comments on commit 9a5e2b8

Please sign in to comment.