diff --git a/cpp/src/arrow/extension/fixed_shape_tensor.cc b/cpp/src/arrow/extension/fixed_shape_tensor.cc index 7f55375b8226..8e53f8835996 100644 --- a/cpp/src/arrow/extension/fixed_shape_tensor.cc +++ b/cpp/src/arrow/extension/fixed_shape_tensor.cc @@ -28,6 +28,7 @@ #include "arrow/array/array_nested.h" #include "arrow/array/array_primitive.h" #include "arrow/tensor.h" +#include "arrow/util/int_util_overflow.h" #include "arrow/util/logging_internal.h" #include "arrow/util/print_internal.h" #include "arrow/util/simdjson_internal.h" @@ -345,10 +346,19 @@ Result> FixedShapeTensorArray::ToTensorWithNulls() const ARROW_ASSIGN_OR_RAISE(auto tensor_strides, internal::ComputeStrides(value_type, shape, permutation)); - const auto& raw_buffer = this->storage()->data()->child_data[0]->buffers[1]; - ARROW_ASSIGN_OR_RAISE( - const auto buffer, - SliceBufferSafe(raw_buffer, this->offset() * cell_size * value_type->byte_width())); + const auto& fsl = internal::checked_cast(*this->storage()); + int64_t value_offset = 0; + if (internal::MultiplyWithOverflow(this->offset(), cell_size, &value_offset)) { + return Status::Invalid("Tensor values offset would not fit in 64-bit integer"); + } + int64_t value_length = 0; + if (internal::MultiplyWithOverflow(this->length(), cell_size, &value_length)) { + return Status::Invalid("Tensor values length would not fit in 64-bit integer"); + } + ARROW_ASSIGN_OR_RAISE(const auto sliced_values, + fsl.values()->SliceSafe(value_offset, value_length)); + ARROW_ASSIGN_OR_RAISE(const auto buffer, + internal::SliceTensorBuffer(*sliced_values, *value_type, shape)); return Tensor::Make(value_type, buffer, shape, tensor_strides, dim_names); } diff --git a/cpp/src/arrow/extension/tensor_extension_array_test.cc b/cpp/src/arrow/extension/tensor_extension_array_test.cc index 29147df6ba6a..c28647515457 100644 --- a/cpp/src/arrow/extension/tensor_extension_array_test.cc +++ b/cpp/src/arrow/extension/tensor_extension_array_test.cc @@ -496,6 +496,76 @@ TEST_F(TestFixedShapeTensorType, ToTensor) { } } +TEST_F(TestFixedShapeTensorType, ToTensorSlicedValues) { + auto buffer = Buffer::Wrap(values_); + auto values = std::make_shared(values_.size(), buffer)->Slice(3, 24); + ASSERT_OK_AND_ASSIGN(auto storage, FixedSizeListArray::FromArrays(values, 6)); + auto type = fixed_shape_tensor(int64(), {2, 3}, {1, 0}, {"x", "y"}); + auto array = ExtensionType::WrapArray(type, storage); + + for (int64_t offset : {0, 1}) { + const int64_t length = offset == 0 ? 4 : 2; + auto sliced = internal::checked_pointer_cast( + array->Slice(offset, length)); + ASSERT_OK(sliced->ValidateFull()); + ASSERT_OK_AND_ASSIGN(auto tensor, sliced->ToTensor()); + ASSERT_OK(tensor->Validate()); + ASSERT_EQ(tensor->shape(), (std::vector{length, 3, 2})); + ASSERT_EQ(tensor->strides(), (std::vector{48, 8, 24})); + ASSERT_EQ(tensor->dim_names(), (std::vector{"", "y", "x"})); + + const int64_t value_offset = 3 + offset * 6; + std::vector expected_values(values_.begin() + value_offset, + values_.begin() + value_offset + length * 6); + ASSERT_TRUE(tensor->data()->Equals(*Buffer::Wrap(expected_values))); + ASSERT_EQ(tensor->data()->data(), buffer->data() + value_offset * sizeof(int64_t)); + ASSERT_EQ(tensor->data()->size(), length * 6 * sizeof(int64_t)); + } +} + +TEST_F(TestFixedShapeTensorType, ToTensorEmptyValues) { + auto values = ArrayFromJSON(int64(), "[0, 1]")->Slice(1, 0); + auto storage = + std::make_shared(fixed_size_list(int64(), 0), 20, values); + auto array = internal::checked_pointer_cast( + ExtensionType::WrapArray(fixed_shape_tensor(int64(), {0, 2}), storage)); + ASSERT_OK(array->ValidateFull()); + ASSERT_OK_AND_ASSIGN(auto tensor, array->ToTensor()); + ASSERT_OK(tensor->Validate()); + ASSERT_EQ(tensor->shape(), (std::vector{20, 0, 2})); + ASSERT_EQ(tensor->size(), 0); + ASSERT_EQ(tensor->data()->size(), 0); + ASSERT_EQ(tensor->data()->data(), values->data()->buffers[1]->data() + sizeof(int64_t)); +} + +TEST_F(TestFixedShapeTensorType, ToTensorNullValuesBuffer) { + struct TestCase { + std::vector shape; + int64_t length; + }; + for (const auto& test_case : {TestCase{{2, 2}, 0}, TestCase{{0, 2}, 2}}) { + auto type = internal::checked_pointer_cast( + fixed_shape_tensor(int64(), test_case.shape)); + for (const auto& buffer : std::vector>{ + nullptr, Buffer::FromString(""), Buffer::FromString("short")}) { + for (int64_t value_offset : {0, 3}) { + auto values = std::make_shared(0, buffer, nullptr, 0, value_offset); + auto storage = std::make_shared(type->storage_type(), + test_case.length, values); + auto array = internal::checked_pointer_cast( + ExtensionType::WrapArray(type, storage)); + ASSERT_OK(array->ValidateFull()); + ASSERT_OK_AND_ASSIGN(auto tensor, array->ToTensor()); + ASSERT_OK(tensor->Validate()); + ASSERT_NE(tensor->data()->data(), nullptr); + ASSERT_EQ(tensor->size(), 0); + ASSERT_EQ(tensor->data()->size(), 0); + ASSERT_FALSE(tensor->is_mutable()); + } + } + } +} + void CheckTensorRoundtrip(const std::shared_ptr& tensor) { ASSERT_OK_AND_ASSIGN(auto ext_arr, FixedShapeTensorArray::FromTensor(tensor)); ASSERT_OK_AND_ASSIGN(auto tensor_from_array, ext_arr->ToTensor()); @@ -729,6 +799,39 @@ TEST_F(TestFixedShapeTensorType, GetTensor) { exact_ext_type->MakeTensor(ext_scalar)); } +TEST_F(TestFixedShapeTensorType, MakeTensorNullValuesBuffer) { + for (const auto& shape : std::vector>{{0, 2}, {2, 0, 3}}) { + for (const auto& buffer : std::vector>{ + nullptr, Buffer::FromString(""), Buffer::FromString("short")}) { + for (int64_t value_offset : {0, 3}) { + auto values = std::make_shared(0, buffer, nullptr, 0, value_offset); + auto scalar = std::make_shared( + std::make_shared(values), + fixed_shape_tensor(int64(), shape)); + ASSERT_OK(scalar->ValidateFull()); + ASSERT_OK_AND_ASSIGN(auto tensor, FixedShapeTensorType::MakeTensor(scalar)); + ASSERT_OK(tensor->Validate()); + ASSERT_NE(tensor->data()->data(), nullptr); + ASSERT_EQ(tensor->size(), 0); + ASSERT_EQ(tensor->data()->size(), 0); + ASSERT_FALSE(tensor->is_mutable()); + } + } + } + + auto values = std::make_shared(4, nullptr, nullptr, 0); + auto type = fixed_shape_tensor(int64(), {2, 2}); + auto scalar = std::make_shared( + std::make_shared(values), type); + EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("Values buffer is null"), + FixedShapeTensorType::MakeTensor(scalar)); + ASSERT_OK_AND_ASSIGN(auto storage, FixedSizeListArray::FromArrays(values, 4)); + auto array = internal::checked_pointer_cast( + ExtensionType::WrapArray(type, storage)); + EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, testing::HasSubstr("Values buffer is null"), + array->ToTensor()); +} + class TestVariableShapeTensorType : public ::testing::Test { public: void SetUp() override { @@ -1002,6 +1105,41 @@ TEST_F(TestVariableShapeTensorType, ComputeStrides) { ASSERT_TRUE(tensor->Equals(*t)); } +TEST_F(TestVariableShapeTensorType, MakeTensorNullValuesBuffer) { + auto make_scalar = [](const char* shape_json, int64_t length, + const std::shared_ptr& buffer, int64_t value_offset) { + auto shape = ArrayFromJSON(int32(), shape_json); + auto type = internal::checked_pointer_cast( + variable_shape_tensor(int64(), static_cast(shape->length()))); + auto values = std::make_shared(length, buffer, nullptr, 0, value_offset); + auto storage = std::make_shared( + ScalarVector{std::make_shared(values), + std::make_shared(shape)}, + type->storage_type()); + return std::make_shared(storage, type); + }; + + for (const auto& shape_json : {"[0, 2]", "[2, 0, 3]"}) { + for (const auto& buffer : std::vector>{ + nullptr, Buffer::FromString(""), Buffer::FromString("short")}) { + for (int64_t value_offset : {0, 3}) { + auto scalar = make_scalar(shape_json, 0, buffer, value_offset); + ASSERT_OK(scalar->ValidateFull()); + ASSERT_OK_AND_ASSIGN(auto tensor, VariableShapeTensorType::MakeTensor(scalar)); + ASSERT_OK(tensor->Validate()); + ASSERT_NE(tensor->data()->data(), nullptr); + ASSERT_EQ(tensor->size(), 0); + ASSERT_EQ(tensor->data()->size(), 0); + ASSERT_FALSE(tensor->is_mutable()); + } + } + } + + EXPECT_RAISES_WITH_MESSAGE_THAT( + Invalid, testing::HasSubstr("Values buffer is null"), + VariableShapeTensorType::MakeTensor(make_scalar("[2, 2]", 4, nullptr, 0))); +} + TEST_F(TestVariableShapeTensorType, ToString) { auto exact_ext_type = internal::checked_pointer_cast(ext_type_); diff --git a/cpp/src/arrow/extension/tensor_internal.cc b/cpp/src/arrow/extension/tensor_internal.cc index d2965bc578f7..fda7329892db 100644 --- a/cpp/src/arrow/extension/tensor_internal.cc +++ b/cpp/src/arrow/extension/tensor_internal.cc @@ -121,8 +121,23 @@ Result> SliceTensorBuffer(const Array& data_array, return Status::Invalid("Expected data array of length ", size, ", got ", data_array.length()); } + if (data_array.offset() < 0) { + return Status::IndexError("Negative data array offset"); + } + const auto& raw_buffer = data_array.data()->buffers[1]; int64_t start_position = 0; + if (size == 0) { + if (raw_buffer == nullptr || + MultiplyWithOverflow(data_array.offset(), byte_width, &start_position) || + start_position > raw_buffer->size()) { + return Buffer::FromString(""); + } + return SliceBufferSafe(raw_buffer, start_position, 0); + } + if (raw_buffer == nullptr) { + return Status::Invalid("Values buffer is null"); + } if (MultiplyWithOverflow(data_array.offset(), byte_width, &start_position)) { return Status::Invalid("Data offset in bytes would not fit in 64-bit integer"); } @@ -131,7 +146,7 @@ Result> SliceTensorBuffer(const Array& data_array, return Status::Invalid("Tensor byte size would not fit in 64-bit integer"); } - return SliceBufferSafe(data_array.data()->buffers[1], start_position, size_bytes); + return SliceBufferSafe(raw_buffer, start_position, size_bytes); } } // namespace arrow::internal