|
| 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. |
| 17 | + |
| 18 | +use crate::types::extension::DFExtensionType; |
| 19 | +use arrow::datatypes::DataType; |
| 20 | +use arrow_schema::ArrowError; |
| 21 | +use arrow_schema::extension::{ExtensionType, VariableShapeTensor}; |
| 22 | + |
| 23 | +/// Defines the extension type logic for the canonical `arrow.variable_shape_tensor` extension type. |
| 24 | +/// |
| 25 | +/// See [`DFExtensionType`] for information on DataFusion's extension type mechanism. |
| 26 | +#[derive(Debug, Clone)] |
| 27 | +pub struct DFVariableShapeTensor { |
| 28 | + inner: VariableShapeTensor, |
| 29 | + /// While we could reconstruct the storage type from the inner [`VariableShapeTensor`], we may |
| 30 | + /// choose a different name for the field within the [`DataType::List`] which can cause problems |
| 31 | + /// down the line (e.g., checking for equality). |
| 32 | + storage_type: DataType, |
| 33 | +} |
| 34 | + |
| 35 | +impl ExtensionType for DFVariableShapeTensor { |
| 36 | + const NAME: &'static str = VariableShapeTensor::NAME; |
| 37 | + type Metadata = <VariableShapeTensor as ExtensionType>::Metadata; |
| 38 | + |
| 39 | + fn metadata(&self) -> &Self::Metadata { |
| 40 | + self.inner.metadata() |
| 41 | + } |
| 42 | + |
| 43 | + fn serialize_metadata(&self) -> Option<String> { |
| 44 | + self.inner.serialize_metadata() |
| 45 | + } |
| 46 | + |
| 47 | + fn deserialize_metadata( |
| 48 | + metadata: Option<&str>, |
| 49 | + ) -> Result<Self::Metadata, ArrowError> { |
| 50 | + VariableShapeTensor::deserialize_metadata(metadata) |
| 51 | + } |
| 52 | + |
| 53 | + fn supports_data_type(&self, data_type: &DataType) -> Result<(), ArrowError> { |
| 54 | + self.inner.supports_data_type(data_type) |
| 55 | + } |
| 56 | + |
| 57 | + fn try_new( |
| 58 | + data_type: &DataType, |
| 59 | + metadata: Self::Metadata, |
| 60 | + ) -> Result<Self, ArrowError> { |
| 61 | + Ok(Self { |
| 62 | + inner: <VariableShapeTensor as ExtensionType>::try_new(data_type, metadata)?, |
| 63 | + storage_type: data_type.clone(), |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl DFExtensionType for DFVariableShapeTensor { |
| 69 | + fn storage_type(&self) -> DataType { |
| 70 | + self.storage_type.clone() |
| 71 | + } |
| 72 | + |
| 73 | + fn serialize_metadata(&self) -> Option<String> { |
| 74 | + self.inner.serialize_metadata() |
| 75 | + } |
| 76 | +} |
0 commit comments