Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse variable scale decimal in debezium json (#10792) #10857

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/connector/src/parser/unified/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::str::FromStr;

use base64::Engine;
use itertools::Itertools;
use num_bigint::{BigInt, Sign};
use risingwave_common::array::{ListValue, StructValue};
use risingwave_common::cast::{
i64_to_timestamp, i64_to_timestamptz, str_to_bytea, str_to_date, str_to_time, str_to_timestamp,
Expand All @@ -29,6 +30,8 @@ use simd_json::{BorrowedValue, TryTypeError, ValueAccess, ValueType};

use super::{Access, AccessError, AccessResult};
use crate::parser::common::json_object_smart_get_value;
use crate::parser::unified::avro::extract_decimal;

#[derive(Clone, Debug)]
pub enum ByteaHandling {
Standard,
Expand Down Expand Up @@ -301,6 +304,27 @@ impl JsonParseOptions {
(Some(DataType::Decimal), ValueType::String) => ScalarImpl::Decimal(
Decimal::from_str(value.as_str().unwrap()).map_err(|_err| create_error())?,
),
(Some(DataType::Decimal), ValueType::Object) => {
// ref https://github.com/risingwavelabs/risingwave/issues/10628
// handle debezium json (variable scale): {"scale": int, "value": bytes}
let scale = value
.get("scale")
.ok_or_else(create_error)?
.as_i32()
.unwrap();
let value = value
.get("value")
.ok_or_else(create_error)?
.as_str()
.unwrap()
.as_bytes();
let decimal = BigInt::from_signed_bytes_be(value);
let negative = decimal.sign() == Sign::Minus;
let (lo, mid, hi) = extract_decimal(decimal.to_bytes_be().1);
let decimal =
rust_decimal::Decimal::from_parts(lo, mid, hi, negative, scale as u32);
ScalarImpl::Decimal(Decimal::Normalized(decimal))
}
// ---- Date -----
(
Some(DataType::Date),
Expand Down