From 9a5e2b886e823e038edd6f42a632112236d5b8d1 Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Mon, 30 Oct 2023 11:28:50 +0800 Subject: [PATCH] feat(parser): support "jsonb" as typed string (#13087) Signed-off-by: Runji Wang --- e2e_test/batch/types/jsonb.slt.part | 6 ++++++ src/frontend/src/binder/expr/mod.rs | 2 +- src/frontend/src/binder/select.rs | 1 + src/sqlparser/src/ast/data_type.rs | 3 +++ src/sqlparser/src/parser.rs | 7 ++++++- 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/e2e_test/batch/types/jsonb.slt.part b/e2e_test/batch/types/jsonb.slt.part index fc2f60d48db3..9812f254ff0c 100644 --- a/e2e_test/batch/types/jsonb.slt.part +++ b/e2e_test/batch/types/jsonb.slt.part @@ -33,6 +33,12 @@ null true NULL +# typed string +query TT +select jsonb 'true', JsonB '{}'; +---- +true {} + query T select 'true'::jsonb::bool; ---- diff --git a/src/frontend/src/binder/expr/mod.rs b/src/frontend/src/binder/expr/mod.rs index 221056f3a482..4433605b03a5 100644 --- a/src/frontend/src/binder/expr/mod.rs +++ b/src/frontend/src/binder/expr/mod.rs @@ -642,7 +642,6 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result { "float4" => DataType::Float32, "float8" => DataType::Float64, "timestamptz" => DataType::Timestamptz, - "jsonb" => DataType::Jsonb, "serial" => { return Err(ErrorCode::NotSupported( "Column type SERIAL is not supported".into(), @@ -654,6 +653,7 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result { } } AstDataType::Bytea => DataType::Bytea, + AstDataType::Jsonb => DataType::Jsonb, AstDataType::Regclass | AstDataType::Regproc | AstDataType::Uuid diff --git a/src/frontend/src/binder/select.rs b/src/frontend/src/binder/select.rs index 48c4290ee7e0..ac1a53e75f63 100644 --- a/src/frontend/src/binder/select.rs +++ b/src/frontend/src/binder/select.rs @@ -905,6 +905,7 @@ fn data_type_to_alias(data_type: &AstDataType) -> Option { 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(_) => { diff --git a/src/sqlparser/src/ast/data_type.rs b/src/sqlparser/src/ast/data_type.rs index e8ad404d4d7d..1e588955f093 100644 --- a/src/sqlparser/src/ast/data_type.rs +++ b/src/sqlparser/src/ast/data_type.rs @@ -62,6 +62,8 @@ pub enum DataType { Text, /// Bytea Bytea, + /// JSONB + Jsonb, /// Custom type such as enums Custom(ObjectName), /// Arrays @@ -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) => { diff --git a/src/sqlparser/src/parser.rs b/src/sqlparser/src/parser.rs index 5cc094a20426..d87488ac3064 100644 --- a/src/sqlparser/src/parser.rs +++ b/src/sqlparser/src/parser.rs @@ -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 => {