From ed10b36cb0e9da399310f545fffdc7d738dfc171 Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Fri, 20 Oct 2023 16:48:50 +0800 Subject: [PATCH] resolve comments Signed-off-by: Runji Wang --- src/common/src/array/jsonb_array.rs | 2 +- src/common/src/types/jsonb.rs | 23 ++++++++++++++++++++--- src/common/src/types/mod.rs | 2 +- src/expr/impl/src/scalar/jsonb_access.rs | 4 ++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/common/src/array/jsonb_array.rs b/src/common/src/array/jsonb_array.rs index ed561a4a86648..8a1b1999c43c3 100644 --- a/src/common/src/array/jsonb_array.rs +++ b/src/common/src/array/jsonb_array.rs @@ -28,7 +28,7 @@ pub struct JsonbArrayBuilder { #[derive(Debug, Clone, PartialEq, Eq)] pub struct JsonbArray { bitmap: Bitmap, - // A JSON array. + /// Elements are stored as a single JSONB array value. data: jsonbb::Value, } diff --git a/src/common/src/types/jsonb.rs b/src/common/src/types/jsonb.rs index 1d2f1ce64d334..53b2992e96ee3 100644 --- a/src/common/src/types/jsonb.rs +++ b/src/common/src/types/jsonb.rs @@ -222,13 +222,30 @@ impl From for JsonbVal { impl From for JsonbVal { fn from(v: F32) -> Self { - Self(v.0.into()) + if v.0 == f32::INFINITY { + Self("Infinity".into()) + } else if v.0 == f32::NEG_INFINITY { + Self("-Infinity".into()) + } else if v.0.is_nan() { + Self("NaN".into()) + } else { + Self(v.0.into()) + } } } +// NOTE: Infinite or NaN values are not JSON numbers. They are stored as strings in Postgres. impl From for JsonbVal { fn from(v: F64) -> Self { - Self(v.0.into()) + if v.0 == f64::INFINITY { + Self("Infinity".into()) + } else if v.0 == f64::NEG_INFINITY { + Self("-Infinity".into()) + } else if v.0.is_nan() { + Self("NaN".into()) + } else { + Self(v.0.into()) + } } } @@ -269,7 +286,7 @@ impl<'a> JsonbRef<'a> { } /// Returns true if this is a jsonb `null`. - pub fn is_null(&self) -> bool { + pub fn is_jsonb_null(&self) -> bool { self.0.as_null().is_some() } diff --git a/src/common/src/types/mod.rs b/src/common/src/types/mod.rs index 386f63280a557..a54b6db8d457e 100644 --- a/src/common/src/types/mod.rs +++ b/src/common/src/types/mod.rs @@ -416,7 +416,7 @@ impl DataType { DataType::Timestamptz => ScalarImpl::Timestamptz(Timestamptz::MIN), DataType::Decimal => ScalarImpl::Decimal(Decimal::NegativeInf), DataType::Interval => ScalarImpl::Interval(Interval::MIN), - DataType::Jsonb => ScalarImpl::Jsonb(JsonbVal::null()), // NOT `min` #7981 + DataType::Jsonb => ScalarImpl::Jsonb(JsonbVal::null()), DataType::Struct(data_types) => ScalarImpl::Struct(StructValue::new( data_types .types() diff --git a/src/expr/impl/src/scalar/jsonb_access.rs b/src/expr/impl/src/scalar/jsonb_access.rs index 913cf59dc3321..8115c1d7214ab 100644 --- a/src/expr/impl/src/scalar/jsonb_access.rs +++ b/src/expr/impl/src/scalar/jsonb_access.rs @@ -42,7 +42,7 @@ pub fn jsonb_array_element(v: JsonbRef<'_>, p: i32) -> Option> { #[function("jsonb_access_str(jsonb, varchar) -> varchar")] pub fn jsonb_object_field_str(v: JsonbRef<'_>, p: &str, writer: &mut impl Write) -> Option<()> { let jsonb = jsonb_object_field(v, p)?; - if jsonb.is_null() { + if jsonb.is_jsonb_null() { return None; } jsonb.force_str(writer).unwrap(); @@ -52,7 +52,7 @@ pub fn jsonb_object_field_str(v: JsonbRef<'_>, p: &str, writer: &mut impl Write) #[function("jsonb_access_str(jsonb, int4) -> varchar")] pub fn jsonb_array_element_str(v: JsonbRef<'_>, p: i32, writer: &mut impl Write) -> Option<()> { let jsonb = jsonb_array_element(v, p)?; - if jsonb.is_null() { + if jsonb.is_jsonb_null() { return None; } jsonb.force_str(writer).unwrap();