parquet_variant/variant/list.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.
17use crate::decoder::{OffsetSizeBytes, map_bytes_to_offsets};
18use crate::utils::{
19 first_byte_from_slice, overflow_error, slice_from_slice, slice_from_slice_at_offset,
20};
21use crate::variant::{Variant, VariantMetadata};
22
23use arrow_schema::ArrowError;
24
25// The value header occupies one byte; use a named constant for readability
26const NUM_HEADER_BYTES: u32 = 1;
27
28/// A parsed version of the variant array value header byte.
29#[derive(Debug, Clone, PartialEq)]
30pub(crate) struct VariantListHeader {
31 num_elements_size: OffsetSizeBytes,
32 offset_size: OffsetSizeBytes,
33}
34
35impl VariantListHeader {
36 // Hide the ugly casting
37 const fn num_elements_size(&self) -> u32 {
38 self.num_elements_size as _
39 }
40 const fn offset_size(&self) -> u32 {
41 self.offset_size as _
42 }
43
44 // Avoid materializing this offset, since it's cheaply and safely computable
45 const fn first_offset_byte(&self) -> u32 {
46 NUM_HEADER_BYTES + self.num_elements_size()
47 }
48
49 pub(crate) fn try_new(header_byte: u8) -> Result<Self, ArrowError> {
50 // The 6 first bits to the left are the value_header and the 2 bits
51 // to the right are the basic type, so we shift to get only the value_header
52 let value_header = header_byte >> 2;
53 let is_large = (value_header & 0x04) != 0; // 3rd bit from the right
54 let field_offset_size_minus_one = value_header & 0x03; // Last two bits
55
56 // The size of the num_elements entry in the array value_data is 4 bytes if
57 // is_large is true, otherwise 1 byte.
58 let num_elements_size = match is_large {
59 true => OffsetSizeBytes::Four,
60 false => OffsetSizeBytes::One,
61 };
62 let offset_size = OffsetSizeBytes::try_new(field_offset_size_minus_one)?;
63
64 Ok(Self {
65 num_elements_size,
66 offset_size,
67 })
68 }
69}
70
71/// [`Variant`] Array.
72///
73/// See the [Variant spec] for details.
74///
75/// NOTE: The "list" naming differs from the variant spec -- which calls it "array" -- in order to be
76/// consistent with Parquet and Arrow type naming. Otherwise, the name would conflict with the
77/// `VariantArray : Array` we must eventually define for variant-typed arrow arrays.
78///
79/// # Validation
80///
81/// Every instance of variant list is either _valid_ or _invalid_. depending on whether the
82/// underlying bytes are a valid encoding of a variant array (see below).
83///
84/// Instances produced by [`Self::try_new`] or [`Self::with_full_validation`] are fully _validated_. They always
85/// contain _valid_ data, and infallible accesses such as iteration and indexing are panic-free. The
86/// validation cost is linear in the number of underlying bytes.
87///
88/// Instances produced by [`Self::new`] are _unvalidated_ and so they may contain either _valid_ or
89/// _invalid_ data. Infallible accesses such as iteration and indexing will panic if the underlying
90/// bytes are _invalid_, and fallible alternatives such as [`Self::iter_try`] and [`Self::get`] are
91/// provided as panic-free alternatives. [`Self::with_full_validation`] can also be used to _validate_ an
92/// _unvalidated_ instance, if desired.
93///
94/// _Unvalidated_ instances can be constructed in constant time. This can be useful if the caller
95/// knows the underlying bytes were already validated previously, or if the caller intends to
96/// perform a small number of (fallible) accesses to a large list.
97///
98/// A _validated_ variant list instance guarantees that:
99///
100/// - header byte is valid
101/// - num_elements is in bounds
102/// - offset array content is in-bounds
103/// - first offset is zero
104/// - last offset is in-bounds
105/// - all other offsets are in-bounds (*)
106/// - all offsets are monotonically increasing (*)
107/// - all values are (recursively) valid variant objects (*)
108/// - the associated variant metadata is [valid] (*)
109///
110/// NOTE: [`Self::new`] only skips expensive (non-constant cost) validation checks (marked by `(*)`
111/// in the list above); it panics any of the other checks fails.
112///
113/// # Safety
114///
115/// Even an _invalid_ variant list instance is still _safe_ to use in the Rust sense. Accessing
116/// it with infallible methods may cause panics but will never lead to undefined behavior.
117///
118/// [valid]: VariantMetadata#Validation
119/// [Variant spec]: https://github.com/apache/parquet-format/blob/master/VariantEncoding.md#value-data-for-array-basic_type3
120#[derive(Debug, Clone)]
121pub struct VariantList<'m, 'v> {
122 pub metadata: VariantMetadata<'m>,
123 pub value: &'v [u8],
124 header: VariantListHeader,
125 num_elements: u32,
126 first_value_byte: u32,
127 validated: bool,
128}
129
130// We don't want this to grow because it could increase the size of `Variant` and hurt performance.
131#[cfg(target_pointer_width = "64")]
132const _: () = crate::utils::expect_size_of::<VariantList>(64);
133
134#[cfg(target_pointer_width = "32")]
135const _: () = crate::utils::expect_size_of::<VariantList>(40);
136
137impl<'m, 'v> VariantList<'m, 'v> {
138 /// Attempts to interpret `value` as a variant array value.
139 ///
140 /// # Validation
141 ///
142 /// This constructor verifies that `value` points to a valid variant array value. In particular,
143 /// that all offsets are in-bounds and point to valid (recursively validated) objects.
144 pub fn try_new(metadata: VariantMetadata<'m>, value: &'v [u8]) -> Result<Self, ArrowError> {
145 Self::try_new_with_shallow_validation(metadata, value)?.with_full_validation()
146 }
147
148 pub fn new(metadata: VariantMetadata<'m>, value: &'v [u8]) -> Self {
149 Self::try_new_with_shallow_validation(metadata, value).expect("Invalid variant list value")
150 }
151
152 /// Attempts to interpet `metadata` and `value` as a variant array, performing only basic
153 /// (constant-cost) [validation].
154 ///
155 /// [validation]: Self#Validation
156 pub(crate) fn try_new_with_shallow_validation(
157 metadata: VariantMetadata<'m>,
158 value: &'v [u8],
159 ) -> Result<Self, ArrowError> {
160 let header_byte = first_byte_from_slice(value)?;
161 let header = VariantListHeader::try_new(header_byte)?;
162
163 // Skip the header byte to read the num_elements; the offset array immediately follows
164 let num_elements =
165 header
166 .num_elements_size
167 .unpack_u32_at_offset(value, NUM_HEADER_BYTES as _, 0)?;
168
169 // (num_elements + 1) * offset_size + first_offset_byte
170 let first_value_byte = num_elements
171 .checked_add(1)
172 .and_then(|n| n.checked_mul(header.offset_size()))
173 .and_then(|n| n.checked_add(header.first_offset_byte()))
174 .ok_or_else(|| overflow_error("offset of variant list values"))?;
175
176 let mut new_self = Self {
177 metadata,
178 value,
179 header,
180 num_elements,
181 first_value_byte,
182 validated: false,
183 };
184
185 // Validate just the first and last offset, ignoring the other offsets and all value bytes.
186 let first_offset = new_self.get_offset(0)?;
187 if first_offset != 0 {
188 return Err(ArrowError::InvalidArgumentError(format!(
189 "First offset is not zero: {first_offset}"
190 )));
191 }
192
193 // Use the last offset to upper-bound the value buffer
194 let last_offset = new_self
195 .get_offset(num_elements as _)?
196 .checked_add(first_value_byte)
197 .ok_or_else(|| overflow_error("variant array size"))?;
198 new_self.value = slice_from_slice(value, ..last_offset as _)?;
199 Ok(new_self)
200 }
201
202 /// True if this instance is fully [validated] for panic-free infallible accesses.
203 ///
204 /// [validated]: Self#Validation
205 pub fn is_fully_validated(&self) -> bool {
206 self.validated
207 }
208
209 /// Performs a full [validation] of this variant array and returns the result.
210 ///
211 /// [validation]: Self#Validation
212 pub fn with_full_validation(mut self) -> Result<Self, ArrowError> {
213 if !self.validated {
214 // Validate the metadata dictionary first, if not already validated, because we pass it
215 // by value to all the children (who would otherwise re-validate it repeatedly).
216 self.metadata = self.metadata.with_full_validation()?;
217
218 let offset_buffer = slice_from_slice(
219 self.value,
220 self.header.first_offset_byte() as _..self.first_value_byte as _,
221 )?;
222
223 let value_buffer = slice_from_slice(self.value, self.first_value_byte as _..)?;
224
225 // Validate whether values are valid variant objects
226 //
227 // Since we use offsets to slice into the value buffer, this also verifies all offsets are in-bounds
228 // and monotonically increasing
229 let mut offset_iter = map_bytes_to_offsets(offset_buffer, self.header.offset_size);
230 let mut current_offset = offset_iter.next().unwrap_or(0);
231
232 for next_offset in offset_iter {
233 let value_bytes = slice_from_slice(value_buffer, current_offset..next_offset)?;
234 Variant::try_new_with_metadata(self.metadata.clone(), value_bytes)?;
235 current_offset = next_offset;
236 }
237
238 self.validated = true;
239 }
240 Ok(self)
241 }
242
243 /// Return the length of this array
244 pub fn len(&self) -> usize {
245 self.num_elements as _
246 }
247
248 /// Is the array of zero length
249 pub fn is_empty(&self) -> bool {
250 self.len() == 0
251 }
252
253 /// Returns element by index in `0..self.len()`, if any. May panic if this list is [invalid].
254 ///
255 /// [invalid]: Self#Validation
256 pub fn get(&self, index: usize) -> Option<Variant<'m, 'v>> {
257 (index < self.len()).then(|| {
258 self.try_get_with_shallow_validation(index)
259 .expect("Invalid variant array element")
260 })
261 }
262
263 /// Fallible version of `get`. Returns element by index, capturing validation errors
264 pub fn try_get(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
265 self.try_get_with_shallow_validation(index)?
266 .with_full_validation()
267 }
268
269 // Fallible version of `get`, performing only basic (constant-time) validation.
270 fn try_get_with_shallow_validation(&self, index: usize) -> Result<Variant<'m, 'v>, ArrowError> {
271 // Fetch the value bytes between the two offsets for this index, from the value array region
272 // of the byte buffer
273 let byte_range = self.get_offset(index)? as _..self.get_offset(index + 1)? as _;
274 let value_bytes =
275 slice_from_slice_at_offset(self.value, self.first_value_byte as _, byte_range)?;
276 Variant::try_new_with_metadata_and_shallow_validation(self.metadata.clone(), value_bytes)
277 }
278
279 /// Iterates over the values of this list. When working with [unvalidated] input, consider
280 /// [`Self::iter_try`] to avoid panics due to invalid data.
281 ///
282 /// [unvalidated]: Self#Validation
283 pub fn iter(&self) -> impl Iterator<Item = Variant<'m, 'v>> + '_ {
284 self.iter_try_with_shallow_validation()
285 .map(|result| result.expect("Invalid variant list entry"))
286 }
287
288 /// Fallible iteration over the elements of this list.
289 pub fn iter_try(&self) -> impl Iterator<Item = Result<Variant<'m, 'v>, ArrowError>> + '_ {
290 self.iter_try_with_shallow_validation()
291 .map(|result| result?.with_full_validation())
292 }
293
294 // Fallible iteration that only performs basic (constant-time) validation.
295 fn iter_try_with_shallow_validation(
296 &self,
297 ) -> impl Iterator<Item = Result<Variant<'m, 'v>, ArrowError>> + '_ {
298 (0..self.len()).map(|i| self.try_get_with_shallow_validation(i))
299 }
300
301 // Attempts to retrieve the ith offset from the offset array region of the byte buffer.
302 fn get_offset(&self, index: usize) -> Result<u32, ArrowError> {
303 let byte_range = self.header.first_offset_byte() as _..self.first_value_byte as _;
304 let offset_bytes = slice_from_slice(self.value, byte_range)?;
305 self.header.offset_size.unpack_u32(offset_bytes, index)
306 }
307}
308
309// Custom implementation of PartialEq for variant arrays
310//
311// Instead of comparing the raw bytes of 2 variant lists, this implementation recursively
312// checks whether their elements are equal.
313impl<'m, 'v> PartialEq for VariantList<'m, 'v> {
314 fn eq(&self, other: &Self) -> bool {
315 if self.num_elements != other.num_elements {
316 return false;
317 }
318
319 self.iter().zip(other.iter()).all(|(a, b)| a == b)
320 }
321}
322
323#[cfg(test)]
324mod tests {
325 use super::*;
326 use crate::VariantBuilder;
327 use std::iter::repeat_n;
328 use std::ops::Range;
329
330 #[test]
331 fn test_variant_list_simple() {
332 // Create simple metadata (empty dictionary for this test)
333 let metadata_bytes = vec![
334 0x01, // header: version=1, sorted=0, offset_size_minus_one=0
335 0, // dictionary_size = 0
336 0, // offset[0] = 0 (end of dictionary)
337 ];
338 let metadata = VariantMetadata::try_new(&metadata_bytes).unwrap();
339
340 // Create list value data for: [42, true, "hi"]
341 // Header: basic_type=3 (array), field_offset_size_minus_one=0, is_large=0
342 // value_header = 0000_0_0_00 = 0x00
343 // So header byte = (0x00 << 2) | 3 = 0x03
344 let list_value = vec![
345 0x03, // header: basic_type=3, value_header=0x00
346 3, // num_elements = 3
347 // Offsets (1 byte each): 4 offsets total
348 0, // offset to first value (int8)
349 2, // offset to second value (boolean true)
350 3, // offset to third value (short string)
351 6, // end offset
352 // Values:
353 0x0C,
354 42, // int8: primitive_header=3, basic_type=0 -> (3 << 2) | 0 = 0x0C, then value 42
355 0x04, // boolean true: primitive_header=1, basic_type=0 -> (1 << 2) | 0 = 0x04
356 0x09, b'h', b'i', // short string: length=2, basic_type=1 -> (2 << 2) | 1 = 0x09
357 ];
358
359 let variant_list = VariantList::try_new(metadata, &list_value).unwrap();
360
361 // Test basic properties
362 assert_eq!(variant_list.len(), 3);
363 assert!(!variant_list.is_empty());
364
365 // Test individual element access
366 let elem0 = variant_list.get(0).unwrap();
367 assert_eq!(elem0.as_int8(), Some(42));
368
369 let elem1 = variant_list.get(1).unwrap();
370 assert_eq!(elem1.as_boolean(), Some(true));
371
372 let elem2 = variant_list.get(2).unwrap();
373 assert_eq!(elem2.as_string(), Some("hi"));
374
375 // Test out of bounds access
376 let out_of_bounds = variant_list.get(3);
377 assert!(out_of_bounds.is_none());
378
379 // Test values iterator
380 let values: Vec<_> = variant_list.iter().collect();
381 assert_eq!(values.len(), 3);
382 assert_eq!(values[0].as_int8(), Some(42));
383 assert_eq!(values[1].as_boolean(), Some(true));
384 assert_eq!(values[2].as_string(), Some("hi"));
385 }
386
387 #[test]
388 fn test_variant_list_empty() {
389 // Create simple metadata (empty dictionary)
390 let metadata_bytes = vec![
391 0x01, // header: version=1, sorted=0, offset_size_minus_one=0
392 0, // dictionary_size = 0
393 0, // offset[0] = 0 (end of dictionary)
394 ];
395 let metadata = VariantMetadata::try_new(&metadata_bytes).unwrap();
396
397 // Create empty list value data: []
398 let list_value = vec![
399 0x03, // header: basic_type=3, value_header=0x00
400 0, // num_elements = 0
401 0, // single offset pointing to end
402 // No values
403 ];
404
405 let variant_list = VariantList::try_new(metadata, &list_value).unwrap();
406
407 // Test basic properties
408 assert_eq!(variant_list.len(), 0);
409 assert!(variant_list.is_empty());
410
411 // Test out of bounds access on empty list
412 let out_of_bounds = variant_list.get(0);
413 assert!(out_of_bounds.is_none());
414
415 // Test values iterator on empty list
416 let values: Vec<_> = variant_list.iter().collect();
417 assert_eq!(values.len(), 0);
418 }
419
420 #[test]
421 fn test_variant_list_large() {
422 // Create simple metadata (empty dictionary)
423 let metadata_bytes = vec![
424 0x01, // header: version=1, sorted=0, offset_size_minus_one=0
425 0, // dictionary_size = 0
426 0, // offset[0] = 0 (end of dictionary)
427 ];
428 let metadata = VariantMetadata::try_new(&metadata_bytes).unwrap();
429
430 // Create large list value data with 2-byte offsets: [null, false]
431 // Header: is_large=1, field_offset_size_minus_one=1, basic_type=3 (array)
432 let list_bytes = vec![
433 0x17, // header = 000_1_01_11 = 0x17
434 2, 0, 0, 0, // num_elements = 2 (4 bytes because is_large=1)
435 // Offsets (2 bytes each): 3 offsets total
436 0x00, 0x00, 0x01, 0x00, // first value (null)
437 0x02, 0x00, // second value (boolean false)
438 // Values:
439 0x00, // null: primitive_header=0, basic_type=0 -> (0 << 2) | 0 = 0x00
440 0x08, // boolean false: primitive_header=2, basic_type=0 -> (2 << 2) | 0 = 0x08
441 ];
442
443 let variant_list = VariantList::try_new(metadata, &list_bytes).unwrap();
444
445 // Test basic properties
446 assert_eq!(variant_list.len(), 2);
447 assert!(!variant_list.is_empty());
448
449 // Test individual element access
450 let elem0 = variant_list.get(0).unwrap();
451 assert_eq!(elem0.as_null(), Some(()));
452
453 let elem1 = variant_list.get(1).unwrap();
454 assert_eq!(elem1.as_boolean(), Some(false));
455 }
456
457 #[test]
458 fn test_large_variant_list_with_total_child_length_between_2_pow_8_and_2_pow_16() {
459 // all the tests below will set the total child size to ~500,
460 // which is larger than 2^8 but less than 2^16.
461 // total child size = list_size * single_child_item_len
462
463 let mut list_size: usize = 1;
464 let mut single_child_item_len: usize = 500;
465
466 // offset size will be OffSizeBytes::Two as the total child length between 2^8 and 2^16
467 let expected_offset_size = OffsetSizeBytes::Two;
468
469 test_large_variant_list_with_child_length(
470 list_size, // the elements in the list
471 single_child_item_len, // this will control the total child size in the list
472 OffsetSizeBytes::One, // will be OffsetSizeBytes::One as the size of the list is less than 256
473 expected_offset_size,
474 );
475
476 list_size = 255;
477 single_child_item_len = 2;
478 test_large_variant_list_with_child_length(
479 list_size,
480 single_child_item_len,
481 OffsetSizeBytes::One, // will be OffsetSizeBytes::One as the size of the list is less than 256
482 expected_offset_size,
483 );
484
485 list_size = 256;
486 single_child_item_len = 2;
487 test_large_variant_list_with_child_length(
488 list_size,
489 single_child_item_len,
490 OffsetSizeBytes::Four, // will be OffsetSizeBytes::Four as the size of the list is bigger than 255
491 expected_offset_size,
492 );
493
494 list_size = 300;
495 single_child_item_len = 2;
496 test_large_variant_list_with_child_length(
497 list_size,
498 single_child_item_len,
499 OffsetSizeBytes::Four, // will be OffsetSizeBytes::Four as the size of the list is bigger than 255
500 expected_offset_size,
501 );
502 }
503
504 #[test]
505 fn test_large_variant_list_with_total_child_length_between_2_pow_16_and_2_pow_24() {
506 // all the tests below will set the total child size to ~70,000,
507 // which is larger than 2^16 but less than 2^24.
508 // total child size = list_size * single_child_item_len
509
510 let mut list_size: usize = 1;
511 let mut single_child_item_len: usize = 70000;
512
513 // offset size will be OffSizeBytes::Two as the total child length between 2^16 and 2^24
514 let expected_offset_size = OffsetSizeBytes::Three;
515
516 test_large_variant_list_with_child_length(
517 list_size,
518 single_child_item_len,
519 OffsetSizeBytes::One, // will be OffsetSizeBytes::One as the size of the list is less than 256
520 expected_offset_size,
521 );
522
523 list_size = 255;
524 single_child_item_len = 275;
525 // total child size = 255 * 275 = 70,125
526 test_large_variant_list_with_child_length(
527 list_size,
528 single_child_item_len,
529 OffsetSizeBytes::One, // will be OffsetSizeBytes::One as the size of the list is less than 256
530 expected_offset_size,
531 );
532
533 list_size = 256;
534 single_child_item_len = 274;
535 // total child size = 256 * 274 = 70,144
536 test_large_variant_list_with_child_length(
537 list_size,
538 single_child_item_len,
539 OffsetSizeBytes::Four, // will be OffsetSizeBytes::Four as the size of the list is bigger than 255
540 expected_offset_size,
541 );
542
543 list_size = 300;
544 single_child_item_len = 234;
545 // total child size = 300 * 234 = 70,200
546 test_large_variant_list_with_child_length(
547 list_size,
548 single_child_item_len,
549 OffsetSizeBytes::Four, // will be OffsetSizeBytes::Four as the size of the list is bigger than 255
550 expected_offset_size,
551 );
552 }
553
554 #[test]
555 fn test_large_variant_list_with_total_child_length_between_2_pow_24_and_2_pow_32() {
556 // all the tests below will set the total child size to ~20,000,000,
557 // which is larger than 2^24 but less than 2^32.
558 // total child size = list_size * single_child_item_len
559
560 let mut list_size: usize = 1;
561 let mut single_child_item_len: usize = 20000000;
562
563 // offset size will be OffSizeBytes::Two as the total child length between 2^24 and 2^32
564 let expected_offset_size = OffsetSizeBytes::Four;
565
566 test_large_variant_list_with_child_length(
567 list_size,
568 single_child_item_len,
569 OffsetSizeBytes::One, // will be OffsetSizeBytes::One as the size of the list is less than 256
570 expected_offset_size,
571 );
572
573 list_size = 255;
574 single_child_item_len = 78432;
575 // total child size = 255 * 78,432 = 20,000,160
576 test_large_variant_list_with_child_length(
577 list_size,
578 single_child_item_len,
579 OffsetSizeBytes::One, // will be OffsetSizeBytes::One as the size of the list is less than 256
580 expected_offset_size,
581 );
582
583 list_size = 256;
584 single_child_item_len = 78125;
585 // total child size = 256 * 78,125 = 20,000,000
586 test_large_variant_list_with_child_length(
587 list_size,
588 single_child_item_len,
589 OffsetSizeBytes::Four, // will be OffsetSizeBytes::Four as the size of the list is bigger than 255
590 expected_offset_size,
591 );
592
593 list_size = 300;
594 single_child_item_len = 66667;
595 // total child size = 300 * 66,667 = 20,000,100
596 test_large_variant_list_with_child_length(
597 list_size,
598 single_child_item_len,
599 OffsetSizeBytes::Four, // will be OffsetSizeBytes::Four as the size of the list is bigger than 255
600 expected_offset_size,
601 );
602 }
603
604 // this function will create a large variant list from VariantBuilder
605 // with specified size and each child item with the given length.
606 // and verify the content and some meta for the variant list in the final.
607 fn test_large_variant_list_with_child_length(
608 list_size: usize,
609 single_child_item_len: usize,
610 expected_num_element_size: OffsetSizeBytes,
611 expected_offset_size_bytes: OffsetSizeBytes,
612 ) {
613 let mut builder = VariantBuilder::new();
614 let mut list_builder = builder.new_list();
615
616 let mut expected_list = vec![];
617 for i in 0..list_size {
618 let random_string: String =
619 repeat_n(char::from((i % 256) as u8), single_child_item_len).collect();
620
621 list_builder.append_value(Variant::String(random_string.as_str()));
622 expected_list.push(random_string);
623 }
624
625 list_builder.finish();
626 // Finish the builder to get the metadata and value
627 let (metadata, value) = builder.finish();
628 // use the Variant API to verify the result
629 let variant = Variant::try_new(&metadata, &value).unwrap();
630
631 let variant_list = variant.as_list().unwrap();
632
633 // verify that the head is expected
634 assert_eq!(expected_offset_size_bytes, variant_list.header.offset_size);
635 assert_eq!(
636 expected_num_element_size,
637 variant_list.header.num_elements_size
638 );
639 assert_eq!(list_size, variant_list.num_elements as usize);
640
641 // verify the data in the variant
642 assert_eq!(list_size, variant_list.len());
643 for i in 0..list_size {
644 let item = variant_list.get(i).unwrap();
645 let item_str = item.as_string().unwrap();
646 assert_eq!(expected_list.get(i).unwrap(), item_str);
647 }
648 }
649
650 #[test]
651 fn test_variant_list_equality() {
652 // Create two lists with the same values (0..10)
653 let (metadata1, value1) = make_listi32(0..10);
654 let list1 = Variant::new(&metadata1, &value1);
655 let (metadata2, value2) = make_listi32(0..10);
656 let list2 = Variant::new(&metadata2, &value2);
657 // They should be equal
658 assert_eq!(list1, list2);
659 }
660
661 #[test]
662 fn test_variant_list_equality_different_length() {
663 // Create two lists with different lengths
664 let (metadata1, value1) = make_listi32(0..10);
665 let list1 = Variant::new(&metadata1, &value1);
666 let (metadata2, value2) = make_listi32(0..5);
667 let list2 = Variant::new(&metadata2, &value2);
668 // They should not be equal
669 assert_ne!(list1, list2);
670 }
671
672 #[test]
673 fn test_variant_list_equality_different_values() {
674 // Create two lists with different values
675 let (metadata1, value1) = make_listi32(0..10);
676 let list1 = Variant::new(&metadata1, &value1);
677 let (metadata2, value2) = make_listi32(5..15);
678 let list2 = Variant::new(&metadata2, &value2);
679 // They should not be equal
680 assert_ne!(list1, list2);
681 }
682
683 #[test]
684 fn test_variant_list_equality_different_types() {
685 // Create two lists with different types
686 let (metadata1, value1) = make_listi32(0i32..10i32);
687 let list1 = Variant::new(&metadata1, &value1);
688 let (metadata2, value2) = make_listi64(0..10);
689 let list2 = Variant::new(&metadata2, &value2);
690 // They should not be equal due to type mismatch
691 assert_ne!(list1, list2);
692 }
693
694 #[test]
695 fn test_variant_list_equality_slices() {
696 // Make an object like this and make sure equality works
697 // when the lists are sub fields
698 //
699 // {
700 // "list1": [0, 1, 2, ..., 9],
701 // "list2": [0, 1, 2, ..., 9],
702 // "list3": [10, 11, 12, ..., 19],
703 // }
704 let (metadata, value) = {
705 let mut builder = VariantBuilder::new();
706 let mut object_builder = builder.new_object();
707 // list1 (0..10)
708 let (metadata1, value1) = make_listi32(0i32..10i32);
709 object_builder.insert("list1", Variant::new(&metadata1, &value1));
710
711 // list2 (0..10)
712 let (metadata2, value2) = make_listi32(0i32..10i32);
713 object_builder.insert("list2", Variant::new(&metadata2, &value2));
714
715 // list3 (10..20)
716 let (metadata3, value3) = make_listi32(10i32..20i32);
717 object_builder.insert("list3", Variant::new(&metadata3, &value3));
718 object_builder.finish();
719 builder.finish()
720 };
721
722 let variant = Variant::try_new(&metadata, &value).unwrap();
723 let object = variant.as_object().unwrap();
724 // Check that list1 and list2 are equal
725 assert_eq!(object.get("list1").unwrap(), object.get("list2").unwrap());
726 // Check that list1 and list3 are not equal
727 assert_ne!(object.get("list1").unwrap(), object.get("list3").unwrap());
728 }
729
730 /// return metadata/value for a simple variant list with values in a range
731 fn make_listi32(range: Range<i32>) -> (Vec<u8>, Vec<u8>) {
732 let mut variant_builder = VariantBuilder::new();
733 let mut list_builder = variant_builder.new_list();
734 list_builder.extend(range);
735 list_builder.finish();
736 variant_builder.finish()
737 }
738
739 /// return metadata/value for a simple variant list with values in a range
740 fn make_listi64(range: Range<i64>) -> (Vec<u8>, Vec<u8>) {
741 let mut variant_builder = VariantBuilder::new();
742 let mut list_builder = variant_builder.new_list();
743 list_builder.extend(range);
744 list_builder.finish();
745 variant_builder.finish()
746 }
747}