scovich commented on code in PR #7921: URL: https://github.com/apache/arrow-rs/pull/7921#discussion_r2218979580
########## parquet-variant-compute/src/shredding.rs: ########## @@ -0,0 +1,280 @@ +// 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. + +use arrow_schema::{ArrowError, DataType, FieldRef, Fields}; + +// Keywords defined by the shredding spec +pub const METADATA: &str = "metadata"; +pub const VALUE: &str = "value"; +pub const TYPED_VALUE: &str = "typed_value"; + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueSchema { + MissingValue, + Value(usize), + ShreddedValue(usize), + PartiallyShredded { + value_idx: usize, + shredded_value_idx: usize, + }, +} + +#[derive(Debug, Clone)] +pub struct VariantSchema { + inner: Fields, + + metadata_idx: usize, + + // these indicies are for the top-level most `value` and `typed_value` columns + value_schema: ValueSchema, +} + +impl VariantSchema { + /// find column metadata and ensure + /// returns the column index + /// # Requirements + /// - Must contain a "metadata" field of type BinaryView + /// - Must contain at least one of "value" (optional BinaryView) or "typed_value" (optional with valid Parquet type) + /// - Both "value" and "typed_value" can only be null simultaneously for shredded object fields + fn validate_metadata(fields: &Fields) -> Result<usize, ArrowError> { + let (metadata_idx, metadata_field) = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == METADATA) + .ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + ) + })?; + + if metadata_field.is_nullable() { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: metadata field can not be nullable".to_string(), + )); + } + + if metadata_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'metadata' field must be BinaryView, got {}", + metadata_field.data_type() + ))); + } + + Ok(metadata_idx) + } + + /// 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: + /// + /// | `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 | + fn validate_value_and_typed_value( + fields: &Fields, + inside_shredded_object: bool, + ) -> Result<ValueSchema, ArrowError> { + let value_field_res = fields.iter().enumerate().find(|(_, f)| f.name() == VALUE); + let typed_value_field_res = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == TYPED_VALUE); + + // validate types + if let Some((_, value_field)) = value_field_res { + if value_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'value' field must be BinaryView, got {}", + value_field.data_type() + ))); + } + } + + if let Some((_, typed_value_field)) = typed_value_field_res { + match typed_value_field.data_type() { + DataType::Boolean + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Date32 + | DataType::Date64 + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::Utf8View + | DataType::BinaryView + | DataType::ListView(_) + | DataType::Struct(_) => {} + foreign => { + return Err(ArrowError::NotYetImplemented(format!( + "Unsupported VariantArray 'typed_value' field, got {foreign}" + ))) + } + } + } + + match (value_field_res, typed_value_field_res) { + (None, None) => { + if inside_shredded_object { + return Ok(ValueSchema::MissingValue); + } + + Err(ArrowError::InvalidArgumentError("Invalid VariantArray: StructArray must contain either `value` or `typed_value` fields or both.".to_string())) + } + (Some(value_field), None) => Ok(ValueSchema::Value(value_field.0)), + (None, Some(shredded_field)) => Ok(ValueSchema::ShreddedValue(shredded_field.0)), + (Some(_value_field), Some(_shredded_field)) => { + todo!("how does a shredded value look like?"); + // ideally here, i would unpack the shredded_field + // and recursively call validate_value_and_typed_value with inside_shredded_object set to true + } + } + } + + pub fn try_new(fields: Fields) -> Result<Self, ArrowError> { + let metadata_idx = Self::validate_metadata(&fields)?; + let value_schema = Self::validate_value_and_typed_value(&fields, false)?; + + Ok(Self { + inner: fields.clone(), + metadata_idx, + value_schema, + }) + } + + pub fn inner(self) -> Fields { + self.inner + } + + pub fn metadata_idx(&self) -> usize { + self.metadata_idx + } + + pub fn metadata(&self) -> &FieldRef { + self.inner.get(self.metadata_idx).unwrap() + } + + pub fn value_idx(&self) -> Option<usize> { + match self.value_schema { + ValueSchema::MissingValue => None, + ValueSchema::ShreddedValue(_) => None, + ValueSchema::Value(value_idx) => Some(value_idx), + ValueSchema::PartiallyShredded { value_idx, .. } => Some(value_idx), Review Comment: Could also consider: ```suggestion use ValueSchema::*; match self.value_schema { MissingValue | ShreddedValue(_) => None, Value(value_idx) | PartiallyShredded { value_idx, .. } => Some(value_idx), ``` Less redundancy... but I'm not sure it actually improves readability very much? ########## parquet-variant-compute/src/shredding.rs: ########## @@ -0,0 +1,280 @@ +// 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. + +use arrow_schema::{ArrowError, DataType, FieldRef, Fields}; + +// Keywords defined by the shredding spec +pub const METADATA: &str = "metadata"; +pub const VALUE: &str = "value"; +pub const TYPED_VALUE: &str = "typed_value"; + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueSchema { + MissingValue, + Value(usize), + ShreddedValue(usize), + PartiallyShredded { + value_idx: usize, + shredded_value_idx: usize, + }, +} + +#[derive(Debug, Clone)] +pub struct VariantSchema { + inner: Fields, + + metadata_idx: usize, + + // these indicies are for the top-level most `value` and `typed_value` columns + value_schema: ValueSchema, +} + +impl VariantSchema { + /// find column metadata and ensure + /// returns the column index + /// # Requirements + /// - Must contain a "metadata" field of type BinaryView + /// - Must contain at least one of "value" (optional BinaryView) or "typed_value" (optional with valid Parquet type) + /// - Both "value" and "typed_value" can only be null simultaneously for shredded object fields + fn validate_metadata(fields: &Fields) -> Result<usize, ArrowError> { + let (metadata_idx, metadata_field) = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == METADATA) + .ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + ) + })?; + + if metadata_field.is_nullable() { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: metadata field can not be nullable".to_string(), + )); + } + + if metadata_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'metadata' field must be BinaryView, got {}", + metadata_field.data_type() + ))); + } + + Ok(metadata_idx) + } + + /// 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: + /// + /// | `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 | + fn validate_value_and_typed_value( + fields: &Fields, + inside_shredded_object: bool, + ) -> Result<ValueSchema, ArrowError> { + let value_field_res = fields.iter().enumerate().find(|(_, f)| f.name() == VALUE); + let typed_value_field_res = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == TYPED_VALUE); + + // validate types + if let Some((_, value_field)) = value_field_res { + if value_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'value' field must be BinaryView, got {}", + value_field.data_type() + ))); + } + } + + if let Some((_, typed_value_field)) = typed_value_field_res { + match typed_value_field.data_type() { + DataType::Boolean + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Date32 + | DataType::Date64 + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::Utf8View + | DataType::BinaryView + | DataType::ListView(_) + | DataType::Struct(_) => {} + foreign => { + return Err(ArrowError::NotYetImplemented(format!( + "Unsupported VariantArray 'typed_value' field, got {foreign}" + ))) + } + } + } + + match (value_field_res, typed_value_field_res) { + (None, None) => { + if inside_shredded_object { + return Ok(ValueSchema::MissingValue); + } + + Err(ArrowError::InvalidArgumentError("Invalid VariantArray: StructArray must contain either `value` or `typed_value` fields or both.".to_string())) + } + (Some(value_field), None) => Ok(ValueSchema::Value(value_field.0)), + (None, Some(shredded_field)) => Ok(ValueSchema::ShreddedValue(shredded_field.0)), + (Some(_value_field), Some(_shredded_field)) => { + todo!("how does a shredded value look like?"); + // ideally here, i would unpack the shredded_field + // and recursively call validate_value_and_typed_value with inside_shredded_object set to true + } + } + } + + pub fn try_new(fields: Fields) -> Result<Self, ArrowError> { + let metadata_idx = Self::validate_metadata(&fields)?; + let value_schema = Self::validate_value_and_typed_value(&fields, false)?; + + Ok(Self { + inner: fields.clone(), + metadata_idx, + value_schema, + }) + } + + pub fn inner(self) -> Fields { Review Comment: isn't this normally called `into_inner`, reflecting that it consumes `self`? ########## parquet-variant-compute/src/shredding.rs: ########## @@ -0,0 +1,280 @@ +// 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. + +use arrow_schema::{ArrowError, DataType, FieldRef, Fields}; + +// Keywords defined by the shredding spec +pub const METADATA: &str = "metadata"; +pub const VALUE: &str = "value"; +pub const TYPED_VALUE: &str = "typed_value"; + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueSchema { + MissingValue, + Value(usize), + ShreddedValue(usize), + PartiallyShredded { + value_idx: usize, + shredded_value_idx: usize, + }, +} + +#[derive(Debug, Clone)] +pub struct VariantSchema { + inner: Fields, + + metadata_idx: usize, + + // these indicies are for the top-level most `value` and `typed_value` columns + value_schema: ValueSchema, +} + +impl VariantSchema { + /// find column metadata and ensure + /// returns the column index + /// # Requirements + /// - Must contain a "metadata" field of type BinaryView + /// - Must contain at least one of "value" (optional BinaryView) or "typed_value" (optional with valid Parquet type) + /// - Both "value" and "typed_value" can only be null simultaneously for shredded object fields + fn validate_metadata(fields: &Fields) -> Result<usize, ArrowError> { + let (metadata_idx, metadata_field) = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == METADATA) + .ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + ) + })?; + + if metadata_field.is_nullable() { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: metadata field can not be nullable".to_string(), + )); + } + + if metadata_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'metadata' field must be BinaryView, got {}", + metadata_field.data_type() + ))); + } + + Ok(metadata_idx) + } + + /// 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: + /// + /// | `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 | + fn validate_value_and_typed_value( + fields: &Fields, + inside_shredded_object: bool, + ) -> Result<ValueSchema, ArrowError> { + let value_field_res = fields.iter().enumerate().find(|(_, f)| f.name() == VALUE); + let typed_value_field_res = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == TYPED_VALUE); + + // validate types + if let Some((_, value_field)) = value_field_res { + if value_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'value' field must be BinaryView, got {}", + value_field.data_type() + ))); + } + } + + if let Some((_, typed_value_field)) = typed_value_field_res { + match typed_value_field.data_type() { + DataType::Boolean + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Date32 + | DataType::Date64 + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::Utf8View + | DataType::BinaryView + | DataType::ListView(_) + | DataType::Struct(_) => {} + foreign => { + return Err(ArrowError::NotYetImplemented(format!( + "Unsupported VariantArray 'typed_value' field, got {foreign}" + ))) + } + } + } + + match (value_field_res, typed_value_field_res) { + (None, None) => { + if inside_shredded_object { + return Ok(ValueSchema::MissingValue); + } + + Err(ArrowError::InvalidArgumentError("Invalid VariantArray: StructArray must contain either `value` or `typed_value` fields or both.".to_string())) + } + (Some(value_field), None) => Ok(ValueSchema::Value(value_field.0)), + (None, Some(shredded_field)) => Ok(ValueSchema::ShreddedValue(shredded_field.0)), + (Some(_value_field), Some(_shredded_field)) => { + todo!("how does a shredded value look like?"); + // ideally here, i would unpack the shredded_field + // and recursively call validate_value_and_typed_value with inside_shredded_object set to true + } + } + } + + pub fn try_new(fields: Fields) -> Result<Self, ArrowError> { + let metadata_idx = Self::validate_metadata(&fields)?; + let value_schema = Self::validate_value_and_typed_value(&fields, false)?; + + Ok(Self { + inner: fields.clone(), + metadata_idx, + value_schema, + }) + } + + pub fn inner(self) -> Fields { + self.inner + } + + pub fn metadata_idx(&self) -> usize { + self.metadata_idx + } + + pub fn metadata(&self) -> &FieldRef { + self.inner.get(self.metadata_idx).unwrap() + } + + pub fn value_idx(&self) -> Option<usize> { + match self.value_schema { + ValueSchema::MissingValue => None, + ValueSchema::ShreddedValue(_) => None, + ValueSchema::Value(value_idx) => Some(value_idx), + ValueSchema::PartiallyShredded { value_idx, .. } => Some(value_idx), + } + } + + pub fn value(&self) -> Option<&FieldRef> { + self.value_idx().map(|i| self.inner.get(i).unwrap()) Review Comment: I realize the `unwrap` should be safe, but is there any harm in using `flat_map` instead to eliminate the possibility of panic? ```suggestion self.value_idx().flat_map(|i| self.inner.get(i)) ``` Downside is, if the value index were ever incorrect, we would silently fail by returning None instead of panicking. But on the other hand, if the value index were ever incorrect, we're just as likely to silently return the wrong field rather than panic. I'm not sure panicking only some of the time actually helps? (again below) ########## parquet-variant-compute/src/variant_get.rs: ########## @@ -45,7 +49,7 @@ pub fn variant_get(input: &ArrayRef, options: GetOptions) -> Result<ArrayRef> { ))); } - let mut builder = VariantArrayBuilder::new(variant_array.len()); + let mut builder = VariantArrayBuilder::try_new(variant_array.len(), shredded_schema).unwrap(); Review Comment: We're in a fallible method... don't panic: ```suggestion let mut builder = VariantArrayBuilder::try_new(variant_array.len(), shredded_schema)?; ``` ########## parquet-variant-compute/src/shredding.rs: ########## @@ -0,0 +1,280 @@ +// 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. + +use arrow_schema::{ArrowError, DataType, FieldRef, Fields}; + +// Keywords defined by the shredding spec +pub const METADATA: &str = "metadata"; +pub const VALUE: &str = "value"; +pub const TYPED_VALUE: &str = "typed_value"; + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueSchema { + MissingValue, + Value(usize), + ShreddedValue(usize), + PartiallyShredded { + value_idx: usize, + shredded_value_idx: usize, + }, +} + +#[derive(Debug, Clone)] +pub struct VariantSchema { + inner: Fields, + + metadata_idx: usize, + + // these indicies are for the top-level most `value` and `typed_value` columns + value_schema: ValueSchema, +} + +impl VariantSchema { + /// find column metadata and ensure + /// returns the column index + /// # Requirements + /// - Must contain a "metadata" field of type BinaryView + /// - Must contain at least one of "value" (optional BinaryView) or "typed_value" (optional with valid Parquet type) + /// - Both "value" and "typed_value" can only be null simultaneously for shredded object fields + fn validate_metadata(fields: &Fields) -> Result<usize, ArrowError> { + let (metadata_idx, metadata_field) = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == METADATA) + .ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + ) + })?; + + if metadata_field.is_nullable() { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: metadata field can not be nullable".to_string(), + )); + } + + if metadata_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'metadata' field must be BinaryView, got {}", + metadata_field.data_type() + ))); + } + + Ok(metadata_idx) + } + + /// 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: + /// + /// | `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 | + fn validate_value_and_typed_value( + fields: &Fields, + inside_shredded_object: bool, + ) -> Result<ValueSchema, ArrowError> { + let value_field_res = fields.iter().enumerate().find(|(_, f)| f.name() == VALUE); + let typed_value_field_res = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == TYPED_VALUE); + + // validate types + if let Some((_, value_field)) = value_field_res { + if value_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'value' field must be BinaryView, got {}", + value_field.data_type() + ))); + } + } + + if let Some((_, typed_value_field)) = typed_value_field_res { + match typed_value_field.data_type() { + DataType::Boolean + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Date32 + | DataType::Date64 + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::Utf8View + | DataType::BinaryView + | DataType::ListView(_) + | DataType::Struct(_) => {} + foreign => { + return Err(ArrowError::NotYetImplemented(format!( + "Unsupported VariantArray 'typed_value' field, got {foreign}" + ))) + } + } + } + + match (value_field_res, typed_value_field_res) { + (None, None) => { + if inside_shredded_object { + return Ok(ValueSchema::MissingValue); + } + + Err(ArrowError::InvalidArgumentError("Invalid VariantArray: StructArray must contain either `value` or `typed_value` fields or both.".to_string())) + } + (Some(value_field), None) => Ok(ValueSchema::Value(value_field.0)), + (None, Some(shredded_field)) => Ok(ValueSchema::ShreddedValue(shredded_field.0)), + (Some(_value_field), Some(_shredded_field)) => { + todo!("how does a shredded value look like?"); + // ideally here, i would unpack the shredded_field + // and recursively call validate_value_and_typed_value with inside_shredded_object set to true + } + } + } + + pub fn try_new(fields: Fields) -> Result<Self, ArrowError> { + let metadata_idx = Self::validate_metadata(&fields)?; + let value_schema = Self::validate_value_and_typed_value(&fields, false)?; + + Ok(Self { + inner: fields.clone(), + metadata_idx, + value_schema, + }) + } + + pub fn inner(self) -> Fields { + self.inner + } + + pub fn metadata_idx(&self) -> usize { + self.metadata_idx + } + + pub fn metadata(&self) -> &FieldRef { + self.inner.get(self.metadata_idx).unwrap() + } + + pub fn value_idx(&self) -> Option<usize> { + match self.value_schema { + ValueSchema::MissingValue => None, + ValueSchema::ShreddedValue(_) => None, + ValueSchema::Value(value_idx) => Some(value_idx), + ValueSchema::PartiallyShredded { value_idx, .. } => Some(value_idx), + } + } + + pub fn value(&self) -> Option<&FieldRef> { + self.value_idx().map(|i| self.inner.get(i).unwrap()) + } + + pub fn shredded_value_idx(&self) -> Option<usize> { + match self.value_schema { + ValueSchema::MissingValue => None, + ValueSchema::Value(_) => None, + ValueSchema::ShreddedValue(shredded_idx) => Some(shredded_idx), + ValueSchema::PartiallyShredded { + shredded_value_idx, .. + } => Some(shredded_value_idx), Review Comment: Or, maybe it just needs the `use ValueSchema::*` part, to avoid the fmt line breaks on `ShreddedValue`? ```suggestion use ValueSchema::*; match self.value_schema { MissingValue => None, Value(_) => None, ShreddedValue(shredded_idx) => Some(shredded_idx), PartiallyShredded { shredded_value_idx, .. } => Some(shredded_value_idx), ``` ########## parquet-variant-compute/src/shredding.rs: ########## @@ -0,0 +1,280 @@ +// 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. + +use arrow_schema::{ArrowError, DataType, FieldRef, Fields}; + +// Keywords defined by the shredding spec +pub const METADATA: &str = "metadata"; +pub const VALUE: &str = "value"; +pub const TYPED_VALUE: &str = "typed_value"; + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueSchema { + MissingValue, + Value(usize), + ShreddedValue(usize), + PartiallyShredded { + value_idx: usize, + shredded_value_idx: usize, + }, +} + +#[derive(Debug, Clone)] +pub struct VariantSchema { + inner: Fields, + + metadata_idx: usize, + + // these indicies are for the top-level most `value` and `typed_value` columns + value_schema: ValueSchema, +} + +impl VariantSchema { + /// find column metadata and ensure + /// returns the column index + /// # Requirements + /// - Must contain a "metadata" field of type BinaryView + /// - Must contain at least one of "value" (optional BinaryView) or "typed_value" (optional with valid Parquet type) + /// - Both "value" and "typed_value" can only be null simultaneously for shredded object fields + fn validate_metadata(fields: &Fields) -> Result<usize, ArrowError> { + let (metadata_idx, metadata_field) = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == METADATA) + .ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + ) + })?; + + if metadata_field.is_nullable() { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: metadata field can not be nullable".to_string(), + )); + } + + if metadata_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'metadata' field must be BinaryView, got {}", + metadata_field.data_type() + ))); + } + + Ok(metadata_idx) + } + + /// 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: + /// + /// | `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 | + fn validate_value_and_typed_value( + fields: &Fields, + inside_shredded_object: bool, + ) -> Result<ValueSchema, ArrowError> { + let value_field_res = fields.iter().enumerate().find(|(_, f)| f.name() == VALUE); + let typed_value_field_res = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == TYPED_VALUE); + + // validate types + if let Some((_, value_field)) = value_field_res { + if value_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'value' field must be BinaryView, got {}", + value_field.data_type() + ))); + } + } + + if let Some((_, typed_value_field)) = typed_value_field_res { + match typed_value_field.data_type() { + DataType::Boolean + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Date32 + | DataType::Date64 + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::Utf8View + | DataType::BinaryView + | DataType::ListView(_) + | DataType::Struct(_) => {} + foreign => { + return Err(ArrowError::NotYetImplemented(format!( + "Unsupported VariantArray 'typed_value' field, got {foreign}" + ))) + } + } + } + + match (value_field_res, typed_value_field_res) { + (None, None) => { + if inside_shredded_object { + return Ok(ValueSchema::MissingValue); + } + + Err(ArrowError::InvalidArgumentError("Invalid VariantArray: StructArray must contain either `value` or `typed_value` fields or both.".to_string())) + } + (Some(value_field), None) => Ok(ValueSchema::Value(value_field.0)), + (None, Some(shredded_field)) => Ok(ValueSchema::ShreddedValue(shredded_field.0)), + (Some(_value_field), Some(_shredded_field)) => { + todo!("how does a shredded value look like?"); + // ideally here, i would unpack the shredded_field + // and recursively call validate_value_and_typed_value with inside_shredded_object set to true Review Comment: You already did the validation for leaf values at L110 above; maybe just finish the job there, by recursing on the `DataType::ListView` and `DataType::Struct` match arms? ########## parquet-variant-compute/src/variant_array_builder.rs: ########## @@ -81,26 +87,19 @@ pub struct VariantArrayBuilder { /// (offset, len) pairs for locations of values in the buffer value_locations: Vec<(usize, usize)>, /// The fields of the final `StructArray` - /// - /// TODO: 1) Add extension type metadata - /// TODO: 2) Add support for shredding Review Comment: Are these TODO actually done? I didn't see anything related to 1/ in this PR, and I would think there's (a lot?) more to 2/ than just adding schema support? ########## parquet-variant-compute/src/shredding.rs: ########## @@ -0,0 +1,280 @@ +// 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. + +use arrow_schema::{ArrowError, DataType, FieldRef, Fields}; + +// Keywords defined by the shredding spec +pub const METADATA: &str = "metadata"; +pub const VALUE: &str = "value"; +pub const TYPED_VALUE: &str = "typed_value"; + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueSchema { + MissingValue, + Value(usize), + ShreddedValue(usize), + PartiallyShredded { + value_idx: usize, + shredded_value_idx: usize, + }, +} + +#[derive(Debug, Clone)] +pub struct VariantSchema { + inner: Fields, + + metadata_idx: usize, + + // these indicies are for the top-level most `value` and `typed_value` columns + value_schema: ValueSchema, +} + +impl VariantSchema { + /// find column metadata and ensure + /// returns the column index + /// # Requirements + /// - Must contain a "metadata" field of type BinaryView + /// - Must contain at least one of "value" (optional BinaryView) or "typed_value" (optional with valid Parquet type) + /// - Both "value" and "typed_value" can only be null simultaneously for shredded object fields + fn validate_metadata(fields: &Fields) -> Result<usize, ArrowError> { + let (metadata_idx, metadata_field) = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == METADATA) + .ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Invalid VariantArray: StructArray must contain a 'metadata' field".to_string(), + ) + })?; + + if metadata_field.is_nullable() { + return Err(ArrowError::InvalidArgumentError( + "Invalid VariantArray: metadata field can not be nullable".to_string(), + )); + } + + if metadata_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'metadata' field must be BinaryView, got {}", + metadata_field.data_type() + ))); + } + + Ok(metadata_idx) + } + + /// 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: + /// + /// | `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 | + fn validate_value_and_typed_value( + fields: &Fields, + inside_shredded_object: bool, + ) -> Result<ValueSchema, ArrowError> { + let value_field_res = fields.iter().enumerate().find(|(_, f)| f.name() == VALUE); + let typed_value_field_res = fields + .iter() + .enumerate() + .find(|(_, f)| f.name() == TYPED_VALUE); + + // validate types + if let Some((_, value_field)) = value_field_res { + if value_field.data_type() != &DataType::BinaryView { + return Err(ArrowError::NotYetImplemented(format!( + "VariantArray 'value' field must be BinaryView, got {}", + value_field.data_type() + ))); + } + } + + if let Some((_, typed_value_field)) = typed_value_field_res { + match typed_value_field.data_type() { + DataType::Boolean + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + | DataType::Decimal32(_, _) + | DataType::Decimal64(_, _) + | DataType::Decimal128(_, _) + | DataType::Date32 + | DataType::Date64 + | DataType::Time32(_) + | DataType::Time64(_) + | DataType::Timestamp(_, _) + | DataType::Utf8View + | DataType::BinaryView + | DataType::ListView(_) + | DataType::Struct(_) => {} + foreign => { + return Err(ArrowError::NotYetImplemented(format!( + "Unsupported VariantArray 'typed_value' field, got {foreign}" + ))) + } + } + } + + match (value_field_res, typed_value_field_res) { + (None, None) => { + if inside_shredded_object { + return Ok(ValueSchema::MissingValue); + } + + Err(ArrowError::InvalidArgumentError("Invalid VariantArray: StructArray must contain either `value` or `typed_value` fields or both.".to_string())) + } + (Some(value_field), None) => Ok(ValueSchema::Value(value_field.0)), + (None, Some(shredded_field)) => Ok(ValueSchema::ShreddedValue(shredded_field.0)), + (Some(_value_field), Some(_shredded_field)) => { + todo!("how does a shredded value look like?"); + // ideally here, i would unpack the shredded_field + // and recursively call validate_value_and_typed_value with inside_shredded_object set to true + } + } + } + + pub fn try_new(fields: Fields) -> Result<Self, ArrowError> { + let metadata_idx = Self::validate_metadata(&fields)?; + let value_schema = Self::validate_value_and_typed_value(&fields, false)?; + + Ok(Self { + inner: fields.clone(), + metadata_idx, + value_schema, + }) + } + + pub fn inner(self) -> Fields { + self.inner + } + + pub fn metadata_idx(&self) -> usize { + self.metadata_idx + } + + pub fn metadata(&self) -> &FieldRef { + self.inner.get(self.metadata_idx).unwrap() + } + + pub fn value_idx(&self) -> Option<usize> { + match self.value_schema { + ValueSchema::MissingValue => None, + ValueSchema::ShreddedValue(_) => None, + ValueSchema::Value(value_idx) => Some(value_idx), + ValueSchema::PartiallyShredded { value_idx, .. } => Some(value_idx), + } + } + + pub fn value(&self) -> Option<&FieldRef> { + self.value_idx().map(|i| self.inner.get(i).unwrap()) + } + + pub fn shredded_value_idx(&self) -> Option<usize> { + match self.value_schema { + ValueSchema::MissingValue => None, + ValueSchema::Value(_) => None, + ValueSchema::ShreddedValue(shredded_idx) => Some(shredded_idx), + ValueSchema::PartiallyShredded { + shredded_value_idx, .. + } => Some(shredded_value_idx), Review Comment: Similar to above, but I think this one _is_ actually a readability improvement: ```suggestion use ValueSchema::*; match self.value_schema { MissingValue | Value(_) => None, ShreddedValue(shredded_idx) | PartiallyShredded { shredded_value_idx, .. } => { Some(shredded_value_idx) } ``` -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org