Skip to content

Commit

Permalink
fix: estimate jsonb's value encoding size (#13643)
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 authored Nov 24, 2023
1 parent 7b21e04 commit 3ccb249
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/src/types/jsonb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/common/src/util/value_encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub fn try_get_exact_serialize_datum_size(arr: &ArrayImpl) -> Option<usize> {
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()),
Expand Down Expand Up @@ -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),
}
Expand Down

0 comments on commit 3ccb249

Please sign in to comment.