From 3ccb249eea311eecb9df1b17e92ebf6e903c57a4 Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Fri, 24 Nov 2023 17:39:12 +0800 Subject: [PATCH] fix: estimate jsonb's value encoding size (#13643) Signed-off-by: Runji Wang --- src/common/src/types/jsonb.rs | 5 +++++ src/common/src/util/value_encoding/mod.rs | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/common/src/types/jsonb.rs b/src/common/src/types/jsonb.rs index 6e27ff5344198..71f33a1d53822 100644 --- a/src/common/src/types/jsonb.rs +++ b/src/common/src/types/jsonb.rs @@ -374,6 +374,11 @@ impl<'a> JsonbRef<'a> { Serializer::with_formatter(FmtToIoUnchecked(f), PrettyFormatter::with_indent(b" ")); self.0.serialize(&mut ser).map_err(|_| std::fmt::Error) } + + /// Returns the capacity of the underlying buffer. + pub fn capacity(self) -> usize { + self.0.capacity() + } } /// A custom implementation for [`serde_json::ser::Formatter`] to match PostgreSQL, which adds extra diff --git a/src/common/src/util/value_encoding/mod.rs b/src/common/src/util/value_encoding/mod.rs index e3c4386f39a20..7068cc427735b 100644 --- a/src/common/src/util/value_encoding/mod.rs +++ b/src/common/src/util/value_encoding/mod.rs @@ -130,7 +130,6 @@ pub fn try_get_exact_serialize_datum_size(arr: &ArrayImpl) -> Option { ArrayImpl::Float32(_) => Some(4), ArrayImpl::Float64(_) => Some(8), ArrayImpl::Bool(_) => Some(1), - ArrayImpl::Jsonb(_) => Some(8), ArrayImpl::Decimal(_) => Some(estimate_serialize_decimal_size()), ArrayImpl::Interval(_) => Some(estimate_serialize_interval_size()), ArrayImpl::Date(_) => Some(estimate_serialize_date_size()), @@ -246,7 +245,8 @@ fn estimate_serialize_scalar_size(value: ScalarRefImpl<'_>) -> usize { ScalarRefImpl::Timestamp(_) => estimate_serialize_timestamp_size(), ScalarRefImpl::Timestamptz(_) => 8, ScalarRefImpl::Time(_) => estimate_serialize_time_size(), - ScalarRefImpl::Jsonb(_) => 8, + // not exact as we use internal encoding size to estimate the json string size + ScalarRefImpl::Jsonb(v) => v.capacity(), ScalarRefImpl::Struct(s) => estimate_serialize_struct_size(s), ScalarRefImpl::List(v) => estimate_serialize_list_size(v), }