Skip to main content

fory_core/serializer/
decimal.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::buffer::{Reader, Writer};
19use crate::context::{ReadContext, WriteContext};
20use crate::error::Error;
21use crate::serializer::util::read_basic_type_info;
22use crate::serializer::Serializer;
23use crate::type_id::TypeId;
24use crate::types::Decimal;
25use num_bigint::{BigInt, Sign};
26use std::convert::TryFrom;
27use std::sync::Arc;
28
29const MAX_DECIMAL_MAGNITUDE_BYTES: usize = 10_000;
30const MAX_DECIMAL_SCALE: i32 = 10_000;
31
32impl Serializer for Decimal {
33    type Target = Self;
34
35    const READ_DATA_ALWAYS_ADVANCES: bool = true;
36
37    #[inline(always)]
38    fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
39        // Keep direct bounds checks because taking abs() overflows for i32::MIN.
40        if value.scale < -MAX_DECIMAL_SCALE || value.scale > MAX_DECIMAL_SCALE {
41            return Err(Error::encode_error(format!(
42                "decimal scale {} exceeds supported range [{}, {}]",
43                value.scale, -MAX_DECIMAL_SCALE, MAX_DECIMAL_SCALE
44            )));
45        }
46        if value.unscaled.bits() > (MAX_DECIMAL_MAGNITUDE_BYTES as u64) * 8 {
47            return Err(Error::encode_error(format!(
48                "decimal magnitude exceeds {} bytes",
49                MAX_DECIMAL_MAGNITUDE_BYTES
50            )));
51        }
52        context.writer.write_var_i32(value.scale);
53        write_decimal_unscaled(&value.unscaled, &mut context.writer)
54    }
55
56    #[inline(always)]
57    #[allow(clippy::manual_range_contains)]
58    fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
59        let scale = context.reader.read_var_i32()?;
60        if scale < -MAX_DECIMAL_SCALE || scale > MAX_DECIMAL_SCALE {
61            return Err(Error::invalid_data(format!(
62                "decimal scale {} exceeds supported range [{}, {}]",
63                scale, -MAX_DECIMAL_SCALE, MAX_DECIMAL_SCALE
64            )));
65        }
66        let unscaled = read_decimal_unscaled(&mut context.reader)?;
67        Ok(Self { unscaled, scale })
68    }
69
70    #[inline(always)]
71    fn default_value(_: &mut ReadContext) -> Result<Self, Error> {
72        Ok(Self {
73            unscaled: BigInt::from(0),
74            scale: 0,
75        })
76    }
77
78    #[inline(always)]
79    fn read_arc_any(
80        context: &mut ReadContext,
81    ) -> Result<Arc<dyn std::any::Any + Send + Sync>, Error> {
82        Ok(Arc::new(Self::read_data(context)?))
83    }
84
85    #[inline(always)]
86    fn static_type_id() -> TypeId {
87        TypeId::DECIMAL
88    }
89
90    #[inline(always)]
91    fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
92        context.writer.write_var_u32(TypeId::DECIMAL as u32);
93        Ok(())
94    }
95
96    #[inline(always)]
97    fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
98        read_basic_type_info::<Self>(context)
99    }
100}
101
102fn write_decimal_unscaled(value: &BigInt, writer: &mut Writer) -> Result<(), Error> {
103    if let Some(small_value) = can_use_small_encoding(value) {
104        writer.write_var_u64(encode_zigzag64(small_value) << 1);
105        return Ok(());
106    }
107
108    let (sign, magnitude_bytes) = value.to_bytes_le();
109    if magnitude_bytes.is_empty() {
110        return Err(Error::invalid_data(
111            "zero must use the small decimal encoding".to_string(),
112        ));
113    }
114    let meta = ((magnitude_bytes.len() as u64) << 1) | u64::from(matches!(sign, Sign::Minus));
115    writer.write_var_u64((meta << 1) | 1);
116    writer.write_bytes(&magnitude_bytes);
117    Ok(())
118}
119
120fn read_decimal_unscaled(reader: &mut Reader) -> Result<BigInt, Error> {
121    let header = reader.read_var_u64()?;
122    if (header & 1) == 0 {
123        return Ok(BigInt::from(decode_zigzag64(header >> 1)));
124    }
125
126    let meta = header >> 1;
127    let sign = (meta & 1) != 0;
128    let len = meta >> 1;
129    if len == 0 {
130        return Err(Error::invalid_data(
131            "invalid decimal magnitude length 0".to_string(),
132        ));
133    }
134    if len > MAX_DECIMAL_MAGNITUDE_BYTES as u64 {
135        return Err(Error::invalid_data(format!(
136            "decimal magnitude length {} exceeds limit {}",
137            len, MAX_DECIMAL_MAGNITUDE_BYTES
138        )));
139    }
140    let len = usize::try_from(len)
141        .map_err(|_| Error::invalid_data(format!("invalid decimal magnitude length {}", len)))?;
142    let magnitude_bytes = reader.read_bytes(len)?;
143    if magnitude_bytes[len - 1] == 0 {
144        return Err(Error::invalid_data(
145            "non-canonical decimal magnitude: trailing zero byte".to_string(),
146        ));
147    }
148    let magnitude = BigInt::from_bytes_le(Sign::Plus, magnitude_bytes);
149    if magnitude == BigInt::from(0) {
150        return Err(Error::invalid_data(
151            "big decimal encoding must not represent zero".to_string(),
152        ));
153    }
154    Ok(if sign { -magnitude } else { magnitude })
155}
156
157fn can_use_small_encoding(value: &BigInt) -> Option<i64> {
158    let small_value = i64::try_from(value).ok()?;
159    if (encode_zigzag64(small_value) & (1u64 << 63)) == 0 {
160        Some(small_value)
161    } else {
162        None
163    }
164}
165
166#[inline(always)]
167fn encode_zigzag64(value: i64) -> u64 {
168    ((value << 1) ^ (value >> 63)) as u64
169}
170
171#[inline(always)]
172fn decode_zigzag64(value: u64) -> i64 {
173    ((value >> 1) as i64) ^ -((value & 1) as i64)
174}