Skip to content

Commit e95b0d5

Browse files
committed
[FIX] refactor code for modularity
1 parent 852d6cd commit e95b0d5

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

parquet-variant-compute/src/variant_array.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl VariantArray {
169169
}
170170

171171
/// Get the metadata bytes for a specific index
172-
pub fn metadata(&self, index: usize) -> &[u8] {
172+
pub fn metadata_bytes(&self, index: usize) -> &[u8] {
173173
self.metadata_field().as_binary_view().value(index).as_ref()
174174
}
175175

@@ -210,19 +210,15 @@ impl VariantArray {
210210
if self.is_null(i) {
211211
builder.append_null();
212212
} else {
213-
match FieldOperations::remove_field_bytes(
214-
self.metadata(i),
213+
let new_value = FieldOperations::remove_field_bytes(
214+
self.metadata_bytes(i),
215215
self.value_bytes(i),
216216
field_name,
217-
)? {
218-
Some(new_value) => {
219-
builder.append_variant_buffers(self.metadata(i), &new_value);
220-
}
221-
None => {
222-
// Field didn't exist, use original value
223-
builder.append_variant_buffers(self.metadata(i), self.value_bytes(i));
224-
}
225-
}
217+
)?;
218+
219+
// Use original value if the field didn't exist
220+
let new_value = new_value.as_deref().unwrap_or_else(|| self.value_bytes(i));
221+
builder.append_variant_buffers(self.metadata_bytes(i), new_value);
226222
}
227223
}
228224

@@ -237,19 +233,15 @@ impl VariantArray {
237233
if self.is_null(i) {
238234
builder.append_null();
239235
} else {
240-
match FieldOperations::remove_fields_bytes(
241-
self.metadata(i),
236+
let new_value = FieldOperations::remove_fields_bytes(
237+
self.metadata_bytes(i),
242238
self.value_bytes(i),
243239
field_names,
244-
)? {
245-
Some(new_value) => {
246-
builder.append_variant_buffers(self.metadata(i), &new_value);
247-
}
248-
None => {
249-
// No fields existed, use original value
250-
builder.append_variant_buffers(self.metadata(i), self.value_bytes(i));
251-
}
252-
}
240+
)?;
241+
242+
// Use original value if no fields existed
243+
let new_value = new_value.as_deref().unwrap_or_else(|| self.value_bytes(i));
244+
builder.append_variant_buffers(self.metadata_bytes(i), new_value);
253245
}
254246
}
255247

parquet-variant-compute/src/variant_parser.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ pub enum VariantBasicType {
2828
Array = 3,
2929
}
3030

31-
/// Variant type enumeration covering all possible types
32-
#[derive(Debug, Clone, PartialEq)]
33-
pub enum VariantType {
34-
Primitive(PrimitiveType),
35-
ShortString(ShortStringHeader),
36-
Object(ObjectHeader),
37-
Array(ArrayHeader),
38-
}
39-
4031
/// Primitive type variants
4132
#[derive(Debug, Clone, PartialEq)]
4233
pub enum PrimitiveType {
@@ -59,6 +50,15 @@ pub enum PrimitiveType {
5950
String,
6051
}
6152

53+
/// Variant type enumeration covering all possible types
54+
#[derive(Debug, Clone, PartialEq)]
55+
pub enum VariantType {
56+
Primitive(PrimitiveType),
57+
ShortString(ShortStringHeader),
58+
Object(ObjectHeader),
59+
Array(ArrayHeader),
60+
}
61+
6262
/// Short string header structure
6363
#[derive(Debug, Clone, PartialEq)]
6464
pub struct ShortStringHeader {
@@ -150,6 +150,19 @@ impl VariantParser {
150150
}
151151
}
152152

153+
/// Get the basic type from header byte
154+
pub fn get_basic_type(header_byte: u8) -> VariantBasicType {
155+
match header_byte & 0x03 {
156+
0 => VariantBasicType::Primitive,
157+
1 => VariantBasicType::ShortString,
158+
2 => VariantBasicType::Object,
159+
3 => VariantBasicType::Array,
160+
_ => panic!("Invalid basic type: {}", header_byte & 0x03),
161+
}
162+
}
163+
164+
165+
153166
/// Parse short string header
154167
pub fn parse_short_string_header(header_byte: u8) -> Result<ShortStringHeader, ArrowError> {
155168
let length = (header_byte >> 2) as usize;
@@ -259,16 +272,7 @@ impl VariantParser {
259272
}
260273
}
261274

262-
/// Get the basic type from header byte
263-
pub fn get_basic_type(header_byte: u8) -> VariantBasicType {
264-
match header_byte & 0x03 {
265-
0 => VariantBasicType::Primitive,
266-
1 => VariantBasicType::ShortString,
267-
2 => VariantBasicType::Object,
268-
3 => VariantBasicType::Array,
269-
_ => panic!("Invalid basic type: {}", header_byte & 0x03),
270-
}
271-
}
275+
272276

273277
/// Check if value bytes represent a primitive
274278
pub fn is_primitive(value_bytes: &[u8]) -> bool {

0 commit comments

Comments
 (0)