westonpace commented on a change in pull request #11542: URL: https://github.com/apache/arrow/pull/11542#discussion_r741577774
########## File path: cpp/src/arrow/compute/kernels/vector_buffer_test.cc ########## @@ -0,0 +1,382 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <cstdint> +#include <functional> +#include <unordered_map> +#include <utility> +#include <vector> + +#include <gtest/gtest.h> + +#include "arrow/array/builder_primitive.h" +#include "arrow/chunked_array.h" +#include "arrow/compute/api_vector.h" +#include "arrow/compute/kernels/test_util.h" +#include "arrow/table.h" +#include "arrow/testing/extension_type.h" + +namespace arrow { + +using internal::checked_pointer_cast; + +namespace compute { + +struct ExpectedRange { + // The index of the expected buffer. If an array shares buffers then multiple + // ExpectedRange objects will have the same index. + int index; + // The start of the expected range, as an offset from the source buffer start + uint64_t offset; + uint64_t length; +}; + +std::shared_ptr<Array> ExpectedRangesToArray( + const std::vector<ExpectedRange>& ranges, + std::function<uint64_t(const ExpectedRange&)> key_func) { + UInt64Builder builder; + for (const auto& range : ranges) { + ARROW_EXPECT_OK(builder.Append(key_func(range))); + } + std::shared_ptr<Array> arr; + ARROW_EXPECT_OK(builder.Finish(&arr)); + return arr; +} + +std::shared_ptr<Array> RangesToOffsets(const std::vector<ExpectedRange>& ranges) { + return ExpectedRangesToArray(ranges, + [](const ExpectedRange& range) { return range.offset; }); +} + +std::shared_ptr<Array> RangesToLengths(const std::vector<ExpectedRange>& ranges) { + return ExpectedRangesToArray(ranges, + [](const ExpectedRange& range) { return range.length; }); +} + +// We can't validate the buffer addresses exactly because they are unpredictable pointer +// values. However, when multiple ranges come from the same buffer we can validate that +// the buffers are the same. +void CheckBufferRangeStarts(const std::shared_ptr<Array>& starts, + const std::vector<ExpectedRange>& expected) { + const std::shared_ptr<UInt64Array>& starts_uint64 = + checked_pointer_cast<UInt64Array>(starts); + std::unordered_map<int, uint64_t> previous_buffer_starts; + ASSERT_NE(nullptr, starts_uint64); + const uint64_t* starts_data = starts_uint64->raw_values(); + for (std::size_t i = 0; i < expected.size(); i++) { + const auto& previous_buffer_start = previous_buffer_starts.find(expected[i].index); + if (previous_buffer_start == previous_buffer_starts.end()) { + previous_buffer_starts.insert({expected[i].index, starts_data[i]}); + } else { + ASSERT_EQ(starts_data[i], previous_buffer_start->second); + } + } +} + +template <typename T> +void CheckBufferRanges(const std::shared_ptr<T>& input, + const std::vector<ExpectedRange>& expected) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> result, GetByteRanges(input)); + ValidateOutput(*result); + std::shared_ptr<StructArray> result_struct = checked_pointer_cast<StructArray>(result); + ASSERT_NE(nullptr, result_struct); + AssertArraysEqual(*result_struct->field(1), *RangesToOffsets(expected)); + AssertArraysEqual(*result_struct->field(2), *RangesToLengths(expected)); + CheckBufferRangeStarts(result_struct->field(0), expected); +} + +template <typename T> +void CheckFixedWidthStarts(const std::shared_ptr<T>& input) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> result, GetByteRanges(input)); + ValidateOutput(*result); + uint64_t expected_values_start = + reinterpret_cast<uint64_t>(input->data()->buffers[1]->data()); + uint64_t expected_validity_start = + reinterpret_cast<uint64_t>(input->null_bitmap_data()); + std::shared_ptr<StructArray> result_struct = checked_pointer_cast<StructArray>(result); + const uint64_t* raw_starts = + checked_pointer_cast<UInt64Array>(result_struct->field(0))->raw_values(); + ASSERT_EQ(expected_validity_start, raw_starts[0]); + ASSERT_EQ(expected_values_start, raw_starts[1]); +} + +TEST(ByteRanges, StartValue) { + std::shared_ptr<Array> bool_arr = ArrayFromJSON( + boolean(), "[true, true, true, null, null, null, true, true, true, true]"); + CheckFixedWidthStarts(bool_arr); + CheckFixedWidthStarts(bool_arr->Slice(9, 1)); + + std::shared_ptr<Array> ts_arr = + ArrayFromJSON(timestamp(TimeUnit::SECOND), + R"(["1970-01-01","2000-02-29","3989-07-14","1900-02-28"])"); + CheckFixedWidthStarts(bool_arr); + CheckFixedWidthStarts(bool_arr->Slice(2, 1)); Review comment: Yep, good catch. ########## File path: cpp/src/arrow/compute/kernels/vector_buffer_test.cc ########## @@ -0,0 +1,382 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <cstdint> +#include <functional> +#include <unordered_map> +#include <utility> +#include <vector> + +#include <gtest/gtest.h> + +#include "arrow/array/builder_primitive.h" +#include "arrow/chunked_array.h" +#include "arrow/compute/api_vector.h" +#include "arrow/compute/kernels/test_util.h" +#include "arrow/table.h" +#include "arrow/testing/extension_type.h" + +namespace arrow { + +using internal::checked_pointer_cast; + +namespace compute { + +struct ExpectedRange { + // The index of the expected buffer. If an array shares buffers then multiple + // ExpectedRange objects will have the same index. + int index; + // The start of the expected range, as an offset from the source buffer start + uint64_t offset; + uint64_t length; +}; + +std::shared_ptr<Array> ExpectedRangesToArray( + const std::vector<ExpectedRange>& ranges, + std::function<uint64_t(const ExpectedRange&)> key_func) { + UInt64Builder builder; + for (const auto& range : ranges) { + ARROW_EXPECT_OK(builder.Append(key_func(range))); + } + std::shared_ptr<Array> arr; + ARROW_EXPECT_OK(builder.Finish(&arr)); + return arr; +} + +std::shared_ptr<Array> RangesToOffsets(const std::vector<ExpectedRange>& ranges) { + return ExpectedRangesToArray(ranges, + [](const ExpectedRange& range) { return range.offset; }); +} + +std::shared_ptr<Array> RangesToLengths(const std::vector<ExpectedRange>& ranges) { + return ExpectedRangesToArray(ranges, + [](const ExpectedRange& range) { return range.length; }); +} + +// We can't validate the buffer addresses exactly because they are unpredictable pointer +// values. However, when multiple ranges come from the same buffer we can validate that +// the buffers are the same. +void CheckBufferRangeStarts(const std::shared_ptr<Array>& starts, + const std::vector<ExpectedRange>& expected) { + const std::shared_ptr<UInt64Array>& starts_uint64 = + checked_pointer_cast<UInt64Array>(starts); + std::unordered_map<int, uint64_t> previous_buffer_starts; + ASSERT_NE(nullptr, starts_uint64); + const uint64_t* starts_data = starts_uint64->raw_values(); + for (std::size_t i = 0; i < expected.size(); i++) { + const auto& previous_buffer_start = previous_buffer_starts.find(expected[i].index); + if (previous_buffer_start == previous_buffer_starts.end()) { + previous_buffer_starts.insert({expected[i].index, starts_data[i]}); + } else { + ASSERT_EQ(starts_data[i], previous_buffer_start->second); + } + } +} + +template <typename T> +void CheckBufferRanges(const std::shared_ptr<T>& input, + const std::vector<ExpectedRange>& expected) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> result, GetByteRanges(input)); + ValidateOutput(*result); + std::shared_ptr<StructArray> result_struct = checked_pointer_cast<StructArray>(result); + ASSERT_NE(nullptr, result_struct); + AssertArraysEqual(*result_struct->field(1), *RangesToOffsets(expected)); + AssertArraysEqual(*result_struct->field(2), *RangesToLengths(expected)); + CheckBufferRangeStarts(result_struct->field(0), expected); +} + +template <typename T> +void CheckFixedWidthStarts(const std::shared_ptr<T>& input) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> result, GetByteRanges(input)); + ValidateOutput(*result); + uint64_t expected_values_start = + reinterpret_cast<uint64_t>(input->data()->buffers[1]->data()); + uint64_t expected_validity_start = + reinterpret_cast<uint64_t>(input->null_bitmap_data()); + std::shared_ptr<StructArray> result_struct = checked_pointer_cast<StructArray>(result); + const uint64_t* raw_starts = + checked_pointer_cast<UInt64Array>(result_struct->field(0))->raw_values(); + ASSERT_EQ(expected_validity_start, raw_starts[0]); + ASSERT_EQ(expected_values_start, raw_starts[1]); +} + +TEST(ByteRanges, StartValue) { + std::shared_ptr<Array> bool_arr = ArrayFromJSON( + boolean(), "[true, true, true, null, null, null, true, true, true, true]"); + CheckFixedWidthStarts(bool_arr); + CheckFixedWidthStarts(bool_arr->Slice(9, 1)); + + std::shared_ptr<Array> ts_arr = + ArrayFromJSON(timestamp(TimeUnit::SECOND), + R"(["1970-01-01","2000-02-29","3989-07-14","1900-02-28"])"); + CheckFixedWidthStarts(bool_arr); + CheckFixedWidthStarts(bool_arr->Slice(2, 1)); +} + +TEST(ByteRanges, FixedWidthTypes) { + std::shared_ptr<Array> bool_arr = ArrayFromJSON( + boolean(), "[true, true, true, null, null, null, true, true, true, true]"); + CheckBufferRanges(bool_arr, {{0, 0, 2}, {1, 0, 2}}); + CheckBufferRanges(bool_arr->Slice(1, 8), {{0, 0, 2}, {1, 0, 2}}); + CheckBufferRanges(bool_arr->Slice(1, 5), {{0, 0, 1}, {1, 0, 1}}); + CheckBufferRanges(bool_arr->Slice(5, 5), {{0, 0, 2}, {1, 0, 2}}); + CheckBufferRanges(bool_arr->Slice(9, 1), {{0, 1, 1}, {1, 1, 1}}); + + std::shared_ptr<Array> bool_arr_no_validity = ArrayFromJSON(boolean(), "[true, true]"); + CheckBufferRanges(bool_arr_no_validity, {{0, 0, 1}}); + + std::shared_ptr<Array> fsb_arr = + ArrayFromJSON(fixed_size_binary(4), R"(["foox", "barz", null])"); + CheckBufferRanges(fsb_arr, {{0, 0, 1}, {1, 0, 12}}); + CheckBufferRanges(fsb_arr->Slice(1, 1), {{0, 0, 1}, {1, 4, 4}}); +} + +TEST(ByteRanges, DictionaryArray) { + std::shared_ptr<Array> dict_arr = + ArrayFromJSON(dictionary(int16(), utf8()), R"(["x", "abc", "x", null])"); + CheckBufferRanges(dict_arr, {{0, 0, 1}, {1, 0, 8}, {2, 0, 8}, {3, 0, 4}}); + CheckBufferRanges(dict_arr->Slice(2, 2), {{0, 0, 1}, {1, 4, 4}, {2, 0, 8}, {3, 0, 4}}); +} + +template <typename Type> +class ByteRangesVariableBinary : public ::testing::Test {}; +typedef ::testing::Types<StringType, LargeStringType, BinaryType, LargeBinaryType> + VariableLengthBinaryTypes; Review comment: Fixed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
