MCPcopy Create free account
hub / github.com/apache/arrow / MakeTensor

Method MakeTensor

cpp/src/arrow/extension/variable_shape_tensor.cc:224–280  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

222}
223
224Result<std::shared_ptr<Tensor>> VariableShapeTensorType::MakeTensor(
225 const std::shared_ptr<ExtensionScalar>& scalar) {
226 const auto& tensor_scalar = internal::checked_cast<const StructScalar&>(*scalar->value);
227 const auto& ext_type =
228 internal::checked_cast<const VariableShapeTensorType&>(*scalar->type);
229
230 if (!tensor_scalar.is_valid) {
231 return Status::Invalid("Cannot convert null scalar to Tensor.");
232 }
233 ARROW_ASSIGN_OR_RAISE(const auto data_scalar, tensor_scalar.field(0));
234 ARROW_ASSIGN_OR_RAISE(const auto shape_scalar, tensor_scalar.field(1));
235 const auto data_array =
236 internal::checked_pointer_cast<BaseListScalar>(data_scalar)->value;
237 const auto shape_array = internal::checked_pointer_cast<Int32Array>(
238 internal::checked_pointer_cast<FixedSizeListScalar>(shape_scalar)->value);
239
240 const auto& value_type =
241 internal::checked_cast<const FixedWidthType&>(*ext_type.value_type());
242
243 if (data_array->null_count() > 0) {
244 return Status::Invalid("Cannot convert data with nulls to Tensor.");
245 }
246
247 auto permutation = ext_type.permutation();
248 if (permutation.empty()) {
249 permutation.resize(ext_type.ndim());
250 std::iota(permutation.begin(), permutation.end(), 0);
251 }
252
253 if (shape_array->length() != ext_type.ndim()) {
254 return Status::Invalid("Expected shape array of length ", ext_type.ndim(), ", got ",
255 shape_array->length());
256 }
257 std::vector<int64_t> shape;
258 shape.reserve(ext_type.ndim());
259 for (int64_t j = 0; j < static_cast<int64_t>(ext_type.ndim()); ++j) {
260 const auto size_value = shape_array->Value(j);
261 if (size_value < 0) {
262 return Status::Invalid("shape must have non-negative values");
263 }
264 shape.push_back(size_value);
265 }
266
267 std::vector<std::string> dim_names = ext_type.dim_names();
268 if (!dim_names.empty()) {
269 internal::Permute<std::string>(permutation, &dim_names);
270 }
271
272 ARROW_ASSIGN_OR_RAISE(
273 auto strides, internal::ComputeStrides(ext_type.value_type(), shape, permutation));
274 internal::Permute<int64_t>(permutation, &shape);
275
276 ARROW_ASSIGN_OR_RAISE(const auto buffer,
277 internal::SliceTensorBuffer(*data_array, value_type, shape));
278
279 return Tensor::Make(ext_type.value_type(), buffer, shape, strides, dim_names);
280}
281

Callers

nothing calls this directly

Calls 12

resizeMethod · 0.80
push_backMethod · 0.80
MakeFunction · 0.70
InvalidFunction · 0.50
value_typeMethod · 0.45
null_countMethod · 0.45
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
lengthMethod · 0.45
reserveMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected