forked from apache/arrow-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariant_array.rs
More file actions
1091 lines (996 loc) · 39.1 KB
/
variant_array.rs
File metadata and controls
1091 lines (996 loc) · 39.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! [`VariantArray`] implementation
use crate::type_conversion::primitive_conversion_single_value;
use arrow::array::{Array, ArrayRef, AsArray, BinaryViewArray, StructArray};
use arrow::buffer::NullBuffer;
use arrow::compute::cast;
use arrow::datatypes::{
Date32Type, Float16Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type,
UInt16Type, UInt32Type, UInt64Type, UInt8Type,
};
use arrow_schema::extension::ExtensionType;
use arrow_schema::{ArrowError, DataType, Field, FieldRef, Fields};
use parquet_variant::Uuid;
use parquet_variant::Variant;
use std::sync::Arc;
/// Arrow Variant [`ExtensionType`].
///
/// Represents the canonical Arrow Extension Type for storing variants.
/// See [`VariantArray`] for more examples of using this extension type.
pub struct VariantType;
impl ExtensionType for VariantType {
const NAME: &'static str = "parquet.variant";
// Variants have no extension metadata
type Metadata = ();
fn metadata(&self) -> &Self::Metadata {
&()
}
fn serialize_metadata(&self) -> Option<String> {
None
}
fn deserialize_metadata(_metadata: Option<&str>) -> Result<Self::Metadata, ArrowError> {
Ok(())
}
fn supports_data_type(&self, data_type: &DataType) -> Result<(), ArrowError> {
if matches!(data_type, DataType::Struct(_)) {
Ok(())
} else {
Err(ArrowError::InvalidArgumentError(format!(
"VariantType only supports StructArray, got {}",
data_type
)))
}
}
fn try_new(data_type: &DataType, _metadata: Self::Metadata) -> Result<Self, ArrowError> {
Self.supports_data_type(data_type)?;
Ok(Self)
}
}
/// An array of Parquet [`Variant`] values
///
/// A [`VariantArray`] wraps an Arrow [`StructArray`] that stores the underlying
/// `metadata` and `value` fields, and adds convenience methods to access
/// the [`Variant`]s.
///
/// See [`VariantArrayBuilder`] for constructing `VariantArray` row by row.
///
/// See the examples below from converting between `VariantArray` and
/// `StructArray`.
///
/// [`VariantArrayBuilder`]: crate::VariantArrayBuilder
///
/// # Documentation
///
/// At the time of this writing, Variant has been accepted as an official
/// extension type but not been published to the [official list of extension
/// types] on the Apache Arrow website. See the [Extension Type for Parquet
/// Variant arrow] ticket for more details.
///
/// [Extension Type for Parquet Variant arrow]: https://github.com/apache/arrow/issues/46908
/// [official list of extension types]: https://arrow.apache.org/docs/format/CanonicalExtensions.html
///
/// # Example: Check if a [`StructArray`] has the [`VariantType`] extension
///
/// Arrow Arrays only provide [`DataType`], but the extension type information
/// is stored on a [`Field`]. Thus, you must have access to the [`Schema`] or
/// [`Field`] to check for the extension type.
///
/// [`Schema`]: arrow_schema::Schema
/// ```
/// # use arrow::array::StructArray;
/// # use arrow_schema::{Schema, Field, DataType};
/// # use parquet_variant::Variant;
/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray, VariantType};
/// # fn get_variant_array() -> VariantArray {
/// # let mut builder = VariantArrayBuilder::new(10);
/// # builder.append_variant(Variant::from("such wow"));
/// # builder.build()
/// # }
/// # fn get_schema() -> Schema {
/// # Schema::new(vec![
/// # Field::new("id", DataType::Int32, false),
/// # get_variant_array().field("var"),
/// # ])
/// # }
/// let schema = get_schema();
/// assert_eq!(schema.fields().len(), 2);
/// // first field is not a Variant
/// assert!(schema.field(0).try_extension_type::<VariantType>().is_err());
/// // second field is a Variant
/// assert!(schema.field(1).try_extension_type::<VariantType>().is_ok());
/// ```
///
/// # Example: Constructing the correct [`Field`] for a [`VariantArray`]
///
/// You can construct the correct [`Field`] for a [`VariantArray`] using the
/// [`VariantArray::field`] method.
///
/// ```
/// # use arrow_schema::{Schema, Field, DataType};
/// # use parquet_variant::Variant;
/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray, VariantType};
/// # fn get_variant_array() -> VariantArray {
/// # let mut builder = VariantArrayBuilder::new(10);
/// # builder.append_variant(Variant::from("such wow"));
/// # builder.build()
/// # }
/// let variant_array = get_variant_array();
/// // First field is an integer id, second field is a variant
/// let schema = Schema::new(vec![
/// Field::new("id", DataType::Int32, false),
/// // call VariantArray::field to get the correct Field
/// variant_array.field("var"),
/// ]);
/// ```
///
/// You can also construct the [`Field`] using [`VariantType`] directly
///
/// ```
/// # use arrow_schema::{Schema, Field, DataType};
/// # use parquet_variant::Variant;
/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray, VariantType};
/// # fn get_variant_array() -> VariantArray {
/// # let mut builder = VariantArrayBuilder::new(10);
/// # builder.append_variant(Variant::from("such wow"));
/// # builder.build()
/// # }
/// # let variant_array = get_variant_array();
/// // The DataType of a VariantArray varies depending on how it is shredded
/// let data_type = variant_array.data_type().clone();
/// // First field is an integer id, second field is a variant
/// let schema = Schema::new(vec![
/// Field::new("id", DataType::Int32, false),
/// Field::new("var", data_type, false)
/// // Add extension metadata to the field using `VariantType`
/// .with_extension_type(VariantType),
/// ]);
/// ```
///
/// # Example: Converting a [`VariantArray`] to a [`StructArray`]
///
/// ```
/// # use arrow::array::StructArray;
/// # use parquet_variant::Variant;
/// # use parquet_variant_compute::VariantArrayBuilder;
/// // Create Variant Array
/// let mut builder = VariantArrayBuilder::new(10);
/// builder.append_variant(Variant::from("such wow"));
/// let variant_array = builder.build();
/// // convert to StructArray
/// let struct_array: StructArray = variant_array.into();
/// ```
///
/// # Example: Converting a [`StructArray`] to a [`VariantArray`]
///
/// ```
/// # use arrow::array::StructArray;
/// # use parquet_variant::Variant;
/// # use parquet_variant_compute::{VariantArrayBuilder, VariantArray};
/// # fn get_struct_array() -> StructArray {
/// # let mut builder = VariantArrayBuilder::new(10);
/// # builder.append_variant(Variant::from("such wow"));
/// # builder.build().into()
/// # }
/// let struct_array: StructArray = get_struct_array();
/// // try and create a VariantArray from it
/// let variant_array = VariantArray::try_new(&struct_array).unwrap();
/// assert_eq!(variant_array.value(0), Variant::from("such wow"));
/// ```
///
#[derive(Clone, Debug)]
pub struct VariantArray {
/// Reference to the underlying StructArray
inner: StructArray,
/// The metadata column of this variant
metadata: BinaryViewArray,
/// how is this variant array shredded?
shredding_state: ShreddingState,
}
impl VariantArray {
/// Creates a new `VariantArray` from a [`StructArray`].
///
/// # Arguments
/// - `inner` - The underlying [`StructArray`] that contains the variant data.
///
/// # Returns
/// - A new instance of `VariantArray`.
///
/// # Errors:
/// - If the `StructArray` does not contain the required fields
///
/// # Requirements of the `StructArray`
///
/// 1. A required field named `metadata` which is binary, large_binary, or
/// binary_view
///
/// 2. An optional field named `value` that is binary, large_binary, or
/// binary_view
///
/// 3. An optional field named `typed_value` which can be any primitive type
/// or be a list, large_list, list_view or struct
///
/// NOTE: It is also permissible for the metadata field to be
/// Dictionary-Encoded, preferably (but not required) with an index type of
/// int8.
///
/// Currently, only [`BinaryViewArray`] are supported.
pub fn try_new(inner: &dyn Array) -> Result<Self, ArrowError> {
// Workaround lack of support for Binary
// https://github.com/apache/arrow-rs/issues/8387
let inner = cast_to_binary_view_arrays(inner)?;
let Some(inner) = inner.as_struct_opt() else {
return Err(ArrowError::InvalidArgumentError(
"Invalid VariantArray: requires StructArray as input".to_string(),
));
};
// Note the specification allows for any order so we must search by name
// Ensure the StructArray has a metadata field of BinaryView
let Some(metadata_field) = inner.column_by_name("metadata") else {
return Err(ArrowError::InvalidArgumentError(
"Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(),
));
};
let Some(metadata) = metadata_field.as_binary_view_opt() else {
return Err(ArrowError::NotYetImplemented(format!(
"VariantArray 'metadata' field must be BinaryView, got {}",
metadata_field.data_type()
)));
};
// Extract value and typed_value fields
let value = if let Some(value_col) = inner.column_by_name("value") {
if let Some(binary_view) = value_col.as_binary_view_opt() {
Some(binary_view.clone())
} else {
return Err(ArrowError::NotYetImplemented(format!(
"VariantArray 'value' field must be BinaryView, got {}",
value_col.data_type()
)));
}
} else {
None
};
let typed_value = inner.column_by_name("typed_value").cloned();
// Note these clones are cheap, they just bump the ref count
Ok(Self {
inner: inner.clone(),
metadata: metadata.clone(),
shredding_state: ShreddingState::new(value, typed_value),
})
}
pub(crate) fn from_parts(
metadata: BinaryViewArray,
value: Option<BinaryViewArray>,
typed_value: Option<ArrayRef>,
nulls: Option<NullBuffer>,
) -> Self {
let mut builder =
StructArrayBuilder::new().with_field("metadata", Arc::new(metadata.clone()), false);
if let Some(value) = value.clone() {
builder = builder.with_field("value", Arc::new(value), true);
}
if let Some(typed_value) = typed_value.clone() {
builder = builder.with_field("typed_value", typed_value, true);
}
if let Some(nulls) = nulls {
builder = builder.with_nulls(nulls);
}
Self {
inner: builder.build(),
metadata,
shredding_state: ShreddingState::new(value, typed_value),
}
}
/// Returns a reference to the underlying [`StructArray`].
pub fn inner(&self) -> &StructArray {
&self.inner
}
/// Returns the inner [`StructArray`], consuming self
pub fn into_inner(self) -> StructArray {
self.inner
}
/// Return the shredding state of this `VariantArray`
pub fn shredding_state(&self) -> &ShreddingState {
&self.shredding_state
}
/// Return the [`Variant`] instance stored at the given row
///
/// Note: This method does not check for nulls and the value is arbitrary
/// (but still well-defined) if [`is_null`](Self::is_null) returns true for the index.
///
/// # Panics
/// * if the index is out of bounds
/// * if the array value is null
///
/// If this is a shredded variant but has no value at the shredded location, it
/// will return [`Variant::Null`].
///
///
/// # Performance Note
///
/// This is certainly not the most efficient way to access values in a
/// `VariantArray`, but it is useful for testing and debugging.
///
/// Note: Does not do deep validation of the [`Variant`], so it is up to the
/// caller to ensure that the metadata and value were constructed correctly.
pub fn value(&self, index: usize) -> Variant<'_, '_> {
match &self.shredding_state {
ShreddingState::Unshredded { value, .. } => {
// Unshredded case
Variant::new(self.metadata.value(index), value.value(index))
}
ShreddingState::Typed { typed_value, .. } => {
// Typed case (formerly PerfectlyShredded)
if typed_value.is_null(index) {
Variant::Null
} else {
typed_value_to_variant(typed_value, index)
}
}
ShreddingState::PartiallyShredded {
value, typed_value, ..
} => {
// PartiallyShredded case (formerly ImperfectlyShredded)
if typed_value.is_null(index) {
Variant::new(self.metadata.value(index), value.value(index))
} else {
typed_value_to_variant(typed_value, index)
}
}
ShreddingState::AllNull => {
// AllNull case: neither value nor typed_value fields exist
// NOTE: This handles the case where neither value nor typed_value fields exist.
// For top-level variants, this returns Variant::Null (JSON null).
// For shredded object fields, this technically should indicate SQL NULL,
// but the current API cannot distinguish these contexts.
Variant::Null
}
}
}
/// Return a reference to the metadata field of the [`StructArray`]
pub fn metadata_field(&self) -> &BinaryViewArray {
&self.metadata
}
/// Return a reference to the value field of the `StructArray`
pub fn value_field(&self) -> Option<&BinaryViewArray> {
self.shredding_state.value_field()
}
/// Return a reference to the typed_value field of the `StructArray`, if present
pub fn typed_value_field(&self) -> Option<&ArrayRef> {
self.shredding_state.typed_value_field()
}
/// Return a field to represent this VariantArray in a `Schema` with
/// a particular name
pub fn field(&self, name: impl Into<String>) -> Field {
Field::new(
name.into(),
self.data_type().clone(),
self.inner.is_nullable(),
)
.with_extension_type(VariantType)
}
/// Returns a new DataType representing this VariantArray's inner type
pub fn data_type(&self) -> &DataType {
self.inner.data_type()
}
pub fn slice(&self, offset: usize, length: usize) -> Self {
let inner = self.inner.slice(offset, length);
let metadata = self.metadata.slice(offset, length);
let shredding_state = self.shredding_state.slice(offset, length);
Self {
inner,
metadata,
shredding_state,
}
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn nulls(&self) -> Option<&NullBuffer> {
self.inner.nulls()
}
/// Is the element at index null?
pub fn is_null(&self, index: usize) -> bool {
self.nulls().is_some_and(|n| n.is_null(index))
}
/// Is the element at index valid (not null)?
pub fn is_valid(&self, index: usize) -> bool {
!self.is_null(index)
}
}
impl From<VariantArray> for StructArray {
fn from(variant_array: VariantArray) -> Self {
variant_array.into_inner()
}
}
impl From<VariantArray> for ArrayRef {
fn from(variant_array: VariantArray) -> Self {
Arc::new(variant_array.into_inner())
}
}
/// One shredded field of a partially or prefectly shredded variant. For example, suppose the
/// shredding schema for variant `v` treats it as an object with a single field `a`, where `a` is
/// itself a struct with the single field `b` of type INT. Then the physical layout of the column
/// is:
///
/// ```text
/// v: VARIANT {
/// metadata: BINARY,
/// value: BINARY,
/// typed_value: STRUCT {
/// a: SHREDDED_VARIANT_FIELD {
/// value: BINARY,
/// typed_value: STRUCT {
/// a: SHREDDED_VARIANT_FIELD {
/// value: BINARY,
/// typed_value: INT,
/// },
/// },
/// },
/// },
/// }
/// ```
///
/// In the above, each row of `v.value` is either a variant value (shredding failed, `v` was not an
/// object at all) or a variant object (partial shredding, `v` was an object but included unexpected
/// fields other than `a`), or is NULL (perfect shredding, `v` was an object containing only the
/// single expected field `a`).
///
/// A similar story unfolds for each `v.typed_value.a.value` -- a variant value if shredding failed
/// (`v:a` was not an object at all), or a variant object (`v:a` was an object with unexpected
/// additional fields), or NULL (`v:a` was an object containing only the single expected field `b`).
///
/// Finally, `v.typed_value.a.typed_value.b.value` is either NULL (`v:a.b` was an integer) or else a
/// variant value (which could be `Variant::Null`).
#[derive(Debug)]
pub struct ShreddedVariantFieldArray {
/// Reference to the underlying StructArray
inner: StructArray,
shredding_state: ShreddingState,
}
#[allow(unused)]
impl ShreddedVariantFieldArray {
/// Creates a new `ShreddedVariantFieldArray` from a [`StructArray`].
///
/// # Arguments
/// - `inner` - The underlying [`StructArray`] that contains the variant data.
///
/// # Returns
/// - A new instance of `ShreddedVariantFieldArray`.
///
/// # Errors:
/// - If the `StructArray` does not contain the required fields
///
/// # Requirements of the `StructArray`
///
/// 1. An optional field named `value` that is binary, large_binary, or
/// binary_view
///
/// 2. An optional field named `typed_value` which can be any primitive type
/// or be a list, large_list, list_view or struct
///
/// Currently, only `value` columns of type [`BinaryViewArray`] are supported.
pub fn try_new(inner: &dyn Array) -> Result<Self, ArrowError> {
let Some(inner_struct) = inner.as_struct_opt() else {
return Err(ArrowError::InvalidArgumentError(
"Invalid ShreddedVariantFieldArray: requires StructArray as input".to_string(),
));
};
// Note this clone is cheap, it just bumps the ref count
Ok(Self {
inner: inner_struct.clone(),
shredding_state: ShreddingState::from(inner_struct),
})
}
/// Return the shredding state of this `VariantArray`
pub fn shredding_state(&self) -> &ShreddingState {
&self.shredding_state
}
/// Return a reference to the value field of the `StructArray`
pub fn value_field(&self) -> Option<&BinaryViewArray> {
self.shredding_state.value_field()
}
/// Return a reference to the typed_value field of the `StructArray`, if present
pub fn typed_value_field(&self) -> Option<&ArrayRef> {
self.shredding_state.typed_value_field()
}
/// Returns a reference to the underlying [`StructArray`].
pub fn inner(&self) -> &StructArray {
&self.inner
}
pub(crate) fn from_parts(
value: Option<BinaryViewArray>,
typed_value: Option<ArrayRef>,
nulls: Option<NullBuffer>,
) -> Self {
let mut builder = StructArrayBuilder::new();
if let Some(value) = value.clone() {
builder = builder.with_field("value", Arc::new(value), true);
}
if let Some(typed_value) = typed_value.clone() {
builder = builder.with_field("typed_value", typed_value, true);
}
if let Some(nulls) = nulls {
builder = builder.with_nulls(nulls);
}
Self {
inner: builder.build(),
shredding_state: ShreddingState::new(value, typed_value),
}
}
/// Returns the inner [`StructArray`], consuming self
pub fn into_inner(self) -> StructArray {
self.inner
}
pub fn data_type(&self) -> &DataType {
self.inner.data_type()
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn offset(&self) -> usize {
self.inner.offset()
}
pub fn nulls(&self) -> Option<&NullBuffer> {
// According to the shredding spec, ShreddedVariantFieldArray should be
// physically non-nullable - SQL NULL is inferred by both value and
// typed_value being physically NULL
None
}
/// Is the element at index null?
pub fn is_null(&self, index: usize) -> bool {
self.nulls().is_some_and(|n| n.is_null(index))
}
/// Is the element at index valid (not null)?
pub fn is_valid(&self, index: usize) -> bool {
!self.is_null(index)
}
}
impl From<ShreddedVariantFieldArray> for ArrayRef {
fn from(array: ShreddedVariantFieldArray) -> Self {
Arc::new(array.into_inner())
}
}
impl From<ShreddedVariantFieldArray> for StructArray {
fn from(array: ShreddedVariantFieldArray) -> Self {
array.into_inner()
}
}
/// Represents the shredding state of a [`VariantArray`]
///
/// [`VariantArray`]s can be shredded according to the [Parquet Variant
/// Shredding Spec]. Shredding means that the actual value is stored in a typed
/// `typed_field` instead of the generic `value` field.
///
/// Both value and typed_value are optional fields used together to encode a
/// single value. Values in the two fields must be interpreted according to the
/// following table (see [Parquet Variant Shredding Spec] for more details):
///
/// | value | typed_value | Meaning |
/// |----------|--------------|---------|
/// | null | null | The value is missing; only valid for shredded object fields |
/// | non-null | null | The value is present and may be any type, including `null` |
/// | null | non-null | The value is present and is the shredded type |
/// | non-null | non-null | The value is present and is a partially shredded object |
///
/// [Parquet Variant Shredding Spec]: https://github.com/apache/parquet-format/blob/master/VariantShredding.md#value-shredding
#[derive(Clone, Debug)]
pub enum ShreddingState {
/// This variant has no typed_value field
Unshredded { value: BinaryViewArray },
/// This variant has a typed_value field and no value field
/// meaning it is the shredded type
Typed { typed_value: ArrayRef },
/// Imperfectly shredded: Shredded values reside in `typed_value` while those that failed to
/// shred reside in `value`. Missing field values are NULL in both columns, while NULL primitive
/// values have NULL `typed_value` and `Variant::Null` in `value`.
///
/// NOTE: A partially shredded struct is a special kind of imperfect shredding, where
/// `typed_value` and `value` are both non-NULL. The `typed_value` is a struct containing the
/// subset of fields for which shredding was attempted (each field will then have its own value
/// and/or typed_value sub-fields that indicate how shredding actually turned out). Meanwhile,
/// the `value` is a variant object containing the subset of fields for which shredding was
/// not even attempted.
PartiallyShredded {
value: BinaryViewArray,
typed_value: ArrayRef,
},
/// All values are null, only metadata is present.
///
/// This state occurs when neither `value` nor `typed_value` fields exist in the schema.
/// Note: By strict spec interpretation, this should only be valid for shredded object fields,
/// not top-level variants. However, we allow it and treat as Variant::Null for pragmatic
/// handling of missing data.
AllNull,
}
impl ShreddingState {
/// try to create a new `ShreddingState` from the given `value` and `typed_value` fields
///
/// Note you can create a `ShreddingState` from a &[`StructArray`] using
/// `ShreddingState::try_from(&struct_array)`, for example:
///
/// ```no_run
/// # use arrow::array::StructArray;
/// # use parquet_variant_compute::ShreddingState;
/// # fn get_struct_array() -> StructArray {
/// # unimplemented!()
/// # }
/// let struct_array: StructArray = get_struct_array();
/// let shredding_state = ShreddingState::try_from(&struct_array).unwrap();
/// ```
pub fn new(value: Option<BinaryViewArray>, typed_value: Option<ArrayRef>) -> Self {
match (value, typed_value) {
(Some(value), Some(typed_value)) => Self::PartiallyShredded { value, typed_value },
(Some(value), None) => Self::Unshredded { value },
(None, Some(typed_value)) => Self::Typed { typed_value },
(None, None) => Self::AllNull,
}
}
/// Return a reference to the value field, if present
pub fn value_field(&self) -> Option<&BinaryViewArray> {
match self {
ShreddingState::Unshredded { value, .. } => Some(value),
ShreddingState::Typed { .. } => None,
ShreddingState::PartiallyShredded { value, .. } => Some(value),
ShreddingState::AllNull => None,
}
}
/// Return a reference to the typed_value field, if present
pub fn typed_value_field(&self) -> Option<&ArrayRef> {
match self {
ShreddingState::Unshredded { .. } => None,
ShreddingState::Typed { typed_value, .. } => Some(typed_value),
ShreddingState::PartiallyShredded { typed_value, .. } => Some(typed_value),
ShreddingState::AllNull => None,
}
}
/// Slice all the underlying arrays
pub fn slice(&self, offset: usize, length: usize) -> Self {
match self {
ShreddingState::Unshredded { value } => ShreddingState::Unshredded {
value: value.slice(offset, length),
},
ShreddingState::Typed { typed_value } => ShreddingState::Typed {
typed_value: typed_value.slice(offset, length),
},
ShreddingState::PartiallyShredded { value, typed_value } => {
ShreddingState::PartiallyShredded {
value: value.slice(offset, length),
typed_value: typed_value.slice(offset, length),
}
}
ShreddingState::AllNull => ShreddingState::AllNull,
}
}
}
impl From<&StructArray> for ShreddingState {
fn from(inner_struct: &StructArray) -> Self {
let value = inner_struct
.column_by_name("value")
.and_then(|col| col.as_binary_view_opt().cloned());
let typed_value = inner_struct.column_by_name("typed_value").cloned();
ShreddingState::new(value, typed_value)
}
}
/// Builds struct arrays from component fields
///
/// TODO: move to arrow crate
#[derive(Debug, Default, Clone)]
pub(crate) struct StructArrayBuilder {
fields: Vec<FieldRef>,
arrays: Vec<ArrayRef>,
nulls: Option<NullBuffer>,
}
impl StructArrayBuilder {
pub fn new() -> Self {
Default::default()
}
/// Add an array to this struct array as a field with the specified name.
pub fn with_field(mut self, field_name: &str, array: ArrayRef, nullable: bool) -> Self {
let field = Field::new(field_name, array.data_type().clone(), nullable);
self.fields.push(Arc::new(field));
self.arrays.push(array);
self
}
/// Set the null buffer for this struct array.
pub fn with_nulls(mut self, nulls: NullBuffer) -> Self {
self.nulls = Some(nulls);
self
}
pub fn build(self) -> StructArray {
let Self {
fields,
arrays,
nulls,
} = self;
StructArray::new(Fields::from(fields), arrays, nulls)
}
}
/// returns the non-null element at index as a Variant
fn typed_value_to_variant(typed_value: &ArrayRef, index: usize) -> Variant<'_, '_> {
match typed_value.data_type() {
DataType::Boolean => {
let boolean_array = typed_value.as_boolean();
let value = boolean_array.value(index);
Variant::from(value)
}
DataType::Date32 => {
let array = typed_value.as_primitive::<Date32Type>();
let value = array.value(index);
let date = Date32Type::to_naive_date(value);
Variant::from(date)
}
DataType::FixedSizeBinary(binary_len) => {
let array = typed_value.as_fixed_size_binary();
// Try to treat 16 byte FixedSizeBinary as UUID
let value = array.value(index);
if *binary_len == 16 {
if let Ok(uuid) = Uuid::from_slice(value) {
return Variant::from(uuid);
}
}
let value = array.value(index);
Variant::from(value)
}
DataType::BinaryView => {
let array = typed_value.as_binary_view();
let value = array.value(index);
Variant::from(value)
}
DataType::Utf8 => {
let array = typed_value.as_string::<i32>();
let value = array.value(index);
Variant::from(value)
}
DataType::Int8 => {
primitive_conversion_single_value!(Int8Type, typed_value, index)
}
DataType::Int16 => {
primitive_conversion_single_value!(Int16Type, typed_value, index)
}
DataType::Int32 => {
primitive_conversion_single_value!(Int32Type, typed_value, index)
}
DataType::Int64 => {
primitive_conversion_single_value!(Int64Type, typed_value, index)
}
DataType::UInt8 => {
primitive_conversion_single_value!(UInt8Type, typed_value, index)
}
DataType::UInt16 => {
primitive_conversion_single_value!(UInt16Type, typed_value, index)
}
DataType::UInt32 => {
primitive_conversion_single_value!(UInt32Type, typed_value, index)
}
DataType::UInt64 => {
primitive_conversion_single_value!(UInt64Type, typed_value, index)
}
DataType::Float16 => {
primitive_conversion_single_value!(Float16Type, typed_value, index)
}
DataType::Float32 => {
primitive_conversion_single_value!(Float32Type, typed_value, index)
}
DataType::Float64 => {
primitive_conversion_single_value!(Float64Type, typed_value, index)
}
// todo other types here (note this is very similar to cast_to_variant.rs)
// so it would be great to figure out how to share this code
_ => {
// We shouldn't panic in production code, but this is a
// placeholder until we implement more types
// https://github.com/apache/arrow-rs/issues/8091
debug_assert!(
false,
"Unsupported typed_value type: {:?}",
typed_value.data_type()
);
Variant::Null
}
}
}
/// Workaround for lack of direct support for BinaryArray
/// <https://github.com/apache/arrow-rs/issues/8387>
///
/// The values are read as
/// * `StructArray<metadata: Binary, value: Binary>`
///
/// but VariantArray needs them as
/// * `StructArray<metadata: BinaryView, value: BinaryView>`
///
/// So cast them to get the right type.
fn cast_to_binary_view_arrays(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
let new_type = rewrite_to_view_types(array.data_type());
cast(array, &new_type)
}
/// replaces all instances of Binary with BinaryView in a DataType
fn rewrite_to_view_types(data_type: &DataType) -> DataType {
match data_type {
DataType::Binary => DataType::BinaryView,
DataType::List(field) => DataType::List(rewrite_field_type(field)),
DataType::Struct(fields) => {
let new_fields: Fields = fields.iter().map(rewrite_field_type).collect();
DataType::Struct(new_fields)
}
_ => data_type.clone(),
}
}
fn rewrite_field_type(field: impl AsRef<Field>) -> Arc<Field> {
let field = field.as_ref();
let new_field = field
.clone()
.with_data_type(rewrite_to_view_types(field.data_type()));
Arc::new(new_field)
}
#[cfg(test)]
mod test {
use super::*;
use arrow::array::{BinaryViewArray, Int32Array};
use arrow_schema::{Field, Fields};
#[test]
fn invalid_not_a_struct_array() {
let array = make_binary_view_array();
// Should fail because the input is not a StructArray
let err = VariantArray::try_new(&array);
assert_eq!(
err.unwrap_err().to_string(),
"Invalid argument error: Invalid VariantArray: requires StructArray as input"
);
}
#[test]
fn invalid_missing_metadata() {
let fields = Fields::from(vec![Field::new("value", DataType::BinaryView, true)]);
let array = StructArray::new(fields, vec![make_binary_view_array()], None);
// Should fail because the StructArray does not contain a 'metadata' field
let err = VariantArray::try_new(&array);
assert_eq!(
err.unwrap_err().to_string(),
"Invalid argument error: Invalid VariantArray: StructArray must contain a 'metadata' field"
);
}
#[test]
fn all_null_missing_value_and_typed_value() {
let fields = Fields::from(vec![Field::new("metadata", DataType::BinaryView, false)]);
let array = StructArray::new(fields, vec![make_binary_view_array()], None);
// NOTE: By strict spec interpretation, this case (top-level variant with null/null)
// should be invalid, but we currently allow it and treat it as Variant::Null.
// This is a pragmatic decision to handle missing data gracefully.
let variant_array = VariantArray::try_new(&array).unwrap();
// Verify the shredding state is AllNull
assert!(matches!(
variant_array.shredding_state(),
ShreddingState::AllNull
));
// Verify that value() returns Variant::Null (compensating for spec violation)
for i in 0..variant_array.len() {
if variant_array.is_valid(i) {
assert_eq!(variant_array.value(i), parquet_variant::Variant::Null);
}
}
}
#[test]
fn invalid_metadata_field_type() {
let fields = Fields::from(vec![
Field::new("metadata", DataType::Int32, true), // not supported
Field::new("value", DataType::BinaryView, true),
]);
let array = StructArray::new(
fields,
vec![make_int32_array(), make_binary_view_array()],
None,
);
let err = VariantArray::try_new(&array);
assert_eq!(
err.unwrap_err().to_string(),
"Not yet implemented: VariantArray 'metadata' field must be BinaryView, got Int32"
);
}
#[test]
fn invalid_value_field_type() {
let fields = Fields::from(vec![
Field::new("metadata", DataType::BinaryView, true),
Field::new("value", DataType::Int32, true), // Not yet supported
]);
let array = StructArray::new(
fields,
vec![make_binary_view_array(), make_int32_array()],
None,
);