alamb commented on code in PR #12245:
URL: https://github.com/apache/arrow-rs/pull/12245#discussion_r4606730902
##########
arrow-schema/src/extension/canonical/variant.rs:
##########
@@ -61,23 +112,116 @@ mod tests {
use super::*;
use crate::extension::CanonicalExtensionType;
use crate::{Field, Fields};
+ use std::sync::Arc;
+
+ fn storage(fields: Vec<Field>) -> DataType {
+ DataType::Struct(Fields::from(fields))
+ }
+
+ fn metadata(data_type: DataType) -> Field {
+ Field::new("metadata", data_type, false)
+ }
+
+ fn value(data_type: DataType) -> Field {
+ Field::new("value", data_type, true)
+ }
+
+ fn typed_value(data_type: DataType) -> Field {
+ Field::new("typed_value", data_type, true)
+ }
#[test]
- fn variant_type_requires_struct() {
- let field = Field::new("v", DataType::Struct(Fields::empty()), true)
- .with_extension_type(VariantType);
+ fn variant_type_canonical_extension() {
+ let data_type = storage(vec![
+ metadata(DataType::BinaryView),
+ value(DataType::BinaryView),
+ ]);
+ let field = Field::new("v", data_type,
true).with_extension_type(VariantType);
assert!(field.has_valid_extension_type::<VariantType>());
assert_eq!(
field.try_canonical_extension_type().unwrap(),
CanonicalExtensionType::Variant(VariantType),
);
+ }
+ #[test]
+ fn variant_type_accepts_valid_storage() {
+ let dictionary = DataType::Dictionary(Box::new(DataType::Int8),
Box::new(DataType::Binary));
+ let run_end_encoded = DataType::RunEndEncoded(
+ Arc::new(Field::new("run_ends", DataType::Int32, false)),
+ Arc::new(Field::new("values", DataType::LargeBinary, true)),
+ );
+ let valid = [
+ storage(vec![metadata(DataType::Binary), value(DataType::Binary)]),
+ storage(vec![
+ value(DataType::LargeBinary),
+ metadata(DataType::LargeBinary),
+ ]),
+ storage(vec![metadata(dictionary), value(DataType::BinaryView)]),
+ storage(vec![metadata(run_end_encoded),
value(DataType::BinaryView)]),
+ storage(vec![
+ metadata(DataType::BinaryView),
+ value(DataType::BinaryView),
+ typed_value(DataType::Int64),
+ ]),
+ // Allowed by the Arrow spec, though the Parquet shredding spec
requires `value`
+ storage(vec![
+ metadata(DataType::BinaryView),
+ typed_value(DataType::Utf8),
+ ]),
+ ];
+ for data_type in valid {
+ VariantType::validate(&data_type, "").unwrap();
+ }
+ }
+
+ #[test]
+ fn variant_type_rejects_invalid_storage() {
let err = Field::new("v", DataType::Binary, true)
.try_with_extension_type(VariantType)
.unwrap_err();
assert_eq!(
err.to_string(),
"Invalid argument error: VariantType only supports StructArray,
got Binary"
);
+
+ let invalid = [
+ (
+ storage(vec![Field::new("unrelated", DataType::Int32, false)]),
+ "Invalid VariantType: StructArray must contain a 'metadata'
field",
+ ),
+ (
+ storage(vec![
+ metadata(DataType::FixedSizeBinary(16)),
+ value(DataType::BinaryView),
+ ]),
+ "VariantType 'metadata' field must be Binary, LargeBinary,
BinaryView, or a Dictionary or RunEndEncoded array of one of those types, got
FixedSizeBinary(16)",
+ ),
+ (
+ storage(vec![
+ metadata(DataType::Dictionary(
+ Box::new(DataType::Int8),
+ Box::new(DataType::Utf8),
+ )),
+ value(DataType::BinaryView),
+ ]),
+ "VariantType 'metadata' field must be Binary, LargeBinary,
BinaryView, or a Dictionary or RunEndEncoded array of one of those types, got
Dictionary(Int8, Utf8)",
Review Comment:
❤️ love it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]