From 198dc1d804a91ee398faead36b1c4e07a49840ad Mon Sep 17 00:00:00 2001 From: mora Date: Sun, 29 Nov 2026 04:21:27 +0800 Subject: [PATCH 1/2] GH-55834: [C++] Preserve child offset and handle null values buffers in tensor extensions Compose outer row slicing with the child values slice in whole-array fixed-shape tensor conversion so child offsets and exact byte lengths are respected with overflow checks, and return a static immutable zero-byte buffer when zero-element tensor arrays or scalars omit the values buffer. Signed-off-by: mora --- cpp/src/arrow/extension/fixed_shape_tensor.cc | 18 ++- .../extension/tensor_extension_array_test.cc | 125 ++++++++++++++++++ cpp/src/arrow/extension/tensor_internal.cc | 10 +- 3 files changed, 148 insertions(+), 5 deletions(-) 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..84e5baf12716 100644 --- a/cpp/src/arrow/extension/tensor_extension_array_test.cc +++ b/cpp/src/arrow/extension/tensor_extension_array_test.cc @@ -496,6 +496,74 @@ 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; + int64_t value_offset; + }; + for (const auto& test_case : + {TestCase{{2, 2}, 0, 0}, TestCase{{0, 2}, 2, 0}, TestCase{{0, 2}, 2, 3}}) { + auto type = internal::checked_pointer_cast( + fixed_shape_tensor(int64(), test_case.shape)); + auto values = + std::make_shared(0, nullptr, nullptr, 0, test_case.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 +797,34 @@ 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}}) { + auto values = std::make_shared(0, nullptr, nullptr, 0); + 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 +1098,35 @@ TEST_F(TestVariableShapeTensorType, ComputeStrides) { ASSERT_TRUE(tensor->Equals(*t)); } +TEST_F(TestVariableShapeTensorType, MakeTensorNullValuesBuffer) { + auto make_scalar = [](const char* shape_json, int64_t length) { + 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, nullptr, nullptr, 0); + 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]"}) { + auto scalar = make_scalar(shape_json, 0); + 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))); +} + 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..8d2bc6109a96 100644 --- a/cpp/src/arrow/extension/tensor_internal.cc +++ b/cpp/src/arrow/extension/tensor_internal.cc @@ -122,6 +122,14 @@ Result> SliceTensorBuffer(const Array& data_array, data_array.length()); } + const auto& raw_buffer = data_array.data()->buffers[1]; + if (raw_buffer == nullptr) { + if (size == 0) { + return Buffer::FromString(""); + } + return Status::Invalid("Values buffer is null"); + } + int64_t start_position = 0; 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 +139,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 From b3e1b6f7f77fda028467a2e3a344164f94cb5783 Mon Sep 17 00:00:00 2001 From: mora Date: Sun, 29 Nov 2026 06:02:40 +0800 Subject: [PATCH 2/2] GH-55834: [C++] Handle non-null empty buffers with nonzero offset in SliceTensorBuffer Return an immutable zero-byte buffer when a zero-element tensor slice starts past a non-null empty or shorter backing buffer, matching ValidateLayout's zero-length contract while keeping zero-copy view offsets for in-range slices. Signed-off-by: mora --- .../extension/tensor_extension_array_test.cc | 89 +++++++++++-------- cpp/src/arrow/extension/tensor_internal.cc | 15 +++- 2 files changed, 62 insertions(+), 42 deletions(-) diff --git a/cpp/src/arrow/extension/tensor_extension_array_test.cc b/cpp/src/arrow/extension/tensor_extension_array_test.cc index 84e5baf12716..c28647515457 100644 --- a/cpp/src/arrow/extension/tensor_extension_array_test.cc +++ b/cpp/src/arrow/extension/tensor_extension_array_test.cc @@ -542,25 +542,27 @@ TEST_F(TestFixedShapeTensorType, ToTensorNullValuesBuffer) { struct TestCase { std::vector shape; int64_t length; - int64_t value_offset; }; - for (const auto& test_case : - {TestCase{{2, 2}, 0, 0}, TestCase{{0, 2}, 2, 0}, TestCase{{0, 2}, 2, 3}}) { + 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)); - auto values = - std::make_shared(0, nullptr, nullptr, 0, test_case.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()); + 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()); + } + } } } @@ -799,17 +801,22 @@ TEST_F(TestFixedShapeTensorType, GetTensor) { TEST_F(TestFixedShapeTensorType, MakeTensorNullValuesBuffer) { for (const auto& shape : std::vector>{{0, 2}, {2, 0, 3}}) { - auto values = std::make_shared(0, nullptr, nullptr, 0); - 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()); + 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); @@ -1099,11 +1106,12 @@ TEST_F(TestVariableShapeTensorType, ComputeStrides) { } TEST_F(TestVariableShapeTensorType, MakeTensorNullValuesBuffer) { - auto make_scalar = [](const char* shape_json, int64_t length) { + 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, nullptr, nullptr, 0); + 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)}, @@ -1112,19 +1120,24 @@ TEST_F(TestVariableShapeTensorType, MakeTensorNullValuesBuffer) { }; for (const auto& shape_json : {"[0, 2]", "[2, 0, 3]"}) { - auto scalar = make_scalar(shape_json, 0); - 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()); + 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))); + VariableShapeTensorType::MakeTensor(make_scalar("[2, 2]", 4, nullptr, 0))); } TEST_F(TestVariableShapeTensorType, ToString) { diff --git a/cpp/src/arrow/extension/tensor_internal.cc b/cpp/src/arrow/extension/tensor_internal.cc index 8d2bc6109a96..fda7329892db 100644 --- a/cpp/src/arrow/extension/tensor_internal.cc +++ b/cpp/src/arrow/extension/tensor_internal.cc @@ -121,16 +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]; - if (raw_buffer == nullptr) { - if (size == 0) { + 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"); } - - int64_t start_position = 0; if (MultiplyWithOverflow(data_array.offset(), byte_width, &start_position)) { return Status::Invalid("Data offset in bytes would not fit in 64-bit integer"); }