|
| 1 | +/******************************************************************************* |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2025 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +use crate::{error, ingress::must_escape_unquoted, Result}; |
| 26 | + |
| 27 | +/// Trait for types that can be serialized as decimal values in the InfluxDB Line Protocol (ILP). |
| 28 | +/// |
| 29 | +/// Decimal values can be serialized in two formats: |
| 30 | +/// |
| 31 | +/// # Text Format |
| 32 | +/// The decimal is written as a string representation followed by a `'d'` suffix. |
| 33 | +/// |
| 34 | +/// Example: `"123.45d"` or `"1.5e-3d"` |
| 35 | +/// |
| 36 | +/// Implementers must: |
| 37 | +/// - Write the decimal's text representation to the output buffer |
| 38 | +/// - Append the `'d'` suffix |
| 39 | +/// - Ensure no ILP reserved characters are present (space, comma, equals, newline, carriage return, backslash) |
| 40 | +/// |
| 41 | +/// # Binary Format |
| 42 | +/// A more compact binary encoding consisting of: |
| 43 | +/// |
| 44 | +/// 1. Binary format marker: `'='` (0x3D) |
| 45 | +/// 2. Type identifier: [`DECIMAL_BINARY_FORMAT_TYPE`](crate::ingress::DECIMAL_BINARY_FORMAT_TYPE) byte |
| 46 | +/// 3. Scale: 1 byte (0-76 inclusive) - number of decimal places |
| 47 | +/// 4. Length: 1 byte - number of bytes in the unscaled value |
| 48 | +/// 5. Unscaled value: variable-length byte array in two's complement format, big-endian |
| 49 | +/// |
| 50 | +/// Example: For decimal `123.45` with scale 2 and unscaled value 12345: |
| 51 | +/// ```text |
| 52 | +/// = [DECIMAL_BINARY_FORMAT_TYPE] [2] [2] [0x30] [0x39] |
| 53 | +/// ``` |
| 54 | +/// |
| 55 | +/// # Binary Format Notes |
| 56 | +/// - Binary format is only supported when `support_binary` is `true` (Protocol V2) |
| 57 | +/// - The unscaled value must be encoded in two's complement big-endian format |
| 58 | +/// - Maximum scale is 76 |
| 59 | +/// - Length byte indicates how many bytes follow for the unscaled value |
| 60 | +pub trait DecimalSerializer { |
| 61 | + /// Serialize this value as a decimal in ILP format. |
| 62 | + /// |
| 63 | + /// # Parameters |
| 64 | + /// |
| 65 | + /// * `out` - The output buffer to write the serialized decimal to |
| 66 | + /// * `support_binary` - If `true`, binary format may be used (Protocol V2). |
| 67 | + /// If `false`, text format must be used (Protocol V1). |
| 68 | + fn serialize(self, out: &mut Vec<u8>, support_binary: bool) -> Result<()>; |
| 69 | +} |
| 70 | + |
| 71 | +/// Implementation for string slices containing decimal representations. |
| 72 | +/// |
| 73 | +/// This implementation always uses the text format, regardless of the `support_binary` parameter, |
| 74 | +/// as it cannot parse the string to extract scale and unscaled value needed for binary encoding. |
| 75 | +/// |
| 76 | +/// # Format |
| 77 | +/// The string is validated and written as-is, followed by the 'd' suffix. |
| 78 | +/// |
| 79 | +/// # Validation |
| 80 | +/// The implementation performs **partial validation only**: |
| 81 | +/// - Rejects ILP reserved characters (space, comma, equals, newline, carriage return, backslash) |
| 82 | +/// - Does NOT validate the actual decimal syntax (e.g., "not-a-number" would pass) |
| 83 | +/// |
| 84 | +/// This is intentional: full parsing would add overhead. The QuestDB server performs complete |
| 85 | +/// validation and will reject malformed decimals. |
| 86 | +/// |
| 87 | +/// # Examples |
| 88 | +/// - `"123.45"` → `"123.45d"` |
| 89 | +/// - `"1.5e-3"` → `"1.5e-3d"` |
| 90 | +/// - `"-0.001"` → `"-0.001d"` |
| 91 | +/// |
| 92 | +/// # Errors |
| 93 | +/// Returns [`Error`] with [`ErrorCode::InvalidDecimal`](crate::error::ErrorCode::InvalidDecimal) |
| 94 | +/// if the string contains ILP reserved characters. |
| 95 | +impl DecimalSerializer for &str { |
| 96 | + fn serialize(self, out: &mut Vec<u8>, _support_binary: bool) -> Result<()> { |
| 97 | + // Pre-allocate space for the string content plus the 'd' suffix |
| 98 | + out.reserve(self.len() + 1); |
| 99 | + |
| 100 | + // Validate and copy each byte, rejecting ILP reserved characters |
| 101 | + // that would break the protocol (space, comma, equals, newline, etc.) |
| 102 | + for b in self.bytes() { |
| 103 | + if must_escape_unquoted(b) { |
| 104 | + return Err(error::fmt!( |
| 105 | + InvalidDecimal, |
| 106 | + "Unexpected character {:?} in decimal str", |
| 107 | + b |
| 108 | + )); |
| 109 | + } |
| 110 | + out.push(b); |
| 111 | + } |
| 112 | + |
| 113 | + // Append the 'd' suffix to mark this as a decimal value |
| 114 | + out.push(b'd'); |
| 115 | + |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +use crate::ingress::DECIMAL_BINARY_FORMAT_TYPE; |
| 121 | + |
| 122 | +/// Helper to format decimal values directly to a byte buffer without heap allocation. |
| 123 | +#[cfg(any(feature = "rust_decimal", feature = "bigdecimal"))] |
| 124 | +struct DecimalWriter<'a> { |
| 125 | + buf: &'a mut Vec<u8>, |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg(any(feature = "rust_decimal", feature = "bigdecimal"))] |
| 129 | +impl<'a> std::fmt::Write for DecimalWriter<'a> { |
| 130 | + fn write_str(&mut self, s: &str) -> std::fmt::Result { |
| 131 | + self.buf.extend_from_slice(s.as_bytes()); |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +#[cfg(feature = "rust_decimal")] |
| 137 | +impl DecimalSerializer for &rust_decimal::Decimal { |
| 138 | + fn serialize(self, out: &mut Vec<u8>, support_binary: bool) -> Result<()> { |
| 139 | + if !support_binary { |
| 140 | + // Text format |
| 141 | + use std::fmt::Write; |
| 142 | + write!(DecimalWriter { buf: out }, "{}", self) |
| 143 | + .map_err(|_| error::fmt!(InvalidDecimal, "Failed to format decimal value"))?; |
| 144 | + out.push(b'd'); |
| 145 | + return Ok(()); |
| 146 | + } |
| 147 | + |
| 148 | + // Binary format: '=' marker + type + scale + length + mantissa bytes |
| 149 | + out.push(b'='); |
| 150 | + out.push(DECIMAL_BINARY_FORMAT_TYPE); |
| 151 | + |
| 152 | + // rust_decimal::Decimal guarantees: |
| 153 | + // - MAX_SCALE is 28, which is within QuestDB's limit of 76 |
| 154 | + // - Mantissa is always 96 bits (12 bytes), never exceeds this size |
| 155 | + debug_assert!(rust_decimal::Decimal::MAX_SCALE <= 76); |
| 156 | + debug_assert!( |
| 157 | + rust_decimal::Decimal::MAX.mantissa() & 0x7FFF_FFFF_0000_0000_0000_0000_0000_0000i128 |
| 158 | + == 0 |
| 159 | + ); |
| 160 | + |
| 161 | + out.push(self.scale() as u8); |
| 162 | + |
| 163 | + // We skip the upper 3 bytes (which are sign-extended) and write the lower 13 bytes |
| 164 | + let mantissa = self.mantissa(); |
| 165 | + out.push(13); |
| 166 | + out.extend_from_slice(&mantissa.to_be_bytes()[3..]); // Skip upper 4 bytes, write lower 12 |
| 167 | + |
| 168 | + Ok(()) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +#[cfg(feature = "bigdecimal")] |
| 173 | +impl DecimalSerializer for &bigdecimal::BigDecimal { |
| 174 | + fn serialize(self, out: &mut Vec<u8>, support_binary: bool) -> Result<()> { |
| 175 | + if !support_binary { |
| 176 | + // Text format |
| 177 | + use std::fmt::Write; |
| 178 | + write!(DecimalWriter { buf: out }, "{}", self) |
| 179 | + .map_err(|_| error::fmt!(InvalidDecimal, "Failed to format decimal value"))?; |
| 180 | + out.push(b'd'); |
| 181 | + return Ok(()); |
| 182 | + } |
| 183 | + |
| 184 | + // Binary format: '=' marker + type + scale + length + mantissa bytes |
| 185 | + out.push(b'='); |
| 186 | + out.push(DECIMAL_BINARY_FORMAT_TYPE); |
| 187 | + |
| 188 | + let (unscaled, mut scale) = self.as_bigint_and_scale(); |
| 189 | + if scale > 76 { |
| 190 | + return Err(error::fmt!( |
| 191 | + InvalidDecimal, |
| 192 | + "QuestDB ILP does not support scale greater than 76, got {}", |
| 193 | + scale |
| 194 | + )); |
| 195 | + } |
| 196 | + |
| 197 | + // QuestDB binary ILP doesn't support negative scale, we need to upscale the |
| 198 | + // unscaled value to be compliant |
| 199 | + let bytes = if scale < 0 { |
| 200 | + use bigdecimal::num_bigint; |
| 201 | + let unscaled = |
| 202 | + unscaled.into_owned() * num_bigint::BigInt::from(10).pow((-scale) as u32); |
| 203 | + scale = 0; |
| 204 | + unscaled.to_signed_bytes_be() |
| 205 | + } else { |
| 206 | + unscaled.to_signed_bytes_be() |
| 207 | + }; |
| 208 | + |
| 209 | + if bytes.len() > i8::MAX as usize { |
| 210 | + return Err(error::fmt!( |
| 211 | + InvalidDecimal, |
| 212 | + "QuestDB ILP does not support values greater than {} bytes, got {}", |
| 213 | + i8::MAX, |
| 214 | + bytes.len() |
| 215 | + )); |
| 216 | + } |
| 217 | + |
| 218 | + out.push(scale as u8); |
| 219 | + |
| 220 | + // Write length byte and mantissa bytes |
| 221 | + out.push(bytes.len() as u8); |
| 222 | + out.extend_from_slice(&bytes); |
| 223 | + |
| 224 | + Ok(()) |
| 225 | + } |
| 226 | +} |
0 commit comments