|
| 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 | +use arrow_schema::ArrowError; |
| 18 | +use std::array::TryFromSliceError; |
| 19 | + |
| 20 | +use crate::utils::{array_from_slice, first_byte_from_slice, string_from_slice}; |
| 21 | + |
| 22 | +#[derive(Debug, Clone, Copy)] |
| 23 | +pub enum VariantBasicType { |
| 24 | + Primitive = 0, |
| 25 | + ShortString = 1, |
| 26 | + Object = 2, |
| 27 | + Array = 3, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, Clone, Copy)] |
| 31 | +pub enum VariantPrimitiveType { |
| 32 | + Null = 0, |
| 33 | + BooleanTrue = 1, |
| 34 | + BooleanFalse = 2, |
| 35 | + Int8 = 3, |
| 36 | + // TODO: Add types for the rest of primitives, once API is agreed upon |
| 37 | + String = 16, |
| 38 | +} |
| 39 | + |
| 40 | +/// Extracts the basic type from a header byte |
| 41 | +pub(crate) fn get_basic_type(header: u8) -> Result<VariantBasicType, ArrowError> { |
| 42 | + // See https://github.com/apache/parquet-format/blob/master/VariantEncoding.md#value-encoding |
| 43 | + let basic_type = header & 0x03; // Basic type is encoded in the first 2 bits |
| 44 | + let basic_type = match basic_type { |
| 45 | + 0 => VariantBasicType::Primitive, |
| 46 | + 1 => VariantBasicType::ShortString, |
| 47 | + 2 => VariantBasicType::Object, |
| 48 | + 3 => VariantBasicType::Array, |
| 49 | + _ => { |
| 50 | + //NOTE: A 2-bit value has a max of 4 different values (0-3), hence this is unreachable as we |
| 51 | + // masked `basic_type` with 0x03 above. |
| 52 | + unreachable!(); |
| 53 | + } |
| 54 | + }; |
| 55 | + Ok(basic_type) |
| 56 | +} |
| 57 | + |
| 58 | +impl TryFrom<u8> for VariantPrimitiveType { |
| 59 | + type Error = ArrowError; |
| 60 | + |
| 61 | + fn try_from(value: u8) -> Result<Self, Self::Error> { |
| 62 | + match value { |
| 63 | + 0 => Ok(VariantPrimitiveType::Null), |
| 64 | + 1 => Ok(VariantPrimitiveType::BooleanTrue), |
| 65 | + 2 => Ok(VariantPrimitiveType::BooleanFalse), |
| 66 | + 3 => Ok(VariantPrimitiveType::Int8), |
| 67 | + // TODO: Add types for the rest, once API is agreed upon |
| 68 | + 16 => Ok(VariantPrimitiveType::String), |
| 69 | + _ => Err(ArrowError::InvalidArgumentError(format!( |
| 70 | + "unknown primitive type: {}", |
| 71 | + value |
| 72 | + ))), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +/// Extract the primitive type from a Variant value-header byte |
| 77 | +pub(crate) fn get_primitive_type(header: u8) -> Result<VariantPrimitiveType, ArrowError> { |
| 78 | + // last 6 bits contain the primitive-type, see spec |
| 79 | + VariantPrimitiveType::try_from(header >> 2) |
| 80 | +} |
| 81 | + |
| 82 | +/// To be used in `map_err` when unpacking an integer from a slice of bytes. |
| 83 | +fn map_try_from_slice_error(e: TryFromSliceError) -> ArrowError { |
| 84 | + ArrowError::InvalidArgumentError(e.to_string()) |
| 85 | +} |
| 86 | + |
| 87 | +/// Decodes an Int8 from the value section of a variant. |
| 88 | +pub(crate) fn decode_int8(value: &[u8]) -> Result<i8, ArrowError> { |
| 89 | + let value = i8::from_le_bytes(array_from_slice(value, 1)?); |
| 90 | + Ok(value) |
| 91 | +} |
| 92 | + |
| 93 | +/// Decodes a long string from the value section of a variant. |
| 94 | +pub(crate) fn decode_long_string(value: &[u8]) -> Result<&str, ArrowError> { |
| 95 | + let len = u32::from_le_bytes(array_from_slice(value, 1)?) as usize; |
| 96 | + let string = string_from_slice(value, 5..5 + len)?; |
| 97 | + Ok(string) |
| 98 | +} |
| 99 | + |
| 100 | +/// Decodes a short string from the value section of a variant. |
| 101 | +pub(crate) fn decode_short_string(value: &[u8]) -> Result<&str, ArrowError> { |
| 102 | + let len = (first_byte_from_slice(value)? >> 2) as usize; |
| 103 | + |
| 104 | + let string = string_from_slice(value, 1..1 + len)?; |
| 105 | + Ok(string) |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_i8() -> Result<(), ArrowError> { |
| 114 | + let value = [ |
| 115 | + 0 | 3 << 2, // Primitive type for i8 |
| 116 | + 42, |
| 117 | + ]; |
| 118 | + let result = decode_int8(&value)?; |
| 119 | + assert_eq!(result, 42); |
| 120 | + Ok(()) |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn test_short_string() -> Result<(), ArrowError> { |
| 125 | + let value = [ |
| 126 | + 1 | 5 << 2, // Basic type for short string | length of short string |
| 127 | + 'H' as u8, |
| 128 | + 'e' as u8, |
| 129 | + 'l' as u8, |
| 130 | + 'l' as u8, |
| 131 | + 'o' as u8, |
| 132 | + 'o' as u8, |
| 133 | + ]; |
| 134 | + let result = decode_short_string(&value)?; |
| 135 | + assert_eq!(result, "Hello"); |
| 136 | + Ok(()) |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_string() -> Result<(), ArrowError> { |
| 141 | + let value = [ |
| 142 | + 0 | 16 << 2, // Basic type for short string | length of short string |
| 143 | + 5, |
| 144 | + 0, |
| 145 | + 0, |
| 146 | + 0, // Length of string |
| 147 | + 'H' as u8, |
| 148 | + 'e' as u8, |
| 149 | + 'l' as u8, |
| 150 | + 'l' as u8, |
| 151 | + 'o' as u8, |
| 152 | + 'o' as u8, |
| 153 | + ]; |
| 154 | + let result = decode_long_string(&value)?; |
| 155 | + assert_eq!(result, "Hello"); |
| 156 | + Ok(()) |
| 157 | + } |
| 158 | +} |
0 commit comments