| 191 | } |
| 192 | |
| 193 | Result<std::shared_ptr<Tensor>> FixedShapeTensorType::MakeTensor( |
| 194 | const std::shared_ptr<ExtensionScalar>& scalar) { |
| 195 | const auto& ext_scalar = internal::checked_cast<const ExtensionScalar&>(*scalar); |
| 196 | const auto& ext_type = |
| 197 | internal::checked_cast<const FixedShapeTensorType&>(*scalar->type); |
| 198 | if (!is_fixed_width(*ext_type.value_type())) { |
| 199 | return Status::TypeError("Cannot convert non-fixed-width values to Tensor."); |
| 200 | } |
| 201 | const auto& array = |
| 202 | internal::checked_cast<const FixedSizeListScalar*>(ext_scalar.value.get())->value; |
| 203 | if (array->null_count() > 0) { |
| 204 | return Status::Invalid("Cannot convert data with nulls to Tensor."); |
| 205 | } |
| 206 | std::vector<int64_t> permutation = ext_type.permutation(); |
| 207 | if (permutation.empty()) { |
| 208 | permutation.resize(ext_type.ndim()); |
| 209 | std::iota(permutation.begin(), permutation.end(), 0); |
| 210 | } |
| 211 | |
| 212 | std::vector<int64_t> shape = ext_type.shape(); |
| 213 | internal::Permute<int64_t>(permutation, &shape); |
| 214 | |
| 215 | std::vector<std::string> dim_names = ext_type.dim_names(); |
| 216 | if (!dim_names.empty()) { |
| 217 | internal::Permute<std::string>(permutation, &dim_names); |
| 218 | } |
| 219 | |
| 220 | ARROW_ASSIGN_OR_RAISE( |
| 221 | auto strides, internal::ComputeStrides(ext_type.value_type(), shape, permutation)); |
| 222 | ARROW_ASSIGN_OR_RAISE(const auto buffer, internal::SliceTensorBuffer( |
| 223 | *array, *ext_type.value_type(), shape)); |
| 224 | |
| 225 | return Tensor::Make(ext_type.value_type(), buffer, shape, strides, dim_names); |
| 226 | } |
| 227 | |
| 228 | Result<std::shared_ptr<FixedShapeTensorArray>> FixedShapeTensorArray::FromTensor( |
| 229 | const std::shared_ptr<Tensor>& tensor) { |