// 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. #pragma once #include #include #include #include #include "arrow/array.h" #include "arrow/array/builder_binary.h" #include "arrow/array/builder_primitive.h" #include "arrow/array/builder_time.h" #include "arrow/buffer.h" #include "arrow/testing/gtest_util.h" #include "arrow/type_fwd.h" #include "arrow/util/bit_util.h" #include "arrow/visit_type_inline.h" namespace arrow { // ArrayFromVector: construct an Array from vectors of C values template void ArrayFromVector(const std::shared_ptr& type, const std::vector& is_valid, const std::vector& values, std::shared_ptr* out) { auto type_id = TYPE::type_id; ASSERT_EQ(type_id, type->id()) << "template parameter and concrete DataType instance don't agree"; std::unique_ptr builder_ptr; ASSERT_OK(MakeBuilder(default_memory_pool(), type, &builder_ptr)); // Get the concrete builder class to access its Append() specializations auto& builder = dynamic_cast::BuilderType&>(*builder_ptr); for (size_t i = 0; i < values.size(); ++i) { if (is_valid[i]) { ASSERT_OK(builder.Append(values[i])); } else { ASSERT_OK(builder.AppendNull()); } } ASSERT_OK(builder.Finish(out)); } template void ArrayFromVector(const std::shared_ptr& type, const std::vector& values, std::shared_ptr* out) { auto type_id = TYPE::type_id; ASSERT_EQ(type_id, type->id()) << "template parameter and concrete DataType instance don't agree"; std::unique_ptr builder_ptr; ASSERT_OK(MakeBuilder(default_memory_pool(), type, &builder_ptr)); // Get the concrete builder class to access its Append() specializations auto& builder = dynamic_cast::BuilderType&>(*builder_ptr); for (size_t i = 0; i < values.size(); ++i) { ASSERT_OK(builder.Append(values[i])); } ASSERT_OK(builder.Finish(out)); } // Overloads without a DataType argument, for parameterless types template void ArrayFromVector(const std::vector& is_valid, const std::vector& values, std::shared_ptr* out) { auto type = TypeTraits::type_singleton(); ArrayFromVector(type, is_valid, values, out); } template void ArrayFromVector(const std::vector& values, std::shared_ptr* out) { auto type = TypeTraits::type_singleton(); ArrayFromVector(type, values, out); } // ChunkedArrayFromVector: construct a ChunkedArray from vectors of C values template void ChunkedArrayFromVector(const std::shared_ptr& type, const std::vector>& is_valid, const std::vector>& values, std::shared_ptr* out) { ArrayVector chunks; ASSERT_EQ(is_valid.size(), values.size()); for (size_t i = 0; i < values.size(); ++i) { std::shared_ptr array; ArrayFromVector(type, is_valid[i], values[i], &array); chunks.push_back(array); } *out = std::make_shared(chunks); } template void ChunkedArrayFromVector(const std::shared_ptr& type, const std::vector>& values, std::shared_ptr* out) { ArrayVector chunks; for (size_t i = 0; i < values.size(); ++i) { std::shared_ptr array; ArrayFromVector(type, values[i], &array); chunks.push_back(array); } *out = std::make_shared(chunks); } // Overloads without a DataType argument, for parameterless types template void ChunkedArrayFromVector(const std::vector>& is_valid, const std::vector>& values, std::shared_ptr* out) { auto type = TypeTraits::type_singleton(); ChunkedArrayFromVector(type, is_valid, values, out); } template void ChunkedArrayFromVector(const std::vector>& values, std::shared_ptr* out) { auto type = TypeTraits::type_singleton(); ChunkedArrayFromVector(type, values, out); } template void FinishAndCheckPadding(BuilderType* builder, std::shared_ptr* out) { ASSERT_OK_AND_ASSIGN(*out, builder->Finish()); AssertZeroPadded(**out); TestInitialized(**out); } template Status MakeArray(const std::vector& valid_bytes, const std::vector& values, int64_t size, Builder* builder, std::shared_ptr* out) { // Append the first 1000 for (int64_t i = 0; i < size; ++i) { if (valid_bytes[i] > 0) { RETURN_NOT_OK(builder->Append(values[i])); } else { RETURN_NOT_OK(builder->AppendNull()); } } return builder->Finish(out); } template struct VisitBuilder { template ::BuilderType, // need to let SFINAE drop this Visit when it would result in // [](NullBuilder*){}(double_builder) typename = decltype(std::declval()(std::declval()))> Status Visit(const T&, ArrayBuilder* builder, Fn&& fn) { fn(internal::checked_cast(builder)); return Status::OK(); } Status Visit(const DataType& t, ArrayBuilder* builder, Fn&& fn) { return Status::NotImplemented("visiting builders of type ", t); } }; template Result> ArrayFromBuilderVisitor( const std::shared_ptr& type, int64_t initial_capacity, int64_t visitor_repetitions, Fn&& fn) { std::unique_ptr builder; RETURN_NOT_OK(MakeBuilder(default_memory_pool(), type, &builder)); if (initial_capacity != 0) { RETURN_NOT_OK(builder->Resize(initial_capacity)); } VisitBuilder visitor; for (int64_t i = 0; i < visitor_repetitions; ++i) { RETURN_NOT_OK( VisitTypeInline(*builder->type(), &visitor, builder.get(), std::forward(fn))); } std::shared_ptr out; RETURN_NOT_OK(builder->Finish(&out)); return std::move(out); } template Result> ArrayFromBuilderVisitor( const std::shared_ptr& type, int64_t length, Fn&& fn) { return ArrayFromBuilderVisitor(type, length, length, std::forward(fn)); } template static inline Status GetBitmapFromVector(const std::vector& is_valid, std::shared_ptr* result) { size_t length = is_valid.size(); ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateEmptyBitmap(length)); uint8_t* bitmap = buffer->mutable_data(); for (size_t i = 0; i < static_cast(length); ++i) { if (is_valid[i]) { bit_util::SetBit(bitmap, i); } } *result = buffer; return Status::OK(); } template inline void BitmapFromVector(const std::vector& is_valid, std::shared_ptr* out) { ASSERT_OK(GetBitmapFromVector(is_valid, out)); } } // namespace arrow