Skip to content

Commit b1afed1

Browse files
committed
[FIX] minor fixes
1 parent be30239 commit b1afed1

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

parquet-variant-compute/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ pub mod to_json;
2727
pub use variant_array::VariantArray;
2828
pub use variant_array_builder::VariantArrayBuilder;
2929
pub use field_operations::{VariantPath, VariantPathElement};
30-
pub use variant_parser::{VariantType, PrimitiveType, ShortStringHeader, ObjectHeader, ArrayHeader};
30+
pub use variant_parser::{VariantType, VariantBasicType, PrimitiveType, ShortStringHeader, ObjectHeader, ArrayHeader};
3131
pub use from_json::batch_json_string_to_variant;
3232
pub use to_json::batch_variant_to_json_string;

parquet-variant-compute/src/variant_parser.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
2020
use arrow::error::ArrowError;
2121

22+
/// Basic variant type enumeration for the first 2 bits of header
23+
#[derive(Debug, Clone, PartialEq)]
24+
pub enum VariantBasicType {
25+
Primitive = 0,
26+
ShortString = 1,
27+
Object = 2,
28+
Array = 3,
29+
}
30+
2231
/// Variant type enumeration covering all possible types
2332
#[derive(Debug, Clone, PartialEq)]
2433
pub enum VariantType {
@@ -94,16 +103,13 @@ pub struct VariantParser;
94103
impl VariantParser {
95104
/// General dispatch function to parse any variant header
96105
pub fn parse_variant_header(header_byte: u8) -> Result<VariantType, ArrowError> {
97-
let basic_type = header_byte & 0x03;
106+
let basic_type = Self::get_basic_type(header_byte);
98107

99108
match basic_type {
100-
0 => Ok(VariantType::Primitive(Self::parse_primitive_header(header_byte)?)),
101-
1 => Ok(VariantType::ShortString(Self::parse_short_string_header(header_byte)?)),
102-
2 => Ok(VariantType::Object(Self::parse_object_header(header_byte)?)),
103-
3 => Ok(VariantType::Array(Self::parse_array_header(header_byte)?)),
104-
_ => Err(ArrowError::InvalidArgumentError(
105-
format!("Invalid basic type: {}", basic_type)
106-
)),
109+
VariantBasicType::Primitive => Ok(VariantType::Primitive(Self::parse_primitive_header(header_byte)?)),
110+
VariantBasicType::ShortString => Ok(VariantType::ShortString(Self::parse_short_string_header(header_byte)?)),
111+
VariantBasicType::Object => Ok(VariantType::Object(Self::parse_object_header(header_byte)?)),
112+
VariantBasicType::Array => Ok(VariantType::Array(Self::parse_array_header(header_byte)?)),
107113
}
108114
}
109115

@@ -236,40 +242,46 @@ impl VariantParser {
236242
}
237243

238244
/// Get the basic type from header byte
239-
pub fn get_basic_type(header_byte: u8) -> u8 {
240-
header_byte & 0x03
245+
pub fn get_basic_type(header_byte: u8) -> VariantBasicType {
246+
match header_byte & 0x03 {
247+
0 => VariantBasicType::Primitive,
248+
1 => VariantBasicType::ShortString,
249+
2 => VariantBasicType::Object,
250+
3 => VariantBasicType::Array,
251+
_ => panic!("Invalid basic type: {}", header_byte & 0x03),
252+
}
241253
}
242254

243255
/// Check if value bytes represent a primitive
244256
pub fn is_primitive(value_bytes: &[u8]) -> bool {
245257
if value_bytes.is_empty() {
246258
return false;
247259
}
248-
Self::get_basic_type(value_bytes[0]) == 0
260+
Self::get_basic_type(value_bytes[0]) == VariantBasicType::Primitive
249261
}
250262

251263
/// Check if value bytes represent a short string
252264
pub fn is_short_string(value_bytes: &[u8]) -> bool {
253265
if value_bytes.is_empty() {
254266
return false;
255267
}
256-
Self::get_basic_type(value_bytes[0]) == 1
268+
Self::get_basic_type(value_bytes[0]) == VariantBasicType::ShortString
257269
}
258270

259271
/// Check if value bytes represent an object
260272
pub fn is_object(value_bytes: &[u8]) -> bool {
261273
if value_bytes.is_empty() {
262274
return false;
263275
}
264-
Self::get_basic_type(value_bytes[0]) == 2
276+
Self::get_basic_type(value_bytes[0]) == VariantBasicType::Object
265277
}
266278

267279
/// Check if value bytes represent an array
268280
pub fn is_array(value_bytes: &[u8]) -> bool {
269281
if value_bytes.is_empty() {
270282
return false;
271283
}
272-
Self::get_basic_type(value_bytes[0]) == 3
284+
Self::get_basic_type(value_bytes[0]) == VariantBasicType::Array
273285
}
274286

275287
/// Get the data length for a primitive type

0 commit comments

Comments
 (0)