Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 92 additions & 62 deletions arrow-schema/src/extension/canonical/fixed_shape_tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ impl FixedShapeTensor {
})
}

// Unlike `supports_data_type`, this needs no tensor instance. Borrowing metadata
// and returning the element type lets `try_new` reuse validation without cloning
// metadata, while `validate` can check it without constructing a tensor.
fn validate_impl<'a>(
Comment thread
Phoenix500526 marked this conversation as resolved.
Resolved
data_type: &'a DataType,
metadata: &FixedShapeTensorMetadata,
) -> Result<&'a DataType, ArrowError> {
match data_type {
DataType::FixedSizeList(field, list_size) if !field.is_nullable() => {
metadata.validate()?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add some tests that would pin the metadata check down for both tensor types?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Added

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one case per test could've been enough. We're only pinning the metadata check itself.

The ext type specific checks are already tested in their impls:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense. Updated.

let expected_size = i32::try_from(metadata.list_size()).expect("overflow");
if *list_size != expected_size {
Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor list size mismatch, expected {expected_size} (metadata), found {list_size} (data type)"
)))
} else {
Ok(field.data_type())
}
}
data_type => Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor data type mismatch, expected FixedSizeList with non-nullable field, found {data_type}"
))),
}
}

/// Returns the value type of the individual tensor elements.
pub fn value_type(&self) -> &DataType {
&self.value_type
Expand Down Expand Up @@ -321,45 +346,47 @@ impl FixedShapeTensorMetadata {
dimension_names: Option<Vec<String>>,
permutations: Option<Vec<usize>>,
) -> Result<Self, ArrowError> {
let shape = shape.into_iter().collect::<Vec<_>>();
let dimensions = shape.len();

let dim_names = dimension_names.map(|dimension_names| {
if dimension_names.len() != dimensions {
Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor dimension names size mismatch, expected {dimensions}, found {}", dimension_names.len()
)))
} else {
Ok(dimension_names)
}
}).transpose()?;
let metadata = Self {
shape: shape.into_iter().collect(),
dim_names: dimension_names,
permutations,
};
metadata.validate()?;
Ok(metadata)
}

let permutations = permutations
.map(|permutations| {
if permutations.len() != dimensions {
Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor permutations size mismatch, expected {dimensions}, found {}",
permutations.len()
)))
} else {
let mut sorted_permutations = permutations.clone();
sorted_permutations.sort_unstable();
if (0..dimensions).zip(sorted_permutations).any(|(a, b)| a != b) {
Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor permutations invalid, expected a permutation of [0, 1, .., N-1], where N is the number of dimensions: {dimensions}"
)))
} else {
Ok(permutations)
}
}
})
.transpose()?;
fn validate(&self) -> Result<(), ArrowError> {
let dimensions = self.dimensions();

Ok(Self {
shape,
dim_names,
permutations,
})
if let Some(dimension_names) = &self.dim_names
&& dimension_names.len() != dimensions
{
return Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor dimension names size mismatch, expected {dimensions}, found {}",
dimension_names.len()
)));
}

if let Some(permutations) = &self.permutations {
if permutations.len() != dimensions {
return Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor permutations size mismatch, expected {dimensions}, found {}",
permutations.len()
)));
}
let mut sorted_permutations = permutations.clone();
sorted_permutations.sort_unstable();
if (0..dimensions)
.zip(sorted_permutations)
.any(|(a, b)| a != b)
{
return Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor permutations invalid, expected a permutation of [0, 1, .., N-1], where N is the number of dimensions: {dimensions}"
)));
}
}

Ok(())
}

/// Returns the physical shape of the contained tensors.
Expand Down Expand Up @@ -447,31 +474,15 @@ impl ExtensionType for FixedShapeTensor {
}

fn try_new(data_type: &DataType, metadata: Self::Metadata) -> Result<Self, ArrowError> {
match data_type {
DataType::FixedSizeList(field, list_size) if !field.is_nullable() => {
// Make sure the metadata is valid.
let metadata = FixedShapeTensorMetadata::try_new(
metadata.shape,
metadata.dim_names,
metadata.permutations,
)?;
// Make sure it is compatible with this data type.
let expected_size = i32::try_from(metadata.list_size()).expect("overflow");
if *list_size != expected_size {
Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor list size mismatch, expected {expected_size} (metadata), found {list_size} (data type)"
)))
} else {
Ok(Self {
value_type: field.data_type().clone(),
metadata,
})
}
}
data_type => Err(ArrowError::InvalidArgumentError(format!(
"FixedShapeTensor data type mismatch, expected FixedSizeList with non-nullable field, found {data_type}"
))),
}
let value_type = Self::validate_impl(data_type, &metadata)?;
Ok(Self {
value_type: value_type.clone(),
metadata,
})
}

fn validate(data_type: &DataType, metadata: Self::Metadata) -> Result<(), ArrowError> {
Self::validate_impl(data_type, &metadata).map(|_| ())
}
}

Expand Down Expand Up @@ -501,6 +512,7 @@ mod tests {
false,
);
field.try_with_extension_type(fixed_shape_tensor.clone())?;
assert!(field.has_valid_extension_type::<FixedShapeTensor>());
assert_eq!(
field.try_extension_type::<FixedShapeTensor>()?,
fixed_shape_tensor
Expand Down Expand Up @@ -602,6 +614,24 @@ mod tests {
field.extension_type::<FixedShapeTensor>();
}

#[test]
fn invalid_deserialized_metadata() -> Result<(), ArrowError> {
let data_type = DataType::new_fixed_size_list(DataType::Float32, 6, false);
let metadata = FixedShapeTensor::deserialize_metadata(Some(
r#"{"shape":[2,3],"dim_names":["height"]}"#,
))?;
for result in [
FixedShapeTensor::validate(&data_type, metadata.clone()),
<FixedShapeTensor as ExtensionType>::try_new(&data_type, metadata).map(|_| ()),
] {
assert!(
matches!(&result, Err(ArrowError::InvalidArgumentError(message)) if message.contains("dimension names size mismatch")),
"expected dimension names size mismatch, got {result:?}"
);
}
Ok(())
}

#[test]
#[should_panic(
expected = "FixedShapeTensor dimension names size mismatch, expected 3, found 2"
Expand Down
3 changes: 3 additions & 0 deletions arrow-schema/src/extension/canonical/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ mod tests {
fn valid() -> Result<(), ArrowError> {
let mut field = Field::new("", DataType::Utf8, false);
field.try_with_extension_type(Json::default())?;
assert!(field.has_valid_extension_type::<Json>());
assert_eq!(
field.metadata().get(EXTENSION_TYPE_METADATA_KEY),
Some(&String::new())
Expand All @@ -192,6 +193,7 @@ mod tests {

let mut field = Field::new("", DataType::LargeUtf8, false);
field.try_with_extension_type(Json(JsonMetadata(Some(Empty {}))))?;
assert!(field.has_valid_extension_type::<Json>());
assert_eq!(
field.metadata().get(EXTENSION_TYPE_METADATA_KEY),
Some(&"{}".to_owned())
Expand All @@ -203,6 +205,7 @@ mod tests {

let mut field = Field::new("", DataType::Utf8View, false);
field.try_with_extension_type(Json::default())?;
assert!(field.has_valid_extension_type::<Json>());
field.try_extension_type::<Json>()?;
#[cfg(feature = "canonical_extension_types")]
assert_eq!(
Expand Down
Loading