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
29impl Serializer for Decimal {
30    type Target = Self;
31
32    #[inline(always)]
33    fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
34        context.writer.write_var_i32(value.scale);
35        write_decimal_unscaled(&value.unscaled, &mut context.writer)
36    }
37
38    #[inline(always)]
39    fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
40        let scale = context.reader.read_var_i32()?;
41        let unscaled = read_decimal_unscaled(&mut context.reader)?;
42        Ok(Self { unscaled, scale })
43    }
44
45    #[inline(always)]
46    fn default_value(_: &mut ReadContext) -> Result<Self, Error> {
47        Ok(Self {
48            unscaled: BigInt::from(0),
49            scale: 0,
50        })
51    }
52
53    #[inline(always)]
54    fn read_arc_any(
55        context: &mut ReadContext,
56    ) -> Result<Arc<dyn std::any::Any + Send + Sync>, Error> {
57        Ok(Arc::new(Self::read_data(context)?))
58    }
59
60    #[inline(always)]
61    fn static_type_id() -> TypeId {
62        TypeId::DECIMAL
63    }
64
65    #[inline(always)]
66    fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
67        context.writer.write_var_u32(TypeId::DECIMAL as u32);
68        Ok(())
69    }
70
71    #[inline(always)]
72    fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
73        read_basic_type_info::<Self>(context)
74    }
75}
76
77fn write_decimal_unscaled(value: &BigInt, writer: &mut Writer) -> Result<(), Error> {
78    if let Some(small_value) = can_use_small_encoding(value) {
79        writer.write_var_u64(encode_zigzag64(small_value) << 1);
80        return Ok(());
81    }
82
83    let (sign, magnitude_bytes) = value.to_bytes_le();
84    if magnitude_bytes.is_empty() {
85        return Err(Error::invalid_data(
86            "zero must use the small decimal encoding".to_string(),
87        ));
88    }
89    let meta = ((magnitude_bytes.len() as u64) << 1) | u64::from(matches!(sign, Sign::Minus));
90    writer.write_var_u64((meta << 1) | 1);
91    writer.write_bytes(&magnitude_bytes);
92    Ok(())
93}
94
95fn read_decimal_unscaled(reader: &mut Reader) -> Result<BigInt, Error> {
96    let header = reader.read_var_u64()?;
97    if (header & 1) == 0 {
98        return Ok(BigInt::from(decode_zigzag64(header >> 1)));
99    }
100
101    let meta = header >> 1;
102    let sign = (meta & 1) != 0;
103    let len = (meta >> 1) as usize;
104    if len == 0 {
105        return Err(Error::invalid_data(
106            "invalid decimal magnitude length 0".to_string(),
107        ));
108    }
109    let magnitude_bytes = reader.read_bytes(len)?;
110    if magnitude_bytes[len - 1] == 0 {
111        return Err(Error::invalid_data(
112            "non-canonical decimal magnitude: trailing zero byte".to_string(),
113        ));
114    }
115    let magnitude = BigInt::from_bytes_le(Sign::Plus, magnitude_bytes);
116    if magnitude == BigInt::from(0) {
117        return Err(Error::invalid_data(
118            "big decimal encoding must not represent zero".to_string(),
119        ));
120    }
121    Ok(if sign { -magnitude } else { magnitude })
122}
123
124fn can_use_small_encoding(value: &BigInt) -> Option<i64> {
125    let small_value = i64::try_from(value).ok()?;
126    if (encode_zigzag64(small_value) & (1u64 << 63)) == 0 {
127        Some(small_value)
128    } else {
129        None
130    }
131}
132
133#[inline(always)]
134fn encode_zigzag64(value: i64) -> u64 {
135    ((value << 1) ^ (value >> 63)) as u64
136}
137
138#[inline(always)]
139fn decode_zigzag64(value: u64) -> i64 {
140    ((value >> 1) as i64) ^ -((value & 1) as i64)
141}