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