lidavidm commented on a change in pull request #11441: URL: https://github.com/apache/arrow/pull/11441#discussion_r735956880
########## File path: cpp/src/arrow/util/byte_size_test.cc ########## @@ -0,0 +1,94 @@ +// 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 "arrow/util/byte_size.h" + +#include <gtest/gtest.h> + +#include "arrow/array.h" +#include "arrow/buffer.h" +#include "arrow/testing/generator.h" +#include "arrow/testing/gtest_util.h" + +namespace arrow { +namespace util { + +TEST(TotalBufferSize, Arrays) { + std::shared_ptr<Array> no_nulls = ArrayFromJSON(int16(), "[1, 2, 3]"); + ASSERT_EQ(6, TotalBufferSize(*no_nulls->data())); + + std::shared_ptr<Array> with_nulls = + ArrayFromJSON(int16(), "[1, 2, 3, 4, null, 6, 7, 8, 9]"); + ASSERT_EQ(20, TotalBufferSize(*with_nulls->data())); +} + +TEST(TotalBufferSize, ArrayWithOffset) { + std::shared_ptr<Array> base_array = + ArrayFromJSON(int16(), "[1, 2, 3, 4, null, 6, 7, 8, 9]"); + std::shared_ptr<Array> sliced = base_array->Slice(8, 1); + ASSERT_EQ(20, TotalBufferSize(*sliced)); +} + +TEST(TotalBufferSize, ArrayWithDict) { + std::shared_ptr<Array> arr = ArrayFromJSON(dictionary(int32(), int8()), "[0, 0, 0]"); Review comment: Is this a valid dictionary array? (Where's the dictionary?) There's a separate helper called DictArrayFromJSON for this kind of use so you can define both the dictionary and the indices. ########## File path: cpp/src/arrow/util/byte_size_test.cc ########## @@ -0,0 +1,94 @@ +// 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 "arrow/util/byte_size.h" + +#include <gtest/gtest.h> + +#include "arrow/array.h" +#include "arrow/buffer.h" +#include "arrow/testing/generator.h" +#include "arrow/testing/gtest_util.h" + +namespace arrow { +namespace util { + +TEST(TotalBufferSize, Arrays) { Review comment: Can we also add a test of a nested type (struct, list, etc)? ########## File path: cpp/src/arrow/util/byte_size.cc ########## @@ -0,0 +1,107 @@ +// 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 "arrow/util/byte_size.h" + +#include <cstdint> +#include <unordered_set> + +#include "arrow/array.h" +#include "arrow/buffer.h" + +namespace arrow { + +namespace util { + +namespace { + +int64_t DoTotalBufferSize(const ArrayData& array_data, + std::unordered_set<const uint8_t*>* seen_buffers) { + int64_t sum = 0; + for (const auto& buffer : array_data.buffers) { + if (buffer && seen_buffers->insert(buffer->data()).second) { Review comment: (Though, probably that won't come up often?) ########## File path: cpp/src/arrow/datum.cc ########## @@ -128,6 +129,25 @@ int64_t Datum::length() const { } } +int64_t Datum::TotalBufferSize() const { + switch (this->kind()) { + case Datum::ARRAY: + return util::TotalBufferSize(*util::get<std::shared_ptr<ArrayData>>(this->value)); + case Datum::CHUNKED_ARRAY: + return util::TotalBufferSize( + *util::get<std::shared_ptr<ChunkedArray>>(this->value)); + case Datum::RECORD_BATCH: + return util::TotalBufferSize(*util::get<std::shared_ptr<RecordBatch>>(this->value)); + case Datum::TABLE: + return util::TotalBufferSize(*util::get<std::shared_ptr<Table>>(this->value)); + case Datum::SCALAR: + return 0; Review comment: I suppose it is well-documented in the docstring. ########## File path: cpp/src/arrow/util/byte_size.cc ########## @@ -0,0 +1,107 @@ +// 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 "arrow/util/byte_size.h" + +#include <cstdint> +#include <unordered_set> + +#include "arrow/array.h" +#include "arrow/buffer.h" + +namespace arrow { + +namespace util { + +namespace { + +int64_t DoTotalBufferSize(const ArrayData& array_data, + std::unordered_set<const uint8_t*>* seen_buffers) { + int64_t sum = 0; + for (const auto& buffer : array_data.buffers) { + if (buffer && seen_buffers->insert(buffer->data()).second) { Review comment: This won't catch the case where one buffer is a subset of another, right? ########## File path: cpp/src/arrow/datum.cc ########## @@ -128,6 +129,25 @@ int64_t Datum::length() const { } } +int64_t Datum::TotalBufferSize() const { + switch (this->kind()) { + case Datum::ARRAY: + return util::TotalBufferSize(*util::get<std::shared_ptr<ArrayData>>(this->value)); + case Datum::CHUNKED_ARRAY: + return util::TotalBufferSize( + *util::get<std::shared_ptr<ChunkedArray>>(this->value)); + case Datum::RECORD_BATCH: + return util::TotalBufferSize(*util::get<std::shared_ptr<RecordBatch>>(this->value)); + case Datum::TABLE: + return util::TotalBufferSize(*util::get<std::shared_ptr<Table>>(this->value)); + case Datum::SCALAR: + return 0; Review comment: Is it worth filing a follow-up for this? I imagine most scalars are fairly trivial but strings may not necessarily be. -- 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]
