Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- ckpts/universal/global_step80/zero/16.mlp.dense_4h_to_h.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/16.mlp.dense_4h_to_h.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/24.attention.dense.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/3.mlp.dense_h_to_4h_swiglu.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/3.mlp.dense_h_to_4h_swiglu.weight/fp32.pt +3 -0
- ckpts/universal/global_step80/zero/8.attention.dense.weight/exp_avg.pt +3 -0
- ckpts/universal/global_step80/zero/8.attention.dense.weight/exp_avg_sq.pt +3 -0
- ckpts/universal/global_step80/zero/8.attention.dense.weight/fp32.pt +3 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_binary.h +329 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_decimal.h +72 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_dict.h +182 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_primitive.h +202 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_adaptive.h +215 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_binary.h +971 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_decimal.h +102 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_primitive.h +555 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_time.h +66 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_union.h +254 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/data.h +634 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/diff.h +76 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/validate.h +56 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/api.h +53 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/cast.h +134 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/function.h +409 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/function_options.h +81 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/ordering.h +120 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/registry.h +126 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/util.h +294 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/chunked_builder.h +68 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/chunker.h +35 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/converter.h +94 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/object_parser.h +54 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/object_writer.h +48 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/options.h +74 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/parser.h +107 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/rapidjson_defs.h +43 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/reader.h +118 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/test_common.h +330 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/type_fwd.h +26 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/async_test_util.h +79 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/builder.h +231 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/executor_util.h +55 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/generator.h +321 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/gtest_compat.h +33 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/random.h +698 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/uniform_real.h +84 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/util.h +140 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/util/bitmap.h +466 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/util/bitmap_ops.h +244 -0
- venv/lib/python3.10/site-packages/pyarrow/include/arrow/util/checked_cast.h +61 -0
ckpts/universal/global_step80/zero/16.mlp.dense_4h_to_h.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f51e6253f50ae9eb088f7dca0a13e0ffa01def0b96eb883d45ca5f1a31129156
|
3 |
+
size 33555612
|
ckpts/universal/global_step80/zero/16.mlp.dense_4h_to_h.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2b49e9761c4b8ccf8c6e14ffc1ce8f21ec363502d008eb46d842207dc1a91c99
|
3 |
+
size 33555533
|
ckpts/universal/global_step80/zero/24.attention.dense.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fdb8251b987c9ee461212dc5703ad28b984fdbedfb38dd8442e1ffaf78f72ea4
|
3 |
+
size 16778411
|
ckpts/universal/global_step80/zero/3.mlp.dense_h_to_4h_swiglu.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8bd2a9b606031b56b575fcdb1b3d3c63d1bf21b34bc2d43fcd8bd03dccd031fe
|
3 |
+
size 33555612
|
ckpts/universal/global_step80/zero/3.mlp.dense_h_to_4h_swiglu.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4bf572747c0c2d138a6dc95595ff956401865f72cbb93ced955383e53cd62dc0
|
3 |
+
size 33555533
|
ckpts/universal/global_step80/zero/8.attention.dense.weight/exp_avg.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3e00cb78804dd24f01a1463321d5c4ca0ad37a567fa7c36b584a3b4530c6a44
|
3 |
+
size 16778396
|
ckpts/universal/global_step80/zero/8.attention.dense.weight/exp_avg_sq.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e24cdc6af74ac6e9b1a093311858a565f1f109e7866e81395fb6708758cc39b
|
3 |
+
size 16778411
|
ckpts/universal/global_step80/zero/8.attention.dense.weight/fp32.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5be5f976557379e87c487aa3c39b5fa755650dd5f3cd917aed7b46601dfae496
|
3 |
+
size 16778317
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_binary.h
ADDED
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// Array accessor classes for Binary, LargeBinary, String, LargeString,
|
19 |
+
// FixedSizeBinary
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <cstdint>
|
24 |
+
#include <memory>
|
25 |
+
#include <optional>
|
26 |
+
#include <string>
|
27 |
+
#include <string_view>
|
28 |
+
#include <vector>
|
29 |
+
|
30 |
+
#include "arrow/array/array_base.h"
|
31 |
+
#include "arrow/array/data.h"
|
32 |
+
#include "arrow/buffer.h"
|
33 |
+
#include "arrow/stl_iterator.h"
|
34 |
+
#include "arrow/type.h"
|
35 |
+
#include "arrow/util/checked_cast.h"
|
36 |
+
#include "arrow/util/macros.h"
|
37 |
+
#include "arrow/util/visibility.h"
|
38 |
+
|
39 |
+
namespace arrow {
|
40 |
+
|
41 |
+
/// \addtogroup binary-arrays
|
42 |
+
///
|
43 |
+
/// @{
|
44 |
+
|
45 |
+
// ----------------------------------------------------------------------
|
46 |
+
// Binary and String
|
47 |
+
|
48 |
+
/// Base class for variable-sized binary arrays, regardless of offset size
|
49 |
+
/// and logical interpretation.
|
50 |
+
template <typename TYPE>
|
51 |
+
class BaseBinaryArray : public FlatArray {
|
52 |
+
public:
|
53 |
+
using TypeClass = TYPE;
|
54 |
+
using offset_type = typename TypeClass::offset_type;
|
55 |
+
using IteratorType = stl::ArrayIterator<BaseBinaryArray<TYPE>>;
|
56 |
+
|
57 |
+
/// Return the pointer to the given elements bytes
|
58 |
+
// XXX should GetValue(int64_t i) return a string_view?
|
59 |
+
const uint8_t* GetValue(int64_t i, offset_type* out_length) const {
|
60 |
+
// Account for base offset
|
61 |
+
i += data_->offset;
|
62 |
+
const offset_type pos = raw_value_offsets_[i];
|
63 |
+
*out_length = raw_value_offsets_[i + 1] - pos;
|
64 |
+
return raw_data_ + pos;
|
65 |
+
}
|
66 |
+
|
67 |
+
/// \brief Get binary value as a string_view
|
68 |
+
///
|
69 |
+
/// \param i the value index
|
70 |
+
/// \return the view over the selected value
|
71 |
+
std::string_view GetView(int64_t i) const {
|
72 |
+
// Account for base offset
|
73 |
+
i += data_->offset;
|
74 |
+
const offset_type pos = raw_value_offsets_[i];
|
75 |
+
return std::string_view(reinterpret_cast<const char*>(raw_data_ + pos),
|
76 |
+
raw_value_offsets_[i + 1] - pos);
|
77 |
+
}
|
78 |
+
|
79 |
+
std::optional<std::string_view> operator[](int64_t i) const {
|
80 |
+
return *IteratorType(*this, i);
|
81 |
+
}
|
82 |
+
|
83 |
+
/// \brief Get binary value as a string_view
|
84 |
+
/// Provided for consistency with other arrays.
|
85 |
+
///
|
86 |
+
/// \param i the value index
|
87 |
+
/// \return the view over the selected value
|
88 |
+
std::string_view Value(int64_t i) const { return GetView(i); }
|
89 |
+
|
90 |
+
/// \brief Get binary value as a std::string
|
91 |
+
///
|
92 |
+
/// \param i the value index
|
93 |
+
/// \return the value copied into a std::string
|
94 |
+
std::string GetString(int64_t i) const { return std::string(GetView(i)); }
|
95 |
+
|
96 |
+
/// Note that this buffer does not account for any slice offset
|
97 |
+
std::shared_ptr<Buffer> value_offsets() const { return data_->buffers[1]; }
|
98 |
+
|
99 |
+
/// Note that this buffer does not account for any slice offset
|
100 |
+
std::shared_ptr<Buffer> value_data() const { return data_->buffers[2]; }
|
101 |
+
|
102 |
+
const offset_type* raw_value_offsets() const {
|
103 |
+
return raw_value_offsets_ + data_->offset;
|
104 |
+
}
|
105 |
+
|
106 |
+
const uint8_t* raw_data() const { return raw_data_; }
|
107 |
+
|
108 |
+
/// \brief Return the data buffer absolute offset of the data for the value
|
109 |
+
/// at the passed index.
|
110 |
+
///
|
111 |
+
/// Does not perform boundschecking
|
112 |
+
offset_type value_offset(int64_t i) const {
|
113 |
+
return raw_value_offsets_[i + data_->offset];
|
114 |
+
}
|
115 |
+
|
116 |
+
/// \brief Return the length of the data for the value at the passed index.
|
117 |
+
///
|
118 |
+
/// Does not perform boundschecking
|
119 |
+
offset_type value_length(int64_t i) const {
|
120 |
+
i += data_->offset;
|
121 |
+
return raw_value_offsets_[i + 1] - raw_value_offsets_[i];
|
122 |
+
}
|
123 |
+
|
124 |
+
/// \brief Return the total length of the memory in the data buffer
|
125 |
+
/// referenced by this array. If the array has been sliced then this may be
|
126 |
+
/// less than the size of the data buffer (data_->buffers[2]).
|
127 |
+
offset_type total_values_length() const {
|
128 |
+
if (data_->length > 0) {
|
129 |
+
return raw_value_offsets_[data_->length + data_->offset] -
|
130 |
+
raw_value_offsets_[data_->offset];
|
131 |
+
} else {
|
132 |
+
return 0;
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
IteratorType begin() const { return IteratorType(*this); }
|
137 |
+
|
138 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
139 |
+
|
140 |
+
protected:
|
141 |
+
// For subclasses
|
142 |
+
BaseBinaryArray() = default;
|
143 |
+
|
144 |
+
// Protected method for constructors
|
145 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
146 |
+
this->Array::SetData(data);
|
147 |
+
raw_value_offsets_ = data->GetValuesSafe<offset_type>(1, /*offset=*/0);
|
148 |
+
raw_data_ = data->GetValuesSafe<uint8_t>(2, /*offset=*/0);
|
149 |
+
}
|
150 |
+
|
151 |
+
const offset_type* raw_value_offsets_ = NULLPTR;
|
152 |
+
const uint8_t* raw_data_ = NULLPTR;
|
153 |
+
};
|
154 |
+
|
155 |
+
/// Concrete Array class for variable-size binary data
|
156 |
+
class ARROW_EXPORT BinaryArray : public BaseBinaryArray<BinaryType> {
|
157 |
+
public:
|
158 |
+
explicit BinaryArray(const std::shared_ptr<ArrayData>& data);
|
159 |
+
|
160 |
+
BinaryArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
161 |
+
const std::shared_ptr<Buffer>& data,
|
162 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
163 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
164 |
+
|
165 |
+
protected:
|
166 |
+
// For subclasses such as StringArray
|
167 |
+
BinaryArray() : BaseBinaryArray() {}
|
168 |
+
};
|
169 |
+
|
170 |
+
/// Concrete Array class for variable-size string (utf-8) data
|
171 |
+
class ARROW_EXPORT StringArray : public BinaryArray {
|
172 |
+
public:
|
173 |
+
using TypeClass = StringType;
|
174 |
+
|
175 |
+
explicit StringArray(const std::shared_ptr<ArrayData>& data);
|
176 |
+
|
177 |
+
StringArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
178 |
+
const std::shared_ptr<Buffer>& data,
|
179 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
180 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
181 |
+
|
182 |
+
/// \brief Validate that this array contains only valid UTF8 entries
|
183 |
+
///
|
184 |
+
/// This check is also implied by ValidateFull()
|
185 |
+
Status ValidateUTF8() const;
|
186 |
+
};
|
187 |
+
|
188 |
+
/// Concrete Array class for large variable-size binary data
|
189 |
+
class ARROW_EXPORT LargeBinaryArray : public BaseBinaryArray<LargeBinaryType> {
|
190 |
+
public:
|
191 |
+
explicit LargeBinaryArray(const std::shared_ptr<ArrayData>& data);
|
192 |
+
|
193 |
+
LargeBinaryArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
194 |
+
const std::shared_ptr<Buffer>& data,
|
195 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
196 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
197 |
+
|
198 |
+
protected:
|
199 |
+
// For subclasses such as LargeStringArray
|
200 |
+
LargeBinaryArray() : BaseBinaryArray() {}
|
201 |
+
};
|
202 |
+
|
203 |
+
/// Concrete Array class for large variable-size string (utf-8) data
|
204 |
+
class ARROW_EXPORT LargeStringArray : public LargeBinaryArray {
|
205 |
+
public:
|
206 |
+
using TypeClass = LargeStringType;
|
207 |
+
|
208 |
+
explicit LargeStringArray(const std::shared_ptr<ArrayData>& data);
|
209 |
+
|
210 |
+
LargeStringArray(int64_t length, const std::shared_ptr<Buffer>& value_offsets,
|
211 |
+
const std::shared_ptr<Buffer>& data,
|
212 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
213 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
214 |
+
|
215 |
+
/// \brief Validate that this array contains only valid UTF8 entries
|
216 |
+
///
|
217 |
+
/// This check is also implied by ValidateFull()
|
218 |
+
Status ValidateUTF8() const;
|
219 |
+
};
|
220 |
+
|
221 |
+
// ----------------------------------------------------------------------
|
222 |
+
// BinaryView and StringView
|
223 |
+
|
224 |
+
/// Concrete Array class for variable-size binary view data using the
|
225 |
+
/// BinaryViewType::c_type struct to reference in-line or out-of-line string values
|
226 |
+
class ARROW_EXPORT BinaryViewArray : public FlatArray {
|
227 |
+
public:
|
228 |
+
using TypeClass = BinaryViewType;
|
229 |
+
using IteratorType = stl::ArrayIterator<BinaryViewArray>;
|
230 |
+
using c_type = BinaryViewType::c_type;
|
231 |
+
|
232 |
+
explicit BinaryViewArray(std::shared_ptr<ArrayData> data);
|
233 |
+
|
234 |
+
BinaryViewArray(std::shared_ptr<DataType> type, int64_t length,
|
235 |
+
std::shared_ptr<Buffer> views, BufferVector data_buffers,
|
236 |
+
std::shared_ptr<Buffer> null_bitmap = NULLPTR,
|
237 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
238 |
+
|
239 |
+
// For API compatibility with BinaryArray etc.
|
240 |
+
std::string_view GetView(int64_t i) const;
|
241 |
+
std::string GetString(int64_t i) const { return std::string{GetView(i)}; }
|
242 |
+
|
243 |
+
const auto& values() const { return data_->buffers[1]; }
|
244 |
+
const c_type* raw_values() const { return raw_values_; }
|
245 |
+
|
246 |
+
std::optional<std::string_view> operator[](int64_t i) const {
|
247 |
+
return *IteratorType(*this, i);
|
248 |
+
}
|
249 |
+
|
250 |
+
IteratorType begin() const { return IteratorType(*this); }
|
251 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
252 |
+
|
253 |
+
protected:
|
254 |
+
using FlatArray::FlatArray;
|
255 |
+
|
256 |
+
void SetData(std::shared_ptr<ArrayData> data) {
|
257 |
+
FlatArray::SetData(std::move(data));
|
258 |
+
raw_values_ = data_->GetValuesSafe<c_type>(1);
|
259 |
+
}
|
260 |
+
|
261 |
+
const c_type* raw_values_;
|
262 |
+
};
|
263 |
+
|
264 |
+
/// Concrete Array class for variable-size string view (utf-8) data using
|
265 |
+
/// BinaryViewType::c_type to reference in-line or out-of-line string values
|
266 |
+
class ARROW_EXPORT StringViewArray : public BinaryViewArray {
|
267 |
+
public:
|
268 |
+
using TypeClass = StringViewType;
|
269 |
+
|
270 |
+
explicit StringViewArray(std::shared_ptr<ArrayData> data);
|
271 |
+
|
272 |
+
using BinaryViewArray::BinaryViewArray;
|
273 |
+
|
274 |
+
/// \brief Validate that this array contains only valid UTF8 entries
|
275 |
+
///
|
276 |
+
/// This check is also implied by ValidateFull()
|
277 |
+
Status ValidateUTF8() const;
|
278 |
+
};
|
279 |
+
|
280 |
+
// ----------------------------------------------------------------------
|
281 |
+
// Fixed width binary
|
282 |
+
|
283 |
+
/// Concrete Array class for fixed-size binary data
|
284 |
+
class ARROW_EXPORT FixedSizeBinaryArray : public PrimitiveArray {
|
285 |
+
public:
|
286 |
+
using TypeClass = FixedSizeBinaryType;
|
287 |
+
using IteratorType = stl::ArrayIterator<FixedSizeBinaryArray>;
|
288 |
+
|
289 |
+
explicit FixedSizeBinaryArray(const std::shared_ptr<ArrayData>& data);
|
290 |
+
|
291 |
+
FixedSizeBinaryArray(const std::shared_ptr<DataType>& type, int64_t length,
|
292 |
+
const std::shared_ptr<Buffer>& data,
|
293 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
294 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
295 |
+
|
296 |
+
const uint8_t* GetValue(int64_t i) const;
|
297 |
+
const uint8_t* Value(int64_t i) const { return GetValue(i); }
|
298 |
+
|
299 |
+
std::string_view GetView(int64_t i) const {
|
300 |
+
return std::string_view(reinterpret_cast<const char*>(GetValue(i)), byte_width());
|
301 |
+
}
|
302 |
+
|
303 |
+
std::optional<std::string_view> operator[](int64_t i) const {
|
304 |
+
return *IteratorType(*this, i);
|
305 |
+
}
|
306 |
+
|
307 |
+
std::string GetString(int64_t i) const { return std::string(GetView(i)); }
|
308 |
+
|
309 |
+
int32_t byte_width() const { return byte_width_; }
|
310 |
+
|
311 |
+
const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width_; }
|
312 |
+
|
313 |
+
IteratorType begin() const { return IteratorType(*this); }
|
314 |
+
|
315 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
316 |
+
|
317 |
+
protected:
|
318 |
+
void SetData(const std::shared_ptr<ArrayData>& data) {
|
319 |
+
this->PrimitiveArray::SetData(data);
|
320 |
+
byte_width_ =
|
321 |
+
internal::checked_cast<const FixedSizeBinaryType&>(*type()).byte_width();
|
322 |
+
}
|
323 |
+
|
324 |
+
int32_t byte_width_;
|
325 |
+
};
|
326 |
+
|
327 |
+
/// @}
|
328 |
+
|
329 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_decimal.h
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <string>
|
23 |
+
|
24 |
+
#include "arrow/array/array_binary.h"
|
25 |
+
#include "arrow/array/data.h"
|
26 |
+
#include "arrow/type.h"
|
27 |
+
#include "arrow/util/visibility.h"
|
28 |
+
|
29 |
+
namespace arrow {
|
30 |
+
|
31 |
+
/// \addtogroup numeric-arrays
|
32 |
+
///
|
33 |
+
/// @{
|
34 |
+
|
35 |
+
// ----------------------------------------------------------------------
|
36 |
+
// Decimal128Array
|
37 |
+
|
38 |
+
/// Concrete Array class for 128-bit decimal data
|
39 |
+
class ARROW_EXPORT Decimal128Array : public FixedSizeBinaryArray {
|
40 |
+
public:
|
41 |
+
using TypeClass = Decimal128Type;
|
42 |
+
|
43 |
+
using FixedSizeBinaryArray::FixedSizeBinaryArray;
|
44 |
+
|
45 |
+
/// \brief Construct Decimal128Array from ArrayData instance
|
46 |
+
explicit Decimal128Array(const std::shared_ptr<ArrayData>& data);
|
47 |
+
|
48 |
+
std::string FormatValue(int64_t i) const;
|
49 |
+
};
|
50 |
+
|
51 |
+
// Backward compatibility
|
52 |
+
using DecimalArray = Decimal128Array;
|
53 |
+
|
54 |
+
// ----------------------------------------------------------------------
|
55 |
+
// Decimal256Array
|
56 |
+
|
57 |
+
/// Concrete Array class for 256-bit decimal data
|
58 |
+
class ARROW_EXPORT Decimal256Array : public FixedSizeBinaryArray {
|
59 |
+
public:
|
60 |
+
using TypeClass = Decimal256Type;
|
61 |
+
|
62 |
+
using FixedSizeBinaryArray::FixedSizeBinaryArray;
|
63 |
+
|
64 |
+
/// \brief Construct Decimal256Array from ArrayData instance
|
65 |
+
explicit Decimal256Array(const std::shared_ptr<ArrayData>& data);
|
66 |
+
|
67 |
+
std::string FormatValue(int64_t i) const;
|
68 |
+
};
|
69 |
+
|
70 |
+
/// @}
|
71 |
+
|
72 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_dict.h
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
|
23 |
+
#include "arrow/array/array_base.h"
|
24 |
+
#include "arrow/array/data.h"
|
25 |
+
#include "arrow/result.h"
|
26 |
+
#include "arrow/status.h"
|
27 |
+
#include "arrow/type.h"
|
28 |
+
#include "arrow/util/macros.h"
|
29 |
+
#include "arrow/util/visibility.h"
|
30 |
+
|
31 |
+
namespace arrow {
|
32 |
+
|
33 |
+
// ----------------------------------------------------------------------
|
34 |
+
// DictionaryArray
|
35 |
+
|
36 |
+
/// \brief Array type for dictionary-encoded data with a
|
37 |
+
/// data-dependent dictionary
|
38 |
+
///
|
39 |
+
/// A dictionary array contains an array of non-negative integers (the
|
40 |
+
/// "dictionary indices") along with a data type containing a "dictionary"
|
41 |
+
/// corresponding to the distinct values represented in the data.
|
42 |
+
///
|
43 |
+
/// For example, the array
|
44 |
+
///
|
45 |
+
/// ["foo", "bar", "foo", "bar", "foo", "bar"]
|
46 |
+
///
|
47 |
+
/// with dictionary ["bar", "foo"], would have dictionary array representation
|
48 |
+
///
|
49 |
+
/// indices: [1, 0, 1, 0, 1, 0]
|
50 |
+
/// dictionary: ["bar", "foo"]
|
51 |
+
///
|
52 |
+
/// The indices in principle may be any integer type.
|
53 |
+
class ARROW_EXPORT DictionaryArray : public Array {
|
54 |
+
public:
|
55 |
+
using TypeClass = DictionaryType;
|
56 |
+
|
57 |
+
explicit DictionaryArray(const std::shared_ptr<ArrayData>& data);
|
58 |
+
|
59 |
+
DictionaryArray(const std::shared_ptr<DataType>& type,
|
60 |
+
const std::shared_ptr<Array>& indices,
|
61 |
+
const std::shared_ptr<Array>& dictionary);
|
62 |
+
|
63 |
+
/// \brief Construct DictionaryArray from dictionary and indices
|
64 |
+
/// array and validate
|
65 |
+
///
|
66 |
+
/// This function does the validation of the indices and input type. It checks if
|
67 |
+
/// all indices are non-negative and smaller than the size of the dictionary.
|
68 |
+
///
|
69 |
+
/// \param[in] type a dictionary type
|
70 |
+
/// \param[in] dictionary the dictionary with same value type as the
|
71 |
+
/// type object
|
72 |
+
/// \param[in] indices an array of non-negative integers smaller than the
|
73 |
+
/// size of the dictionary
|
74 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
75 |
+
const std::shared_ptr<DataType>& type, const std::shared_ptr<Array>& indices,
|
76 |
+
const std::shared_ptr<Array>& dictionary);
|
77 |
+
|
78 |
+
static Result<std::shared_ptr<Array>> FromArrays(
|
79 |
+
const std::shared_ptr<Array>& indices, const std::shared_ptr<Array>& dictionary) {
|
80 |
+
return FromArrays(::arrow::dictionary(indices->type(), dictionary->type()), indices,
|
81 |
+
dictionary);
|
82 |
+
}
|
83 |
+
|
84 |
+
/// \brief Transpose this DictionaryArray
|
85 |
+
///
|
86 |
+
/// This method constructs a new dictionary array with the given dictionary
|
87 |
+
/// type, transposing indices using the transpose map. The type and the
|
88 |
+
/// transpose map are typically computed using DictionaryUnifier.
|
89 |
+
///
|
90 |
+
/// \param[in] type the new type object
|
91 |
+
/// \param[in] dictionary the new dictionary
|
92 |
+
/// \param[in] transpose_map transposition array of this array's indices
|
93 |
+
/// into the target array's indices
|
94 |
+
/// \param[in] pool a pool to allocate the array data from
|
95 |
+
Result<std::shared_ptr<Array>> Transpose(
|
96 |
+
const std::shared_ptr<DataType>& type, const std::shared_ptr<Array>& dictionary,
|
97 |
+
const int32_t* transpose_map, MemoryPool* pool = default_memory_pool()) const;
|
98 |
+
|
99 |
+
Result<std::shared_ptr<Array>> Compact(MemoryPool* pool = default_memory_pool()) const;
|
100 |
+
|
101 |
+
/// \brief Determine whether dictionary arrays may be compared without unification
|
102 |
+
bool CanCompareIndices(const DictionaryArray& other) const;
|
103 |
+
|
104 |
+
/// \brief Return the dictionary for this array, which is stored as
|
105 |
+
/// a member of the ArrayData internal structure
|
106 |
+
const std::shared_ptr<Array>& dictionary() const;
|
107 |
+
const std::shared_ptr<Array>& indices() const;
|
108 |
+
|
109 |
+
/// \brief Return the ith value of indices, cast to int64_t. Not recommended
|
110 |
+
/// for use in performance-sensitive code. Does not validate whether the
|
111 |
+
/// value is null or out-of-bounds.
|
112 |
+
int64_t GetValueIndex(int64_t i) const;
|
113 |
+
|
114 |
+
const DictionaryType* dict_type() const { return dict_type_; }
|
115 |
+
|
116 |
+
private:
|
117 |
+
void SetData(const std::shared_ptr<ArrayData>& data);
|
118 |
+
const DictionaryType* dict_type_;
|
119 |
+
std::shared_ptr<Array> indices_;
|
120 |
+
|
121 |
+
// Lazily initialized when invoking dictionary()
|
122 |
+
mutable std::shared_ptr<Array> dictionary_;
|
123 |
+
};
|
124 |
+
|
125 |
+
/// \brief Helper class for incremental dictionary unification
|
126 |
+
class ARROW_EXPORT DictionaryUnifier {
|
127 |
+
public:
|
128 |
+
virtual ~DictionaryUnifier() = default;
|
129 |
+
|
130 |
+
/// \brief Construct a DictionaryUnifier
|
131 |
+
/// \param[in] value_type the data type of the dictionaries
|
132 |
+
/// \param[in] pool MemoryPool to use for memory allocations
|
133 |
+
static Result<std::unique_ptr<DictionaryUnifier>> Make(
|
134 |
+
std::shared_ptr<DataType> value_type, MemoryPool* pool = default_memory_pool());
|
135 |
+
|
136 |
+
/// \brief Unify dictionaries across array chunks
|
137 |
+
///
|
138 |
+
/// The dictionaries in the array chunks will be unified, their indices
|
139 |
+
/// accordingly transposed.
|
140 |
+
///
|
141 |
+
/// Only dictionaries with a primitive value type are currently supported.
|
142 |
+
/// However, dictionaries nested inside a more complex type are correctly unified.
|
143 |
+
static Result<std::shared_ptr<ChunkedArray>> UnifyChunkedArray(
|
144 |
+
const std::shared_ptr<ChunkedArray>& array,
|
145 |
+
MemoryPool* pool = default_memory_pool());
|
146 |
+
|
147 |
+
/// \brief Unify dictionaries across the chunks of each table column
|
148 |
+
///
|
149 |
+
/// The dictionaries in each table column will be unified, their indices
|
150 |
+
/// accordingly transposed.
|
151 |
+
///
|
152 |
+
/// Only dictionaries with a primitive value type are currently supported.
|
153 |
+
/// However, dictionaries nested inside a more complex type are correctly unified.
|
154 |
+
static Result<std::shared_ptr<Table>> UnifyTable(
|
155 |
+
const Table& table, MemoryPool* pool = default_memory_pool());
|
156 |
+
|
157 |
+
/// \brief Append dictionary to the internal memo
|
158 |
+
virtual Status Unify(const Array& dictionary) = 0;
|
159 |
+
|
160 |
+
/// \brief Append dictionary and compute transpose indices
|
161 |
+
/// \param[in] dictionary the dictionary values to unify
|
162 |
+
/// \param[out] out_transpose a Buffer containing computed transpose indices
|
163 |
+
/// as int32_t values equal in length to the passed dictionary. The value in
|
164 |
+
/// each slot corresponds to the new index value for each original index
|
165 |
+
/// for a DictionaryArray with the old dictionary
|
166 |
+
virtual Status Unify(const Array& dictionary,
|
167 |
+
std::shared_ptr<Buffer>* out_transpose) = 0;
|
168 |
+
|
169 |
+
/// \brief Return a result DictionaryType with the smallest possible index
|
170 |
+
/// type to accommodate the unified dictionary. The unifier cannot be used
|
171 |
+
/// after this is called
|
172 |
+
virtual Status GetResult(std::shared_ptr<DataType>* out_type,
|
173 |
+
std::shared_ptr<Array>* out_dict) = 0;
|
174 |
+
|
175 |
+
/// \brief Return a unified dictionary with the given index type. If
|
176 |
+
/// the index type is not large enough then an invalid status will be returned.
|
177 |
+
/// The unifier cannot be used after this is called
|
178 |
+
virtual Status GetResultWithIndexType(const std::shared_ptr<DataType>& index_type,
|
179 |
+
std::shared_ptr<Array>* out_dict) = 0;
|
180 |
+
};
|
181 |
+
|
182 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/array_primitive.h
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// Array accessor types for primitive/C-type-based arrays, such as numbers,
|
19 |
+
// boolean, and temporal types.
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <cstdint>
|
24 |
+
#include <memory>
|
25 |
+
|
26 |
+
#include "arrow/array/array_base.h"
|
27 |
+
#include "arrow/array/data.h"
|
28 |
+
#include "arrow/stl_iterator.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/type_fwd.h" // IWYU pragma: export
|
31 |
+
#include "arrow/type_traits.h"
|
32 |
+
#include "arrow/util/bit_util.h"
|
33 |
+
#include "arrow/util/macros.h"
|
34 |
+
#include "arrow/util/visibility.h"
|
35 |
+
|
36 |
+
namespace arrow {
|
37 |
+
|
38 |
+
/// Concrete Array class for boolean data
|
39 |
+
class ARROW_EXPORT BooleanArray : public PrimitiveArray {
|
40 |
+
public:
|
41 |
+
using TypeClass = BooleanType;
|
42 |
+
using IteratorType = stl::ArrayIterator<BooleanArray>;
|
43 |
+
|
44 |
+
explicit BooleanArray(const std::shared_ptr<ArrayData>& data);
|
45 |
+
|
46 |
+
BooleanArray(int64_t length, const std::shared_ptr<Buffer>& data,
|
47 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
48 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
49 |
+
|
50 |
+
bool Value(int64_t i) const {
|
51 |
+
return bit_util::GetBit(reinterpret_cast<const uint8_t*>(raw_values_),
|
52 |
+
i + data_->offset);
|
53 |
+
}
|
54 |
+
|
55 |
+
bool GetView(int64_t i) const { return Value(i); }
|
56 |
+
|
57 |
+
std::optional<bool> operator[](int64_t i) const { return *IteratorType(*this, i); }
|
58 |
+
|
59 |
+
/// \brief Return the number of false (0) values among the valid
|
60 |
+
/// values. Result is not cached.
|
61 |
+
int64_t false_count() const;
|
62 |
+
|
63 |
+
/// \brief Return the number of true (1) values among the valid
|
64 |
+
/// values. Result is not cached.
|
65 |
+
int64_t true_count() const;
|
66 |
+
|
67 |
+
IteratorType begin() const { return IteratorType(*this); }
|
68 |
+
|
69 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
70 |
+
|
71 |
+
protected:
|
72 |
+
using PrimitiveArray::PrimitiveArray;
|
73 |
+
};
|
74 |
+
|
75 |
+
/// \addtogroup numeric-arrays
|
76 |
+
///
|
77 |
+
/// @{
|
78 |
+
|
79 |
+
/// \brief Concrete Array class for numeric data with a corresponding C type
|
80 |
+
///
|
81 |
+
/// This class is templated on the corresponding DataType subclass for the
|
82 |
+
/// given data, for example NumericArray<Int8Type> or NumericArray<Date32Type>.
|
83 |
+
///
|
84 |
+
/// Note that convenience aliases are available for all accepted types
|
85 |
+
/// (for example Int8Array for NumericArray<Int8Type>).
|
86 |
+
template <typename TYPE>
|
87 |
+
class NumericArray : public PrimitiveArray {
|
88 |
+
public:
|
89 |
+
using TypeClass = TYPE;
|
90 |
+
using value_type = typename TypeClass::c_type;
|
91 |
+
using IteratorType = stl::ArrayIterator<NumericArray<TYPE>>;
|
92 |
+
|
93 |
+
explicit NumericArray(const std::shared_ptr<ArrayData>& data) : PrimitiveArray(data) {}
|
94 |
+
|
95 |
+
// Only enable this constructor without a type argument for types without additional
|
96 |
+
// metadata
|
97 |
+
template <typename T1 = TYPE>
|
98 |
+
NumericArray(enable_if_parameter_free<T1, int64_t> length,
|
99 |
+
const std::shared_ptr<Buffer>& data,
|
100 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
101 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
102 |
+
: PrimitiveArray(TypeTraits<T1>::type_singleton(), length, data, null_bitmap,
|
103 |
+
null_count, offset) {}
|
104 |
+
|
105 |
+
const value_type* raw_values() const {
|
106 |
+
return reinterpret_cast<const value_type*>(raw_values_) + data_->offset;
|
107 |
+
}
|
108 |
+
|
109 |
+
value_type Value(int64_t i) const { return raw_values()[i]; }
|
110 |
+
|
111 |
+
// For API compatibility with BinaryArray etc.
|
112 |
+
value_type GetView(int64_t i) const { return Value(i); }
|
113 |
+
|
114 |
+
std::optional<value_type> operator[](int64_t i) const {
|
115 |
+
return *IteratorType(*this, i);
|
116 |
+
}
|
117 |
+
|
118 |
+
IteratorType begin() const { return IteratorType(*this); }
|
119 |
+
|
120 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
121 |
+
|
122 |
+
protected:
|
123 |
+
using PrimitiveArray::PrimitiveArray;
|
124 |
+
};
|
125 |
+
|
126 |
+
/// DayTimeArray
|
127 |
+
/// ---------------------
|
128 |
+
/// \brief Array of Day and Millisecond values.
|
129 |
+
class ARROW_EXPORT DayTimeIntervalArray : public PrimitiveArray {
|
130 |
+
public:
|
131 |
+
using TypeClass = DayTimeIntervalType;
|
132 |
+
using IteratorType = stl::ArrayIterator<DayTimeIntervalArray>;
|
133 |
+
|
134 |
+
explicit DayTimeIntervalArray(const std::shared_ptr<ArrayData>& data);
|
135 |
+
|
136 |
+
DayTimeIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
|
137 |
+
const std::shared_ptr<Buffer>& data,
|
138 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
139 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
140 |
+
|
141 |
+
DayTimeIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
|
142 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
143 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
144 |
+
|
145 |
+
TypeClass::DayMilliseconds GetValue(int64_t i) const;
|
146 |
+
TypeClass::DayMilliseconds Value(int64_t i) const { return GetValue(i); }
|
147 |
+
|
148 |
+
// For compatibility with Take kernel.
|
149 |
+
TypeClass::DayMilliseconds GetView(int64_t i) const { return GetValue(i); }
|
150 |
+
|
151 |
+
IteratorType begin() const { return IteratorType(*this); }
|
152 |
+
|
153 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
154 |
+
|
155 |
+
std::optional<TypeClass::DayMilliseconds> operator[](int64_t i) const {
|
156 |
+
return *IteratorType(*this, i);
|
157 |
+
}
|
158 |
+
|
159 |
+
int32_t byte_width() const { return sizeof(TypeClass::DayMilliseconds); }
|
160 |
+
|
161 |
+
const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
|
162 |
+
};
|
163 |
+
|
164 |
+
/// \brief Array of Month, Day and nanosecond values.
|
165 |
+
class ARROW_EXPORT MonthDayNanoIntervalArray : public PrimitiveArray {
|
166 |
+
public:
|
167 |
+
using TypeClass = MonthDayNanoIntervalType;
|
168 |
+
using IteratorType = stl::ArrayIterator<MonthDayNanoIntervalArray>;
|
169 |
+
|
170 |
+
explicit MonthDayNanoIntervalArray(const std::shared_ptr<ArrayData>& data);
|
171 |
+
|
172 |
+
MonthDayNanoIntervalArray(const std::shared_ptr<DataType>& type, int64_t length,
|
173 |
+
const std::shared_ptr<Buffer>& data,
|
174 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
175 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
176 |
+
|
177 |
+
MonthDayNanoIntervalArray(int64_t length, const std::shared_ptr<Buffer>& data,
|
178 |
+
const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
|
179 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
180 |
+
|
181 |
+
TypeClass::MonthDayNanos GetValue(int64_t i) const;
|
182 |
+
TypeClass::MonthDayNanos Value(int64_t i) const { return GetValue(i); }
|
183 |
+
|
184 |
+
// For compatibility with Take kernel.
|
185 |
+
TypeClass::MonthDayNanos GetView(int64_t i) const { return GetValue(i); }
|
186 |
+
|
187 |
+
IteratorType begin() const { return IteratorType(*this); }
|
188 |
+
|
189 |
+
IteratorType end() const { return IteratorType(*this, length()); }
|
190 |
+
|
191 |
+
std::optional<TypeClass::MonthDayNanos> operator[](int64_t i) const {
|
192 |
+
return *IteratorType(*this, i);
|
193 |
+
}
|
194 |
+
|
195 |
+
int32_t byte_width() const { return sizeof(TypeClass::MonthDayNanos); }
|
196 |
+
|
197 |
+
const uint8_t* raw_values() const { return raw_values_ + data_->offset * byte_width(); }
|
198 |
+
};
|
199 |
+
|
200 |
+
/// @}
|
201 |
+
|
202 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_adaptive.h
ADDED
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <cstring>
|
22 |
+
#include <memory>
|
23 |
+
#include <type_traits>
|
24 |
+
|
25 |
+
#include "arrow/array/builder_base.h"
|
26 |
+
#include "arrow/buffer.h"
|
27 |
+
#include "arrow/status.h"
|
28 |
+
#include "arrow/type.h"
|
29 |
+
#include "arrow/util/macros.h"
|
30 |
+
#include "arrow/util/visibility.h"
|
31 |
+
|
32 |
+
namespace arrow {
|
33 |
+
|
34 |
+
/// \addtogroup numeric-builders
|
35 |
+
///
|
36 |
+
/// @{
|
37 |
+
|
38 |
+
namespace internal {
|
39 |
+
|
40 |
+
class ARROW_EXPORT AdaptiveIntBuilderBase : public ArrayBuilder {
|
41 |
+
public:
|
42 |
+
AdaptiveIntBuilderBase(uint8_t start_int_size, MemoryPool* pool,
|
43 |
+
int64_t alignment = kDefaultBufferAlignment);
|
44 |
+
|
45 |
+
explicit AdaptiveIntBuilderBase(MemoryPool* pool,
|
46 |
+
int64_t alignment = kDefaultBufferAlignment)
|
47 |
+
: AdaptiveIntBuilderBase(sizeof(uint8_t), pool, alignment) {}
|
48 |
+
|
49 |
+
/// \brief Append multiple nulls
|
50 |
+
/// \param[in] length the number of nulls to append
|
51 |
+
Status AppendNulls(int64_t length) final {
|
52 |
+
ARROW_RETURN_NOT_OK(CommitPendingData());
|
53 |
+
if (ARROW_PREDICT_TRUE(length > 0)) {
|
54 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
55 |
+
memset(data_->mutable_data() + length_ * int_size_, 0, int_size_ * length);
|
56 |
+
UnsafeSetNull(length);
|
57 |
+
}
|
58 |
+
return Status::OK();
|
59 |
+
}
|
60 |
+
|
61 |
+
Status AppendNull() final {
|
62 |
+
pending_data_[pending_pos_] = 0;
|
63 |
+
pending_valid_[pending_pos_] = 0;
|
64 |
+
pending_has_nulls_ = true;
|
65 |
+
++pending_pos_;
|
66 |
+
++length_;
|
67 |
+
++null_count_;
|
68 |
+
|
69 |
+
if (ARROW_PREDICT_FALSE(pending_pos_ >= pending_size_)) {
|
70 |
+
return CommitPendingData();
|
71 |
+
}
|
72 |
+
return Status::OK();
|
73 |
+
}
|
74 |
+
|
75 |
+
Status AppendEmptyValues(int64_t length) final {
|
76 |
+
ARROW_RETURN_NOT_OK(CommitPendingData());
|
77 |
+
if (ARROW_PREDICT_TRUE(length > 0)) {
|
78 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
79 |
+
memset(data_->mutable_data() + length_ * int_size_, 0, int_size_ * length);
|
80 |
+
UnsafeSetNotNull(length);
|
81 |
+
}
|
82 |
+
return Status::OK();
|
83 |
+
}
|
84 |
+
|
85 |
+
Status AppendEmptyValue() final {
|
86 |
+
pending_data_[pending_pos_] = 0;
|
87 |
+
pending_valid_[pending_pos_] = 1;
|
88 |
+
++pending_pos_;
|
89 |
+
++length_;
|
90 |
+
|
91 |
+
if (ARROW_PREDICT_FALSE(pending_pos_ >= pending_size_)) {
|
92 |
+
return CommitPendingData();
|
93 |
+
}
|
94 |
+
return Status::OK();
|
95 |
+
}
|
96 |
+
|
97 |
+
void Reset() override;
|
98 |
+
Status Resize(int64_t capacity) override;
|
99 |
+
|
100 |
+
protected:
|
101 |
+
Status AppendInternal(const uint64_t val) {
|
102 |
+
pending_data_[pending_pos_] = val;
|
103 |
+
pending_valid_[pending_pos_] = 1;
|
104 |
+
++pending_pos_;
|
105 |
+
++length_;
|
106 |
+
|
107 |
+
if (ARROW_PREDICT_FALSE(pending_pos_ >= pending_size_)) {
|
108 |
+
return CommitPendingData();
|
109 |
+
}
|
110 |
+
return Status::OK();
|
111 |
+
}
|
112 |
+
|
113 |
+
virtual Status CommitPendingData() = 0;
|
114 |
+
|
115 |
+
template <typename new_type, typename old_type>
|
116 |
+
typename std::enable_if<sizeof(old_type) >= sizeof(new_type), Status>::type
|
117 |
+
ExpandIntSizeInternal();
|
118 |
+
template <typename new_type, typename old_type>
|
119 |
+
typename std::enable_if<(sizeof(old_type) < sizeof(new_type)), Status>::type
|
120 |
+
ExpandIntSizeInternal();
|
121 |
+
|
122 |
+
std::shared_ptr<ResizableBuffer> data_;
|
123 |
+
uint8_t* raw_data_ = NULLPTR;
|
124 |
+
|
125 |
+
const uint8_t start_int_size_;
|
126 |
+
uint8_t int_size_;
|
127 |
+
|
128 |
+
static constexpr int32_t pending_size_ = 1024;
|
129 |
+
uint8_t pending_valid_[pending_size_];
|
130 |
+
uint64_t pending_data_[pending_size_];
|
131 |
+
int32_t pending_pos_ = 0;
|
132 |
+
bool pending_has_nulls_ = false;
|
133 |
+
};
|
134 |
+
|
135 |
+
} // namespace internal
|
136 |
+
|
137 |
+
class ARROW_EXPORT AdaptiveUIntBuilder : public internal::AdaptiveIntBuilderBase {
|
138 |
+
public:
|
139 |
+
explicit AdaptiveUIntBuilder(uint8_t start_int_size,
|
140 |
+
MemoryPool* pool = default_memory_pool());
|
141 |
+
|
142 |
+
explicit AdaptiveUIntBuilder(MemoryPool* pool = default_memory_pool())
|
143 |
+
: AdaptiveUIntBuilder(sizeof(uint8_t), pool) {}
|
144 |
+
|
145 |
+
using internal::AdaptiveIntBuilderBase::Reset;
|
146 |
+
|
147 |
+
/// Scalar append
|
148 |
+
Status Append(const uint64_t val) { return AppendInternal(val); }
|
149 |
+
|
150 |
+
/// \brief Append a sequence of elements in one shot
|
151 |
+
/// \param[in] values a contiguous C array of values
|
152 |
+
/// \param[in] length the number of values to append
|
153 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
154 |
+
/// indicates a valid (non-null) value
|
155 |
+
/// \return Status
|
156 |
+
Status AppendValues(const uint64_t* values, int64_t length,
|
157 |
+
const uint8_t* valid_bytes = NULLPTR);
|
158 |
+
|
159 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
160 |
+
|
161 |
+
std::shared_ptr<DataType> type() const override;
|
162 |
+
|
163 |
+
protected:
|
164 |
+
Status CommitPendingData() override;
|
165 |
+
Status ExpandIntSize(uint8_t new_int_size);
|
166 |
+
|
167 |
+
Status AppendValuesInternal(const uint64_t* values, int64_t length,
|
168 |
+
const uint8_t* valid_bytes);
|
169 |
+
|
170 |
+
template <typename new_type>
|
171 |
+
Status ExpandIntSizeN();
|
172 |
+
};
|
173 |
+
|
174 |
+
class ARROW_EXPORT AdaptiveIntBuilder : public internal::AdaptiveIntBuilderBase {
|
175 |
+
public:
|
176 |
+
explicit AdaptiveIntBuilder(uint8_t start_int_size,
|
177 |
+
MemoryPool* pool = default_memory_pool(),
|
178 |
+
int64_t alignment = kDefaultBufferAlignment);
|
179 |
+
|
180 |
+
explicit AdaptiveIntBuilder(MemoryPool* pool = default_memory_pool(),
|
181 |
+
int64_t alignment = kDefaultBufferAlignment)
|
182 |
+
: AdaptiveIntBuilder(sizeof(uint8_t), pool, alignment) {}
|
183 |
+
|
184 |
+
using internal::AdaptiveIntBuilderBase::Reset;
|
185 |
+
|
186 |
+
/// Scalar append
|
187 |
+
Status Append(const int64_t val) { return AppendInternal(static_cast<uint64_t>(val)); }
|
188 |
+
|
189 |
+
/// \brief Append a sequence of elements in one shot
|
190 |
+
/// \param[in] values a contiguous C array of values
|
191 |
+
/// \param[in] length the number of values to append
|
192 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
193 |
+
/// indicates a valid (non-null) value
|
194 |
+
/// \return Status
|
195 |
+
Status AppendValues(const int64_t* values, int64_t length,
|
196 |
+
const uint8_t* valid_bytes = NULLPTR);
|
197 |
+
|
198 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
199 |
+
|
200 |
+
std::shared_ptr<DataType> type() const override;
|
201 |
+
|
202 |
+
protected:
|
203 |
+
Status CommitPendingData() override;
|
204 |
+
Status ExpandIntSize(uint8_t new_int_size);
|
205 |
+
|
206 |
+
Status AppendValuesInternal(const int64_t* values, int64_t length,
|
207 |
+
const uint8_t* valid_bytes);
|
208 |
+
|
209 |
+
template <typename new_type>
|
210 |
+
Status ExpandIntSizeN();
|
211 |
+
};
|
212 |
+
|
213 |
+
/// @}
|
214 |
+
|
215 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_binary.h
ADDED
@@ -0,0 +1,971 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <array>
|
21 |
+
#include <cstddef>
|
22 |
+
#include <cstdint>
|
23 |
+
#include <cstring>
|
24 |
+
#include <limits>
|
25 |
+
#include <memory>
|
26 |
+
#include <numeric>
|
27 |
+
#include <string>
|
28 |
+
#include <string_view>
|
29 |
+
#include <vector>
|
30 |
+
|
31 |
+
#include "arrow/array/array_base.h"
|
32 |
+
#include "arrow/array/array_binary.h"
|
33 |
+
#include "arrow/array/builder_base.h"
|
34 |
+
#include "arrow/array/data.h"
|
35 |
+
#include "arrow/buffer.h"
|
36 |
+
#include "arrow/buffer_builder.h"
|
37 |
+
#include "arrow/status.h"
|
38 |
+
#include "arrow/type.h"
|
39 |
+
#include "arrow/util/binary_view_util.h"
|
40 |
+
#include "arrow/util/macros.h"
|
41 |
+
#include "arrow/util/visibility.h"
|
42 |
+
|
43 |
+
namespace arrow {
|
44 |
+
|
45 |
+
/// \addtogroup binary-builders
|
46 |
+
///
|
47 |
+
/// @{
|
48 |
+
|
49 |
+
// ----------------------------------------------------------------------
|
50 |
+
// Binary and String
|
51 |
+
|
52 |
+
template <typename TYPE>
|
53 |
+
class BaseBinaryBuilder
|
54 |
+
: public ArrayBuilder,
|
55 |
+
public internal::ArrayBuilderExtraOps<BaseBinaryBuilder<TYPE>, std::string_view> {
|
56 |
+
public:
|
57 |
+
using TypeClass = TYPE;
|
58 |
+
using offset_type = typename TypeClass::offset_type;
|
59 |
+
|
60 |
+
explicit BaseBinaryBuilder(MemoryPool* pool = default_memory_pool(),
|
61 |
+
int64_t alignment = kDefaultBufferAlignment)
|
62 |
+
: ArrayBuilder(pool, alignment),
|
63 |
+
offsets_builder_(pool, alignment),
|
64 |
+
value_data_builder_(pool, alignment) {}
|
65 |
+
|
66 |
+
BaseBinaryBuilder(const std::shared_ptr<DataType>& type, MemoryPool* pool)
|
67 |
+
: BaseBinaryBuilder(pool) {}
|
68 |
+
|
69 |
+
Status Append(const uint8_t* value, offset_type length) {
|
70 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
71 |
+
UnsafeAppendNextOffset();
|
72 |
+
// Safety check for UBSAN.
|
73 |
+
if (ARROW_PREDICT_TRUE(length > 0)) {
|
74 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(length));
|
75 |
+
ARROW_RETURN_NOT_OK(value_data_builder_.Append(value, length));
|
76 |
+
}
|
77 |
+
|
78 |
+
UnsafeAppendToBitmap(true);
|
79 |
+
return Status::OK();
|
80 |
+
}
|
81 |
+
|
82 |
+
Status Append(const char* value, offset_type length) {
|
83 |
+
return Append(reinterpret_cast<const uint8_t*>(value), length);
|
84 |
+
}
|
85 |
+
|
86 |
+
Status Append(std::string_view value) {
|
87 |
+
return Append(value.data(), static_cast<offset_type>(value.size()));
|
88 |
+
}
|
89 |
+
|
90 |
+
/// Extend the last appended value by appending more data at the end
|
91 |
+
///
|
92 |
+
/// Unlike Append, this does not create a new offset.
|
93 |
+
Status ExtendCurrent(const uint8_t* value, offset_type length) {
|
94 |
+
// Safety check for UBSAN.
|
95 |
+
if (ARROW_PREDICT_TRUE(length > 0)) {
|
96 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(length));
|
97 |
+
ARROW_RETURN_NOT_OK(value_data_builder_.Append(value, length));
|
98 |
+
}
|
99 |
+
return Status::OK();
|
100 |
+
}
|
101 |
+
|
102 |
+
Status ExtendCurrent(std::string_view value) {
|
103 |
+
return ExtendCurrent(reinterpret_cast<const uint8_t*>(value.data()),
|
104 |
+
static_cast<offset_type>(value.size()));
|
105 |
+
}
|
106 |
+
|
107 |
+
Status AppendNulls(int64_t length) final {
|
108 |
+
const int64_t num_bytes = value_data_builder_.length();
|
109 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
110 |
+
for (int64_t i = 0; i < length; ++i) {
|
111 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
112 |
+
}
|
113 |
+
UnsafeAppendToBitmap(length, false);
|
114 |
+
return Status::OK();
|
115 |
+
}
|
116 |
+
|
117 |
+
Status AppendNull() final {
|
118 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
119 |
+
UnsafeAppendNextOffset();
|
120 |
+
UnsafeAppendToBitmap(false);
|
121 |
+
return Status::OK();
|
122 |
+
}
|
123 |
+
|
124 |
+
Status AppendEmptyValue() final {
|
125 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
126 |
+
UnsafeAppendNextOffset();
|
127 |
+
UnsafeAppendToBitmap(true);
|
128 |
+
return Status::OK();
|
129 |
+
}
|
130 |
+
|
131 |
+
Status AppendEmptyValues(int64_t length) final {
|
132 |
+
const int64_t num_bytes = value_data_builder_.length();
|
133 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
134 |
+
for (int64_t i = 0; i < length; ++i) {
|
135 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
136 |
+
}
|
137 |
+
UnsafeAppendToBitmap(length, true);
|
138 |
+
return Status::OK();
|
139 |
+
}
|
140 |
+
|
141 |
+
/// \brief Append without checking capacity
|
142 |
+
///
|
143 |
+
/// Offsets and data should have been presized using Reserve() and
|
144 |
+
/// ReserveData(), respectively.
|
145 |
+
void UnsafeAppend(const uint8_t* value, offset_type length) {
|
146 |
+
UnsafeAppendNextOffset();
|
147 |
+
value_data_builder_.UnsafeAppend(value, length);
|
148 |
+
UnsafeAppendToBitmap(true);
|
149 |
+
}
|
150 |
+
|
151 |
+
void UnsafeAppend(const char* value, offset_type length) {
|
152 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value), length);
|
153 |
+
}
|
154 |
+
|
155 |
+
void UnsafeAppend(const std::string& value) {
|
156 |
+
UnsafeAppend(value.c_str(), static_cast<offset_type>(value.size()));
|
157 |
+
}
|
158 |
+
|
159 |
+
void UnsafeAppend(std::string_view value) {
|
160 |
+
UnsafeAppend(value.data(), static_cast<offset_type>(value.size()));
|
161 |
+
}
|
162 |
+
|
163 |
+
/// Like ExtendCurrent, but do not check capacity
|
164 |
+
void UnsafeExtendCurrent(const uint8_t* value, offset_type length) {
|
165 |
+
value_data_builder_.UnsafeAppend(value, length);
|
166 |
+
}
|
167 |
+
|
168 |
+
void UnsafeExtendCurrent(std::string_view value) {
|
169 |
+
UnsafeExtendCurrent(reinterpret_cast<const uint8_t*>(value.data()),
|
170 |
+
static_cast<offset_type>(value.size()));
|
171 |
+
}
|
172 |
+
|
173 |
+
void UnsafeAppendNull() {
|
174 |
+
const int64_t num_bytes = value_data_builder_.length();
|
175 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
176 |
+
UnsafeAppendToBitmap(false);
|
177 |
+
}
|
178 |
+
|
179 |
+
void UnsafeAppendEmptyValue() {
|
180 |
+
const int64_t num_bytes = value_data_builder_.length();
|
181 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
182 |
+
UnsafeAppendToBitmap(true);
|
183 |
+
}
|
184 |
+
|
185 |
+
/// \brief Append a sequence of strings in one shot.
|
186 |
+
///
|
187 |
+
/// \param[in] values a vector of strings
|
188 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
189 |
+
/// indicates a valid (non-null) value
|
190 |
+
/// \return Status
|
191 |
+
Status AppendValues(const std::vector<std::string>& values,
|
192 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
193 |
+
std::size_t total_length = std::accumulate(
|
194 |
+
values.begin(), values.end(), 0ULL,
|
195 |
+
[](uint64_t sum, const std::string& str) { return sum + str.size(); });
|
196 |
+
ARROW_RETURN_NOT_OK(Reserve(values.size()));
|
197 |
+
ARROW_RETURN_NOT_OK(ReserveData(total_length));
|
198 |
+
|
199 |
+
if (valid_bytes != NULLPTR) {
|
200 |
+
for (std::size_t i = 0; i < values.size(); ++i) {
|
201 |
+
UnsafeAppendNextOffset();
|
202 |
+
if (valid_bytes[i]) {
|
203 |
+
value_data_builder_.UnsafeAppend(
|
204 |
+
reinterpret_cast<const uint8_t*>(values[i].data()), values[i].size());
|
205 |
+
}
|
206 |
+
}
|
207 |
+
} else {
|
208 |
+
for (const auto& value : values) {
|
209 |
+
UnsafeAppendNextOffset();
|
210 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(value.data()),
|
211 |
+
value.size());
|
212 |
+
}
|
213 |
+
}
|
214 |
+
|
215 |
+
UnsafeAppendToBitmap(valid_bytes, values.size());
|
216 |
+
return Status::OK();
|
217 |
+
}
|
218 |
+
|
219 |
+
/// \brief Append a sequence of nul-terminated strings in one shot.
|
220 |
+
/// If one of the values is NULL, it is processed as a null
|
221 |
+
/// value even if the corresponding valid_bytes entry is 1.
|
222 |
+
///
|
223 |
+
/// \param[in] values a contiguous C array of nul-terminated char *
|
224 |
+
/// \param[in] length the number of values to append
|
225 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
226 |
+
/// indicates a valid (non-null) value
|
227 |
+
/// \return Status
|
228 |
+
Status AppendValues(const char** values, int64_t length,
|
229 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
230 |
+
std::size_t total_length = 0;
|
231 |
+
std::vector<std::size_t> value_lengths(length);
|
232 |
+
bool have_null_value = false;
|
233 |
+
for (int64_t i = 0; i < length; ++i) {
|
234 |
+
if (values[i] != NULLPTR) {
|
235 |
+
auto value_length = strlen(values[i]);
|
236 |
+
value_lengths[i] = value_length;
|
237 |
+
total_length += value_length;
|
238 |
+
} else {
|
239 |
+
have_null_value = true;
|
240 |
+
}
|
241 |
+
}
|
242 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
243 |
+
ARROW_RETURN_NOT_OK(ReserveData(total_length));
|
244 |
+
|
245 |
+
if (valid_bytes) {
|
246 |
+
int64_t valid_bytes_offset = 0;
|
247 |
+
for (int64_t i = 0; i < length; ++i) {
|
248 |
+
UnsafeAppendNextOffset();
|
249 |
+
if (valid_bytes[i]) {
|
250 |
+
if (values[i]) {
|
251 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(values[i]),
|
252 |
+
value_lengths[i]);
|
253 |
+
} else {
|
254 |
+
UnsafeAppendToBitmap(valid_bytes + valid_bytes_offset,
|
255 |
+
i - valid_bytes_offset);
|
256 |
+
UnsafeAppendToBitmap(false);
|
257 |
+
valid_bytes_offset = i + 1;
|
258 |
+
}
|
259 |
+
}
|
260 |
+
}
|
261 |
+
UnsafeAppendToBitmap(valid_bytes + valid_bytes_offset, length - valid_bytes_offset);
|
262 |
+
} else {
|
263 |
+
if (have_null_value) {
|
264 |
+
std::vector<uint8_t> valid_vector(length, 0);
|
265 |
+
for (int64_t i = 0; i < length; ++i) {
|
266 |
+
UnsafeAppendNextOffset();
|
267 |
+
if (values[i]) {
|
268 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(values[i]),
|
269 |
+
value_lengths[i]);
|
270 |
+
valid_vector[i] = 1;
|
271 |
+
}
|
272 |
+
}
|
273 |
+
UnsafeAppendToBitmap(valid_vector.data(), length);
|
274 |
+
} else {
|
275 |
+
for (int64_t i = 0; i < length; ++i) {
|
276 |
+
UnsafeAppendNextOffset();
|
277 |
+
value_data_builder_.UnsafeAppend(reinterpret_cast<const uint8_t*>(values[i]),
|
278 |
+
value_lengths[i]);
|
279 |
+
}
|
280 |
+
UnsafeAppendToBitmap(NULLPTR, length);
|
281 |
+
}
|
282 |
+
}
|
283 |
+
return Status::OK();
|
284 |
+
}
|
285 |
+
|
286 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
287 |
+
int64_t length) override {
|
288 |
+
auto bitmap = array.GetValues<uint8_t>(0, 0);
|
289 |
+
auto offsets = array.GetValues<offset_type>(1);
|
290 |
+
auto data = array.GetValues<uint8_t>(2, 0);
|
291 |
+
auto total_length = offsets[offset + length] - offsets[offset];
|
292 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
293 |
+
ARROW_RETURN_NOT_OK(ReserveData(total_length));
|
294 |
+
for (int64_t i = 0; i < length; i++) {
|
295 |
+
if (!bitmap || bit_util::GetBit(bitmap, array.offset + offset + i)) {
|
296 |
+
const offset_type start = offsets[offset + i];
|
297 |
+
const offset_type end = offsets[offset + i + 1];
|
298 |
+
UnsafeAppend(data + start, end - start);
|
299 |
+
} else {
|
300 |
+
UnsafeAppendNull();
|
301 |
+
}
|
302 |
+
}
|
303 |
+
return Status::OK();
|
304 |
+
}
|
305 |
+
|
306 |
+
void Reset() override {
|
307 |
+
ArrayBuilder::Reset();
|
308 |
+
offsets_builder_.Reset();
|
309 |
+
value_data_builder_.Reset();
|
310 |
+
}
|
311 |
+
|
312 |
+
Status ValidateOverflow(int64_t new_bytes) {
|
313 |
+
auto new_size = value_data_builder_.length() + new_bytes;
|
314 |
+
if (ARROW_PREDICT_FALSE(new_size > memory_limit())) {
|
315 |
+
return Status::CapacityError("array cannot contain more than ", memory_limit(),
|
316 |
+
" bytes, have ", new_size);
|
317 |
+
} else {
|
318 |
+
return Status::OK();
|
319 |
+
}
|
320 |
+
}
|
321 |
+
|
322 |
+
Status Resize(int64_t capacity) override {
|
323 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
324 |
+
// One more than requested for offsets
|
325 |
+
ARROW_RETURN_NOT_OK(offsets_builder_.Resize(capacity + 1));
|
326 |
+
return ArrayBuilder::Resize(capacity);
|
327 |
+
}
|
328 |
+
|
329 |
+
/// \brief Ensures there is enough allocated capacity to append the indicated
|
330 |
+
/// number of bytes to the value data buffer without additional allocations
|
331 |
+
Status ReserveData(int64_t elements) {
|
332 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(elements));
|
333 |
+
return value_data_builder_.Reserve(elements);
|
334 |
+
}
|
335 |
+
|
336 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
|
337 |
+
// Write final offset (values length)
|
338 |
+
ARROW_RETURN_NOT_OK(AppendNextOffset());
|
339 |
+
|
340 |
+
// These buffers' padding zeroed by BufferBuilder
|
341 |
+
std::shared_ptr<Buffer> offsets, value_data, null_bitmap;
|
342 |
+
ARROW_RETURN_NOT_OK(offsets_builder_.Finish(&offsets));
|
343 |
+
ARROW_RETURN_NOT_OK(value_data_builder_.Finish(&value_data));
|
344 |
+
ARROW_RETURN_NOT_OK(null_bitmap_builder_.Finish(&null_bitmap));
|
345 |
+
|
346 |
+
*out = ArrayData::Make(type(), length_, {null_bitmap, offsets, value_data},
|
347 |
+
null_count_, 0);
|
348 |
+
Reset();
|
349 |
+
return Status::OK();
|
350 |
+
}
|
351 |
+
|
352 |
+
/// \return data pointer of the value date builder
|
353 |
+
const uint8_t* value_data() const { return value_data_builder_.data(); }
|
354 |
+
/// \return size of values buffer so far
|
355 |
+
int64_t value_data_length() const { return value_data_builder_.length(); }
|
356 |
+
/// \return capacity of values buffer
|
357 |
+
int64_t value_data_capacity() const { return value_data_builder_.capacity(); }
|
358 |
+
|
359 |
+
/// \return data pointer of the value date builder
|
360 |
+
const offset_type* offsets_data() const { return offsets_builder_.data(); }
|
361 |
+
|
362 |
+
/// Temporary access to a value.
|
363 |
+
///
|
364 |
+
/// This pointer becomes invalid on the next modifying operation.
|
365 |
+
const uint8_t* GetValue(int64_t i, offset_type* out_length) const {
|
366 |
+
const offset_type* offsets = offsets_builder_.data();
|
367 |
+
const auto offset = offsets[i];
|
368 |
+
if (i == (length_ - 1)) {
|
369 |
+
*out_length = static_cast<offset_type>(value_data_builder_.length()) - offset;
|
370 |
+
} else {
|
371 |
+
*out_length = offsets[i + 1] - offset;
|
372 |
+
}
|
373 |
+
return value_data_builder_.data() + offset;
|
374 |
+
}
|
375 |
+
|
376 |
+
offset_type offset(int64_t i) const { return offsets_data()[i]; }
|
377 |
+
|
378 |
+
/// Temporary access to a value.
|
379 |
+
///
|
380 |
+
/// This view becomes invalid on the next modifying operation.
|
381 |
+
std::string_view GetView(int64_t i) const {
|
382 |
+
offset_type value_length;
|
383 |
+
const uint8_t* value_data = GetValue(i, &value_length);
|
384 |
+
return std::string_view(reinterpret_cast<const char*>(value_data), value_length);
|
385 |
+
}
|
386 |
+
|
387 |
+
// Cannot make this a static attribute because of linking issues
|
388 |
+
static constexpr int64_t memory_limit() {
|
389 |
+
return std::numeric_limits<offset_type>::max() - 1;
|
390 |
+
}
|
391 |
+
|
392 |
+
protected:
|
393 |
+
TypedBufferBuilder<offset_type> offsets_builder_;
|
394 |
+
TypedBufferBuilder<uint8_t> value_data_builder_;
|
395 |
+
|
396 |
+
Status AppendNextOffset() {
|
397 |
+
const int64_t num_bytes = value_data_builder_.length();
|
398 |
+
return offsets_builder_.Append(static_cast<offset_type>(num_bytes));
|
399 |
+
}
|
400 |
+
|
401 |
+
void UnsafeAppendNextOffset() {
|
402 |
+
const int64_t num_bytes = value_data_builder_.length();
|
403 |
+
offsets_builder_.UnsafeAppend(static_cast<offset_type>(num_bytes));
|
404 |
+
}
|
405 |
+
};
|
406 |
+
|
407 |
+
/// \class BinaryBuilder
|
408 |
+
/// \brief Builder class for variable-length binary data
|
409 |
+
class ARROW_EXPORT BinaryBuilder : public BaseBinaryBuilder<BinaryType> {
|
410 |
+
public:
|
411 |
+
using BaseBinaryBuilder::BaseBinaryBuilder;
|
412 |
+
|
413 |
+
/// \cond FALSE
|
414 |
+
using ArrayBuilder::Finish;
|
415 |
+
/// \endcond
|
416 |
+
|
417 |
+
Status Finish(std::shared_ptr<BinaryArray>* out) { return FinishTyped(out); }
|
418 |
+
|
419 |
+
std::shared_ptr<DataType> type() const override { return binary(); }
|
420 |
+
};
|
421 |
+
|
422 |
+
/// \class StringBuilder
|
423 |
+
/// \brief Builder class for UTF8 strings
|
424 |
+
class ARROW_EXPORT StringBuilder : public BinaryBuilder {
|
425 |
+
public:
|
426 |
+
using BinaryBuilder::BinaryBuilder;
|
427 |
+
|
428 |
+
/// \cond FALSE
|
429 |
+
using ArrayBuilder::Finish;
|
430 |
+
/// \endcond
|
431 |
+
|
432 |
+
Status Finish(std::shared_ptr<StringArray>* out) { return FinishTyped(out); }
|
433 |
+
|
434 |
+
std::shared_ptr<DataType> type() const override { return utf8(); }
|
435 |
+
};
|
436 |
+
|
437 |
+
/// \class LargeBinaryBuilder
|
438 |
+
/// \brief Builder class for large variable-length binary data
|
439 |
+
class ARROW_EXPORT LargeBinaryBuilder : public BaseBinaryBuilder<LargeBinaryType> {
|
440 |
+
public:
|
441 |
+
using BaseBinaryBuilder::BaseBinaryBuilder;
|
442 |
+
|
443 |
+
/// \cond FALSE
|
444 |
+
using ArrayBuilder::Finish;
|
445 |
+
/// \endcond
|
446 |
+
|
447 |
+
Status Finish(std::shared_ptr<LargeBinaryArray>* out) { return FinishTyped(out); }
|
448 |
+
|
449 |
+
std::shared_ptr<DataType> type() const override { return large_binary(); }
|
450 |
+
};
|
451 |
+
|
452 |
+
/// \class LargeStringBuilder
|
453 |
+
/// \brief Builder class for large UTF8 strings
|
454 |
+
class ARROW_EXPORT LargeStringBuilder : public LargeBinaryBuilder {
|
455 |
+
public:
|
456 |
+
using LargeBinaryBuilder::LargeBinaryBuilder;
|
457 |
+
|
458 |
+
/// \cond FALSE
|
459 |
+
using ArrayBuilder::Finish;
|
460 |
+
/// \endcond
|
461 |
+
|
462 |
+
Status Finish(std::shared_ptr<LargeStringArray>* out) { return FinishTyped(out); }
|
463 |
+
|
464 |
+
std::shared_ptr<DataType> type() const override { return large_utf8(); }
|
465 |
+
};
|
466 |
+
|
467 |
+
// ----------------------------------------------------------------------
|
468 |
+
// BinaryViewBuilder, StringViewBuilder
|
469 |
+
//
|
470 |
+
// These builders do not support building raw pointer view arrays.
|
471 |
+
|
472 |
+
namespace internal {
|
473 |
+
|
474 |
+
// We allocate medium-sized memory chunks and accumulate data in those, which
|
475 |
+
// may result in some waste if there are many large-ish strings. If a string
|
476 |
+
// comes along that does not fit into a block, we allocate a new block and
|
477 |
+
// write into that.
|
478 |
+
//
|
479 |
+
// Later we can implement optimizations to continuing filling underfull blocks
|
480 |
+
// after encountering a large string that required allocating a new block.
|
481 |
+
class ARROW_EXPORT StringHeapBuilder {
|
482 |
+
public:
|
483 |
+
static constexpr int64_t kDefaultBlocksize = 32 << 10; // 32KB
|
484 |
+
|
485 |
+
StringHeapBuilder(MemoryPool* pool, int64_t alignment)
|
486 |
+
: pool_(pool), alignment_(alignment) {}
|
487 |
+
|
488 |
+
void SetBlockSize(int64_t blocksize) { blocksize_ = blocksize; }
|
489 |
+
|
490 |
+
using c_type = BinaryViewType::c_type;
|
491 |
+
|
492 |
+
template <bool Safe>
|
493 |
+
std::conditional_t<Safe, Result<c_type>, c_type> Append(const uint8_t* value,
|
494 |
+
int64_t length) {
|
495 |
+
if (length <= BinaryViewType::kInlineSize) {
|
496 |
+
return util::ToInlineBinaryView(value, static_cast<int32_t>(length));
|
497 |
+
}
|
498 |
+
|
499 |
+
if constexpr (Safe) {
|
500 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
501 |
+
}
|
502 |
+
|
503 |
+
auto v =
|
504 |
+
util::ToBinaryView(value, static_cast<int32_t>(length),
|
505 |
+
static_cast<int32_t>(blocks_.size() - 1), current_offset_);
|
506 |
+
|
507 |
+
memcpy(current_out_buffer_, value, static_cast<size_t>(length));
|
508 |
+
current_out_buffer_ += length;
|
509 |
+
current_remaining_bytes_ -= length;
|
510 |
+
current_offset_ += static_cast<int32_t>(length);
|
511 |
+
return v;
|
512 |
+
}
|
513 |
+
|
514 |
+
static constexpr int64_t ValueSizeLimit() {
|
515 |
+
return std::numeric_limits<int32_t>::max();
|
516 |
+
}
|
517 |
+
|
518 |
+
/// \brief Ensure that the indicated number of bytes can be appended via
|
519 |
+
/// UnsafeAppend operations without the need to allocate more memory
|
520 |
+
Status Reserve(int64_t num_bytes) {
|
521 |
+
if (ARROW_PREDICT_FALSE(num_bytes > ValueSizeLimit())) {
|
522 |
+
return Status::CapacityError(
|
523 |
+
"BinaryView or StringView elements cannot reference "
|
524 |
+
"strings larger than 2GB");
|
525 |
+
}
|
526 |
+
if (num_bytes > current_remaining_bytes_) {
|
527 |
+
ARROW_RETURN_NOT_OK(FinishLastBlock());
|
528 |
+
current_remaining_bytes_ = num_bytes > blocksize_ ? num_bytes : blocksize_;
|
529 |
+
ARROW_ASSIGN_OR_RAISE(
|
530 |
+
std::shared_ptr<ResizableBuffer> new_block,
|
531 |
+
AllocateResizableBuffer(current_remaining_bytes_, alignment_, pool_));
|
532 |
+
current_offset_ = 0;
|
533 |
+
current_out_buffer_ = new_block->mutable_data();
|
534 |
+
blocks_.emplace_back(std::move(new_block));
|
535 |
+
}
|
536 |
+
return Status::OK();
|
537 |
+
}
|
538 |
+
|
539 |
+
void Reset() {
|
540 |
+
current_offset_ = 0;
|
541 |
+
current_out_buffer_ = NULLPTR;
|
542 |
+
current_remaining_bytes_ = 0;
|
543 |
+
blocks_.clear();
|
544 |
+
}
|
545 |
+
|
546 |
+
int64_t current_remaining_bytes() const { return current_remaining_bytes_; }
|
547 |
+
|
548 |
+
Result<std::vector<std::shared_ptr<ResizableBuffer>>> Finish() {
|
549 |
+
if (!blocks_.empty()) {
|
550 |
+
ARROW_RETURN_NOT_OK(FinishLastBlock());
|
551 |
+
}
|
552 |
+
current_offset_ = 0;
|
553 |
+
current_out_buffer_ = NULLPTR;
|
554 |
+
current_remaining_bytes_ = 0;
|
555 |
+
return std::move(blocks_);
|
556 |
+
}
|
557 |
+
|
558 |
+
private:
|
559 |
+
Status FinishLastBlock() {
|
560 |
+
if (current_remaining_bytes_ > 0) {
|
561 |
+
// Avoid leaking uninitialized bytes from the allocator
|
562 |
+
ARROW_RETURN_NOT_OK(
|
563 |
+
blocks_.back()->Resize(blocks_.back()->size() - current_remaining_bytes_,
|
564 |
+
/*shrink_to_fit=*/true));
|
565 |
+
blocks_.back()->ZeroPadding();
|
566 |
+
}
|
567 |
+
return Status::OK();
|
568 |
+
}
|
569 |
+
|
570 |
+
MemoryPool* pool_;
|
571 |
+
int64_t alignment_;
|
572 |
+
int64_t blocksize_ = kDefaultBlocksize;
|
573 |
+
std::vector<std::shared_ptr<ResizableBuffer>> blocks_;
|
574 |
+
|
575 |
+
int32_t current_offset_ = 0;
|
576 |
+
uint8_t* current_out_buffer_ = NULLPTR;
|
577 |
+
int64_t current_remaining_bytes_ = 0;
|
578 |
+
};
|
579 |
+
|
580 |
+
} // namespace internal
|
581 |
+
|
582 |
+
class ARROW_EXPORT BinaryViewBuilder : public ArrayBuilder {
|
583 |
+
public:
|
584 |
+
using TypeClass = BinaryViewType;
|
585 |
+
|
586 |
+
// this constructor provided for MakeBuilder compatibility
|
587 |
+
BinaryViewBuilder(const std::shared_ptr<DataType>&, MemoryPool* pool);
|
588 |
+
|
589 |
+
explicit BinaryViewBuilder(MemoryPool* pool = default_memory_pool(),
|
590 |
+
int64_t alignment = kDefaultBufferAlignment)
|
591 |
+
: ArrayBuilder(pool, alignment),
|
592 |
+
data_builder_(pool, alignment),
|
593 |
+
data_heap_builder_(pool, alignment) {}
|
594 |
+
|
595 |
+
/// Set the size for future preallocated data buffers.
|
596 |
+
///
|
597 |
+
/// The default size is 32KB, so after each 32KB of string data appended to the builder
|
598 |
+
/// a new data buffer will be allocated. Adjust this to a larger value to decrease the
|
599 |
+
/// frequency of allocation, or to a smaller value to lower the overhead of each
|
600 |
+
/// allocation.
|
601 |
+
void SetBlockSize(int64_t blocksize) { data_heap_builder_.SetBlockSize(blocksize); }
|
602 |
+
|
603 |
+
/// The number of bytes which can be appended to this builder without allocating another
|
604 |
+
/// data buffer.
|
605 |
+
int64_t current_block_bytes_remaining() const {
|
606 |
+
return data_heap_builder_.current_remaining_bytes();
|
607 |
+
}
|
608 |
+
|
609 |
+
Status Append(const uint8_t* value, int64_t length) {
|
610 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
611 |
+
UnsafeAppendToBitmap(true);
|
612 |
+
ARROW_ASSIGN_OR_RAISE(auto v,
|
613 |
+
data_heap_builder_.Append</*Safe=*/true>(value, length));
|
614 |
+
data_builder_.UnsafeAppend(v);
|
615 |
+
return Status::OK();
|
616 |
+
}
|
617 |
+
|
618 |
+
Status Append(const char* value, int64_t length) {
|
619 |
+
return Append(reinterpret_cast<const uint8_t*>(value), length);
|
620 |
+
}
|
621 |
+
|
622 |
+
Status Append(std::string_view value) {
|
623 |
+
return Append(value.data(), static_cast<int64_t>(value.size()));
|
624 |
+
}
|
625 |
+
|
626 |
+
/// \brief Append without checking capacity
|
627 |
+
///
|
628 |
+
/// Builder should have been presized using Reserve() and ReserveData(),
|
629 |
+
/// respectively, and the value must not be larger than 2GB
|
630 |
+
void UnsafeAppend(const uint8_t* value, int64_t length) {
|
631 |
+
UnsafeAppendToBitmap(true);
|
632 |
+
auto v = data_heap_builder_.Append</*Safe=*/false>(value, length);
|
633 |
+
data_builder_.UnsafeAppend(v);
|
634 |
+
}
|
635 |
+
|
636 |
+
void UnsafeAppend(const char* value, int64_t length) {
|
637 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value), length);
|
638 |
+
}
|
639 |
+
|
640 |
+
void UnsafeAppend(const std::string& value) {
|
641 |
+
UnsafeAppend(value.c_str(), static_cast<int64_t>(value.size()));
|
642 |
+
}
|
643 |
+
|
644 |
+
void UnsafeAppend(std::string_view value) {
|
645 |
+
UnsafeAppend(value.data(), static_cast<int64_t>(value.size()));
|
646 |
+
}
|
647 |
+
|
648 |
+
/// \brief Ensures there is enough allocated available capacity in the
|
649 |
+
/// out-of-line data heap to append the indicated number of bytes without
|
650 |
+
/// additional allocations
|
651 |
+
Status ReserveData(int64_t length);
|
652 |
+
|
653 |
+
Status AppendNulls(int64_t length) final {
|
654 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
655 |
+
data_builder_.UnsafeAppend(length, BinaryViewType::c_type{});
|
656 |
+
UnsafeSetNull(length);
|
657 |
+
return Status::OK();
|
658 |
+
}
|
659 |
+
|
660 |
+
/// \brief Append a single null element
|
661 |
+
Status AppendNull() final {
|
662 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
663 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
664 |
+
UnsafeAppendToBitmap(false);
|
665 |
+
return Status::OK();
|
666 |
+
}
|
667 |
+
|
668 |
+
/// \brief Append a empty element (length-0 inline string)
|
669 |
+
Status AppendEmptyValue() final {
|
670 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
671 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
672 |
+
UnsafeAppendToBitmap(true);
|
673 |
+
return Status::OK();
|
674 |
+
}
|
675 |
+
|
676 |
+
/// \brief Append several empty elements
|
677 |
+
Status AppendEmptyValues(int64_t length) final {
|
678 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
679 |
+
data_builder_.UnsafeAppend(length, BinaryViewType::c_type{});
|
680 |
+
UnsafeSetNotNull(length);
|
681 |
+
return Status::OK();
|
682 |
+
}
|
683 |
+
|
684 |
+
void UnsafeAppendNull() {
|
685 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
686 |
+
UnsafeAppendToBitmap(false);
|
687 |
+
}
|
688 |
+
|
689 |
+
void UnsafeAppendEmptyValue() {
|
690 |
+
data_builder_.UnsafeAppend(BinaryViewType::c_type{});
|
691 |
+
UnsafeAppendToBitmap(true);
|
692 |
+
}
|
693 |
+
|
694 |
+
/// \brief Append a slice of a BinaryViewArray passed as an ArraySpan. Copies
|
695 |
+
/// the underlying out-of-line string memory to avoid memory lifetime issues
|
696 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
697 |
+
int64_t length) override;
|
698 |
+
|
699 |
+
void Reset() override;
|
700 |
+
|
701 |
+
Status Resize(int64_t capacity) override {
|
702 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
703 |
+
capacity = std::max(capacity, kMinBuilderCapacity);
|
704 |
+
ARROW_RETURN_NOT_OK(data_builder_.Resize(capacity));
|
705 |
+
return ArrayBuilder::Resize(capacity);
|
706 |
+
}
|
707 |
+
|
708 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
709 |
+
|
710 |
+
std::shared_ptr<DataType> type() const override { return binary_view(); }
|
711 |
+
|
712 |
+
protected:
|
713 |
+
TypedBufferBuilder<BinaryViewType::c_type> data_builder_;
|
714 |
+
|
715 |
+
// Accumulates out-of-line data in fixed-size chunks which are then attached
|
716 |
+
// to the resulting ArrayData
|
717 |
+
internal::StringHeapBuilder data_heap_builder_;
|
718 |
+
};
|
719 |
+
|
720 |
+
class ARROW_EXPORT StringViewBuilder : public BinaryViewBuilder {
|
721 |
+
public:
|
722 |
+
using BinaryViewBuilder::BinaryViewBuilder;
|
723 |
+
std::shared_ptr<DataType> type() const override { return utf8_view(); }
|
724 |
+
};
|
725 |
+
|
726 |
+
// ----------------------------------------------------------------------
|
727 |
+
// FixedSizeBinaryBuilder
|
728 |
+
|
729 |
+
class ARROW_EXPORT FixedSizeBinaryBuilder : public ArrayBuilder {
|
730 |
+
public:
|
731 |
+
using TypeClass = FixedSizeBinaryType;
|
732 |
+
|
733 |
+
explicit FixedSizeBinaryBuilder(const std::shared_ptr<DataType>& type,
|
734 |
+
MemoryPool* pool = default_memory_pool(),
|
735 |
+
int64_t alignment = kDefaultBufferAlignment);
|
736 |
+
|
737 |
+
Status Append(const uint8_t* value) {
|
738 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
739 |
+
UnsafeAppend(value);
|
740 |
+
return Status::OK();
|
741 |
+
}
|
742 |
+
|
743 |
+
Status Append(const char* value) {
|
744 |
+
return Append(reinterpret_cast<const uint8_t*>(value));
|
745 |
+
}
|
746 |
+
|
747 |
+
Status Append(std::string_view view) {
|
748 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
749 |
+
UnsafeAppend(view);
|
750 |
+
return Status::OK();
|
751 |
+
}
|
752 |
+
|
753 |
+
Status Append(const std::string& s) {
|
754 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
755 |
+
UnsafeAppend(s);
|
756 |
+
return Status::OK();
|
757 |
+
}
|
758 |
+
|
759 |
+
Status Append(const Buffer& s) {
|
760 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
761 |
+
UnsafeAppend(s);
|
762 |
+
return Status::OK();
|
763 |
+
}
|
764 |
+
|
765 |
+
Status Append(const std::shared_ptr<Buffer>& s) { return Append(*s); }
|
766 |
+
|
767 |
+
template <size_t NBYTES>
|
768 |
+
Status Append(const std::array<uint8_t, NBYTES>& value) {
|
769 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
770 |
+
UnsafeAppend(
|
771 |
+
std::string_view(reinterpret_cast<const char*>(value.data()), value.size()));
|
772 |
+
return Status::OK();
|
773 |
+
}
|
774 |
+
|
775 |
+
Status AppendValues(const uint8_t* data, int64_t length,
|
776 |
+
const uint8_t* valid_bytes = NULLPTR);
|
777 |
+
|
778 |
+
Status AppendValues(const uint8_t* data, int64_t length, const uint8_t* validity,
|
779 |
+
int64_t bitmap_offset);
|
780 |
+
|
781 |
+
Status AppendNull() final;
|
782 |
+
Status AppendNulls(int64_t length) final;
|
783 |
+
|
784 |
+
Status AppendEmptyValue() final;
|
785 |
+
Status AppendEmptyValues(int64_t length) final;
|
786 |
+
|
787 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
788 |
+
int64_t length) override {
|
789 |
+
return AppendValues(
|
790 |
+
array.GetValues<uint8_t>(1, 0) + ((array.offset + offset) * byte_width_), length,
|
791 |
+
array.GetValues<uint8_t>(0, 0), array.offset + offset);
|
792 |
+
}
|
793 |
+
|
794 |
+
void UnsafeAppend(const uint8_t* value) {
|
795 |
+
UnsafeAppendToBitmap(true);
|
796 |
+
if (ARROW_PREDICT_TRUE(byte_width_ > 0)) {
|
797 |
+
byte_builder_.UnsafeAppend(value, byte_width_);
|
798 |
+
}
|
799 |
+
}
|
800 |
+
|
801 |
+
void UnsafeAppend(const char* value) {
|
802 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value));
|
803 |
+
}
|
804 |
+
|
805 |
+
void UnsafeAppend(std::string_view value) {
|
806 |
+
#ifndef NDEBUG
|
807 |
+
CheckValueSize(static_cast<size_t>(value.size()));
|
808 |
+
#endif
|
809 |
+
UnsafeAppend(reinterpret_cast<const uint8_t*>(value.data()));
|
810 |
+
}
|
811 |
+
|
812 |
+
void UnsafeAppend(const Buffer& s) { UnsafeAppend(std::string_view{s}); }
|
813 |
+
|
814 |
+
void UnsafeAppend(const std::shared_ptr<Buffer>& s) { UnsafeAppend(*s); }
|
815 |
+
|
816 |
+
void UnsafeAppendNull() {
|
817 |
+
UnsafeAppendToBitmap(false);
|
818 |
+
byte_builder_.UnsafeAppend(/*num_copies=*/byte_width_, 0);
|
819 |
+
}
|
820 |
+
|
821 |
+
Status ValidateOverflow(int64_t new_bytes) const {
|
822 |
+
auto new_size = byte_builder_.length() + new_bytes;
|
823 |
+
if (ARROW_PREDICT_FALSE(new_size > memory_limit())) {
|
824 |
+
return Status::CapacityError("array cannot contain more than ", memory_limit(),
|
825 |
+
" bytes, have ", new_size);
|
826 |
+
} else {
|
827 |
+
return Status::OK();
|
828 |
+
}
|
829 |
+
}
|
830 |
+
|
831 |
+
/// \brief Ensures there is enough allocated capacity to append the indicated
|
832 |
+
/// number of bytes to the value data buffer without additional allocations
|
833 |
+
Status ReserveData(int64_t elements) {
|
834 |
+
ARROW_RETURN_NOT_OK(ValidateOverflow(elements));
|
835 |
+
return byte_builder_.Reserve(elements);
|
836 |
+
}
|
837 |
+
|
838 |
+
void Reset() override;
|
839 |
+
Status Resize(int64_t capacity) override;
|
840 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
841 |
+
|
842 |
+
/// \cond FALSE
|
843 |
+
using ArrayBuilder::Finish;
|
844 |
+
/// \endcond
|
845 |
+
|
846 |
+
Status Finish(std::shared_ptr<FixedSizeBinaryArray>* out) { return FinishTyped(out); }
|
847 |
+
|
848 |
+
/// \return size of values buffer so far
|
849 |
+
int64_t value_data_length() const { return byte_builder_.length(); }
|
850 |
+
|
851 |
+
int32_t byte_width() const { return byte_width_; }
|
852 |
+
|
853 |
+
/// Temporary access to a value.
|
854 |
+
///
|
855 |
+
/// This pointer becomes invalid on the next modifying operation.
|
856 |
+
const uint8_t* GetValue(int64_t i) const;
|
857 |
+
|
858 |
+
/// Temporary access to a value.
|
859 |
+
///
|
860 |
+
/// This view becomes invalid on the next modifying operation.
|
861 |
+
std::string_view GetView(int64_t i) const;
|
862 |
+
|
863 |
+
static constexpr int64_t memory_limit() {
|
864 |
+
return std::numeric_limits<int64_t>::max() - 1;
|
865 |
+
}
|
866 |
+
|
867 |
+
std::shared_ptr<DataType> type() const override {
|
868 |
+
return fixed_size_binary(byte_width_);
|
869 |
+
}
|
870 |
+
|
871 |
+
protected:
|
872 |
+
int32_t byte_width_;
|
873 |
+
BufferBuilder byte_builder_;
|
874 |
+
|
875 |
+
/// Temporary access to a value.
|
876 |
+
///
|
877 |
+
/// This pointer becomes invalid on the next modifying operation.
|
878 |
+
uint8_t* GetMutableValue(int64_t i) {
|
879 |
+
uint8_t* data_ptr = byte_builder_.mutable_data();
|
880 |
+
return data_ptr + i * byte_width_;
|
881 |
+
}
|
882 |
+
|
883 |
+
void CheckValueSize(int64_t size);
|
884 |
+
};
|
885 |
+
|
886 |
+
/// @}
|
887 |
+
|
888 |
+
// ----------------------------------------------------------------------
|
889 |
+
// Chunked builders: build a sequence of BinaryArray or StringArray that are
|
890 |
+
// limited to a particular size (to the upper limit of 2GB)
|
891 |
+
|
892 |
+
namespace internal {
|
893 |
+
|
894 |
+
class ARROW_EXPORT ChunkedBinaryBuilder {
|
895 |
+
public:
|
896 |
+
explicit ChunkedBinaryBuilder(int32_t max_chunk_value_length,
|
897 |
+
MemoryPool* pool = default_memory_pool());
|
898 |
+
|
899 |
+
ChunkedBinaryBuilder(int32_t max_chunk_value_length, int32_t max_chunk_length,
|
900 |
+
MemoryPool* pool = default_memory_pool());
|
901 |
+
|
902 |
+
virtual ~ChunkedBinaryBuilder() = default;
|
903 |
+
|
904 |
+
Status Append(const uint8_t* value, int32_t length) {
|
905 |
+
if (ARROW_PREDICT_FALSE(length + builder_->value_data_length() >
|
906 |
+
max_chunk_value_length_)) {
|
907 |
+
if (builder_->value_data_length() == 0) {
|
908 |
+
// The current item is larger than max_chunk_size_;
|
909 |
+
// this chunk will be oversize and hold *only* this item
|
910 |
+
ARROW_RETURN_NOT_OK(builder_->Append(value, length));
|
911 |
+
return NextChunk();
|
912 |
+
}
|
913 |
+
// The current item would cause builder_->value_data_length() to exceed
|
914 |
+
// max_chunk_size_, so finish this chunk and append the current item to the next
|
915 |
+
// chunk
|
916 |
+
ARROW_RETURN_NOT_OK(NextChunk());
|
917 |
+
return Append(value, length);
|
918 |
+
}
|
919 |
+
|
920 |
+
if (ARROW_PREDICT_FALSE(builder_->length() == max_chunk_length_)) {
|
921 |
+
// The current item would cause builder_->length() to exceed max_chunk_length_, so
|
922 |
+
// finish this chunk and append the current item to the next chunk
|
923 |
+
ARROW_RETURN_NOT_OK(NextChunk());
|
924 |
+
}
|
925 |
+
|
926 |
+
return builder_->Append(value, length);
|
927 |
+
}
|
928 |
+
|
929 |
+
Status Append(std::string_view value) {
|
930 |
+
return Append(reinterpret_cast<const uint8_t*>(value.data()),
|
931 |
+
static_cast<int32_t>(value.size()));
|
932 |
+
}
|
933 |
+
|
934 |
+
Status AppendNull() {
|
935 |
+
if (ARROW_PREDICT_FALSE(builder_->length() == max_chunk_length_)) {
|
936 |
+
ARROW_RETURN_NOT_OK(NextChunk());
|
937 |
+
}
|
938 |
+
return builder_->AppendNull();
|
939 |
+
}
|
940 |
+
|
941 |
+
Status Reserve(int64_t values);
|
942 |
+
|
943 |
+
virtual Status Finish(ArrayVector* out);
|
944 |
+
|
945 |
+
protected:
|
946 |
+
Status NextChunk();
|
947 |
+
|
948 |
+
// maximum total character data size per chunk
|
949 |
+
int64_t max_chunk_value_length_;
|
950 |
+
|
951 |
+
// maximum elements allowed per chunk
|
952 |
+
int64_t max_chunk_length_ = kListMaximumElements;
|
953 |
+
|
954 |
+
// when Reserve() would cause builder_ to exceed its max_chunk_length_,
|
955 |
+
// add to extra_capacity_ instead and wait to reserve until the next chunk
|
956 |
+
int64_t extra_capacity_ = 0;
|
957 |
+
|
958 |
+
std::unique_ptr<BinaryBuilder> builder_;
|
959 |
+
std::vector<std::shared_ptr<Array>> chunks_;
|
960 |
+
};
|
961 |
+
|
962 |
+
class ARROW_EXPORT ChunkedStringBuilder : public ChunkedBinaryBuilder {
|
963 |
+
public:
|
964 |
+
using ChunkedBinaryBuilder::ChunkedBinaryBuilder;
|
965 |
+
|
966 |
+
Status Finish(ArrayVector* out) override;
|
967 |
+
};
|
968 |
+
|
969 |
+
} // namespace internal
|
970 |
+
|
971 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_decimal.h
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
|
22 |
+
#include "arrow/array/array_decimal.h"
|
23 |
+
#include "arrow/array/builder_base.h"
|
24 |
+
#include "arrow/array/builder_binary.h"
|
25 |
+
#include "arrow/array/data.h"
|
26 |
+
#include "arrow/status.h"
|
27 |
+
#include "arrow/type.h"
|
28 |
+
#include "arrow/util/visibility.h"
|
29 |
+
|
30 |
+
namespace arrow {
|
31 |
+
|
32 |
+
/// \addtogroup numeric-builders
|
33 |
+
///
|
34 |
+
/// @{
|
35 |
+
|
36 |
+
class ARROW_EXPORT Decimal128Builder : public FixedSizeBinaryBuilder {
|
37 |
+
public:
|
38 |
+
using TypeClass = Decimal128Type;
|
39 |
+
using ValueType = Decimal128;
|
40 |
+
|
41 |
+
explicit Decimal128Builder(const std::shared_ptr<DataType>& type,
|
42 |
+
MemoryPool* pool = default_memory_pool(),
|
43 |
+
int64_t alignment = kDefaultBufferAlignment);
|
44 |
+
|
45 |
+
using FixedSizeBinaryBuilder::Append;
|
46 |
+
using FixedSizeBinaryBuilder::AppendValues;
|
47 |
+
using FixedSizeBinaryBuilder::Reset;
|
48 |
+
|
49 |
+
Status Append(Decimal128 val);
|
50 |
+
void UnsafeAppend(Decimal128 val);
|
51 |
+
void UnsafeAppend(std::string_view val);
|
52 |
+
|
53 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
54 |
+
|
55 |
+
/// \cond FALSE
|
56 |
+
using ArrayBuilder::Finish;
|
57 |
+
/// \endcond
|
58 |
+
|
59 |
+
Status Finish(std::shared_ptr<Decimal128Array>* out) { return FinishTyped(out); }
|
60 |
+
|
61 |
+
std::shared_ptr<DataType> type() const override { return decimal_type_; }
|
62 |
+
|
63 |
+
protected:
|
64 |
+
std::shared_ptr<Decimal128Type> decimal_type_;
|
65 |
+
};
|
66 |
+
|
67 |
+
class ARROW_EXPORT Decimal256Builder : public FixedSizeBinaryBuilder {
|
68 |
+
public:
|
69 |
+
using TypeClass = Decimal256Type;
|
70 |
+
using ValueType = Decimal256;
|
71 |
+
|
72 |
+
explicit Decimal256Builder(const std::shared_ptr<DataType>& type,
|
73 |
+
MemoryPool* pool = default_memory_pool(),
|
74 |
+
int64_t alignment = kDefaultBufferAlignment);
|
75 |
+
|
76 |
+
using FixedSizeBinaryBuilder::Append;
|
77 |
+
using FixedSizeBinaryBuilder::AppendValues;
|
78 |
+
using FixedSizeBinaryBuilder::Reset;
|
79 |
+
|
80 |
+
Status Append(const Decimal256& val);
|
81 |
+
void UnsafeAppend(const Decimal256& val);
|
82 |
+
void UnsafeAppend(std::string_view val);
|
83 |
+
|
84 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
85 |
+
|
86 |
+
/// \cond FALSE
|
87 |
+
using ArrayBuilder::Finish;
|
88 |
+
/// \endcond
|
89 |
+
|
90 |
+
Status Finish(std::shared_ptr<Decimal256Array>* out) { return FinishTyped(out); }
|
91 |
+
|
92 |
+
std::shared_ptr<DataType> type() const override { return decimal_type_; }
|
93 |
+
|
94 |
+
protected:
|
95 |
+
std::shared_ptr<Decimal256Type> decimal_type_;
|
96 |
+
};
|
97 |
+
|
98 |
+
using DecimalBuilder = Decimal128Builder;
|
99 |
+
|
100 |
+
/// @}
|
101 |
+
|
102 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_primitive.h
ADDED
@@ -0,0 +1,555 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm>
|
21 |
+
#include <memory>
|
22 |
+
#include <vector>
|
23 |
+
|
24 |
+
#include "arrow/array/builder_base.h"
|
25 |
+
#include "arrow/array/data.h"
|
26 |
+
#include "arrow/result.h"
|
27 |
+
#include "arrow/type.h"
|
28 |
+
#include "arrow/type_traits.h"
|
29 |
+
|
30 |
+
namespace arrow {
|
31 |
+
|
32 |
+
class ARROW_EXPORT NullBuilder : public ArrayBuilder {
|
33 |
+
public:
|
34 |
+
explicit NullBuilder(MemoryPool* pool = default_memory_pool(),
|
35 |
+
int64_t alignment = kDefaultBufferAlignment)
|
36 |
+
: ArrayBuilder(pool) {}
|
37 |
+
explicit NullBuilder(const std::shared_ptr<DataType>& type,
|
38 |
+
MemoryPool* pool = default_memory_pool(),
|
39 |
+
int64_t alignment = kDefaultBufferAlignment)
|
40 |
+
: NullBuilder(pool, alignment) {}
|
41 |
+
|
42 |
+
/// \brief Append the specified number of null elements
|
43 |
+
Status AppendNulls(int64_t length) final {
|
44 |
+
if (length < 0) return Status::Invalid("length must be positive");
|
45 |
+
null_count_ += length;
|
46 |
+
length_ += length;
|
47 |
+
return Status::OK();
|
48 |
+
}
|
49 |
+
|
50 |
+
/// \brief Append a single null element
|
51 |
+
Status AppendNull() final { return AppendNulls(1); }
|
52 |
+
|
53 |
+
Status AppendEmptyValues(int64_t length) final { return AppendNulls(length); }
|
54 |
+
|
55 |
+
Status AppendEmptyValue() final { return AppendEmptyValues(1); }
|
56 |
+
|
57 |
+
Status Append(std::nullptr_t) { return AppendNull(); }
|
58 |
+
|
59 |
+
Status AppendArraySlice(const ArraySpan&, int64_t, int64_t length) override {
|
60 |
+
return AppendNulls(length);
|
61 |
+
}
|
62 |
+
|
63 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
64 |
+
|
65 |
+
/// \cond FALSE
|
66 |
+
using ArrayBuilder::Finish;
|
67 |
+
/// \endcond
|
68 |
+
|
69 |
+
std::shared_ptr<DataType> type() const override { return null(); }
|
70 |
+
|
71 |
+
Status Finish(std::shared_ptr<NullArray>* out) { return FinishTyped(out); }
|
72 |
+
};
|
73 |
+
|
74 |
+
/// \addtogroup numeric-builders
|
75 |
+
///
|
76 |
+
/// @{
|
77 |
+
|
78 |
+
/// Base class for all Builders that emit an Array of a scalar numerical type.
|
79 |
+
template <typename T>
|
80 |
+
class NumericBuilder
|
81 |
+
: public ArrayBuilder,
|
82 |
+
public internal::ArrayBuilderExtraOps<NumericBuilder<T>, typename T::c_type> {
|
83 |
+
public:
|
84 |
+
using TypeClass = T;
|
85 |
+
using value_type = typename T::c_type;
|
86 |
+
using ArrayType = typename TypeTraits<T>::ArrayType;
|
87 |
+
|
88 |
+
template <typename T1 = T>
|
89 |
+
explicit NumericBuilder(
|
90 |
+
enable_if_parameter_free<T1, MemoryPool*> pool = default_memory_pool(),
|
91 |
+
int64_t alignment = kDefaultBufferAlignment)
|
92 |
+
: ArrayBuilder(pool, alignment),
|
93 |
+
type_(TypeTraits<T>::type_singleton()),
|
94 |
+
data_builder_(pool, alignment) {}
|
95 |
+
|
96 |
+
NumericBuilder(const std::shared_ptr<DataType>& type, MemoryPool* pool,
|
97 |
+
int64_t alignment = kDefaultBufferAlignment)
|
98 |
+
: ArrayBuilder(pool, alignment), type_(type), data_builder_(pool, alignment) {}
|
99 |
+
|
100 |
+
/// Append a single scalar and increase the size if necessary.
|
101 |
+
Status Append(const value_type val) {
|
102 |
+
ARROW_RETURN_NOT_OK(ArrayBuilder::Reserve(1));
|
103 |
+
UnsafeAppend(val);
|
104 |
+
return Status::OK();
|
105 |
+
}
|
106 |
+
|
107 |
+
/// Write nulls as uint8_t* (0 value indicates null) into pre-allocated memory
|
108 |
+
/// The memory at the corresponding data slot is set to 0 to prevent
|
109 |
+
/// uninitialized memory access
|
110 |
+
Status AppendNulls(int64_t length) final {
|
111 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
112 |
+
data_builder_.UnsafeAppend(length, value_type{}); // zero
|
113 |
+
UnsafeSetNull(length);
|
114 |
+
return Status::OK();
|
115 |
+
}
|
116 |
+
|
117 |
+
/// \brief Append a single null element
|
118 |
+
Status AppendNull() final {
|
119 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
120 |
+
data_builder_.UnsafeAppend(value_type{}); // zero
|
121 |
+
UnsafeAppendToBitmap(false);
|
122 |
+
return Status::OK();
|
123 |
+
}
|
124 |
+
|
125 |
+
/// \brief Append a empty element
|
126 |
+
Status AppendEmptyValue() final {
|
127 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
128 |
+
data_builder_.UnsafeAppend(value_type{}); // zero
|
129 |
+
UnsafeAppendToBitmap(true);
|
130 |
+
return Status::OK();
|
131 |
+
}
|
132 |
+
|
133 |
+
/// \brief Append several empty elements
|
134 |
+
Status AppendEmptyValues(int64_t length) final {
|
135 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
136 |
+
data_builder_.UnsafeAppend(length, value_type{}); // zero
|
137 |
+
UnsafeSetNotNull(length);
|
138 |
+
return Status::OK();
|
139 |
+
}
|
140 |
+
|
141 |
+
value_type GetValue(int64_t index) const { return data_builder_.data()[index]; }
|
142 |
+
|
143 |
+
void Reset() override {
|
144 |
+
data_builder_.Reset();
|
145 |
+
ArrayBuilder::Reset();
|
146 |
+
}
|
147 |
+
|
148 |
+
Status Resize(int64_t capacity) override {
|
149 |
+
ARROW_RETURN_NOT_OK(CheckCapacity(capacity));
|
150 |
+
capacity = std::max(capacity, kMinBuilderCapacity);
|
151 |
+
ARROW_RETURN_NOT_OK(data_builder_.Resize(capacity));
|
152 |
+
return ArrayBuilder::Resize(capacity);
|
153 |
+
}
|
154 |
+
|
155 |
+
value_type operator[](int64_t index) const { return GetValue(index); }
|
156 |
+
|
157 |
+
value_type& operator[](int64_t index) {
|
158 |
+
return reinterpret_cast<value_type*>(data_builder_.mutable_data())[index];
|
159 |
+
}
|
160 |
+
|
161 |
+
/// \brief Append a sequence of elements in one shot
|
162 |
+
/// \param[in] values a contiguous C array of values
|
163 |
+
/// \param[in] length the number of values to append
|
164 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
165 |
+
/// indicates a valid (non-null) value
|
166 |
+
/// \return Status
|
167 |
+
Status AppendValues(const value_type* values, int64_t length,
|
168 |
+
const uint8_t* valid_bytes = NULLPTR) {
|
169 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
170 |
+
data_builder_.UnsafeAppend(values, length);
|
171 |
+
// length_ is update by these
|
172 |
+
ArrayBuilder::UnsafeAppendToBitmap(valid_bytes, length);
|
173 |
+
return Status::OK();
|
174 |
+
}
|
175 |
+
|
176 |
+
/// \brief Append a sequence of elements in one shot
|
177 |
+
/// \param[in] values a contiguous C array of values
|
178 |
+
/// \param[in] length the number of values to append
|
179 |
+
/// \param[in] bitmap a validity bitmap to copy (may be null)
|
180 |
+
/// \param[in] bitmap_offset an offset into the validity bitmap
|
181 |
+
/// \return Status
|
182 |
+
Status AppendValues(const value_type* values, int64_t length, const uint8_t* bitmap,
|
183 |
+
int64_t bitmap_offset) {
|
184 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
185 |
+
data_builder_.UnsafeAppend(values, length);
|
186 |
+
// length_ is update by these
|
187 |
+
ArrayBuilder::UnsafeAppendToBitmap(bitmap, bitmap_offset, length);
|
188 |
+
return Status::OK();
|
189 |
+
}
|
190 |
+
|
191 |
+
/// \brief Append a sequence of elements in one shot
|
192 |
+
/// \param[in] values a contiguous C array of values
|
193 |
+
/// \param[in] length the number of values to append
|
194 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
195 |
+
/// (0). Equal in length to values
|
196 |
+
/// \return Status
|
197 |
+
Status AppendValues(const value_type* values, int64_t length,
|
198 |
+
const std::vector<bool>& is_valid) {
|
199 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
200 |
+
data_builder_.UnsafeAppend(values, length);
|
201 |
+
// length_ is update by these
|
202 |
+
ArrayBuilder::UnsafeAppendToBitmap(is_valid);
|
203 |
+
return Status::OK();
|
204 |
+
}
|
205 |
+
|
206 |
+
/// \brief Append a sequence of elements in one shot
|
207 |
+
/// \param[in] values a std::vector of values
|
208 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
209 |
+
/// (0). Equal in length to values
|
210 |
+
/// \return Status
|
211 |
+
Status AppendValues(const std::vector<value_type>& values,
|
212 |
+
const std::vector<bool>& is_valid) {
|
213 |
+
return AppendValues(values.data(), static_cast<int64_t>(values.size()), is_valid);
|
214 |
+
}
|
215 |
+
|
216 |
+
/// \brief Append a sequence of elements in one shot
|
217 |
+
/// \param[in] values a std::vector of values
|
218 |
+
/// \return Status
|
219 |
+
Status AppendValues(const std::vector<value_type>& values) {
|
220 |
+
return AppendValues(values.data(), static_cast<int64_t>(values.size()));
|
221 |
+
}
|
222 |
+
|
223 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override {
|
224 |
+
ARROW_ASSIGN_OR_RAISE(auto null_bitmap,
|
225 |
+
null_bitmap_builder_.FinishWithLength(length_));
|
226 |
+
ARROW_ASSIGN_OR_RAISE(auto data, data_builder_.FinishWithLength(length_));
|
227 |
+
*out = ArrayData::Make(type(), length_, {null_bitmap, data}, null_count_);
|
228 |
+
capacity_ = length_ = null_count_ = 0;
|
229 |
+
return Status::OK();
|
230 |
+
}
|
231 |
+
|
232 |
+
/// \cond FALSE
|
233 |
+
using ArrayBuilder::Finish;
|
234 |
+
/// \endcond
|
235 |
+
|
236 |
+
Status Finish(std::shared_ptr<ArrayType>* out) { return FinishTyped(out); }
|
237 |
+
|
238 |
+
/// \brief Append a sequence of elements in one shot
|
239 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
240 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
241 |
+
/// \return Status
|
242 |
+
template <typename ValuesIter>
|
243 |
+
Status AppendValues(ValuesIter values_begin, ValuesIter values_end) {
|
244 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
245 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
246 |
+
data_builder_.UnsafeAppend(values_begin, values_end);
|
247 |
+
// this updates the length_
|
248 |
+
UnsafeSetNotNull(length);
|
249 |
+
return Status::OK();
|
250 |
+
}
|
251 |
+
|
252 |
+
/// \brief Append a sequence of elements in one shot, with a specified nullmap
|
253 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
254 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
255 |
+
/// \param[in] valid_begin InputIterator with elements indication valid(1)
|
256 |
+
/// or null(0) values.
|
257 |
+
/// \return Status
|
258 |
+
template <typename ValuesIter, typename ValidIter>
|
259 |
+
enable_if_t<!std::is_pointer<ValidIter>::value, Status> AppendValues(
|
260 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
261 |
+
static_assert(!internal::is_null_pointer<ValidIter>::value,
|
262 |
+
"Don't pass a NULLPTR directly as valid_begin, use the 2-argument "
|
263 |
+
"version instead");
|
264 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
265 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
266 |
+
data_builder_.UnsafeAppend(values_begin, values_end);
|
267 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
268 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
269 |
+
length_ = null_bitmap_builder_.length();
|
270 |
+
null_count_ = null_bitmap_builder_.false_count();
|
271 |
+
return Status::OK();
|
272 |
+
}
|
273 |
+
|
274 |
+
// Same as above, with a pointer type ValidIter
|
275 |
+
template <typename ValuesIter, typename ValidIter>
|
276 |
+
enable_if_t<std::is_pointer<ValidIter>::value, Status> AppendValues(
|
277 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
278 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
279 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
280 |
+
data_builder_.UnsafeAppend(values_begin, values_end);
|
281 |
+
// this updates the length_
|
282 |
+
if (valid_begin == NULLPTR) {
|
283 |
+
UnsafeSetNotNull(length);
|
284 |
+
} else {
|
285 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
286 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
287 |
+
length_ = null_bitmap_builder_.length();
|
288 |
+
null_count_ = null_bitmap_builder_.false_count();
|
289 |
+
}
|
290 |
+
|
291 |
+
return Status::OK();
|
292 |
+
}
|
293 |
+
|
294 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
295 |
+
int64_t length) override {
|
296 |
+
return AppendValues(array.GetValues<value_type>(1) + offset, length,
|
297 |
+
array.GetValues<uint8_t>(0, 0), array.offset + offset);
|
298 |
+
}
|
299 |
+
|
300 |
+
/// Append a single scalar under the assumption that the underlying Buffer is
|
301 |
+
/// large enough.
|
302 |
+
///
|
303 |
+
/// This method does not capacity-check; make sure to call Reserve
|
304 |
+
/// beforehand.
|
305 |
+
void UnsafeAppend(const value_type val) {
|
306 |
+
ArrayBuilder::UnsafeAppendToBitmap(true);
|
307 |
+
data_builder_.UnsafeAppend(val);
|
308 |
+
}
|
309 |
+
|
310 |
+
void UnsafeAppendNull() {
|
311 |
+
ArrayBuilder::UnsafeAppendToBitmap(false);
|
312 |
+
data_builder_.UnsafeAppend(value_type{}); // zero
|
313 |
+
}
|
314 |
+
|
315 |
+
std::shared_ptr<DataType> type() const override { return type_; }
|
316 |
+
|
317 |
+
protected:
|
318 |
+
std::shared_ptr<DataType> type_;
|
319 |
+
TypedBufferBuilder<value_type> data_builder_;
|
320 |
+
};
|
321 |
+
|
322 |
+
// Builders
|
323 |
+
|
324 |
+
using UInt8Builder = NumericBuilder<UInt8Type>;
|
325 |
+
using UInt16Builder = NumericBuilder<UInt16Type>;
|
326 |
+
using UInt32Builder = NumericBuilder<UInt32Type>;
|
327 |
+
using UInt64Builder = NumericBuilder<UInt64Type>;
|
328 |
+
|
329 |
+
using Int8Builder = NumericBuilder<Int8Type>;
|
330 |
+
using Int16Builder = NumericBuilder<Int16Type>;
|
331 |
+
using Int32Builder = NumericBuilder<Int32Type>;
|
332 |
+
using Int64Builder = NumericBuilder<Int64Type>;
|
333 |
+
|
334 |
+
using HalfFloatBuilder = NumericBuilder<HalfFloatType>;
|
335 |
+
using FloatBuilder = NumericBuilder<FloatType>;
|
336 |
+
using DoubleBuilder = NumericBuilder<DoubleType>;
|
337 |
+
|
338 |
+
/// @}
|
339 |
+
|
340 |
+
/// \addtogroup temporal-builders
|
341 |
+
///
|
342 |
+
/// @{
|
343 |
+
|
344 |
+
using Date32Builder = NumericBuilder<Date32Type>;
|
345 |
+
using Date64Builder = NumericBuilder<Date64Type>;
|
346 |
+
using Time32Builder = NumericBuilder<Time32Type>;
|
347 |
+
using Time64Builder = NumericBuilder<Time64Type>;
|
348 |
+
using TimestampBuilder = NumericBuilder<TimestampType>;
|
349 |
+
using MonthIntervalBuilder = NumericBuilder<MonthIntervalType>;
|
350 |
+
using DurationBuilder = NumericBuilder<DurationType>;
|
351 |
+
|
352 |
+
/// @}
|
353 |
+
|
354 |
+
class ARROW_EXPORT BooleanBuilder
|
355 |
+
: public ArrayBuilder,
|
356 |
+
public internal::ArrayBuilderExtraOps<BooleanBuilder, bool> {
|
357 |
+
public:
|
358 |
+
using TypeClass = BooleanType;
|
359 |
+
using value_type = bool;
|
360 |
+
|
361 |
+
explicit BooleanBuilder(MemoryPool* pool = default_memory_pool(),
|
362 |
+
int64_t alignment = kDefaultBufferAlignment);
|
363 |
+
|
364 |
+
BooleanBuilder(const std::shared_ptr<DataType>& type,
|
365 |
+
MemoryPool* pool = default_memory_pool(),
|
366 |
+
int64_t alignment = kDefaultBufferAlignment);
|
367 |
+
|
368 |
+
/// Write nulls as uint8_t* (0 value indicates null) into pre-allocated memory
|
369 |
+
Status AppendNulls(int64_t length) final {
|
370 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
371 |
+
data_builder_.UnsafeAppend(length, false);
|
372 |
+
UnsafeSetNull(length);
|
373 |
+
return Status::OK();
|
374 |
+
}
|
375 |
+
|
376 |
+
Status AppendNull() final {
|
377 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
378 |
+
UnsafeAppendNull();
|
379 |
+
return Status::OK();
|
380 |
+
}
|
381 |
+
|
382 |
+
Status AppendEmptyValue() final {
|
383 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
384 |
+
data_builder_.UnsafeAppend(false);
|
385 |
+
UnsafeSetNotNull(1);
|
386 |
+
return Status::OK();
|
387 |
+
}
|
388 |
+
|
389 |
+
Status AppendEmptyValues(int64_t length) final {
|
390 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
391 |
+
data_builder_.UnsafeAppend(length, false);
|
392 |
+
UnsafeSetNotNull(length);
|
393 |
+
return Status::OK();
|
394 |
+
}
|
395 |
+
|
396 |
+
/// Scalar append
|
397 |
+
Status Append(const bool val) {
|
398 |
+
ARROW_RETURN_NOT_OK(Reserve(1));
|
399 |
+
UnsafeAppend(val);
|
400 |
+
return Status::OK();
|
401 |
+
}
|
402 |
+
|
403 |
+
Status Append(const uint8_t val) { return Append(val != 0); }
|
404 |
+
|
405 |
+
/// Scalar append, without checking for capacity
|
406 |
+
void UnsafeAppend(const bool val) {
|
407 |
+
data_builder_.UnsafeAppend(val);
|
408 |
+
UnsafeAppendToBitmap(true);
|
409 |
+
}
|
410 |
+
|
411 |
+
void UnsafeAppendNull() {
|
412 |
+
data_builder_.UnsafeAppend(false);
|
413 |
+
UnsafeAppendToBitmap(false);
|
414 |
+
}
|
415 |
+
|
416 |
+
void UnsafeAppend(const uint8_t val) { UnsafeAppend(val != 0); }
|
417 |
+
|
418 |
+
/// \brief Append a sequence of elements in one shot
|
419 |
+
/// \param[in] values a contiguous array of bytes (non-zero is 1)
|
420 |
+
/// \param[in] length the number of values to append
|
421 |
+
/// \param[in] valid_bytes an optional sequence of bytes where non-zero
|
422 |
+
/// indicates a valid (non-null) value
|
423 |
+
/// \return Status
|
424 |
+
Status AppendValues(const uint8_t* values, int64_t length,
|
425 |
+
const uint8_t* valid_bytes = NULLPTR);
|
426 |
+
|
427 |
+
/// \brief Append a sequence of elements in one shot
|
428 |
+
/// \param[in] values a bitmap of values
|
429 |
+
/// \param[in] length the number of values to append
|
430 |
+
/// \param[in] validity a validity bitmap to copy (may be null)
|
431 |
+
/// \param[in] offset an offset into the values and validity bitmaps
|
432 |
+
/// \return Status
|
433 |
+
Status AppendValues(const uint8_t* values, int64_t length, const uint8_t* validity,
|
434 |
+
int64_t offset);
|
435 |
+
|
436 |
+
/// \brief Append a sequence of elements in one shot
|
437 |
+
/// \param[in] values a contiguous C array of values
|
438 |
+
/// \param[in] length the number of values to append
|
439 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
440 |
+
/// (0). Equal in length to values
|
441 |
+
/// \return Status
|
442 |
+
Status AppendValues(const uint8_t* values, int64_t length,
|
443 |
+
const std::vector<bool>& is_valid);
|
444 |
+
|
445 |
+
/// \brief Append a sequence of elements in one shot
|
446 |
+
/// \param[in] values a std::vector of bytes
|
447 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
448 |
+
/// (0). Equal in length to values
|
449 |
+
/// \return Status
|
450 |
+
Status AppendValues(const std::vector<uint8_t>& values,
|
451 |
+
const std::vector<bool>& is_valid);
|
452 |
+
|
453 |
+
/// \brief Append a sequence of elements in one shot
|
454 |
+
/// \param[in] values a std::vector of bytes
|
455 |
+
/// \return Status
|
456 |
+
Status AppendValues(const std::vector<uint8_t>& values);
|
457 |
+
|
458 |
+
/// \brief Append a sequence of elements in one shot
|
459 |
+
/// \param[in] values an std::vector<bool> indicating true (1) or false
|
460 |
+
/// \param[in] is_valid an std::vector<bool> indicating valid (1) or null
|
461 |
+
/// (0). Equal in length to values
|
462 |
+
/// \return Status
|
463 |
+
Status AppendValues(const std::vector<bool>& values, const std::vector<bool>& is_valid);
|
464 |
+
|
465 |
+
/// \brief Append a sequence of elements in one shot
|
466 |
+
/// \param[in] values an std::vector<bool> indicating true (1) or false
|
467 |
+
/// \return Status
|
468 |
+
Status AppendValues(const std::vector<bool>& values);
|
469 |
+
|
470 |
+
/// \brief Append a sequence of elements in one shot
|
471 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
472 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
473 |
+
/// or null(0) values
|
474 |
+
/// \return Status
|
475 |
+
template <typename ValuesIter>
|
476 |
+
Status AppendValues(ValuesIter values_begin, ValuesIter values_end) {
|
477 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
478 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
479 |
+
data_builder_.UnsafeAppend<false>(
|
480 |
+
length, [&values_begin]() -> bool { return *values_begin++; });
|
481 |
+
// this updates length_
|
482 |
+
UnsafeSetNotNull(length);
|
483 |
+
return Status::OK();
|
484 |
+
}
|
485 |
+
|
486 |
+
/// \brief Append a sequence of elements in one shot, with a specified nullmap
|
487 |
+
/// \param[in] values_begin InputIterator to the beginning of the values
|
488 |
+
/// \param[in] values_end InputIterator pointing to the end of the values
|
489 |
+
/// \param[in] valid_begin InputIterator with elements indication valid(1)
|
490 |
+
/// or null(0) values
|
491 |
+
/// \return Status
|
492 |
+
template <typename ValuesIter, typename ValidIter>
|
493 |
+
enable_if_t<!std::is_pointer<ValidIter>::value, Status> AppendValues(
|
494 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
495 |
+
static_assert(!internal::is_null_pointer<ValidIter>::value,
|
496 |
+
"Don't pass a NULLPTR directly as valid_begin, use the 2-argument "
|
497 |
+
"version instead");
|
498 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
499 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
500 |
+
|
501 |
+
data_builder_.UnsafeAppend<false>(
|
502 |
+
length, [&values_begin]() -> bool { return *values_begin++; });
|
503 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
504 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
505 |
+
length_ = null_bitmap_builder_.length();
|
506 |
+
null_count_ = null_bitmap_builder_.false_count();
|
507 |
+
return Status::OK();
|
508 |
+
}
|
509 |
+
|
510 |
+
// Same as above, for a pointer type ValidIter
|
511 |
+
template <typename ValuesIter, typename ValidIter>
|
512 |
+
enable_if_t<std::is_pointer<ValidIter>::value, Status> AppendValues(
|
513 |
+
ValuesIter values_begin, ValuesIter values_end, ValidIter valid_begin) {
|
514 |
+
int64_t length = static_cast<int64_t>(std::distance(values_begin, values_end));
|
515 |
+
ARROW_RETURN_NOT_OK(Reserve(length));
|
516 |
+
data_builder_.UnsafeAppend<false>(
|
517 |
+
length, [&values_begin]() -> bool { return *values_begin++; });
|
518 |
+
|
519 |
+
if (valid_begin == NULLPTR) {
|
520 |
+
UnsafeSetNotNull(length);
|
521 |
+
} else {
|
522 |
+
null_bitmap_builder_.UnsafeAppend<true>(
|
523 |
+
length, [&valid_begin]() -> bool { return *valid_begin++; });
|
524 |
+
}
|
525 |
+
length_ = null_bitmap_builder_.length();
|
526 |
+
null_count_ = null_bitmap_builder_.false_count();
|
527 |
+
return Status::OK();
|
528 |
+
}
|
529 |
+
|
530 |
+
Status AppendValues(int64_t length, bool value);
|
531 |
+
|
532 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
533 |
+
int64_t length) override {
|
534 |
+
return AppendValues(array.GetValues<uint8_t>(1, 0), length,
|
535 |
+
array.GetValues<uint8_t>(0, 0), array.offset + offset);
|
536 |
+
}
|
537 |
+
|
538 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
539 |
+
|
540 |
+
/// \cond FALSE
|
541 |
+
using ArrayBuilder::Finish;
|
542 |
+
/// \endcond
|
543 |
+
|
544 |
+
Status Finish(std::shared_ptr<BooleanArray>* out) { return FinishTyped(out); }
|
545 |
+
|
546 |
+
void Reset() override;
|
547 |
+
Status Resize(int64_t capacity) override;
|
548 |
+
|
549 |
+
std::shared_ptr<DataType> type() const override { return boolean(); }
|
550 |
+
|
551 |
+
protected:
|
552 |
+
TypedBufferBuilder<bool> data_builder_;
|
553 |
+
};
|
554 |
+
|
555 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_time.h
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// Contains declarations of time related Arrow builder types.
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#include <memory>
|
23 |
+
|
24 |
+
#include "arrow/array/builder_base.h"
|
25 |
+
#include "arrow/array/builder_primitive.h"
|
26 |
+
|
27 |
+
namespace arrow {
|
28 |
+
|
29 |
+
/// \addtogroup temporal-builders
|
30 |
+
///
|
31 |
+
/// @{
|
32 |
+
|
33 |
+
// TODO(ARROW-7938): this class is untested
|
34 |
+
|
35 |
+
class ARROW_EXPORT DayTimeIntervalBuilder : public NumericBuilder<DayTimeIntervalType> {
|
36 |
+
public:
|
37 |
+
using DayMilliseconds = DayTimeIntervalType::DayMilliseconds;
|
38 |
+
|
39 |
+
explicit DayTimeIntervalBuilder(MemoryPool* pool = default_memory_pool(),
|
40 |
+
int64_t alignment = kDefaultBufferAlignment)
|
41 |
+
: DayTimeIntervalBuilder(day_time_interval(), pool, alignment) {}
|
42 |
+
|
43 |
+
explicit DayTimeIntervalBuilder(std::shared_ptr<DataType> type,
|
44 |
+
MemoryPool* pool = default_memory_pool(),
|
45 |
+
int64_t alignment = kDefaultBufferAlignment)
|
46 |
+
: NumericBuilder<DayTimeIntervalType>(type, pool, alignment) {}
|
47 |
+
};
|
48 |
+
|
49 |
+
class ARROW_EXPORT MonthDayNanoIntervalBuilder
|
50 |
+
: public NumericBuilder<MonthDayNanoIntervalType> {
|
51 |
+
public:
|
52 |
+
using MonthDayNanos = MonthDayNanoIntervalType::MonthDayNanos;
|
53 |
+
|
54 |
+
explicit MonthDayNanoIntervalBuilder(MemoryPool* pool = default_memory_pool(),
|
55 |
+
int64_t alignment = kDefaultBufferAlignment)
|
56 |
+
: MonthDayNanoIntervalBuilder(month_day_nano_interval(), pool, alignment) {}
|
57 |
+
|
58 |
+
explicit MonthDayNanoIntervalBuilder(std::shared_ptr<DataType> type,
|
59 |
+
MemoryPool* pool = default_memory_pool(),
|
60 |
+
int64_t alignment = kDefaultBufferAlignment)
|
61 |
+
: NumericBuilder<MonthDayNanoIntervalType>(type, pool, alignment) {}
|
62 |
+
};
|
63 |
+
|
64 |
+
/// @}
|
65 |
+
|
66 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/builder_union.h
ADDED
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <string>
|
23 |
+
#include <vector>
|
24 |
+
|
25 |
+
#include "arrow/array/array_nested.h"
|
26 |
+
#include "arrow/array/builder_base.h"
|
27 |
+
#include "arrow/array/data.h"
|
28 |
+
#include "arrow/buffer_builder.h"
|
29 |
+
#include "arrow/memory_pool.h"
|
30 |
+
#include "arrow/status.h"
|
31 |
+
#include "arrow/type.h"
|
32 |
+
#include "arrow/util/visibility.h"
|
33 |
+
|
34 |
+
namespace arrow {
|
35 |
+
|
36 |
+
/// \addtogroup nested-builders
|
37 |
+
///
|
38 |
+
/// @{
|
39 |
+
|
40 |
+
/// \brief Base class for union array builds.
|
41 |
+
///
|
42 |
+
/// Note that while we subclass ArrayBuilder, as union types do not have a
|
43 |
+
/// validity bitmap, the bitmap builder member of ArrayBuilder is not used.
|
44 |
+
class ARROW_EXPORT BasicUnionBuilder : public ArrayBuilder {
|
45 |
+
public:
|
46 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
47 |
+
|
48 |
+
/// \cond FALSE
|
49 |
+
using ArrayBuilder::Finish;
|
50 |
+
/// \endcond
|
51 |
+
|
52 |
+
Status Finish(std::shared_ptr<UnionArray>* out) { return FinishTyped(out); }
|
53 |
+
|
54 |
+
/// \brief Make a new child builder available to the UnionArray
|
55 |
+
///
|
56 |
+
/// \param[in] new_child the child builder
|
57 |
+
/// \param[in] field_name the name of the field in the union array type
|
58 |
+
/// if type inference is used
|
59 |
+
/// \return child index, which is the "type" argument that needs
|
60 |
+
/// to be passed to the "Append" method to add a new element to
|
61 |
+
/// the union array.
|
62 |
+
int8_t AppendChild(const std::shared_ptr<ArrayBuilder>& new_child,
|
63 |
+
const std::string& field_name = "");
|
64 |
+
|
65 |
+
std::shared_ptr<DataType> type() const override;
|
66 |
+
|
67 |
+
int64_t length() const override { return types_builder_.length(); }
|
68 |
+
|
69 |
+
protected:
|
70 |
+
BasicUnionBuilder(MemoryPool* pool, int64_t alignment,
|
71 |
+
const std::vector<std::shared_ptr<ArrayBuilder>>& children,
|
72 |
+
const std::shared_ptr<DataType>& type);
|
73 |
+
|
74 |
+
int8_t NextTypeId();
|
75 |
+
|
76 |
+
std::vector<std::shared_ptr<Field>> child_fields_;
|
77 |
+
std::vector<int8_t> type_codes_;
|
78 |
+
UnionMode::type mode_;
|
79 |
+
|
80 |
+
std::vector<ArrayBuilder*> type_id_to_children_;
|
81 |
+
std::vector<int> type_id_to_child_id_;
|
82 |
+
// for all type_id < dense_type_id_, type_id_to_children_[type_id] != nullptr
|
83 |
+
int8_t dense_type_id_ = 0;
|
84 |
+
TypedBufferBuilder<int8_t> types_builder_;
|
85 |
+
};
|
86 |
+
|
87 |
+
/// \class DenseUnionBuilder
|
88 |
+
///
|
89 |
+
/// This API is EXPERIMENTAL.
|
90 |
+
class ARROW_EXPORT DenseUnionBuilder : public BasicUnionBuilder {
|
91 |
+
public:
|
92 |
+
/// Use this constructor to initialize the UnionBuilder with no child builders,
|
93 |
+
/// allowing type to be inferred. You will need to call AppendChild for each of the
|
94 |
+
/// children builders you want to use.
|
95 |
+
explicit DenseUnionBuilder(MemoryPool* pool,
|
96 |
+
int64_t alignment = kDefaultBufferAlignment)
|
97 |
+
: BasicUnionBuilder(pool, alignment, {}, dense_union(FieldVector{})),
|
98 |
+
offsets_builder_(pool, alignment) {}
|
99 |
+
|
100 |
+
/// Use this constructor to specify the type explicitly.
|
101 |
+
/// You can still add child builders to the union after using this constructor
|
102 |
+
DenseUnionBuilder(MemoryPool* pool,
|
103 |
+
const std::vector<std::shared_ptr<ArrayBuilder>>& children,
|
104 |
+
const std::shared_ptr<DataType>& type,
|
105 |
+
int64_t alignment = kDefaultBufferAlignment)
|
106 |
+
: BasicUnionBuilder(pool, alignment, children, type),
|
107 |
+
offsets_builder_(pool, alignment) {}
|
108 |
+
|
109 |
+
Status AppendNull() final {
|
110 |
+
const int8_t first_child_code = type_codes_[0];
|
111 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
112 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(first_child_code));
|
113 |
+
ARROW_RETURN_NOT_OK(
|
114 |
+
offsets_builder_.Append(static_cast<int32_t>(child_builder->length())));
|
115 |
+
// Append a null arbitrarily to the first child
|
116 |
+
return child_builder->AppendNull();
|
117 |
+
}
|
118 |
+
|
119 |
+
Status AppendNulls(int64_t length) final {
|
120 |
+
const int8_t first_child_code = type_codes_[0];
|
121 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
122 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, first_child_code));
|
123 |
+
ARROW_RETURN_NOT_OK(
|
124 |
+
offsets_builder_.Append(length, static_cast<int32_t>(child_builder->length())));
|
125 |
+
// Append just a single null to the first child
|
126 |
+
return child_builder->AppendNull();
|
127 |
+
}
|
128 |
+
|
129 |
+
Status AppendEmptyValue() final {
|
130 |
+
const int8_t first_child_code = type_codes_[0];
|
131 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
132 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(first_child_code));
|
133 |
+
ARROW_RETURN_NOT_OK(
|
134 |
+
offsets_builder_.Append(static_cast<int32_t>(child_builder->length())));
|
135 |
+
// Append an empty value arbitrarily to the first child
|
136 |
+
return child_builder->AppendEmptyValue();
|
137 |
+
}
|
138 |
+
|
139 |
+
Status AppendEmptyValues(int64_t length) final {
|
140 |
+
const int8_t first_child_code = type_codes_[0];
|
141 |
+
ArrayBuilder* child_builder = type_id_to_children_[first_child_code];
|
142 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, first_child_code));
|
143 |
+
ARROW_RETURN_NOT_OK(
|
144 |
+
offsets_builder_.Append(length, static_cast<int32_t>(child_builder->length())));
|
145 |
+
// Append just a single empty value to the first child
|
146 |
+
return child_builder->AppendEmptyValue();
|
147 |
+
}
|
148 |
+
|
149 |
+
/// \brief Append an element to the UnionArray. This must be followed
|
150 |
+
/// by an append to the appropriate child builder.
|
151 |
+
///
|
152 |
+
/// \param[in] next_type type_id of the child to which the next value will be appended.
|
153 |
+
///
|
154 |
+
/// The corresponding child builder must be appended to independently after this method
|
155 |
+
/// is called.
|
156 |
+
Status Append(int8_t next_type) {
|
157 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(next_type));
|
158 |
+
if (type_id_to_children_[next_type]->length() == kListMaximumElements) {
|
159 |
+
return Status::CapacityError(
|
160 |
+
"a dense UnionArray cannot contain more than 2^31 - 1 elements from a single "
|
161 |
+
"child");
|
162 |
+
}
|
163 |
+
auto offset = static_cast<int32_t>(type_id_to_children_[next_type]->length());
|
164 |
+
return offsets_builder_.Append(offset);
|
165 |
+
}
|
166 |
+
|
167 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
168 |
+
int64_t length) override;
|
169 |
+
|
170 |
+
Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
|
171 |
+
|
172 |
+
private:
|
173 |
+
TypedBufferBuilder<int32_t> offsets_builder_;
|
174 |
+
};
|
175 |
+
|
176 |
+
/// \class SparseUnionBuilder
|
177 |
+
///
|
178 |
+
/// This API is EXPERIMENTAL.
|
179 |
+
class ARROW_EXPORT SparseUnionBuilder : public BasicUnionBuilder {
|
180 |
+
public:
|
181 |
+
/// Use this constructor to initialize the UnionBuilder with no child builders,
|
182 |
+
/// allowing type to be inferred. You will need to call AppendChild for each of the
|
183 |
+
/// children builders you want to use.
|
184 |
+
explicit SparseUnionBuilder(MemoryPool* pool,
|
185 |
+
int64_t alignment = kDefaultBufferAlignment)
|
186 |
+
: BasicUnionBuilder(pool, alignment, {}, sparse_union(FieldVector{})) {}
|
187 |
+
|
188 |
+
/// Use this constructor to specify the type explicitly.
|
189 |
+
/// You can still add child builders to the union after using this constructor
|
190 |
+
SparseUnionBuilder(MemoryPool* pool,
|
191 |
+
const std::vector<std::shared_ptr<ArrayBuilder>>& children,
|
192 |
+
const std::shared_ptr<DataType>& type,
|
193 |
+
int64_t alignment = kDefaultBufferAlignment)
|
194 |
+
: BasicUnionBuilder(pool, alignment, children, type) {}
|
195 |
+
|
196 |
+
/// \brief Append a null value.
|
197 |
+
///
|
198 |
+
/// A null is appended to the first child, empty values to the other children.
|
199 |
+
Status AppendNull() final {
|
200 |
+
const auto first_child_code = type_codes_[0];
|
201 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(first_child_code));
|
202 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[first_child_code]->AppendNull());
|
203 |
+
for (int i = 1; i < static_cast<int>(type_codes_.size()); ++i) {
|
204 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[type_codes_[i]]->AppendEmptyValue());
|
205 |
+
}
|
206 |
+
return Status::OK();
|
207 |
+
}
|
208 |
+
|
209 |
+
/// \brief Append multiple null values.
|
210 |
+
///
|
211 |
+
/// Nulls are appended to the first child, empty values to the other children.
|
212 |
+
Status AppendNulls(int64_t length) final {
|
213 |
+
const auto first_child_code = type_codes_[0];
|
214 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, first_child_code));
|
215 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[first_child_code]->AppendNulls(length));
|
216 |
+
for (int i = 1; i < static_cast<int>(type_codes_.size()); ++i) {
|
217 |
+
ARROW_RETURN_NOT_OK(
|
218 |
+
type_id_to_children_[type_codes_[i]]->AppendEmptyValues(length));
|
219 |
+
}
|
220 |
+
return Status::OK();
|
221 |
+
}
|
222 |
+
|
223 |
+
Status AppendEmptyValue() final {
|
224 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(type_codes_[0]));
|
225 |
+
for (int8_t code : type_codes_) {
|
226 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[code]->AppendEmptyValue());
|
227 |
+
}
|
228 |
+
return Status::OK();
|
229 |
+
}
|
230 |
+
|
231 |
+
Status AppendEmptyValues(int64_t length) final {
|
232 |
+
ARROW_RETURN_NOT_OK(types_builder_.Append(length, type_codes_[0]));
|
233 |
+
for (int8_t code : type_codes_) {
|
234 |
+
ARROW_RETURN_NOT_OK(type_id_to_children_[code]->AppendEmptyValues(length));
|
235 |
+
}
|
236 |
+
return Status::OK();
|
237 |
+
}
|
238 |
+
|
239 |
+
/// \brief Append an element to the UnionArray. This must be followed
|
240 |
+
/// by an append to the appropriate child builder.
|
241 |
+
///
|
242 |
+
/// \param[in] next_type type_id of the child to which the next value will be appended.
|
243 |
+
///
|
244 |
+
/// The corresponding child builder must be appended to independently after this method
|
245 |
+
/// is called, and all other child builders must have null or empty value appended.
|
246 |
+
Status Append(int8_t next_type) { return types_builder_.Append(next_type); }
|
247 |
+
|
248 |
+
Status AppendArraySlice(const ArraySpan& array, int64_t offset,
|
249 |
+
int64_t length) override;
|
250 |
+
};
|
251 |
+
|
252 |
+
/// @}
|
253 |
+
|
254 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/data.h
ADDED
@@ -0,0 +1,634 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <atomic> // IWYU pragma: export
|
21 |
+
#include <cassert>
|
22 |
+
#include <cstdint>
|
23 |
+
#include <memory>
|
24 |
+
#include <utility>
|
25 |
+
#include <vector>
|
26 |
+
|
27 |
+
#include "arrow/buffer.h"
|
28 |
+
#include "arrow/result.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/type_fwd.h"
|
31 |
+
#include "arrow/util/bit_util.h"
|
32 |
+
#include "arrow/util/macros.h"
|
33 |
+
#include "arrow/util/span.h"
|
34 |
+
#include "arrow/util/visibility.h"
|
35 |
+
|
36 |
+
namespace arrow {
|
37 |
+
|
38 |
+
namespace internal {
|
39 |
+
// ----------------------------------------------------------------------
|
40 |
+
// Null handling for types without a validity bitmap and the dictionary type
|
41 |
+
|
42 |
+
ARROW_EXPORT bool IsNullSparseUnion(const ArrayData& data, int64_t i);
|
43 |
+
ARROW_EXPORT bool IsNullDenseUnion(const ArrayData& data, int64_t i);
|
44 |
+
ARROW_EXPORT bool IsNullRunEndEncoded(const ArrayData& data, int64_t i);
|
45 |
+
|
46 |
+
ARROW_EXPORT bool UnionMayHaveLogicalNulls(const ArrayData& data);
|
47 |
+
ARROW_EXPORT bool RunEndEncodedMayHaveLogicalNulls(const ArrayData& data);
|
48 |
+
ARROW_EXPORT bool DictionaryMayHaveLogicalNulls(const ArrayData& data);
|
49 |
+
} // namespace internal
|
50 |
+
|
51 |
+
// When slicing, we do not know the null count of the sliced range without
|
52 |
+
// doing some computation. To avoid doing this eagerly, we set the null count
|
53 |
+
// to -1 (any negative number will do). When Array::null_count is called the
|
54 |
+
// first time, the null count will be computed. See ARROW-33
|
55 |
+
constexpr int64_t kUnknownNullCount = -1;
|
56 |
+
|
57 |
+
// ----------------------------------------------------------------------
|
58 |
+
// Generic array data container
|
59 |
+
|
60 |
+
/// \class ArrayData
|
61 |
+
/// \brief Mutable container for generic Arrow array data
|
62 |
+
///
|
63 |
+
/// This data structure is a self-contained representation of the memory and
|
64 |
+
/// metadata inside an Arrow array data structure (called vectors in Java). The
|
65 |
+
/// classes arrow::Array and its subclasses provide strongly-typed accessors
|
66 |
+
/// with support for the visitor pattern and other affordances.
|
67 |
+
///
|
68 |
+
/// This class is designed for easy internal data manipulation, analytical data
|
69 |
+
/// processing, and data transport to and from IPC messages. For example, we
|
70 |
+
/// could cast from int64 to float64 like so:
|
71 |
+
///
|
72 |
+
/// Int64Array arr = GetMyData();
|
73 |
+
/// auto new_data = arr.data()->Copy();
|
74 |
+
/// new_data->type = arrow::float64();
|
75 |
+
/// DoubleArray double_arr(new_data);
|
76 |
+
///
|
77 |
+
/// This object is also useful in an analytics setting where memory may be
|
78 |
+
/// reused. For example, if we had a group of operations all returning doubles,
|
79 |
+
/// say:
|
80 |
+
///
|
81 |
+
/// Log(Sqrt(Expr(arr)))
|
82 |
+
///
|
83 |
+
/// Then the low-level implementations of each of these functions could have
|
84 |
+
/// the signatures
|
85 |
+
///
|
86 |
+
/// void Log(const ArrayData& values, ArrayData* out);
|
87 |
+
///
|
88 |
+
/// As another example a function may consume one or more memory buffers in an
|
89 |
+
/// input array and replace them with newly-allocated data, changing the output
|
90 |
+
/// data type as well.
|
91 |
+
struct ARROW_EXPORT ArrayData {
|
92 |
+
ArrayData() = default;
|
93 |
+
|
94 |
+
ArrayData(std::shared_ptr<DataType> type, int64_t length,
|
95 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
96 |
+
: type(std::move(type)), length(length), null_count(null_count), offset(offset) {}
|
97 |
+
|
98 |
+
ArrayData(std::shared_ptr<DataType> type, int64_t length,
|
99 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
100 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
101 |
+
: ArrayData(std::move(type), length, null_count, offset) {
|
102 |
+
this->buffers = std::move(buffers);
|
103 |
+
}
|
104 |
+
|
105 |
+
ArrayData(std::shared_ptr<DataType> type, int64_t length,
|
106 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
107 |
+
std::vector<std::shared_ptr<ArrayData>> child_data,
|
108 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0)
|
109 |
+
: ArrayData(std::move(type), length, null_count, offset) {
|
110 |
+
this->buffers = std::move(buffers);
|
111 |
+
this->child_data = std::move(child_data);
|
112 |
+
}
|
113 |
+
|
114 |
+
static std::shared_ptr<ArrayData> Make(std::shared_ptr<DataType> type, int64_t length,
|
115 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
116 |
+
int64_t null_count = kUnknownNullCount,
|
117 |
+
int64_t offset = 0);
|
118 |
+
|
119 |
+
static std::shared_ptr<ArrayData> Make(
|
120 |
+
std::shared_ptr<DataType> type, int64_t length,
|
121 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
122 |
+
std::vector<std::shared_ptr<ArrayData>> child_data,
|
123 |
+
int64_t null_count = kUnknownNullCount, int64_t offset = 0);
|
124 |
+
|
125 |
+
static std::shared_ptr<ArrayData> Make(
|
126 |
+
std::shared_ptr<DataType> type, int64_t length,
|
127 |
+
std::vector<std::shared_ptr<Buffer>> buffers,
|
128 |
+
std::vector<std::shared_ptr<ArrayData>> child_data,
|
129 |
+
std::shared_ptr<ArrayData> dictionary, int64_t null_count = kUnknownNullCount,
|
130 |
+
int64_t offset = 0);
|
131 |
+
|
132 |
+
static std::shared_ptr<ArrayData> Make(std::shared_ptr<DataType> type, int64_t length,
|
133 |
+
int64_t null_count = kUnknownNullCount,
|
134 |
+
int64_t offset = 0);
|
135 |
+
|
136 |
+
// Move constructor
|
137 |
+
ArrayData(ArrayData&& other) noexcept
|
138 |
+
: type(std::move(other.type)),
|
139 |
+
length(other.length),
|
140 |
+
offset(other.offset),
|
141 |
+
buffers(std::move(other.buffers)),
|
142 |
+
child_data(std::move(other.child_data)),
|
143 |
+
dictionary(std::move(other.dictionary)) {
|
144 |
+
SetNullCount(other.null_count);
|
145 |
+
}
|
146 |
+
|
147 |
+
// Copy constructor
|
148 |
+
ArrayData(const ArrayData& other) noexcept
|
149 |
+
: type(other.type),
|
150 |
+
length(other.length),
|
151 |
+
offset(other.offset),
|
152 |
+
buffers(other.buffers),
|
153 |
+
child_data(other.child_data),
|
154 |
+
dictionary(other.dictionary) {
|
155 |
+
SetNullCount(other.null_count);
|
156 |
+
}
|
157 |
+
|
158 |
+
// Move assignment
|
159 |
+
ArrayData& operator=(ArrayData&& other) {
|
160 |
+
type = std::move(other.type);
|
161 |
+
length = other.length;
|
162 |
+
SetNullCount(other.null_count);
|
163 |
+
offset = other.offset;
|
164 |
+
buffers = std::move(other.buffers);
|
165 |
+
child_data = std::move(other.child_data);
|
166 |
+
dictionary = std::move(other.dictionary);
|
167 |
+
return *this;
|
168 |
+
}
|
169 |
+
|
170 |
+
// Copy assignment
|
171 |
+
ArrayData& operator=(const ArrayData& other) {
|
172 |
+
type = other.type;
|
173 |
+
length = other.length;
|
174 |
+
SetNullCount(other.null_count);
|
175 |
+
offset = other.offset;
|
176 |
+
buffers = other.buffers;
|
177 |
+
child_data = other.child_data;
|
178 |
+
dictionary = other.dictionary;
|
179 |
+
return *this;
|
180 |
+
}
|
181 |
+
|
182 |
+
std::shared_ptr<ArrayData> Copy() const { return std::make_shared<ArrayData>(*this); }
|
183 |
+
|
184 |
+
/// \brief Copy all buffers and children recursively to destination MemoryManager
|
185 |
+
///
|
186 |
+
/// This utilizes MemoryManager::CopyBuffer to create a new ArrayData object
|
187 |
+
/// recursively copying the buffers and all child buffers to the destination
|
188 |
+
/// memory manager. This includes dictionaries if applicable.
|
189 |
+
Result<std::shared_ptr<ArrayData>> CopyTo(
|
190 |
+
const std::shared_ptr<MemoryManager>& to) const;
|
191 |
+
/// \brief View or Copy this ArrayData to destination memory manager.
|
192 |
+
///
|
193 |
+
/// Tries to view the buffer contents on the given memory manager's device
|
194 |
+
/// if possible (to avoid a copy) but falls back to copying if a no-copy view
|
195 |
+
/// isn't supported.
|
196 |
+
Result<std::shared_ptr<ArrayData>> ViewOrCopyTo(
|
197 |
+
const std::shared_ptr<MemoryManager>& to) const;
|
198 |
+
|
199 |
+
bool IsNull(int64_t i) const { return !IsValid(i); }
|
200 |
+
|
201 |
+
bool IsValid(int64_t i) const {
|
202 |
+
if (buffers[0] != NULLPTR) {
|
203 |
+
return bit_util::GetBit(buffers[0]->data(), i + offset);
|
204 |
+
}
|
205 |
+
const auto type = this->type->id();
|
206 |
+
if (type == Type::SPARSE_UNION) {
|
207 |
+
return !internal::IsNullSparseUnion(*this, i);
|
208 |
+
}
|
209 |
+
if (type == Type::DENSE_UNION) {
|
210 |
+
return !internal::IsNullDenseUnion(*this, i);
|
211 |
+
}
|
212 |
+
if (type == Type::RUN_END_ENCODED) {
|
213 |
+
return !internal::IsNullRunEndEncoded(*this, i);
|
214 |
+
}
|
215 |
+
return null_count.load() != length;
|
216 |
+
}
|
217 |
+
|
218 |
+
// Access a buffer's data as a typed C pointer
|
219 |
+
template <typename T>
|
220 |
+
inline const T* GetValues(int i, int64_t absolute_offset) const {
|
221 |
+
if (buffers[i]) {
|
222 |
+
return reinterpret_cast<const T*>(buffers[i]->data()) + absolute_offset;
|
223 |
+
} else {
|
224 |
+
return NULLPTR;
|
225 |
+
}
|
226 |
+
}
|
227 |
+
|
228 |
+
template <typename T>
|
229 |
+
inline const T* GetValues(int i) const {
|
230 |
+
return GetValues<T>(i, offset);
|
231 |
+
}
|
232 |
+
|
233 |
+
// Like GetValues, but returns NULLPTR instead of aborting if the underlying
|
234 |
+
// buffer is not a CPU buffer.
|
235 |
+
template <typename T>
|
236 |
+
inline const T* GetValuesSafe(int i, int64_t absolute_offset) const {
|
237 |
+
if (buffers[i] && buffers[i]->is_cpu()) {
|
238 |
+
return reinterpret_cast<const T*>(buffers[i]->data()) + absolute_offset;
|
239 |
+
} else {
|
240 |
+
return NULLPTR;
|
241 |
+
}
|
242 |
+
}
|
243 |
+
|
244 |
+
template <typename T>
|
245 |
+
inline const T* GetValuesSafe(int i) const {
|
246 |
+
return GetValuesSafe<T>(i, offset);
|
247 |
+
}
|
248 |
+
|
249 |
+
// Access a buffer's data as a typed C pointer
|
250 |
+
template <typename T>
|
251 |
+
inline T* GetMutableValues(int i, int64_t absolute_offset) {
|
252 |
+
if (buffers[i]) {
|
253 |
+
return reinterpret_cast<T*>(buffers[i]->mutable_data()) + absolute_offset;
|
254 |
+
} else {
|
255 |
+
return NULLPTR;
|
256 |
+
}
|
257 |
+
}
|
258 |
+
|
259 |
+
template <typename T>
|
260 |
+
inline T* GetMutableValues(int i) {
|
261 |
+
return GetMutableValues<T>(i, offset);
|
262 |
+
}
|
263 |
+
|
264 |
+
/// \brief Construct a zero-copy slice of the data with the given offset and length
|
265 |
+
std::shared_ptr<ArrayData> Slice(int64_t offset, int64_t length) const;
|
266 |
+
|
267 |
+
/// \brief Input-checking variant of Slice
|
268 |
+
///
|
269 |
+
/// An Invalid Status is returned if the requested slice falls out of bounds.
|
270 |
+
/// Note that unlike Slice, `length` isn't clamped to the available buffer size.
|
271 |
+
Result<std::shared_ptr<ArrayData>> SliceSafe(int64_t offset, int64_t length) const;
|
272 |
+
|
273 |
+
void SetNullCount(int64_t v) { null_count.store(v); }
|
274 |
+
|
275 |
+
/// \brief Return physical null count, or compute and set it if it's not known
|
276 |
+
int64_t GetNullCount() const;
|
277 |
+
|
278 |
+
/// \brief Return true if the data has a validity bitmap and the physical null
|
279 |
+
/// count is known to be non-zero or not yet known.
|
280 |
+
///
|
281 |
+
/// Note that this is not the same as MayHaveLogicalNulls, which also checks
|
282 |
+
/// for the presence of nulls in child data for types like unions and run-end
|
283 |
+
/// encoded types.
|
284 |
+
///
|
285 |
+
/// \see HasValidityBitmap
|
286 |
+
/// \see MayHaveLogicalNulls
|
287 |
+
bool MayHaveNulls() const {
|
288 |
+
// If an ArrayData is slightly malformed it may have kUnknownNullCount set
|
289 |
+
// but no buffer
|
290 |
+
return null_count.load() != 0 && buffers[0] != NULLPTR;
|
291 |
+
}
|
292 |
+
|
293 |
+
/// \brief Return true if the data has a validity bitmap
|
294 |
+
bool HasValidityBitmap() const { return buffers[0] != NULLPTR; }
|
295 |
+
|
296 |
+
/// \brief Return true if the validity bitmap may have 0's in it, or if the
|
297 |
+
/// child arrays (in the case of types without a validity bitmap) may have
|
298 |
+
/// nulls, or if the dictionary of dictionay array may have nulls.
|
299 |
+
///
|
300 |
+
/// This is not a drop-in replacement for MayHaveNulls, as historically
|
301 |
+
/// MayHaveNulls() has been used to check for the presence of a validity
|
302 |
+
/// bitmap that needs to be checked.
|
303 |
+
///
|
304 |
+
/// Code that previously used MayHaveNulls() and then dealt with the validity
|
305 |
+
/// bitmap directly can be fixed to handle all types correctly without
|
306 |
+
/// performance degradation when handling most types by adopting
|
307 |
+
/// HasValidityBitmap and MayHaveLogicalNulls.
|
308 |
+
///
|
309 |
+
/// Before:
|
310 |
+
///
|
311 |
+
/// uint8_t* validity = array.MayHaveNulls() ? array.buffers[0].data : NULLPTR;
|
312 |
+
/// for (int64_t i = 0; i < array.length; ++i) {
|
313 |
+
/// if (validity && !bit_util::GetBit(validity, i)) {
|
314 |
+
/// continue; // skip a NULL
|
315 |
+
/// }
|
316 |
+
/// ...
|
317 |
+
/// }
|
318 |
+
///
|
319 |
+
/// After:
|
320 |
+
///
|
321 |
+
/// bool all_valid = !array.MayHaveLogicalNulls();
|
322 |
+
/// uint8_t* validity = array.HasValidityBitmap() ? array.buffers[0].data : NULLPTR;
|
323 |
+
/// for (int64_t i = 0; i < array.length; ++i) {
|
324 |
+
/// bool is_valid = all_valid ||
|
325 |
+
/// (validity && bit_util::GetBit(validity, i)) ||
|
326 |
+
/// array.IsValid(i);
|
327 |
+
/// if (!is_valid) {
|
328 |
+
/// continue; // skip a NULL
|
329 |
+
/// }
|
330 |
+
/// ...
|
331 |
+
/// }
|
332 |
+
bool MayHaveLogicalNulls() const {
|
333 |
+
if (buffers[0] != NULLPTR) {
|
334 |
+
return null_count.load() != 0;
|
335 |
+
}
|
336 |
+
const auto t = type->id();
|
337 |
+
if (t == Type::SPARSE_UNION || t == Type::DENSE_UNION) {
|
338 |
+
return internal::UnionMayHaveLogicalNulls(*this);
|
339 |
+
}
|
340 |
+
if (t == Type::RUN_END_ENCODED) {
|
341 |
+
return internal::RunEndEncodedMayHaveLogicalNulls(*this);
|
342 |
+
}
|
343 |
+
if (t == Type::DICTIONARY) {
|
344 |
+
return internal::DictionaryMayHaveLogicalNulls(*this);
|
345 |
+
}
|
346 |
+
return null_count.load() != 0;
|
347 |
+
}
|
348 |
+
|
349 |
+
/// \brief Computes the logical null count for arrays of all types including
|
350 |
+
/// those that do not have a validity bitmap like union and run-end encoded
|
351 |
+
/// arrays
|
352 |
+
///
|
353 |
+
/// If the array has a validity bitmap, this function behaves the same as
|
354 |
+
/// GetNullCount. For types that have no validity bitmap, this function will
|
355 |
+
/// recompute the null count every time it is called.
|
356 |
+
///
|
357 |
+
/// \see GetNullCount
|
358 |
+
int64_t ComputeLogicalNullCount() const;
|
359 |
+
|
360 |
+
std::shared_ptr<DataType> type;
|
361 |
+
int64_t length = 0;
|
362 |
+
mutable std::atomic<int64_t> null_count{0};
|
363 |
+
// The logical start point into the physical buffers (in values, not bytes).
|
364 |
+
// Note that, for child data, this must be *added* to the child data's own offset.
|
365 |
+
int64_t offset = 0;
|
366 |
+
std::vector<std::shared_ptr<Buffer>> buffers;
|
367 |
+
std::vector<std::shared_ptr<ArrayData>> child_data;
|
368 |
+
|
369 |
+
// The dictionary for this Array, if any. Only used for dictionary type
|
370 |
+
std::shared_ptr<ArrayData> dictionary;
|
371 |
+
};
|
372 |
+
|
373 |
+
/// \brief A non-owning Buffer reference
|
374 |
+
struct ARROW_EXPORT BufferSpan {
|
375 |
+
// It is the user of this class's responsibility to ensure that
|
376 |
+
// buffers that were const originally are not written to
|
377 |
+
// accidentally.
|
378 |
+
uint8_t* data = NULLPTR;
|
379 |
+
int64_t size = 0;
|
380 |
+
// Pointer back to buffer that owns this memory
|
381 |
+
const std::shared_ptr<Buffer>* owner = NULLPTR;
|
382 |
+
|
383 |
+
template <typename T>
|
384 |
+
const T* data_as() const {
|
385 |
+
return reinterpret_cast<const T*>(data);
|
386 |
+
}
|
387 |
+
template <typename T>
|
388 |
+
T* mutable_data_as() {
|
389 |
+
return reinterpret_cast<T*>(data);
|
390 |
+
}
|
391 |
+
};
|
392 |
+
|
393 |
+
/// \brief EXPERIMENTAL: A non-owning ArrayData reference that is cheaply
|
394 |
+
/// copyable and does not contain any shared_ptr objects. Do not use in public
|
395 |
+
/// APIs aside from compute kernels for now
|
396 |
+
struct ARROW_EXPORT ArraySpan {
|
397 |
+
const DataType* type = NULLPTR;
|
398 |
+
int64_t length = 0;
|
399 |
+
mutable int64_t null_count = kUnknownNullCount;
|
400 |
+
int64_t offset = 0;
|
401 |
+
BufferSpan buffers[3];
|
402 |
+
|
403 |
+
ArraySpan() = default;
|
404 |
+
|
405 |
+
explicit ArraySpan(const DataType* type, int64_t length) : type(type), length(length) {}
|
406 |
+
|
407 |
+
ArraySpan(const ArrayData& data) { // NOLINT implicit conversion
|
408 |
+
SetMembers(data);
|
409 |
+
}
|
410 |
+
explicit ArraySpan(const Scalar& data) { FillFromScalar(data); }
|
411 |
+
|
412 |
+
/// If dictionary-encoded, put dictionary in the first entry
|
413 |
+
std::vector<ArraySpan> child_data;
|
414 |
+
|
415 |
+
/// \brief Populate ArraySpan to look like an array of length 1 pointing at
|
416 |
+
/// the data members of a Scalar value
|
417 |
+
void FillFromScalar(const Scalar& value);
|
418 |
+
|
419 |
+
void SetMembers(const ArrayData& data);
|
420 |
+
|
421 |
+
void SetBuffer(int index, const std::shared_ptr<Buffer>& buffer) {
|
422 |
+
this->buffers[index].data = const_cast<uint8_t*>(buffer->data());
|
423 |
+
this->buffers[index].size = buffer->size();
|
424 |
+
this->buffers[index].owner = &buffer;
|
425 |
+
}
|
426 |
+
|
427 |
+
const ArraySpan& dictionary() const { return child_data[0]; }
|
428 |
+
|
429 |
+
/// \brief Return the number of buffers (out of 3) that are used to
|
430 |
+
/// constitute this array
|
431 |
+
int num_buffers() const;
|
432 |
+
|
433 |
+
// Access a buffer's data as a typed C pointer
|
434 |
+
template <typename T>
|
435 |
+
inline T* GetValues(int i, int64_t absolute_offset) {
|
436 |
+
return reinterpret_cast<T*>(buffers[i].data) + absolute_offset;
|
437 |
+
}
|
438 |
+
|
439 |
+
template <typename T>
|
440 |
+
inline T* GetValues(int i) {
|
441 |
+
return GetValues<T>(i, this->offset);
|
442 |
+
}
|
443 |
+
|
444 |
+
// Access a buffer's data as a typed C pointer
|
445 |
+
template <typename T>
|
446 |
+
inline const T* GetValues(int i, int64_t absolute_offset) const {
|
447 |
+
return reinterpret_cast<const T*>(buffers[i].data) + absolute_offset;
|
448 |
+
}
|
449 |
+
|
450 |
+
template <typename T>
|
451 |
+
inline const T* GetValues(int i) const {
|
452 |
+
return GetValues<T>(i, this->offset);
|
453 |
+
}
|
454 |
+
|
455 |
+
/// \brief Access a buffer's data as a span
|
456 |
+
///
|
457 |
+
/// \param i The buffer index
|
458 |
+
/// \param length The required length (in number of typed values) of the requested span
|
459 |
+
/// \pre i > 0
|
460 |
+
/// \pre length <= the length of the buffer (in number of values) that's expected for
|
461 |
+
/// this array type
|
462 |
+
/// \return A span<const T> of the requested length
|
463 |
+
template <typename T>
|
464 |
+
util::span<const T> GetSpan(int i, int64_t length) const {
|
465 |
+
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
|
466 |
+
assert(i > 0 && length + offset <= buffer_length);
|
467 |
+
ARROW_UNUSED(buffer_length);
|
468 |
+
return util::span<const T>(buffers[i].data_as<T>() + this->offset, length);
|
469 |
+
}
|
470 |
+
|
471 |
+
/// \brief Access a buffer's data as a span
|
472 |
+
///
|
473 |
+
/// \param i The buffer index
|
474 |
+
/// \param length The required length (in number of typed values) of the requested span
|
475 |
+
/// \pre i > 0
|
476 |
+
/// \pre length <= the length of the buffer (in number of values) that's expected for
|
477 |
+
/// this array type
|
478 |
+
/// \return A span<T> of the requested length
|
479 |
+
template <typename T>
|
480 |
+
util::span<T> GetSpan(int i, int64_t length) {
|
481 |
+
const int64_t buffer_length = buffers[i].size / static_cast<int64_t>(sizeof(T));
|
482 |
+
assert(i > 0 && length + offset <= buffer_length);
|
483 |
+
ARROW_UNUSED(buffer_length);
|
484 |
+
return util::span<T>(buffers[i].mutable_data_as<T>() + this->offset, length);
|
485 |
+
}
|
486 |
+
|
487 |
+
inline bool IsNull(int64_t i) const { return !IsValid(i); }
|
488 |
+
|
489 |
+
inline bool IsValid(int64_t i) const {
|
490 |
+
if (this->buffers[0].data != NULLPTR) {
|
491 |
+
return bit_util::GetBit(this->buffers[0].data, i + this->offset);
|
492 |
+
} else {
|
493 |
+
const auto type = this->type->id();
|
494 |
+
if (type == Type::SPARSE_UNION) {
|
495 |
+
return !IsNullSparseUnion(i);
|
496 |
+
}
|
497 |
+
if (type == Type::DENSE_UNION) {
|
498 |
+
return !IsNullDenseUnion(i);
|
499 |
+
}
|
500 |
+
if (type == Type::RUN_END_ENCODED) {
|
501 |
+
return !IsNullRunEndEncoded(i);
|
502 |
+
}
|
503 |
+
return this->null_count != this->length;
|
504 |
+
}
|
505 |
+
}
|
506 |
+
|
507 |
+
std::shared_ptr<ArrayData> ToArrayData() const;
|
508 |
+
|
509 |
+
std::shared_ptr<Array> ToArray() const;
|
510 |
+
|
511 |
+
std::shared_ptr<Buffer> GetBuffer(int index) const {
|
512 |
+
const BufferSpan& buf = this->buffers[index];
|
513 |
+
if (buf.owner) {
|
514 |
+
return *buf.owner;
|
515 |
+
} else if (buf.data != NULLPTR) {
|
516 |
+
// Buffer points to some memory without an owning buffer
|
517 |
+
return std::make_shared<Buffer>(buf.data, buf.size);
|
518 |
+
} else {
|
519 |
+
return NULLPTR;
|
520 |
+
}
|
521 |
+
}
|
522 |
+
|
523 |
+
void SetSlice(int64_t offset, int64_t length) {
|
524 |
+
this->offset = offset;
|
525 |
+
this->length = length;
|
526 |
+
if (this->type->id() == Type::NA) {
|
527 |
+
this->null_count = this->length;
|
528 |
+
} else if (this->MayHaveNulls()) {
|
529 |
+
this->null_count = kUnknownNullCount;
|
530 |
+
} else {
|
531 |
+
this->null_count = 0;
|
532 |
+
}
|
533 |
+
}
|
534 |
+
|
535 |
+
/// \brief Return physical null count, or compute and set it if it's not known
|
536 |
+
int64_t GetNullCount() const;
|
537 |
+
|
538 |
+
/// \brief Return true if the array has a validity bitmap and the physical null
|
539 |
+
/// count is known to be non-zero or not yet known
|
540 |
+
///
|
541 |
+
/// Note that this is not the same as MayHaveLogicalNulls, which also checks
|
542 |
+
/// for the presence of nulls in child data for types like unions and run-end
|
543 |
+
/// encoded types.
|
544 |
+
///
|
545 |
+
/// \see HasValidityBitmap
|
546 |
+
/// \see MayHaveLogicalNulls
|
547 |
+
bool MayHaveNulls() const {
|
548 |
+
// If an ArrayData is slightly malformed it may have kUnknownNullCount set
|
549 |
+
// but no buffer
|
550 |
+
return null_count != 0 && buffers[0].data != NULLPTR;
|
551 |
+
}
|
552 |
+
|
553 |
+
/// \brief Return true if the array has a validity bitmap
|
554 |
+
bool HasValidityBitmap() const { return buffers[0].data != NULLPTR; }
|
555 |
+
|
556 |
+
/// \brief Return true if the validity bitmap may have 0's in it, or if the
|
557 |
+
/// child arrays (in the case of types without a validity bitmap) may have
|
558 |
+
/// nulls, or if the dictionary of dictionay array may have nulls.
|
559 |
+
///
|
560 |
+
/// \see ArrayData::MayHaveLogicalNulls
|
561 |
+
bool MayHaveLogicalNulls() const {
|
562 |
+
if (buffers[0].data != NULLPTR) {
|
563 |
+
return null_count != 0;
|
564 |
+
}
|
565 |
+
const auto t = type->id();
|
566 |
+
if (t == Type::SPARSE_UNION || t == Type::DENSE_UNION) {
|
567 |
+
return UnionMayHaveLogicalNulls();
|
568 |
+
}
|
569 |
+
if (t == Type::RUN_END_ENCODED) {
|
570 |
+
return RunEndEncodedMayHaveLogicalNulls();
|
571 |
+
}
|
572 |
+
if (t == Type::DICTIONARY) {
|
573 |
+
return DictionaryMayHaveLogicalNulls();
|
574 |
+
}
|
575 |
+
return null_count != 0;
|
576 |
+
}
|
577 |
+
|
578 |
+
/// \brief Compute the logical null count for arrays of all types including
|
579 |
+
/// those that do not have a validity bitmap like union and run-end encoded
|
580 |
+
/// arrays
|
581 |
+
///
|
582 |
+
/// If the array has a validity bitmap, this function behaves the same as
|
583 |
+
/// GetNullCount. For types that have no validity bitmap, this function will
|
584 |
+
/// recompute the logical null count every time it is called.
|
585 |
+
///
|
586 |
+
/// \see GetNullCount
|
587 |
+
int64_t ComputeLogicalNullCount() const;
|
588 |
+
|
589 |
+
/// Some DataTypes (StringView, BinaryView) may have an arbitrary number of variadic
|
590 |
+
/// buffers. Since ArraySpan only has 3 buffers, we pack the variadic buffers into
|
591 |
+
/// buffers[2]; IE buffers[2].data points to the first shared_ptr<Buffer> of the
|
592 |
+
/// variadic set and buffers[2].size is the number of variadic buffers times
|
593 |
+
/// sizeof(shared_ptr<Buffer>).
|
594 |
+
///
|
595 |
+
/// \see HasVariadicBuffers
|
596 |
+
util::span<const std::shared_ptr<Buffer>> GetVariadicBuffers() const;
|
597 |
+
bool HasVariadicBuffers() const;
|
598 |
+
|
599 |
+
private:
|
600 |
+
ARROW_FRIEND_EXPORT friend bool internal::IsNullRunEndEncoded(const ArrayData& span,
|
601 |
+
int64_t i);
|
602 |
+
|
603 |
+
bool IsNullSparseUnion(int64_t i) const;
|
604 |
+
bool IsNullDenseUnion(int64_t i) const;
|
605 |
+
|
606 |
+
/// \brief Return true if the value at logical index i is null
|
607 |
+
///
|
608 |
+
/// This function uses binary-search, so it has a O(log N) cost.
|
609 |
+
/// Iterating over the whole array and calling IsNull is O(N log N), so
|
610 |
+
/// for better performance it is recommended to use a
|
611 |
+
/// ree_util::RunEndEncodedArraySpan to iterate run by run instead.
|
612 |
+
bool IsNullRunEndEncoded(int64_t i) const;
|
613 |
+
|
614 |
+
bool UnionMayHaveLogicalNulls() const;
|
615 |
+
bool RunEndEncodedMayHaveLogicalNulls() const;
|
616 |
+
bool DictionaryMayHaveLogicalNulls() const;
|
617 |
+
};
|
618 |
+
|
619 |
+
namespace internal {
|
620 |
+
|
621 |
+
void FillZeroLengthArray(const DataType* type, ArraySpan* span);
|
622 |
+
|
623 |
+
/// Construct a zero-copy view of this ArrayData with the given type.
|
624 |
+
///
|
625 |
+
/// This method checks if the types are layout-compatible.
|
626 |
+
/// Nested types are traversed in depth-first order. Data buffers must have
|
627 |
+
/// the same item sizes, even though the logical types may be different.
|
628 |
+
/// An error is returned if the types are not layout-compatible.
|
629 |
+
ARROW_EXPORT
|
630 |
+
Result<std::shared_ptr<ArrayData>> GetArrayView(const std::shared_ptr<ArrayData>& data,
|
631 |
+
const std::shared_ptr<DataType>& type);
|
632 |
+
|
633 |
+
} // namespace internal
|
634 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/diff.h
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <functional>
|
22 |
+
#include <iosfwd>
|
23 |
+
#include <memory>
|
24 |
+
|
25 |
+
#include "arrow/array/array_base.h"
|
26 |
+
#include "arrow/array/array_nested.h"
|
27 |
+
#include "arrow/result.h"
|
28 |
+
#include "arrow/status.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/util/visibility.h"
|
31 |
+
|
32 |
+
namespace arrow {
|
33 |
+
|
34 |
+
/// \brief Compare two arrays, returning an edit script which expresses the difference
|
35 |
+
/// between them
|
36 |
+
///
|
37 |
+
/// An edit script is an array of struct(insert: bool, run_length: int64_t).
|
38 |
+
/// Each element of "insert" determines whether an element was inserted into (true)
|
39 |
+
/// or deleted from (false) base. Each insertion or deletion is followed by a run of
|
40 |
+
/// elements which are unchanged from base to target; the length of this run is stored
|
41 |
+
/// in "run_length". (Note that the edit script begins and ends with a run of shared
|
42 |
+
/// elements but both fields of the struct must have the same length. To accommodate this
|
43 |
+
/// the first element of "insert" should be ignored.)
|
44 |
+
///
|
45 |
+
/// For example for base "hlloo" and target "hello", the edit script would be
|
46 |
+
/// [
|
47 |
+
/// {"insert": false, "run_length": 1}, // leading run of length 1 ("h")
|
48 |
+
/// {"insert": true, "run_length": 3}, // insert("e") then a run of length 3 ("llo")
|
49 |
+
/// {"insert": false, "run_length": 0} // delete("o") then an empty run
|
50 |
+
/// ]
|
51 |
+
///
|
52 |
+
/// Diffing arrays containing nulls is not currently supported.
|
53 |
+
///
|
54 |
+
/// \param[in] base baseline for comparison
|
55 |
+
/// \param[in] target an array of identical type to base whose elements differ from base's
|
56 |
+
/// \param[in] pool memory to store the result will be allocated from this memory pool
|
57 |
+
/// \return an edit script array which can be applied to base to produce target
|
58 |
+
ARROW_EXPORT
|
59 |
+
Result<std::shared_ptr<StructArray>> Diff(const Array& base, const Array& target,
|
60 |
+
MemoryPool* pool = default_memory_pool());
|
61 |
+
|
62 |
+
/// \brief visitor interface for easy traversal of an edit script
|
63 |
+
///
|
64 |
+
/// visitor will be called for each hunk of insertions and deletions.
|
65 |
+
ARROW_EXPORT Status VisitEditScript(
|
66 |
+
const Array& edits,
|
67 |
+
const std::function<Status(int64_t delete_begin, int64_t delete_end,
|
68 |
+
int64_t insert_begin, int64_t insert_end)>& visitor);
|
69 |
+
|
70 |
+
/// \brief return a function which will format an edit script in unified
|
71 |
+
/// diff format to os, given base and target arrays of type
|
72 |
+
ARROW_EXPORT Result<
|
73 |
+
std::function<Status(const Array& edits, const Array& base, const Array& target)>>
|
74 |
+
MakeUnifiedDiffFormatter(const DataType& type, std::ostream* os);
|
75 |
+
|
76 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/array/validate.h
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include "arrow/status.h"
|
21 |
+
#include "arrow/type_fwd.h"
|
22 |
+
#include "arrow/util/visibility.h"
|
23 |
+
|
24 |
+
namespace arrow {
|
25 |
+
namespace internal {
|
26 |
+
|
27 |
+
// Internal functions implementing Array::Validate() and friends.
|
28 |
+
|
29 |
+
// O(1) array metadata validation
|
30 |
+
|
31 |
+
ARROW_EXPORT
|
32 |
+
Status ValidateArray(const Array& array);
|
33 |
+
|
34 |
+
ARROW_EXPORT
|
35 |
+
Status ValidateArray(const ArrayData& data);
|
36 |
+
|
37 |
+
// O(N) array data validation.
|
38 |
+
// Note that, starting from 7.0.0, "full" routines also validate metadata.
|
39 |
+
// Before, ValidateArray() needed to be called before ValidateArrayFull()
|
40 |
+
// to ensure metadata correctness, otherwise invalid memory accesses
|
41 |
+
// may occur.
|
42 |
+
|
43 |
+
ARROW_EXPORT
|
44 |
+
Status ValidateArrayFull(const Array& array);
|
45 |
+
|
46 |
+
ARROW_EXPORT
|
47 |
+
Status ValidateArrayFull(const ArrayData& data);
|
48 |
+
|
49 |
+
ARROW_EXPORT
|
50 |
+
Status ValidateUTF8(const Array& array);
|
51 |
+
|
52 |
+
ARROW_EXPORT
|
53 |
+
Status ValidateUTF8(const ArrayData& data);
|
54 |
+
|
55 |
+
} // namespace internal
|
56 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/api.h
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// NOTE: API is EXPERIMENTAL and will change without going through a
|
19 |
+
// deprecation cycle
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
/// \defgroup compute-functions Abstract compute function API
|
24 |
+
/// @{
|
25 |
+
/// @}
|
26 |
+
|
27 |
+
/// \defgroup compute-concrete-options Concrete option classes for compute functions
|
28 |
+
/// @{
|
29 |
+
/// @}
|
30 |
+
|
31 |
+
#include "arrow/compute/api_aggregate.h" // IWYU pragma: export
|
32 |
+
#include "arrow/compute/api_scalar.h" // IWYU pragma: export
|
33 |
+
#include "arrow/compute/api_vector.h" // IWYU pragma: export
|
34 |
+
#include "arrow/compute/cast.h" // IWYU pragma: export
|
35 |
+
#include "arrow/compute/function.h" // IWYU pragma: export
|
36 |
+
#include "arrow/compute/function_options.h" // IWYU pragma: export
|
37 |
+
#include "arrow/compute/kernel.h" // IWYU pragma: export
|
38 |
+
#include "arrow/compute/registry.h" // IWYU pragma: export
|
39 |
+
#include "arrow/datum.h" // IWYU pragma: export
|
40 |
+
|
41 |
+
#include "arrow/compute/expression.h" // IWYU pragma: export
|
42 |
+
|
43 |
+
/// \defgroup execnode-row Utilities for working with data in a row-major format
|
44 |
+
/// @{
|
45 |
+
/// @}
|
46 |
+
|
47 |
+
#include "arrow/compute/row/grouper.h" // IWYU pragma: export
|
48 |
+
|
49 |
+
/// \defgroup acero-internals Acero internals, useful for those extending Acero
|
50 |
+
/// @{
|
51 |
+
/// @}
|
52 |
+
|
53 |
+
#include "arrow/compute/exec.h" // IWYU pragma: export
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/cast.h
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <string>
|
22 |
+
#include <vector>
|
23 |
+
|
24 |
+
#include "arrow/compute/function.h"
|
25 |
+
#include "arrow/compute/function_options.h"
|
26 |
+
#include "arrow/compute/type_fwd.h"
|
27 |
+
#include "arrow/result.h"
|
28 |
+
#include "arrow/status.h"
|
29 |
+
#include "arrow/type.h"
|
30 |
+
#include "arrow/util/macros.h"
|
31 |
+
#include "arrow/util/visibility.h"
|
32 |
+
|
33 |
+
namespace arrow {
|
34 |
+
|
35 |
+
class Array;
|
36 |
+
|
37 |
+
namespace compute {
|
38 |
+
|
39 |
+
class ExecContext;
|
40 |
+
|
41 |
+
/// \addtogroup compute-concrete-options
|
42 |
+
/// @{
|
43 |
+
|
44 |
+
class ARROW_EXPORT CastOptions : public FunctionOptions {
|
45 |
+
public:
|
46 |
+
explicit CastOptions(bool safe = true);
|
47 |
+
|
48 |
+
static constexpr char const kTypeName[] = "CastOptions";
|
49 |
+
static CastOptions Safe(TypeHolder to_type = {}) {
|
50 |
+
CastOptions safe(true);
|
51 |
+
safe.to_type = std::move(to_type);
|
52 |
+
return safe;
|
53 |
+
}
|
54 |
+
|
55 |
+
static CastOptions Unsafe(TypeHolder to_type = {}) {
|
56 |
+
CastOptions unsafe(false);
|
57 |
+
unsafe.to_type = std::move(to_type);
|
58 |
+
return unsafe;
|
59 |
+
}
|
60 |
+
|
61 |
+
// Type being casted to. May be passed separate to eager function
|
62 |
+
// compute::Cast
|
63 |
+
TypeHolder to_type;
|
64 |
+
|
65 |
+
bool allow_int_overflow;
|
66 |
+
bool allow_time_truncate;
|
67 |
+
bool allow_time_overflow;
|
68 |
+
bool allow_decimal_truncate;
|
69 |
+
bool allow_float_truncate;
|
70 |
+
// Indicate if conversions from Binary/FixedSizeBinary to string must
|
71 |
+
// validate the utf8 payload.
|
72 |
+
bool allow_invalid_utf8;
|
73 |
+
|
74 |
+
/// true if the safety options all match CastOptions::Safe
|
75 |
+
///
|
76 |
+
/// Note, if this returns false it does not mean is_unsafe will return true
|
77 |
+
bool is_safe() const;
|
78 |
+
/// true if the safety options all match CastOptions::Unsafe
|
79 |
+
///
|
80 |
+
/// Note, if this returns false it does not mean is_safe will return true
|
81 |
+
bool is_unsafe() const;
|
82 |
+
};
|
83 |
+
|
84 |
+
/// @}
|
85 |
+
|
86 |
+
/// \brief Return true if a cast function is defined
|
87 |
+
ARROW_EXPORT
|
88 |
+
bool CanCast(const DataType& from_type, const DataType& to_type);
|
89 |
+
|
90 |
+
// ----------------------------------------------------------------------
|
91 |
+
// Convenience invocation APIs for a number of kernels
|
92 |
+
|
93 |
+
/// \brief Cast from one array type to another
|
94 |
+
/// \param[in] value array to cast
|
95 |
+
/// \param[in] to_type type to cast to
|
96 |
+
/// \param[in] options casting options
|
97 |
+
/// \param[in] ctx the function execution context, optional
|
98 |
+
/// \return the resulting array
|
99 |
+
///
|
100 |
+
/// \since 1.0.0
|
101 |
+
/// \note API not yet finalized
|
102 |
+
ARROW_EXPORT
|
103 |
+
Result<std::shared_ptr<Array>> Cast(const Array& value, const TypeHolder& to_type,
|
104 |
+
const CastOptions& options = CastOptions::Safe(),
|
105 |
+
ExecContext* ctx = NULLPTR);
|
106 |
+
|
107 |
+
/// \brief Cast from one array type to another
|
108 |
+
/// \param[in] value array to cast
|
109 |
+
/// \param[in] options casting options. The "to_type" field must be populated
|
110 |
+
/// \param[in] ctx the function execution context, optional
|
111 |
+
/// \return the resulting array
|
112 |
+
///
|
113 |
+
/// \since 1.0.0
|
114 |
+
/// \note API not yet finalized
|
115 |
+
ARROW_EXPORT
|
116 |
+
Result<Datum> Cast(const Datum& value, const CastOptions& options,
|
117 |
+
ExecContext* ctx = NULLPTR);
|
118 |
+
|
119 |
+
/// \brief Cast from one value to another
|
120 |
+
/// \param[in] value datum to cast
|
121 |
+
/// \param[in] to_type type to cast to
|
122 |
+
/// \param[in] options casting options
|
123 |
+
/// \param[in] ctx the function execution context, optional
|
124 |
+
/// \return the resulting datum
|
125 |
+
///
|
126 |
+
/// \since 1.0.0
|
127 |
+
/// \note API not yet finalized
|
128 |
+
ARROW_EXPORT
|
129 |
+
Result<Datum> Cast(const Datum& value, const TypeHolder& to_type,
|
130 |
+
const CastOptions& options = CastOptions::Safe(),
|
131 |
+
ExecContext* ctx = NULLPTR);
|
132 |
+
|
133 |
+
} // namespace compute
|
134 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/function.h
ADDED
@@ -0,0 +1,409 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// NOTE: API is EXPERIMENTAL and will change without going through a
|
19 |
+
// deprecation cycle.
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <string>
|
24 |
+
#include <utility>
|
25 |
+
#include <vector>
|
26 |
+
|
27 |
+
#include "arrow/compute/kernel.h"
|
28 |
+
#include "arrow/compute/type_fwd.h"
|
29 |
+
#include "arrow/datum.h"
|
30 |
+
#include "arrow/result.h"
|
31 |
+
#include "arrow/status.h"
|
32 |
+
#include "arrow/util/compare.h"
|
33 |
+
#include "arrow/util/macros.h"
|
34 |
+
#include "arrow/util/visibility.h"
|
35 |
+
|
36 |
+
namespace arrow {
|
37 |
+
namespace compute {
|
38 |
+
|
39 |
+
/// \addtogroup compute-functions
|
40 |
+
/// @{
|
41 |
+
|
42 |
+
/// \brief Contains the number of required arguments for the function.
|
43 |
+
///
|
44 |
+
/// Naming conventions taken from https://en.wikipedia.org/wiki/Arity.
|
45 |
+
struct ARROW_EXPORT Arity {
|
46 |
+
/// \brief A function taking no arguments
|
47 |
+
static Arity Nullary() { return Arity(0, false); }
|
48 |
+
|
49 |
+
/// \brief A function taking 1 argument
|
50 |
+
static Arity Unary() { return Arity(1, false); }
|
51 |
+
|
52 |
+
/// \brief A function taking 2 arguments
|
53 |
+
static Arity Binary() { return Arity(2, false); }
|
54 |
+
|
55 |
+
/// \brief A function taking 3 arguments
|
56 |
+
static Arity Ternary() { return Arity(3, false); }
|
57 |
+
|
58 |
+
/// \brief A function taking a variable number of arguments
|
59 |
+
///
|
60 |
+
/// \param[in] min_args the minimum number of arguments required when
|
61 |
+
/// invoking the function
|
62 |
+
static Arity VarArgs(int min_args = 0) { return Arity(min_args, true); }
|
63 |
+
|
64 |
+
// NOTE: the 0-argument form (default constructor) is required for Cython
|
65 |
+
explicit Arity(int num_args = 0, bool is_varargs = false)
|
66 |
+
: num_args(num_args), is_varargs(is_varargs) {}
|
67 |
+
|
68 |
+
/// The number of required arguments (or the minimum number for varargs
|
69 |
+
/// functions).
|
70 |
+
int num_args;
|
71 |
+
|
72 |
+
/// If true, then the num_args is the minimum number of required arguments.
|
73 |
+
bool is_varargs = false;
|
74 |
+
};
|
75 |
+
|
76 |
+
struct ARROW_EXPORT FunctionDoc {
|
77 |
+
/// \brief A one-line summary of the function, using a verb.
|
78 |
+
///
|
79 |
+
/// For example, "Add two numeric arrays or scalars".
|
80 |
+
std::string summary;
|
81 |
+
|
82 |
+
/// \brief A detailed description of the function, meant to follow the summary.
|
83 |
+
std::string description;
|
84 |
+
|
85 |
+
/// \brief Symbolic names (identifiers) for the function arguments.
|
86 |
+
///
|
87 |
+
/// Some bindings may use this to generate nicer function signatures.
|
88 |
+
std::vector<std::string> arg_names;
|
89 |
+
|
90 |
+
// TODO add argument descriptions?
|
91 |
+
|
92 |
+
/// \brief Name of the options class, if any.
|
93 |
+
std::string options_class;
|
94 |
+
|
95 |
+
/// \brief Whether options are required for function execution
|
96 |
+
///
|
97 |
+
/// If false, then either the function does not have an options class
|
98 |
+
/// or there is a usable default options value.
|
99 |
+
bool options_required;
|
100 |
+
|
101 |
+
FunctionDoc() = default;
|
102 |
+
|
103 |
+
FunctionDoc(std::string summary, std::string description,
|
104 |
+
std::vector<std::string> arg_names, std::string options_class = "",
|
105 |
+
bool options_required = false)
|
106 |
+
: summary(std::move(summary)),
|
107 |
+
description(std::move(description)),
|
108 |
+
arg_names(std::move(arg_names)),
|
109 |
+
options_class(std::move(options_class)),
|
110 |
+
options_required(options_required) {}
|
111 |
+
|
112 |
+
static const FunctionDoc& Empty();
|
113 |
+
};
|
114 |
+
|
115 |
+
/// \brief An executor of a function with a preconfigured kernel
|
116 |
+
class ARROW_EXPORT FunctionExecutor {
|
117 |
+
public:
|
118 |
+
virtual ~FunctionExecutor() = default;
|
119 |
+
/// \brief Initialize or re-initialize the preconfigured kernel
|
120 |
+
///
|
121 |
+
/// This method may be called zero or more times. Depending on how
|
122 |
+
/// the FunctionExecutor was obtained, it may already have been initialized.
|
123 |
+
virtual Status Init(const FunctionOptions* options = NULLPTR,
|
124 |
+
ExecContext* exec_ctx = NULLPTR) = 0;
|
125 |
+
/// \brief Execute the preconfigured kernel with arguments that must fit it
|
126 |
+
///
|
127 |
+
/// The method requires the arguments be castable to the preconfigured types.
|
128 |
+
///
|
129 |
+
/// \param[in] args Arguments to execute the function on
|
130 |
+
/// \param[in] length Length of arguments batch or -1 to default it. If the
|
131 |
+
/// function has no parameters, this determines the batch length, defaulting
|
132 |
+
/// to 0. Otherwise, if the function is scalar, this must equal the argument
|
133 |
+
/// batch's inferred length or be -1 to default to it. This is ignored for
|
134 |
+
/// vector functions.
|
135 |
+
virtual Result<Datum> Execute(const std::vector<Datum>& args, int64_t length = -1) = 0;
|
136 |
+
};
|
137 |
+
|
138 |
+
/// \brief Base class for compute functions. Function implementations contain a
|
139 |
+
/// collection of "kernels" which are implementations of the function for
|
140 |
+
/// specific argument types. Selecting a viable kernel for executing a function
|
141 |
+
/// is referred to as "dispatching".
|
142 |
+
class ARROW_EXPORT Function {
|
143 |
+
public:
|
144 |
+
/// \brief The kind of function, which indicates in what contexts it is
|
145 |
+
/// valid for use.
|
146 |
+
enum Kind {
|
147 |
+
/// A function that performs scalar data operations on whole arrays of
|
148 |
+
/// data. Can generally process Array or Scalar values. The size of the
|
149 |
+
/// output will be the same as the size (or broadcasted size, in the case
|
150 |
+
/// of mixing Array and Scalar inputs) of the input.
|
151 |
+
SCALAR,
|
152 |
+
|
153 |
+
/// A function with array input and output whose behavior depends on the
|
154 |
+
/// values of the entire arrays passed, rather than the value of each scalar
|
155 |
+
/// value.
|
156 |
+
VECTOR,
|
157 |
+
|
158 |
+
/// A function that computes scalar summary statistics from array input.
|
159 |
+
SCALAR_AGGREGATE,
|
160 |
+
|
161 |
+
/// A function that computes grouped summary statistics from array input
|
162 |
+
/// and an array of group identifiers.
|
163 |
+
HASH_AGGREGATE,
|
164 |
+
|
165 |
+
/// A function that dispatches to other functions and does not contain its
|
166 |
+
/// own kernels.
|
167 |
+
META
|
168 |
+
};
|
169 |
+
|
170 |
+
virtual ~Function() = default;
|
171 |
+
|
172 |
+
/// \brief The name of the kernel. The registry enforces uniqueness of names.
|
173 |
+
const std::string& name() const { return name_; }
|
174 |
+
|
175 |
+
/// \brief The kind of kernel, which indicates in what contexts it is valid
|
176 |
+
/// for use.
|
177 |
+
Function::Kind kind() const { return kind_; }
|
178 |
+
|
179 |
+
/// \brief Contains the number of arguments the function requires, or if the
|
180 |
+
/// function accepts variable numbers of arguments.
|
181 |
+
const Arity& arity() const { return arity_; }
|
182 |
+
|
183 |
+
/// \brief Return the function documentation
|
184 |
+
const FunctionDoc& doc() const { return doc_; }
|
185 |
+
|
186 |
+
/// \brief Returns the number of registered kernels for this function.
|
187 |
+
virtual int num_kernels() const = 0;
|
188 |
+
|
189 |
+
/// \brief Return a kernel that can execute the function given the exact
|
190 |
+
/// argument types (without implicit type casts).
|
191 |
+
///
|
192 |
+
/// NB: This function is overridden in CastFunction.
|
193 |
+
virtual Result<const Kernel*> DispatchExact(const std::vector<TypeHolder>& types) const;
|
194 |
+
|
195 |
+
/// \brief Return a best-match kernel that can execute the function given the argument
|
196 |
+
/// types, after implicit casts are applied.
|
197 |
+
///
|
198 |
+
/// \param[in,out] values Argument types. An element may be modified to
|
199 |
+
/// indicate that the returned kernel only approximately matches the input
|
200 |
+
/// value descriptors; callers are responsible for casting inputs to the type
|
201 |
+
/// required by the kernel.
|
202 |
+
virtual Result<const Kernel*> DispatchBest(std::vector<TypeHolder>* values) const;
|
203 |
+
|
204 |
+
/// \brief Get a function executor with a best-matching kernel
|
205 |
+
///
|
206 |
+
/// The returned executor will by default work with the default FunctionOptions
|
207 |
+
/// and KernelContext. If you want to change that, call `FunctionExecutor::Init`.
|
208 |
+
virtual Result<std::shared_ptr<FunctionExecutor>> GetBestExecutor(
|
209 |
+
std::vector<TypeHolder> inputs) const;
|
210 |
+
|
211 |
+
/// \brief Execute the function eagerly with the passed input arguments with
|
212 |
+
/// kernel dispatch, batch iteration, and memory allocation details taken
|
213 |
+
/// care of.
|
214 |
+
///
|
215 |
+
/// If the `options` pointer is null, then `default_options()` will be used.
|
216 |
+
///
|
217 |
+
/// This function can be overridden in subclasses.
|
218 |
+
virtual Result<Datum> Execute(const std::vector<Datum>& args,
|
219 |
+
const FunctionOptions* options, ExecContext* ctx) const;
|
220 |
+
|
221 |
+
virtual Result<Datum> Execute(const ExecBatch& batch, const FunctionOptions* options,
|
222 |
+
ExecContext* ctx) const;
|
223 |
+
|
224 |
+
/// \brief Returns the default options for this function.
|
225 |
+
///
|
226 |
+
/// Whatever option semantics a Function has, implementations must guarantee
|
227 |
+
/// that default_options() is valid to pass to Execute as options.
|
228 |
+
const FunctionOptions* default_options() const { return default_options_; }
|
229 |
+
|
230 |
+
virtual Status Validate() const;
|
231 |
+
|
232 |
+
/// \brief Returns the pure property for this function.
|
233 |
+
///
|
234 |
+
/// Impure functions are those that may return different results for the same
|
235 |
+
/// input arguments. For example, a function that returns a random number is
|
236 |
+
/// not pure. An expression containing only pure functions can be simplified by
|
237 |
+
/// pre-evaluating any sub-expressions that have constant arguments.
|
238 |
+
virtual bool is_pure() const { return true; }
|
239 |
+
|
240 |
+
protected:
|
241 |
+
Function(std::string name, Function::Kind kind, const Arity& arity, FunctionDoc doc,
|
242 |
+
const FunctionOptions* default_options)
|
243 |
+
: name_(std::move(name)),
|
244 |
+
kind_(kind),
|
245 |
+
arity_(arity),
|
246 |
+
doc_(std::move(doc)),
|
247 |
+
default_options_(default_options) {}
|
248 |
+
|
249 |
+
Status CheckArity(size_t num_args) const;
|
250 |
+
|
251 |
+
std::string name_;
|
252 |
+
Function::Kind kind_;
|
253 |
+
Arity arity_;
|
254 |
+
const FunctionDoc doc_;
|
255 |
+
const FunctionOptions* default_options_ = NULLPTR;
|
256 |
+
};
|
257 |
+
|
258 |
+
namespace detail {
|
259 |
+
|
260 |
+
template <typename KernelType>
|
261 |
+
class FunctionImpl : public Function {
|
262 |
+
public:
|
263 |
+
/// \brief Return pointers to current-available kernels for inspection
|
264 |
+
std::vector<const KernelType*> kernels() const {
|
265 |
+
std::vector<const KernelType*> result;
|
266 |
+
for (const auto& kernel : kernels_) {
|
267 |
+
result.push_back(&kernel);
|
268 |
+
}
|
269 |
+
return result;
|
270 |
+
}
|
271 |
+
|
272 |
+
int num_kernels() const override { return static_cast<int>(kernels_.size()); }
|
273 |
+
|
274 |
+
protected:
|
275 |
+
FunctionImpl(std::string name, Function::Kind kind, const Arity& arity, FunctionDoc doc,
|
276 |
+
const FunctionOptions* default_options)
|
277 |
+
: Function(std::move(name), kind, arity, std::move(doc), default_options) {}
|
278 |
+
|
279 |
+
std::vector<KernelType> kernels_;
|
280 |
+
};
|
281 |
+
|
282 |
+
/// \brief Look up a kernel in a function. If no Kernel is found, nullptr is returned.
|
283 |
+
ARROW_EXPORT
|
284 |
+
const Kernel* DispatchExactImpl(const Function* func, const std::vector<TypeHolder>&);
|
285 |
+
|
286 |
+
/// \brief Return an error message if no Kernel is found.
|
287 |
+
ARROW_EXPORT
|
288 |
+
Status NoMatchingKernel(const Function* func, const std::vector<TypeHolder>&);
|
289 |
+
|
290 |
+
} // namespace detail
|
291 |
+
|
292 |
+
/// \brief A function that executes elementwise operations on arrays or
|
293 |
+
/// scalars, and therefore whose results generally do not depend on the order
|
294 |
+
/// of the values in the arguments. Accepts and returns arrays that are all of
|
295 |
+
/// the same size. These functions roughly correspond to the functions used in
|
296 |
+
/// SQL expressions.
|
297 |
+
class ARROW_EXPORT ScalarFunction : public detail::FunctionImpl<ScalarKernel> {
|
298 |
+
public:
|
299 |
+
using KernelType = ScalarKernel;
|
300 |
+
|
301 |
+
ScalarFunction(std::string name, const Arity& arity, FunctionDoc doc,
|
302 |
+
const FunctionOptions* default_options = NULLPTR, bool is_pure = true)
|
303 |
+
: detail::FunctionImpl<ScalarKernel>(std::move(name), Function::SCALAR, arity,
|
304 |
+
std::move(doc), default_options),
|
305 |
+
is_pure_(is_pure) {}
|
306 |
+
|
307 |
+
/// \brief Add a kernel with given input/output types, no required state
|
308 |
+
/// initialization, preallocation for fixed-width types, and default null
|
309 |
+
/// handling (intersect validity bitmaps of inputs).
|
310 |
+
Status AddKernel(std::vector<InputType> in_types, OutputType out_type,
|
311 |
+
ArrayKernelExec exec, KernelInit init = NULLPTR);
|
312 |
+
|
313 |
+
/// \brief Add a kernel (function implementation). Returns error if the
|
314 |
+
/// kernel's signature does not match the function's arity.
|
315 |
+
Status AddKernel(ScalarKernel kernel);
|
316 |
+
|
317 |
+
/// \brief Returns the pure property for this function.
|
318 |
+
bool is_pure() const override { return is_pure_; }
|
319 |
+
|
320 |
+
private:
|
321 |
+
const bool is_pure_;
|
322 |
+
};
|
323 |
+
|
324 |
+
/// \brief A function that executes general array operations that may yield
|
325 |
+
/// outputs of different sizes or have results that depend on the whole array
|
326 |
+
/// contents. These functions roughly correspond to the functions found in
|
327 |
+
/// non-SQL array languages like APL and its derivatives.
|
328 |
+
class ARROW_EXPORT VectorFunction : public detail::FunctionImpl<VectorKernel> {
|
329 |
+
public:
|
330 |
+
using KernelType = VectorKernel;
|
331 |
+
|
332 |
+
VectorFunction(std::string name, const Arity& arity, FunctionDoc doc,
|
333 |
+
const FunctionOptions* default_options = NULLPTR)
|
334 |
+
: detail::FunctionImpl<VectorKernel>(std::move(name), Function::VECTOR, arity,
|
335 |
+
std::move(doc), default_options) {}
|
336 |
+
|
337 |
+
/// \brief Add a simple kernel with given input/output types, no required
|
338 |
+
/// state initialization, no data preallocation, and no preallocation of the
|
339 |
+
/// validity bitmap.
|
340 |
+
Status AddKernel(std::vector<InputType> in_types, OutputType out_type,
|
341 |
+
ArrayKernelExec exec, KernelInit init = NULLPTR);
|
342 |
+
|
343 |
+
/// \brief Add a kernel (function implementation). Returns error if the
|
344 |
+
/// kernel's signature does not match the function's arity.
|
345 |
+
Status AddKernel(VectorKernel kernel);
|
346 |
+
};
|
347 |
+
|
348 |
+
class ARROW_EXPORT ScalarAggregateFunction
|
349 |
+
: public detail::FunctionImpl<ScalarAggregateKernel> {
|
350 |
+
public:
|
351 |
+
using KernelType = ScalarAggregateKernel;
|
352 |
+
|
353 |
+
ScalarAggregateFunction(std::string name, const Arity& arity, FunctionDoc doc,
|
354 |
+
const FunctionOptions* default_options = NULLPTR)
|
355 |
+
: detail::FunctionImpl<ScalarAggregateKernel>(std::move(name),
|
356 |
+
Function::SCALAR_AGGREGATE, arity,
|
357 |
+
std::move(doc), default_options) {}
|
358 |
+
|
359 |
+
/// \brief Add a kernel (function implementation). Returns error if the
|
360 |
+
/// kernel's signature does not match the function's arity.
|
361 |
+
Status AddKernel(ScalarAggregateKernel kernel);
|
362 |
+
};
|
363 |
+
|
364 |
+
class ARROW_EXPORT HashAggregateFunction
|
365 |
+
: public detail::FunctionImpl<HashAggregateKernel> {
|
366 |
+
public:
|
367 |
+
using KernelType = HashAggregateKernel;
|
368 |
+
|
369 |
+
HashAggregateFunction(std::string name, const Arity& arity, FunctionDoc doc,
|
370 |
+
const FunctionOptions* default_options = NULLPTR)
|
371 |
+
: detail::FunctionImpl<HashAggregateKernel>(std::move(name),
|
372 |
+
Function::HASH_AGGREGATE, arity,
|
373 |
+
std::move(doc), default_options) {}
|
374 |
+
|
375 |
+
/// \brief Add a kernel (function implementation). Returns error if the
|
376 |
+
/// kernel's signature does not match the function's arity.
|
377 |
+
Status AddKernel(HashAggregateKernel kernel);
|
378 |
+
};
|
379 |
+
|
380 |
+
/// \brief A function that dispatches to other functions. Must implement
|
381 |
+
/// MetaFunction::ExecuteImpl.
|
382 |
+
///
|
383 |
+
/// For Array, ChunkedArray, and Scalar Datum kinds, may rely on the execution
|
384 |
+
/// of concrete Function types, but must handle other Datum kinds on its own.
|
385 |
+
class ARROW_EXPORT MetaFunction : public Function {
|
386 |
+
public:
|
387 |
+
int num_kernels() const override { return 0; }
|
388 |
+
|
389 |
+
Result<Datum> Execute(const std::vector<Datum>& args, const FunctionOptions* options,
|
390 |
+
ExecContext* ctx) const override;
|
391 |
+
|
392 |
+
Result<Datum> Execute(const ExecBatch& batch, const FunctionOptions* options,
|
393 |
+
ExecContext* ctx) const override;
|
394 |
+
|
395 |
+
protected:
|
396 |
+
virtual Result<Datum> ExecuteImpl(const std::vector<Datum>& args,
|
397 |
+
const FunctionOptions* options,
|
398 |
+
ExecContext* ctx) const = 0;
|
399 |
+
|
400 |
+
MetaFunction(std::string name, const Arity& arity, FunctionDoc doc,
|
401 |
+
const FunctionOptions* default_options = NULLPTR)
|
402 |
+
: Function(std::move(name), Function::META, arity, std::move(doc),
|
403 |
+
default_options) {}
|
404 |
+
};
|
405 |
+
|
406 |
+
/// @}
|
407 |
+
|
408 |
+
} // namespace compute
|
409 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/function_options.h
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// NOTE: API is EXPERIMENTAL and will change without going through a
|
19 |
+
// deprecation cycle.
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include "arrow/compute/type_fwd.h"
|
24 |
+
#include "arrow/result.h"
|
25 |
+
#include "arrow/status.h"
|
26 |
+
#include "arrow/type_fwd.h"
|
27 |
+
#include "arrow/util/visibility.h"
|
28 |
+
|
29 |
+
namespace arrow {
|
30 |
+
namespace compute {
|
31 |
+
|
32 |
+
/// \addtogroup compute-functions
|
33 |
+
/// @{
|
34 |
+
|
35 |
+
/// \brief Extension point for defining options outside libarrow (but
|
36 |
+
/// still within this project).
|
37 |
+
class ARROW_EXPORT FunctionOptionsType {
|
38 |
+
public:
|
39 |
+
virtual ~FunctionOptionsType() = default;
|
40 |
+
|
41 |
+
virtual const char* type_name() const = 0;
|
42 |
+
virtual std::string Stringify(const FunctionOptions&) const = 0;
|
43 |
+
virtual bool Compare(const FunctionOptions&, const FunctionOptions&) const = 0;
|
44 |
+
virtual Result<std::shared_ptr<Buffer>> Serialize(const FunctionOptions&) const;
|
45 |
+
virtual Result<std::unique_ptr<FunctionOptions>> Deserialize(
|
46 |
+
const Buffer& buffer) const;
|
47 |
+
virtual std::unique_ptr<FunctionOptions> Copy(const FunctionOptions&) const = 0;
|
48 |
+
};
|
49 |
+
|
50 |
+
/// \brief Base class for specifying options configuring a function's behavior,
|
51 |
+
/// such as error handling.
|
52 |
+
class ARROW_EXPORT FunctionOptions : public util::EqualityComparable<FunctionOptions> {
|
53 |
+
public:
|
54 |
+
virtual ~FunctionOptions() = default;
|
55 |
+
|
56 |
+
const FunctionOptionsType* options_type() const { return options_type_; }
|
57 |
+
const char* type_name() const { return options_type()->type_name(); }
|
58 |
+
|
59 |
+
bool Equals(const FunctionOptions& other) const;
|
60 |
+
std::string ToString() const;
|
61 |
+
std::unique_ptr<FunctionOptions> Copy() const;
|
62 |
+
/// \brief Serialize an options struct to a buffer.
|
63 |
+
Result<std::shared_ptr<Buffer>> Serialize() const;
|
64 |
+
/// \brief Deserialize an options struct from a buffer.
|
65 |
+
/// Note: this will only look for `type_name` in the default FunctionRegistry;
|
66 |
+
/// to use a custom FunctionRegistry, look up the FunctionOptionsType, then
|
67 |
+
/// call FunctionOptionsType::Deserialize().
|
68 |
+
static Result<std::unique_ptr<FunctionOptions>> Deserialize(
|
69 |
+
const std::string& type_name, const Buffer& buffer);
|
70 |
+
|
71 |
+
protected:
|
72 |
+
explicit FunctionOptions(const FunctionOptionsType* type) : options_type_(type) {}
|
73 |
+
const FunctionOptionsType* options_type_;
|
74 |
+
};
|
75 |
+
|
76 |
+
ARROW_EXPORT void PrintTo(const FunctionOptions&, std::ostream*);
|
77 |
+
|
78 |
+
/// @}
|
79 |
+
|
80 |
+
} // namespace compute
|
81 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/ordering.h
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <string>
|
21 |
+
#include <vector>
|
22 |
+
|
23 |
+
#include "arrow/type.h"
|
24 |
+
#include "arrow/util/compare.h"
|
25 |
+
#include "arrow/util/visibility.h"
|
26 |
+
|
27 |
+
namespace arrow {
|
28 |
+
namespace compute {
|
29 |
+
|
30 |
+
enum class SortOrder {
|
31 |
+
/// Arrange values in increasing order
|
32 |
+
Ascending,
|
33 |
+
/// Arrange values in decreasing order
|
34 |
+
Descending,
|
35 |
+
};
|
36 |
+
|
37 |
+
enum class NullPlacement {
|
38 |
+
/// Place nulls and NaNs before any non-null values.
|
39 |
+
/// NaNs will come after nulls.
|
40 |
+
AtStart,
|
41 |
+
/// Place nulls and NaNs after any non-null values.
|
42 |
+
/// NaNs will come before nulls.
|
43 |
+
AtEnd,
|
44 |
+
};
|
45 |
+
|
46 |
+
/// \brief One sort key for PartitionNthIndices (TODO) and SortIndices
|
47 |
+
class ARROW_EXPORT SortKey : public util::EqualityComparable<SortKey> {
|
48 |
+
public:
|
49 |
+
explicit SortKey(FieldRef target, SortOrder order = SortOrder::Ascending)
|
50 |
+
: target(std::move(target)), order(order) {}
|
51 |
+
|
52 |
+
bool Equals(const SortKey& other) const;
|
53 |
+
std::string ToString() const;
|
54 |
+
|
55 |
+
/// A FieldRef targeting the sort column.
|
56 |
+
FieldRef target;
|
57 |
+
/// How to order by this sort key.
|
58 |
+
SortOrder order;
|
59 |
+
};
|
60 |
+
|
61 |
+
class ARROW_EXPORT Ordering : public util::EqualityComparable<Ordering> {
|
62 |
+
public:
|
63 |
+
Ordering(std::vector<SortKey> sort_keys,
|
64 |
+
NullPlacement null_placement = NullPlacement::AtStart)
|
65 |
+
: sort_keys_(std::move(sort_keys)), null_placement_(null_placement) {}
|
66 |
+
/// true if data ordered by other is also ordered by this
|
67 |
+
///
|
68 |
+
/// For example, if data is ordered by [a, b, c] then it is also ordered
|
69 |
+
/// by [a, b] but not by [b, c] or [a, b, c, d].
|
70 |
+
///
|
71 |
+
/// [a, b].IsSuborderOf([a, b, c]) - true
|
72 |
+
/// [a, b, c].IsSuborderOf([a, b, c]) - true
|
73 |
+
/// [b, c].IsSuborderOf([a, b, c]) - false
|
74 |
+
/// [a, b, c, d].IsSuborderOf([a, b, c]) - false
|
75 |
+
///
|
76 |
+
/// The implicit ordering is not a suborder of any other ordering and
|
77 |
+
/// no other ordering is a suborder of it. The implicit ordering is not a
|
78 |
+
/// suborder of itself.
|
79 |
+
///
|
80 |
+
/// The unordered ordering is a suborder of all other orderings but no
|
81 |
+
/// other ordering is a suborder of it. The unordered ordering is a suborder
|
82 |
+
/// of itself.
|
83 |
+
///
|
84 |
+
/// The unordered ordering is a suborder of the implicit ordering.
|
85 |
+
bool IsSuborderOf(const Ordering& other) const;
|
86 |
+
|
87 |
+
bool Equals(const Ordering& other) const;
|
88 |
+
std::string ToString() const;
|
89 |
+
|
90 |
+
bool is_implicit() const { return is_implicit_; }
|
91 |
+
bool is_unordered() const { return !is_implicit_ && sort_keys_.empty(); }
|
92 |
+
|
93 |
+
const std::vector<SortKey>& sort_keys() const { return sort_keys_; }
|
94 |
+
NullPlacement null_placement() const { return null_placement_; }
|
95 |
+
|
96 |
+
static const Ordering& Implicit() {
|
97 |
+
static const Ordering kImplicit(true);
|
98 |
+
return kImplicit;
|
99 |
+
}
|
100 |
+
|
101 |
+
static const Ordering& Unordered() {
|
102 |
+
static const Ordering kUnordered(false);
|
103 |
+
// It is also possible to get an unordered ordering by passing in an empty vector
|
104 |
+
// using the normal constructor. This is ok and useful when ordering comes from user
|
105 |
+
// input.
|
106 |
+
return kUnordered;
|
107 |
+
}
|
108 |
+
|
109 |
+
private:
|
110 |
+
explicit Ordering(bool is_implicit)
|
111 |
+
: null_placement_(NullPlacement::AtStart), is_implicit_(is_implicit) {}
|
112 |
+
/// Column key(s) to order by and how to order by these sort keys.
|
113 |
+
std::vector<SortKey> sort_keys_;
|
114 |
+
/// Whether nulls and NaNs are placed at the start or at the end
|
115 |
+
NullPlacement null_placement_;
|
116 |
+
bool is_implicit_ = false;
|
117 |
+
};
|
118 |
+
|
119 |
+
} // namespace compute
|
120 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/registry.h
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// NOTE: API is EXPERIMENTAL and will change without going through a
|
19 |
+
// deprecation cycle
|
20 |
+
|
21 |
+
#pragma once
|
22 |
+
|
23 |
+
#include <memory>
|
24 |
+
#include <string>
|
25 |
+
#include <vector>
|
26 |
+
|
27 |
+
#include "arrow/result.h"
|
28 |
+
#include "arrow/status.h"
|
29 |
+
#include "arrow/util/visibility.h"
|
30 |
+
|
31 |
+
namespace arrow {
|
32 |
+
namespace compute {
|
33 |
+
|
34 |
+
class Function;
|
35 |
+
class FunctionOptionsType;
|
36 |
+
|
37 |
+
/// \brief A mutable central function registry for built-in functions as well
|
38 |
+
/// as user-defined functions. Functions are implementations of
|
39 |
+
/// arrow::compute::Function.
|
40 |
+
///
|
41 |
+
/// Generally, each function contains kernels which are implementations of a
|
42 |
+
/// function for a specific argument signature. After looking up a function in
|
43 |
+
/// the registry, one can either execute it eagerly with Function::Execute or
|
44 |
+
/// use one of the function's dispatch methods to pick a suitable kernel for
|
45 |
+
/// lower-level function execution.
|
46 |
+
class ARROW_EXPORT FunctionRegistry {
|
47 |
+
public:
|
48 |
+
~FunctionRegistry();
|
49 |
+
|
50 |
+
/// \brief Construct a new registry.
|
51 |
+
///
|
52 |
+
/// Most users only need to use the global registry.
|
53 |
+
static std::unique_ptr<FunctionRegistry> Make();
|
54 |
+
|
55 |
+
/// \brief Construct a new nested registry with the given parent.
|
56 |
+
///
|
57 |
+
/// Most users only need to use the global registry. The returned registry never changes
|
58 |
+
/// its parent, even when an operation allows overwriting.
|
59 |
+
static std::unique_ptr<FunctionRegistry> Make(FunctionRegistry* parent);
|
60 |
+
|
61 |
+
/// \brief Check whether a new function can be added to the registry.
|
62 |
+
///
|
63 |
+
/// \returns Status::KeyError if a function with the same name is already registered.
|
64 |
+
Status CanAddFunction(std::shared_ptr<Function> function, bool allow_overwrite = false);
|
65 |
+
|
66 |
+
/// \brief Add a new function to the registry.
|
67 |
+
///
|
68 |
+
/// \returns Status::KeyError if a function with the same name is already registered.
|
69 |
+
Status AddFunction(std::shared_ptr<Function> function, bool allow_overwrite = false);
|
70 |
+
|
71 |
+
/// \brief Check whether an alias can be added for the given function name.
|
72 |
+
///
|
73 |
+
/// \returns Status::KeyError if the function with the given name is not registered.
|
74 |
+
Status CanAddAlias(const std::string& target_name, const std::string& source_name);
|
75 |
+
|
76 |
+
/// \brief Add alias for the given function name.
|
77 |
+
///
|
78 |
+
/// \returns Status::KeyError if the function with the given name is not registered.
|
79 |
+
Status AddAlias(const std::string& target_name, const std::string& source_name);
|
80 |
+
|
81 |
+
/// \brief Check whether a new function options type can be added to the registry.
|
82 |
+
///
|
83 |
+
/// \return Status::KeyError if a function options type with the same name is already
|
84 |
+
/// registered.
|
85 |
+
Status CanAddFunctionOptionsType(const FunctionOptionsType* options_type,
|
86 |
+
bool allow_overwrite = false);
|
87 |
+
|
88 |
+
/// \brief Add a new function options type to the registry.
|
89 |
+
///
|
90 |
+
/// \returns Status::KeyError if a function options type with the same name is already
|
91 |
+
/// registered.
|
92 |
+
Status AddFunctionOptionsType(const FunctionOptionsType* options_type,
|
93 |
+
bool allow_overwrite = false);
|
94 |
+
|
95 |
+
/// \brief Retrieve a function by name from the registry.
|
96 |
+
Result<std::shared_ptr<Function>> GetFunction(const std::string& name) const;
|
97 |
+
|
98 |
+
/// \brief Return vector of all entry names in the registry.
|
99 |
+
///
|
100 |
+
/// Helpful for displaying a manifest of available functions.
|
101 |
+
std::vector<std::string> GetFunctionNames() const;
|
102 |
+
|
103 |
+
/// \brief Retrieve a function options type by name from the registry.
|
104 |
+
Result<const FunctionOptionsType*> GetFunctionOptionsType(
|
105 |
+
const std::string& name) const;
|
106 |
+
|
107 |
+
/// \brief The number of currently registered functions.
|
108 |
+
int num_functions() const;
|
109 |
+
|
110 |
+
/// \brief The cast function object registered in AddFunction.
|
111 |
+
///
|
112 |
+
/// Helpful for get cast function as needed.
|
113 |
+
const Function* cast_function() const;
|
114 |
+
|
115 |
+
private:
|
116 |
+
FunctionRegistry();
|
117 |
+
|
118 |
+
// Use PIMPL pattern to not have std::unordered_map here
|
119 |
+
class FunctionRegistryImpl;
|
120 |
+
std::unique_ptr<FunctionRegistryImpl> impl_;
|
121 |
+
|
122 |
+
explicit FunctionRegistry(FunctionRegistryImpl* impl);
|
123 |
+
};
|
124 |
+
|
125 |
+
} // namespace compute
|
126 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/compute/util.h
ADDED
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <atomic>
|
21 |
+
#include <cstdint>
|
22 |
+
#include <optional>
|
23 |
+
#include <thread>
|
24 |
+
#include <unordered_map>
|
25 |
+
#include <vector>
|
26 |
+
|
27 |
+
#include "arrow/buffer.h"
|
28 |
+
#include "arrow/compute/expression.h"
|
29 |
+
#include "arrow/compute/type_fwd.h"
|
30 |
+
#include "arrow/memory_pool.h"
|
31 |
+
#include "arrow/result.h"
|
32 |
+
#include "arrow/status.h"
|
33 |
+
#include "arrow/util/bit_util.h"
|
34 |
+
#include "arrow/util/cpu_info.h"
|
35 |
+
#include "arrow/util/mutex.h"
|
36 |
+
#include "arrow/util/thread_pool.h"
|
37 |
+
#include "arrow/util/type_fwd.h"
|
38 |
+
|
39 |
+
#if defined(__clang__) || defined(__GNUC__)
|
40 |
+
#define BYTESWAP(x) __builtin_bswap64(x)
|
41 |
+
#define ROTL(x, n) (((x) << (n)) | ((x) >> ((-n) & 31)))
|
42 |
+
#define ROTL64(x, n) (((x) << (n)) | ((x) >> ((-n) & 63)))
|
43 |
+
#define PREFETCH(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
|
44 |
+
#elif defined(_MSC_VER)
|
45 |
+
#include <intrin.h>
|
46 |
+
#define BYTESWAP(x) _byteswap_uint64(x)
|
47 |
+
#define ROTL(x, n) _rotl((x), (n))
|
48 |
+
#define ROTL64(x, n) _rotl64((x), (n))
|
49 |
+
#if defined(_M_X64) || defined(_M_I86)
|
50 |
+
#include <mmintrin.h> // https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx
|
51 |
+
#define PREFETCH(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
|
52 |
+
#else
|
53 |
+
#define PREFETCH(ptr) (void)(ptr) /* disabled */
|
54 |
+
#endif
|
55 |
+
#endif
|
56 |
+
|
57 |
+
namespace arrow {
|
58 |
+
namespace util {
|
59 |
+
|
60 |
+
// Some platforms typedef int64_t as long int instead of long long int,
|
61 |
+
// which breaks the _mm256_i64gather_epi64 and _mm256_i32gather_epi64 intrinsics
|
62 |
+
// which need long long.
|
63 |
+
// We use the cast to the type below in these intrinsics to make the code
|
64 |
+
// compile in all cases.
|
65 |
+
//
|
66 |
+
using int64_for_gather_t = const long long int; // NOLINT runtime-int
|
67 |
+
|
68 |
+
// All MiniBatch... classes use TempVectorStack for vector allocations and can
|
69 |
+
// only work with vectors up to 1024 elements.
|
70 |
+
//
|
71 |
+
// They should only be allocated on the stack to guarantee the right sequence
|
72 |
+
// of allocation and deallocation of vectors from TempVectorStack.
|
73 |
+
//
|
74 |
+
class MiniBatch {
|
75 |
+
public:
|
76 |
+
static constexpr int kLogMiniBatchLength = 10;
|
77 |
+
static constexpr int kMiniBatchLength = 1 << kLogMiniBatchLength;
|
78 |
+
};
|
79 |
+
|
80 |
+
/// Storage used to allocate temporary vectors of a batch size.
|
81 |
+
/// Temporary vectors should resemble allocating temporary variables on the stack
|
82 |
+
/// but in the context of vectorized processing where we need to store a vector of
|
83 |
+
/// temporaries instead of a single value.
|
84 |
+
class ARROW_EXPORT TempVectorStack {
|
85 |
+
template <typename>
|
86 |
+
friend class TempVectorHolder;
|
87 |
+
|
88 |
+
public:
|
89 |
+
Status Init(MemoryPool* pool, int64_t size) {
|
90 |
+
num_vectors_ = 0;
|
91 |
+
top_ = 0;
|
92 |
+
buffer_size_ = EstimatedAllocationSize(size);
|
93 |
+
ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateResizableBuffer(size, pool));
|
94 |
+
// Ensure later operations don't accidentally read uninitialized memory.
|
95 |
+
std::memset(buffer->mutable_data(), 0xFF, size);
|
96 |
+
buffer_ = std::move(buffer);
|
97 |
+
return Status::OK();
|
98 |
+
}
|
99 |
+
|
100 |
+
private:
|
101 |
+
static int64_t EstimatedAllocationSize(int64_t size) {
|
102 |
+
return PaddedAllocationSize(size) + 2 * sizeof(uint64_t);
|
103 |
+
}
|
104 |
+
|
105 |
+
static int64_t PaddedAllocationSize(int64_t num_bytes) {
|
106 |
+
// Round up allocation size to multiple of 8 bytes
|
107 |
+
// to avoid returning temp vectors with unaligned address.
|
108 |
+
//
|
109 |
+
// Also add padding at the end to facilitate loads and stores
|
110 |
+
// using SIMD when number of vector elements is not divisible
|
111 |
+
// by the number of SIMD lanes.
|
112 |
+
//
|
113 |
+
return ::arrow::bit_util::RoundUp(num_bytes, sizeof(int64_t)) + kPadding;
|
114 |
+
}
|
115 |
+
void alloc(uint32_t num_bytes, uint8_t** data, int* id);
|
116 |
+
void release(int id, uint32_t num_bytes);
|
117 |
+
static constexpr uint64_t kGuard1 = 0x3141592653589793ULL;
|
118 |
+
static constexpr uint64_t kGuard2 = 0x0577215664901532ULL;
|
119 |
+
static constexpr int64_t kPadding = 64;
|
120 |
+
int num_vectors_;
|
121 |
+
int64_t top_;
|
122 |
+
std::unique_ptr<Buffer> buffer_;
|
123 |
+
int64_t buffer_size_;
|
124 |
+
};
|
125 |
+
|
126 |
+
template <typename T>
|
127 |
+
class TempVectorHolder {
|
128 |
+
friend class TempVectorStack;
|
129 |
+
|
130 |
+
public:
|
131 |
+
~TempVectorHolder() { stack_->release(id_, num_elements_ * sizeof(T)); }
|
132 |
+
T* mutable_data() { return reinterpret_cast<T*>(data_); }
|
133 |
+
TempVectorHolder(TempVectorStack* stack, uint32_t num_elements) {
|
134 |
+
stack_ = stack;
|
135 |
+
num_elements_ = num_elements;
|
136 |
+
stack_->alloc(num_elements * sizeof(T), &data_, &id_);
|
137 |
+
}
|
138 |
+
|
139 |
+
private:
|
140 |
+
TempVectorStack* stack_;
|
141 |
+
uint8_t* data_;
|
142 |
+
int id_;
|
143 |
+
uint32_t num_elements_;
|
144 |
+
};
|
145 |
+
|
146 |
+
namespace bit_util {
|
147 |
+
|
148 |
+
ARROW_EXPORT void bits_to_indexes(int bit_to_search, int64_t hardware_flags,
|
149 |
+
const int num_bits, const uint8_t* bits,
|
150 |
+
int* num_indexes, uint16_t* indexes,
|
151 |
+
int bit_offset = 0);
|
152 |
+
|
153 |
+
ARROW_EXPORT void bits_filter_indexes(int bit_to_search, int64_t hardware_flags,
|
154 |
+
const int num_bits, const uint8_t* bits,
|
155 |
+
const uint16_t* input_indexes, int* num_indexes,
|
156 |
+
uint16_t* indexes, int bit_offset = 0);
|
157 |
+
|
158 |
+
// Input and output indexes may be pointing to the same data (in-place filtering).
|
159 |
+
ARROW_EXPORT void bits_split_indexes(int64_t hardware_flags, const int num_bits,
|
160 |
+
const uint8_t* bits, int* num_indexes_bit0,
|
161 |
+
uint16_t* indexes_bit0, uint16_t* indexes_bit1,
|
162 |
+
int bit_offset = 0);
|
163 |
+
|
164 |
+
// Bit 1 is replaced with byte 0xFF.
|
165 |
+
ARROW_EXPORT void bits_to_bytes(int64_t hardware_flags, const int num_bits,
|
166 |
+
const uint8_t* bits, uint8_t* bytes, int bit_offset = 0);
|
167 |
+
|
168 |
+
// Return highest bit of each byte.
|
169 |
+
ARROW_EXPORT void bytes_to_bits(int64_t hardware_flags, const int num_bits,
|
170 |
+
const uint8_t* bytes, uint8_t* bits, int bit_offset = 0);
|
171 |
+
|
172 |
+
ARROW_EXPORT bool are_all_bytes_zero(int64_t hardware_flags, const uint8_t* bytes,
|
173 |
+
uint32_t num_bytes);
|
174 |
+
|
175 |
+
#if defined(ARROW_HAVE_RUNTIME_AVX2) && defined(ARROW_HAVE_RUNTIME_BMI2)
|
176 |
+
// The functions below use BMI2 instructions, be careful before calling!
|
177 |
+
|
178 |
+
namespace avx2 {
|
179 |
+
ARROW_EXPORT void bits_filter_indexes_avx2(int bit_to_search, const int num_bits,
|
180 |
+
const uint8_t* bits,
|
181 |
+
const uint16_t* input_indexes,
|
182 |
+
int* num_indexes, uint16_t* indexes);
|
183 |
+
ARROW_EXPORT void bits_to_indexes_avx2(int bit_to_search, const int num_bits,
|
184 |
+
const uint8_t* bits, int* num_indexes,
|
185 |
+
uint16_t* indexes, uint16_t base_index = 0);
|
186 |
+
ARROW_EXPORT void bits_to_bytes_avx2(const int num_bits, const uint8_t* bits,
|
187 |
+
uint8_t* bytes);
|
188 |
+
ARROW_EXPORT void bytes_to_bits_avx2(const int num_bits, const uint8_t* bytes,
|
189 |
+
uint8_t* bits);
|
190 |
+
ARROW_EXPORT bool are_all_bytes_zero_avx2(const uint8_t* bytes, uint32_t num_bytes);
|
191 |
+
} // namespace avx2
|
192 |
+
|
193 |
+
#endif
|
194 |
+
|
195 |
+
} // namespace bit_util
|
196 |
+
} // namespace util
|
197 |
+
|
198 |
+
namespace compute {
|
199 |
+
|
200 |
+
/// Modify an Expression with pre-order and post-order visitation.
|
201 |
+
/// `pre` will be invoked on each Expression. `pre` will visit Calls before their
|
202 |
+
/// arguments, `post_call` will visit Calls (and no other Expressions) after their
|
203 |
+
/// arguments. Visitors should return the Identical expression to indicate no change; this
|
204 |
+
/// will prevent unnecessary construction in the common case where a modification is not
|
205 |
+
/// possible/necessary/...
|
206 |
+
///
|
207 |
+
/// If an argument was modified, `post_call` visits a reconstructed Call with the modified
|
208 |
+
/// arguments but also receives a pointer to the unmodified Expression as a second
|
209 |
+
/// argument. If no arguments were modified the unmodified Expression* will be nullptr.
|
210 |
+
template <typename PreVisit, typename PostVisitCall>
|
211 |
+
Result<Expression> ModifyExpression(Expression expr, const PreVisit& pre,
|
212 |
+
const PostVisitCall& post_call) {
|
213 |
+
ARROW_ASSIGN_OR_RAISE(expr, Result<Expression>(pre(std::move(expr))));
|
214 |
+
|
215 |
+
auto call = expr.call();
|
216 |
+
if (!call) return expr;
|
217 |
+
|
218 |
+
bool at_least_one_modified = false;
|
219 |
+
std::vector<Expression> modified_arguments;
|
220 |
+
|
221 |
+
for (size_t i = 0; i < call->arguments.size(); ++i) {
|
222 |
+
ARROW_ASSIGN_OR_RAISE(auto modified_argument,
|
223 |
+
ModifyExpression(call->arguments[i], pre, post_call));
|
224 |
+
|
225 |
+
if (Identical(modified_argument, call->arguments[i])) {
|
226 |
+
continue;
|
227 |
+
}
|
228 |
+
|
229 |
+
if (!at_least_one_modified) {
|
230 |
+
modified_arguments = call->arguments;
|
231 |
+
at_least_one_modified = true;
|
232 |
+
}
|
233 |
+
|
234 |
+
modified_arguments[i] = std::move(modified_argument);
|
235 |
+
}
|
236 |
+
|
237 |
+
if (at_least_one_modified) {
|
238 |
+
// reconstruct the call expression with the modified arguments
|
239 |
+
auto modified_call = *call;
|
240 |
+
modified_call.arguments = std::move(modified_arguments);
|
241 |
+
return post_call(Expression(std::move(modified_call)), &expr);
|
242 |
+
}
|
243 |
+
|
244 |
+
return post_call(std::move(expr), NULLPTR);
|
245 |
+
}
|
246 |
+
|
247 |
+
// Helper class to calculate the modified number of rows to process using SIMD.
|
248 |
+
//
|
249 |
+
// Some array elements at the end will be skipped in order to avoid buffer
|
250 |
+
// overrun, when doing memory loads and stores using larger word size than a
|
251 |
+
// single array element.
|
252 |
+
//
|
253 |
+
class TailSkipForSIMD {
|
254 |
+
public:
|
255 |
+
static int64_t FixBitAccess(int num_bytes_accessed_together, int64_t num_rows,
|
256 |
+
int bit_offset) {
|
257 |
+
int64_t num_bytes = bit_util::BytesForBits(num_rows + bit_offset);
|
258 |
+
int64_t num_bytes_safe =
|
259 |
+
std::max(static_cast<int64_t>(0LL), num_bytes - num_bytes_accessed_together + 1);
|
260 |
+
int64_t num_rows_safe =
|
261 |
+
std::max(static_cast<int64_t>(0LL), 8 * num_bytes_safe - bit_offset);
|
262 |
+
return std::min(num_rows_safe, num_rows);
|
263 |
+
}
|
264 |
+
static int64_t FixBinaryAccess(int num_bytes_accessed_together, int64_t num_rows,
|
265 |
+
int64_t length) {
|
266 |
+
int64_t num_rows_to_skip = bit_util::CeilDiv(length, num_bytes_accessed_together);
|
267 |
+
int64_t num_rows_safe =
|
268 |
+
std::max(static_cast<int64_t>(0LL), num_rows - num_rows_to_skip);
|
269 |
+
return num_rows_safe;
|
270 |
+
}
|
271 |
+
static int64_t FixVarBinaryAccess(int num_bytes_accessed_together, int64_t num_rows,
|
272 |
+
const uint32_t* offsets) {
|
273 |
+
// Do not process rows that could read past the end of the buffer using N
|
274 |
+
// byte loads/stores.
|
275 |
+
//
|
276 |
+
int64_t num_rows_safe = num_rows;
|
277 |
+
while (num_rows_safe > 0 &&
|
278 |
+
offsets[num_rows_safe] + num_bytes_accessed_together > offsets[num_rows]) {
|
279 |
+
--num_rows_safe;
|
280 |
+
}
|
281 |
+
return num_rows_safe;
|
282 |
+
}
|
283 |
+
static int FixSelection(int64_t num_rows_safe, int num_selected,
|
284 |
+
const uint16_t* selection) {
|
285 |
+
int num_selected_safe = num_selected;
|
286 |
+
while (num_selected_safe > 0 && selection[num_selected_safe - 1] >= num_rows_safe) {
|
287 |
+
--num_selected_safe;
|
288 |
+
}
|
289 |
+
return num_selected_safe;
|
290 |
+
}
|
291 |
+
};
|
292 |
+
|
293 |
+
} // namespace compute
|
294 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/chunked_builder.h
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <vector>
|
22 |
+
|
23 |
+
#include "arrow/status.h"
|
24 |
+
#include "arrow/type_fwd.h"
|
25 |
+
#include "arrow/util/type_fwd.h"
|
26 |
+
#include "arrow/util/visibility.h"
|
27 |
+
|
28 |
+
namespace arrow {
|
29 |
+
namespace json {
|
30 |
+
|
31 |
+
class PromotionGraph;
|
32 |
+
|
33 |
+
class ARROW_EXPORT ChunkedArrayBuilder {
|
34 |
+
public:
|
35 |
+
virtual ~ChunkedArrayBuilder() = default;
|
36 |
+
|
37 |
+
/// Spawn a task that will try to convert and insert the given JSON block
|
38 |
+
virtual void Insert(int64_t block_index,
|
39 |
+
const std::shared_ptr<Field>& unconverted_field,
|
40 |
+
const std::shared_ptr<Array>& unconverted) = 0;
|
41 |
+
|
42 |
+
/// Return the final chunked array.
|
43 |
+
/// Every chunk must be inserted before this is called!
|
44 |
+
virtual Status Finish(std::shared_ptr<ChunkedArray>* out) = 0;
|
45 |
+
|
46 |
+
/// Finish current task group and substitute a new one
|
47 |
+
virtual Status ReplaceTaskGroup(
|
48 |
+
const std::shared_ptr<arrow::internal::TaskGroup>& task_group) = 0;
|
49 |
+
|
50 |
+
protected:
|
51 |
+
explicit ChunkedArrayBuilder(
|
52 |
+
const std::shared_ptr<arrow::internal::TaskGroup>& task_group)
|
53 |
+
: task_group_(task_group) {}
|
54 |
+
|
55 |
+
std::shared_ptr<arrow::internal::TaskGroup> task_group_;
|
56 |
+
};
|
57 |
+
|
58 |
+
/// create a chunked builder
|
59 |
+
///
|
60 |
+
/// if unexpected fields and promotion need to be handled, promotion_graph must be
|
61 |
+
/// non-null
|
62 |
+
ARROW_EXPORT Status MakeChunkedArrayBuilder(
|
63 |
+
const std::shared_ptr<arrow::internal::TaskGroup>& task_group, MemoryPool* pool,
|
64 |
+
const PromotionGraph* promotion_graph, const std::shared_ptr<DataType>& type,
|
65 |
+
std::shared_ptr<ChunkedArrayBuilder>* out);
|
66 |
+
|
67 |
+
} // namespace json
|
68 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/chunker.h
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
|
22 |
+
#include "arrow/util/delimiting.h"
|
23 |
+
#include "arrow/util/macros.h"
|
24 |
+
#include "arrow/util/visibility.h"
|
25 |
+
|
26 |
+
namespace arrow {
|
27 |
+
namespace json {
|
28 |
+
|
29 |
+
struct ParseOptions;
|
30 |
+
|
31 |
+
ARROW_EXPORT
|
32 |
+
std::unique_ptr<Chunker> MakeChunker(const ParseOptions& options);
|
33 |
+
|
34 |
+
} // namespace json
|
35 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/converter.h
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <string>
|
22 |
+
|
23 |
+
#include "arrow/status.h"
|
24 |
+
#include "arrow/util/macros.h"
|
25 |
+
#include "arrow/util/visibility.h"
|
26 |
+
|
27 |
+
namespace arrow {
|
28 |
+
|
29 |
+
class Array;
|
30 |
+
class DataType;
|
31 |
+
class Field;
|
32 |
+
class MemoryPool;
|
33 |
+
|
34 |
+
namespace json {
|
35 |
+
|
36 |
+
/// \brief interface for conversion of Arrays
|
37 |
+
///
|
38 |
+
/// Converters are not required to be correct for arbitrary input- only
|
39 |
+
/// for unconverted arrays emitted by a corresponding parser.
|
40 |
+
class ARROW_EXPORT Converter {
|
41 |
+
public:
|
42 |
+
virtual ~Converter() = default;
|
43 |
+
|
44 |
+
/// convert an array
|
45 |
+
/// on failure, this converter may be promoted to another converter which
|
46 |
+
/// *can* convert the given input.
|
47 |
+
virtual Status Convert(const std::shared_ptr<Array>& in,
|
48 |
+
std::shared_ptr<Array>* out) = 0;
|
49 |
+
|
50 |
+
std::shared_ptr<DataType> out_type() const { return out_type_; }
|
51 |
+
|
52 |
+
MemoryPool* pool() { return pool_; }
|
53 |
+
|
54 |
+
protected:
|
55 |
+
ARROW_DISALLOW_COPY_AND_ASSIGN(Converter);
|
56 |
+
|
57 |
+
Converter(MemoryPool* pool, const std::shared_ptr<DataType>& out_type)
|
58 |
+
: pool_(pool), out_type_(out_type) {}
|
59 |
+
|
60 |
+
MemoryPool* pool_;
|
61 |
+
std::shared_ptr<DataType> out_type_;
|
62 |
+
};
|
63 |
+
|
64 |
+
/// \brief produce a single converter to the specified out_type
|
65 |
+
ARROW_EXPORT Status MakeConverter(const std::shared_ptr<DataType>& out_type,
|
66 |
+
MemoryPool* pool, std::shared_ptr<Converter>* out);
|
67 |
+
|
68 |
+
class ARROW_EXPORT PromotionGraph {
|
69 |
+
public:
|
70 |
+
virtual ~PromotionGraph() = default;
|
71 |
+
|
72 |
+
/// \brief produce a valid field which will be inferred as null
|
73 |
+
virtual std::shared_ptr<Field> Null(const std::string& name) const = 0;
|
74 |
+
|
75 |
+
/// \brief given an unexpected field encountered during parsing, return a type to which
|
76 |
+
/// it may be convertible (may return null if none is available)
|
77 |
+
virtual std::shared_ptr<DataType> Infer(
|
78 |
+
const std::shared_ptr<Field>& unexpected_field) const = 0;
|
79 |
+
|
80 |
+
/// \brief given a type to which conversion failed, return a promoted type to which
|
81 |
+
/// conversion may succeed (may return null if none is available)
|
82 |
+
virtual std::shared_ptr<DataType> Promote(
|
83 |
+
const std::shared_ptr<DataType>& failed,
|
84 |
+
const std::shared_ptr<Field>& unexpected_field) const = 0;
|
85 |
+
|
86 |
+
protected:
|
87 |
+
ARROW_DISALLOW_COPY_AND_ASSIGN(PromotionGraph);
|
88 |
+
PromotionGraph() = default;
|
89 |
+
};
|
90 |
+
|
91 |
+
ARROW_EXPORT const PromotionGraph* GetPromotionGraph();
|
92 |
+
|
93 |
+
} // namespace json
|
94 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/object_parser.h
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <string_view>
|
22 |
+
#include <unordered_map>
|
23 |
+
|
24 |
+
#include "arrow/result.h"
|
25 |
+
#include "arrow/util/visibility.h"
|
26 |
+
|
27 |
+
namespace arrow {
|
28 |
+
namespace json {
|
29 |
+
namespace internal {
|
30 |
+
|
31 |
+
/// This class is a helper to parse a json object from a string.
|
32 |
+
/// It uses rapidjson::Document in implementation.
|
33 |
+
class ARROW_EXPORT ObjectParser {
|
34 |
+
public:
|
35 |
+
ObjectParser();
|
36 |
+
~ObjectParser();
|
37 |
+
|
38 |
+
Status Parse(std::string_view json);
|
39 |
+
|
40 |
+
Result<std::string> GetString(const char* key) const;
|
41 |
+
|
42 |
+
Result<bool> GetBool(const char* key) const;
|
43 |
+
|
44 |
+
// Get all members of the object as a map from string keys to string values
|
45 |
+
Result<std::unordered_map<std::string, std::string>> GetStringMap() const;
|
46 |
+
|
47 |
+
private:
|
48 |
+
class Impl;
|
49 |
+
std::unique_ptr<Impl> impl_;
|
50 |
+
};
|
51 |
+
|
52 |
+
} // namespace internal
|
53 |
+
} // namespace json
|
54 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/object_writer.h
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <string_view>
|
22 |
+
|
23 |
+
#include "arrow/util/visibility.h"
|
24 |
+
|
25 |
+
namespace arrow {
|
26 |
+
namespace json {
|
27 |
+
namespace internal {
|
28 |
+
|
29 |
+
/// This class is a helper to serialize a json object to a string.
|
30 |
+
/// It uses rapidjson in implementation.
|
31 |
+
class ARROW_EXPORT ObjectWriter {
|
32 |
+
public:
|
33 |
+
ObjectWriter();
|
34 |
+
~ObjectWriter();
|
35 |
+
|
36 |
+
void SetString(std::string_view key, std::string_view value);
|
37 |
+
void SetBool(std::string_view key, bool value);
|
38 |
+
|
39 |
+
std::string Serialize();
|
40 |
+
|
41 |
+
private:
|
42 |
+
class Impl;
|
43 |
+
std::unique_ptr<Impl> impl_;
|
44 |
+
};
|
45 |
+
|
46 |
+
} // namespace internal
|
47 |
+
} // namespace json
|
48 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/options.h
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
|
23 |
+
#include "arrow/json/type_fwd.h"
|
24 |
+
#include "arrow/util/visibility.h"
|
25 |
+
|
26 |
+
namespace arrow {
|
27 |
+
|
28 |
+
class DataType;
|
29 |
+
class Schema;
|
30 |
+
|
31 |
+
namespace json {
|
32 |
+
|
33 |
+
enum class UnexpectedFieldBehavior : char {
|
34 |
+
/// Unexpected JSON fields are ignored
|
35 |
+
Ignore,
|
36 |
+
/// Unexpected JSON fields error out
|
37 |
+
Error,
|
38 |
+
/// Unexpected JSON fields are type-inferred and included in the output
|
39 |
+
InferType
|
40 |
+
};
|
41 |
+
|
42 |
+
struct ARROW_EXPORT ParseOptions {
|
43 |
+
// Parsing options
|
44 |
+
|
45 |
+
/// Optional explicit schema (disables type inference on those fields)
|
46 |
+
std::shared_ptr<Schema> explicit_schema;
|
47 |
+
|
48 |
+
/// Whether objects may be printed across multiple lines (for example pretty-printed)
|
49 |
+
///
|
50 |
+
/// If true, parsing may be slower.
|
51 |
+
bool newlines_in_values = false;
|
52 |
+
|
53 |
+
/// How JSON fields outside of explicit_schema (if given) are treated
|
54 |
+
UnexpectedFieldBehavior unexpected_field_behavior = UnexpectedFieldBehavior::InferType;
|
55 |
+
|
56 |
+
/// Create parsing options with default values
|
57 |
+
static ParseOptions Defaults();
|
58 |
+
};
|
59 |
+
|
60 |
+
struct ARROW_EXPORT ReadOptions {
|
61 |
+
// Reader options
|
62 |
+
|
63 |
+
/// Whether to use the global CPU thread pool
|
64 |
+
bool use_threads = true;
|
65 |
+
/// Block size we request from the IO layer; also determines the size of
|
66 |
+
/// chunks when use_threads is true
|
67 |
+
int32_t block_size = 1 << 20; // 1 MB
|
68 |
+
|
69 |
+
/// Create read options with default values
|
70 |
+
static ReadOptions Defaults();
|
71 |
+
};
|
72 |
+
|
73 |
+
} // namespace json
|
74 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/parser.h
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <string>
|
22 |
+
|
23 |
+
#include "arrow/json/options.h"
|
24 |
+
#include "arrow/status.h"
|
25 |
+
#include "arrow/util/key_value_metadata.h"
|
26 |
+
#include "arrow/util/macros.h"
|
27 |
+
#include "arrow/util/visibility.h"
|
28 |
+
|
29 |
+
namespace arrow {
|
30 |
+
|
31 |
+
class Array;
|
32 |
+
class Buffer;
|
33 |
+
class MemoryPool;
|
34 |
+
class KeyValueMetadata;
|
35 |
+
class ResizableBuffer;
|
36 |
+
|
37 |
+
namespace json {
|
38 |
+
|
39 |
+
struct Kind {
|
40 |
+
enum type : uint8_t {
|
41 |
+
kNull,
|
42 |
+
kBoolean,
|
43 |
+
kNumber,
|
44 |
+
kString,
|
45 |
+
kArray,
|
46 |
+
kObject,
|
47 |
+
kNumberOrString
|
48 |
+
};
|
49 |
+
|
50 |
+
static const std::string& Name(Kind::type);
|
51 |
+
|
52 |
+
static const std::shared_ptr<const KeyValueMetadata>& Tag(Kind::type);
|
53 |
+
|
54 |
+
static Kind::type FromTag(const std::shared_ptr<const KeyValueMetadata>& tag);
|
55 |
+
|
56 |
+
static Status ForType(const DataType& type, Kind::type* kind);
|
57 |
+
};
|
58 |
+
|
59 |
+
/// \class BlockParser
|
60 |
+
/// \brief A reusable block-based parser for JSON data
|
61 |
+
///
|
62 |
+
/// The parser takes a block of newline delimited JSON data and extracts Arrays
|
63 |
+
/// of unconverted strings which can be fed to a Converter to obtain a usable Array.
|
64 |
+
///
|
65 |
+
/// Note that in addition to parse errors (such as malformed JSON) some conversion
|
66 |
+
/// errors are caught at parse time:
|
67 |
+
/// - A null value in non-nullable column
|
68 |
+
/// - Change in the JSON kind of a column. For example, if an explicit schema is provided
|
69 |
+
/// which stipulates that field "a" is integral, a row of {"a": "not a number"} will
|
70 |
+
/// result in an error. This also applies to fields outside an explicit schema.
|
71 |
+
class ARROW_EXPORT BlockParser {
|
72 |
+
public:
|
73 |
+
virtual ~BlockParser() = default;
|
74 |
+
|
75 |
+
/// \brief Reserve storage for scalars parsed from a block of json
|
76 |
+
virtual Status ReserveScalarStorage(int64_t nbytes) = 0;
|
77 |
+
|
78 |
+
/// \brief Parse a block of data
|
79 |
+
virtual Status Parse(const std::shared_ptr<Buffer>& json) = 0;
|
80 |
+
|
81 |
+
/// \brief Extract parsed data
|
82 |
+
virtual Status Finish(std::shared_ptr<Array>* parsed) = 0;
|
83 |
+
|
84 |
+
/// \brief Return the number of parsed rows
|
85 |
+
int32_t num_rows() const { return num_rows_; }
|
86 |
+
|
87 |
+
/// \brief Construct a BlockParser
|
88 |
+
///
|
89 |
+
/// \param[in] pool MemoryPool to use when constructing parsed array
|
90 |
+
/// \param[in] options ParseOptions to use when parsing JSON
|
91 |
+
/// \param[out] out constructed BlockParser
|
92 |
+
static Status Make(MemoryPool* pool, const ParseOptions& options,
|
93 |
+
std::unique_ptr<BlockParser>* out);
|
94 |
+
|
95 |
+
static Status Make(const ParseOptions& options, std::unique_ptr<BlockParser>* out);
|
96 |
+
|
97 |
+
protected:
|
98 |
+
ARROW_DISALLOW_COPY_AND_ASSIGN(BlockParser);
|
99 |
+
|
100 |
+
explicit BlockParser(MemoryPool* pool) : pool_(pool) {}
|
101 |
+
|
102 |
+
MemoryPool* pool_;
|
103 |
+
int32_t num_rows_ = 0;
|
104 |
+
};
|
105 |
+
|
106 |
+
} // namespace json
|
107 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/rapidjson_defs.h
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// Include this file before including any RapidJSON headers.
|
19 |
+
|
20 |
+
#pragma once
|
21 |
+
|
22 |
+
#define RAPIDJSON_HAS_STDSTRING 1
|
23 |
+
#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
|
24 |
+
#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1
|
25 |
+
|
26 |
+
// rapidjson will be defined in namespace arrow::rapidjson
|
27 |
+
#define RAPIDJSON_NAMESPACE arrow::rapidjson
|
28 |
+
#define RAPIDJSON_NAMESPACE_BEGIN \
|
29 |
+
namespace arrow { \
|
30 |
+
namespace rapidjson {
|
31 |
+
#define RAPIDJSON_NAMESPACE_END \
|
32 |
+
} \
|
33 |
+
}
|
34 |
+
|
35 |
+
// enable SIMD whitespace skipping, if available
|
36 |
+
#if defined(ARROW_HAVE_SSE4_2)
|
37 |
+
#define RAPIDJSON_SSE2 1
|
38 |
+
#define RAPIDJSON_SSE42 1
|
39 |
+
#endif
|
40 |
+
|
41 |
+
#if defined(ARROW_HAVE_NEON)
|
42 |
+
#define RAPIDJSON_NEON 1
|
43 |
+
#endif
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/reader.h
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
|
22 |
+
#include "arrow/io/type_fwd.h"
|
23 |
+
#include "arrow/json/options.h"
|
24 |
+
#include "arrow/record_batch.h"
|
25 |
+
#include "arrow/result.h"
|
26 |
+
#include "arrow/status.h"
|
27 |
+
#include "arrow/util/macros.h"
|
28 |
+
#include "arrow/util/type_fwd.h"
|
29 |
+
#include "arrow/util/visibility.h"
|
30 |
+
|
31 |
+
namespace arrow {
|
32 |
+
namespace json {
|
33 |
+
|
34 |
+
/// A class that reads an entire JSON file into a Arrow Table
|
35 |
+
///
|
36 |
+
/// The file is expected to consist of individual line-separated JSON objects
|
37 |
+
class ARROW_EXPORT TableReader {
|
38 |
+
public:
|
39 |
+
virtual ~TableReader() = default;
|
40 |
+
|
41 |
+
/// Read the entire JSON file and convert it to a Arrow Table
|
42 |
+
virtual Result<std::shared_ptr<Table>> Read() = 0;
|
43 |
+
|
44 |
+
/// Create a TableReader instance
|
45 |
+
static Result<std::shared_ptr<TableReader>> Make(MemoryPool* pool,
|
46 |
+
std::shared_ptr<io::InputStream> input,
|
47 |
+
const ReadOptions&,
|
48 |
+
const ParseOptions&);
|
49 |
+
};
|
50 |
+
|
51 |
+
ARROW_EXPORT Result<std::shared_ptr<RecordBatch>> ParseOne(ParseOptions options,
|
52 |
+
std::shared_ptr<Buffer> json);
|
53 |
+
|
54 |
+
/// \brief A class that reads a JSON file incrementally
|
55 |
+
///
|
56 |
+
/// JSON data is read from a stream in fixed-size blocks (configurable with
|
57 |
+
/// `ReadOptions::block_size`). Each block is converted to a `RecordBatch`. Yielded
|
58 |
+
/// batches have a consistent schema but may differ in row count.
|
59 |
+
///
|
60 |
+
/// The supplied `ParseOptions` are used to determine a schema, based either on a
|
61 |
+
/// provided explicit schema or inferred from the first non-empty block.
|
62 |
+
/// Afterwards, the target schema is frozen. If `UnexpectedFieldBehavior::InferType` is
|
63 |
+
/// specified, unexpected fields will only be inferred for the first block. Afterwards
|
64 |
+
/// they'll be treated as errors.
|
65 |
+
///
|
66 |
+
/// If `ReadOptions::use_threads` is `true`, each block's parsing/decoding task will be
|
67 |
+
/// parallelized on the given `cpu_executor` (with readahead corresponding to the
|
68 |
+
/// executor's capacity). If an executor isn't provided, the global thread pool will be
|
69 |
+
/// used.
|
70 |
+
///
|
71 |
+
/// If `ReadOptions::use_threads` is `false`, computations will be run on the calling
|
72 |
+
/// thread and `cpu_executor` will be ignored.
|
73 |
+
class ARROW_EXPORT StreamingReader : public RecordBatchReader {
|
74 |
+
public:
|
75 |
+
virtual ~StreamingReader() = default;
|
76 |
+
|
77 |
+
/// \brief Read the next `RecordBatch` asynchronously
|
78 |
+
/// This function is async-reentrant (but not synchronously reentrant). However, if
|
79 |
+
/// threading is disabled, this will block until completion.
|
80 |
+
virtual Future<std::shared_ptr<RecordBatch>> ReadNextAsync() = 0;
|
81 |
+
|
82 |
+
/// Get the number of bytes which have been successfully converted to record batches
|
83 |
+
/// and consumed
|
84 |
+
[[nodiscard]] virtual int64_t bytes_processed() const = 0;
|
85 |
+
|
86 |
+
/// \brief Create a `StreamingReader` from an `InputStream`
|
87 |
+
/// Blocks until the initial batch is loaded
|
88 |
+
///
|
89 |
+
/// \param[in] stream JSON source stream
|
90 |
+
/// \param[in] read_options Options for reading
|
91 |
+
/// \param[in] parse_options Options for chunking, parsing, and conversion
|
92 |
+
/// \param[in] io_context Context for IO operations (optional)
|
93 |
+
/// \param[in] cpu_executor Executor for computation tasks (optional)
|
94 |
+
/// \return The initialized reader
|
95 |
+
static Result<std::shared_ptr<StreamingReader>> Make(
|
96 |
+
std::shared_ptr<io::InputStream> stream, const ReadOptions& read_options,
|
97 |
+
const ParseOptions& parse_options,
|
98 |
+
const io::IOContext& io_context = io::default_io_context(),
|
99 |
+
::arrow::internal::Executor* cpu_executor = NULLPTR);
|
100 |
+
|
101 |
+
/// \brief Create a `StreamingReader` from an `InputStream` asynchronously
|
102 |
+
/// Returned future completes after loading the first batch
|
103 |
+
///
|
104 |
+
/// \param[in] stream JSON source stream
|
105 |
+
/// \param[in] read_options Options for reading
|
106 |
+
/// \param[in] parse_options Options for chunking, parsing, and conversion
|
107 |
+
/// \param[in] io_context Context for IO operations (optional)
|
108 |
+
/// \param[in] cpu_executor Executor for computation tasks (optional)
|
109 |
+
/// \return Future for the initialized reader
|
110 |
+
static Future<std::shared_ptr<StreamingReader>> MakeAsync(
|
111 |
+
std::shared_ptr<io::InputStream> stream, const ReadOptions& read_options,
|
112 |
+
const ParseOptions& parse_options,
|
113 |
+
const io::IOContext& io_context = io::default_io_context(),
|
114 |
+
::arrow::internal::Executor* cpu_executor = NULLPTR);
|
115 |
+
};
|
116 |
+
|
117 |
+
} // namespace json
|
118 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/test_common.h
ADDED
@@ -0,0 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <random>
|
22 |
+
#include <sstream>
|
23 |
+
#include <string>
|
24 |
+
#include <string_view>
|
25 |
+
#include <utility>
|
26 |
+
#include <vector>
|
27 |
+
|
28 |
+
#include "arrow/array.h"
|
29 |
+
#include "arrow/array/builder_binary.h"
|
30 |
+
#include "arrow/io/memory.h"
|
31 |
+
#include "arrow/json/converter.h"
|
32 |
+
#include "arrow/json/options.h"
|
33 |
+
#include "arrow/json/parser.h"
|
34 |
+
#include "arrow/json/rapidjson_defs.h"
|
35 |
+
#include "arrow/testing/gtest_util.h"
|
36 |
+
#include "arrow/type.h"
|
37 |
+
#include "arrow/util/checked_cast.h"
|
38 |
+
#include "arrow/visit_type_inline.h"
|
39 |
+
|
40 |
+
#include "rapidjson/document.h"
|
41 |
+
#include "rapidjson/prettywriter.h"
|
42 |
+
#include "rapidjson/reader.h"
|
43 |
+
#include "rapidjson/writer.h"
|
44 |
+
|
45 |
+
namespace arrow {
|
46 |
+
|
47 |
+
using internal::checked_cast;
|
48 |
+
|
49 |
+
namespace json {
|
50 |
+
|
51 |
+
namespace rj = arrow::rapidjson;
|
52 |
+
|
53 |
+
using rj::StringBuffer;
|
54 |
+
using std::string_view;
|
55 |
+
using Writer = rj::Writer<StringBuffer>;
|
56 |
+
|
57 |
+
struct GenerateOptions {
|
58 |
+
// Probability of a field being written
|
59 |
+
double field_probability = 1.0;
|
60 |
+
// Probability of a value being null
|
61 |
+
double null_probability = 0.2;
|
62 |
+
// Whether to randomize the order of written fields
|
63 |
+
bool randomize_field_order = false;
|
64 |
+
|
65 |
+
static constexpr GenerateOptions Defaults() { return GenerateOptions{}; }
|
66 |
+
};
|
67 |
+
|
68 |
+
inline static Status OK(bool ok) { return ok ? Status::OK() : Status::Invalid(""); }
|
69 |
+
|
70 |
+
template <typename Engine>
|
71 |
+
inline static Status Generate(
|
72 |
+
const std::shared_ptr<DataType>& type, Engine& e, Writer* writer,
|
73 |
+
const GenerateOptions& options = GenerateOptions::Defaults());
|
74 |
+
|
75 |
+
template <typename Engine>
|
76 |
+
inline static Status Generate(
|
77 |
+
const std::vector<std::shared_ptr<Field>>& fields, Engine& e, Writer* writer,
|
78 |
+
const GenerateOptions& options = GenerateOptions::Defaults());
|
79 |
+
|
80 |
+
template <typename Engine>
|
81 |
+
inline static Status Generate(
|
82 |
+
const std::shared_ptr<Schema>& schm, Engine& e, Writer* writer,
|
83 |
+
const GenerateOptions& options = GenerateOptions::Defaults()) {
|
84 |
+
return Generate(schm->fields(), e, writer, options);
|
85 |
+
}
|
86 |
+
|
87 |
+
template <typename Engine>
|
88 |
+
struct GenerateImpl {
|
89 |
+
Status Visit(const NullType&) { return OK(writer.Null()); }
|
90 |
+
|
91 |
+
Status Visit(const BooleanType&) {
|
92 |
+
return OK(writer.Bool(std::uniform_int_distribution<uint16_t>{}(e)&1));
|
93 |
+
}
|
94 |
+
|
95 |
+
template <typename T>
|
96 |
+
enable_if_physical_unsigned_integer<T, Status> Visit(const T&) {
|
97 |
+
auto val = std::uniform_int_distribution<>{}(e);
|
98 |
+
return OK(writer.Uint64(static_cast<typename T::c_type>(val)));
|
99 |
+
}
|
100 |
+
|
101 |
+
template <typename T>
|
102 |
+
enable_if_physical_signed_integer<T, Status> Visit(const T&) {
|
103 |
+
auto val = std::uniform_int_distribution<>{}(e);
|
104 |
+
return OK(writer.Int64(static_cast<typename T::c_type>(val)));
|
105 |
+
}
|
106 |
+
|
107 |
+
template <typename T>
|
108 |
+
enable_if_physical_floating_point<T, Status> Visit(const T&) {
|
109 |
+
auto val = std::normal_distribution<typename T::c_type>{0, 1 << 10}(e);
|
110 |
+
return OK(writer.Double(val));
|
111 |
+
}
|
112 |
+
|
113 |
+
Status GenerateAscii(const DataType&) {
|
114 |
+
auto size = std::poisson_distribution<>{4}(e);
|
115 |
+
std::uniform_int_distribution<uint16_t> gen_char(32, 126); // FIXME generate UTF8
|
116 |
+
std::string s(size, '\0');
|
117 |
+
for (char& ch : s) ch = static_cast<char>(gen_char(e));
|
118 |
+
return OK(writer.String(s.c_str()));
|
119 |
+
}
|
120 |
+
|
121 |
+
template <typename T>
|
122 |
+
enable_if_base_binary<T, Status> Visit(const T& t) {
|
123 |
+
return GenerateAscii(t);
|
124 |
+
}
|
125 |
+
|
126 |
+
Status Visit(const BinaryViewType& t) { return GenerateAscii(t); }
|
127 |
+
|
128 |
+
template <typename T>
|
129 |
+
enable_if_list_like<T, Status> Visit(const T& t) {
|
130 |
+
auto size = std::poisson_distribution<>{4}(e);
|
131 |
+
writer.StartArray();
|
132 |
+
for (int i = 0; i < size; ++i) {
|
133 |
+
RETURN_NOT_OK(Generate(t.value_type(), e, &writer, options));
|
134 |
+
}
|
135 |
+
return OK(writer.EndArray(size));
|
136 |
+
}
|
137 |
+
|
138 |
+
Status Visit(const ListViewType& t) { return NotImplemented(t); }
|
139 |
+
|
140 |
+
Status Visit(const LargeListViewType& t) { return NotImplemented(t); }
|
141 |
+
|
142 |
+
Status Visit(const StructType& t) { return Generate(t.fields(), e, &writer, options); }
|
143 |
+
|
144 |
+
Status Visit(const DayTimeIntervalType& t) { return NotImplemented(t); }
|
145 |
+
|
146 |
+
Status Visit(const MonthDayNanoIntervalType& t) { return NotImplemented(t); }
|
147 |
+
|
148 |
+
Status Visit(const DictionaryType& t) { return NotImplemented(t); }
|
149 |
+
|
150 |
+
Status Visit(const ExtensionType& t) { return NotImplemented(t); }
|
151 |
+
|
152 |
+
Status Visit(const Decimal128Type& t) { return NotImplemented(t); }
|
153 |
+
|
154 |
+
Status Visit(const FixedSizeBinaryType& t) { return NotImplemented(t); }
|
155 |
+
|
156 |
+
Status Visit(const UnionType& t) { return NotImplemented(t); }
|
157 |
+
|
158 |
+
Status Visit(const RunEndEncodedType& t) { return NotImplemented(t); }
|
159 |
+
|
160 |
+
Status NotImplemented(const DataType& t) {
|
161 |
+
return Status::NotImplemented("random generation of arrays of type ", t);
|
162 |
+
}
|
163 |
+
|
164 |
+
Engine& e;
|
165 |
+
rj::Writer<rj::StringBuffer>& writer;
|
166 |
+
const GenerateOptions& options;
|
167 |
+
};
|
168 |
+
|
169 |
+
template <typename Engine>
|
170 |
+
inline static Status Generate(const std::shared_ptr<DataType>& type, Engine& e,
|
171 |
+
Writer* writer, const GenerateOptions& options) {
|
172 |
+
if (std::bernoulli_distribution(options.null_probability)(e)) {
|
173 |
+
writer->Null();
|
174 |
+
return Status::OK();
|
175 |
+
}
|
176 |
+
GenerateImpl<Engine> visitor = {e, *writer, options};
|
177 |
+
return VisitTypeInline(*type, &visitor);
|
178 |
+
}
|
179 |
+
|
180 |
+
template <typename Engine>
|
181 |
+
inline static Status Generate(const std::vector<std::shared_ptr<Field>>& fields,
|
182 |
+
Engine& e, Writer* writer, const GenerateOptions& options) {
|
183 |
+
RETURN_NOT_OK(OK(writer->StartObject()));
|
184 |
+
|
185 |
+
int num_fields = 0;
|
186 |
+
auto write_field = [&](const Field& f) {
|
187 |
+
++num_fields;
|
188 |
+
writer->Key(f.name().c_str());
|
189 |
+
return Generate(f.type(), e, writer, options);
|
190 |
+
};
|
191 |
+
|
192 |
+
std::bernoulli_distribution bool_dist(options.field_probability);
|
193 |
+
if (options.randomize_field_order) {
|
194 |
+
std::vector<size_t> indices;
|
195 |
+
indices.reserve(static_cast<size_t>(fields.size() * options.field_probability));
|
196 |
+
for (size_t i = 0; i < fields.size(); ++i) {
|
197 |
+
if (bool_dist(e)) {
|
198 |
+
indices.push_back(i);
|
199 |
+
}
|
200 |
+
}
|
201 |
+
std::shuffle(indices.begin(), indices.end(), e);
|
202 |
+
for (auto i : indices) {
|
203 |
+
RETURN_NOT_OK(write_field(*fields[i]));
|
204 |
+
}
|
205 |
+
} else {
|
206 |
+
for (const auto& f : fields) {
|
207 |
+
if (bool_dist(e)) {
|
208 |
+
RETURN_NOT_OK(write_field(*f));
|
209 |
+
}
|
210 |
+
}
|
211 |
+
}
|
212 |
+
|
213 |
+
return OK(writer->EndObject(num_fields));
|
214 |
+
}
|
215 |
+
|
216 |
+
inline static Status MakeStream(string_view src_str,
|
217 |
+
std::shared_ptr<io::InputStream>* out) {
|
218 |
+
auto src = std::make_shared<Buffer>(src_str);
|
219 |
+
*out = std::make_shared<io::BufferReader>(src);
|
220 |
+
return Status::OK();
|
221 |
+
}
|
222 |
+
|
223 |
+
// scalar values (numbers and strings) are parsed into a
|
224 |
+
// dictionary<index:int32, value:string>. This can be decoded for ease of comparison
|
225 |
+
inline static Status DecodeStringDictionary(const DictionaryArray& dict_array,
|
226 |
+
std::shared_ptr<Array>* decoded) {
|
227 |
+
const StringArray& dict = checked_cast<const StringArray&>(*dict_array.dictionary());
|
228 |
+
const Int32Array& indices = checked_cast<const Int32Array&>(*dict_array.indices());
|
229 |
+
StringBuilder builder;
|
230 |
+
RETURN_NOT_OK(builder.Resize(indices.length()));
|
231 |
+
for (int64_t i = 0; i < indices.length(); ++i) {
|
232 |
+
if (indices.IsNull(i)) {
|
233 |
+
builder.UnsafeAppendNull();
|
234 |
+
continue;
|
235 |
+
}
|
236 |
+
auto value = dict.GetView(indices.GetView(i));
|
237 |
+
RETURN_NOT_OK(builder.ReserveData(value.size()));
|
238 |
+
builder.UnsafeAppend(value);
|
239 |
+
}
|
240 |
+
return builder.Finish(decoded);
|
241 |
+
}
|
242 |
+
|
243 |
+
inline static Status ParseFromString(ParseOptions options, string_view src_str,
|
244 |
+
std::shared_ptr<Array>* parsed) {
|
245 |
+
auto src = std::make_shared<Buffer>(src_str);
|
246 |
+
std::unique_ptr<BlockParser> parser;
|
247 |
+
RETURN_NOT_OK(BlockParser::Make(options, &parser));
|
248 |
+
RETURN_NOT_OK(parser->Parse(src));
|
249 |
+
return parser->Finish(parsed);
|
250 |
+
}
|
251 |
+
|
252 |
+
inline static Status ParseFromString(ParseOptions options, string_view src_str,
|
253 |
+
std::shared_ptr<StructArray>* parsed) {
|
254 |
+
std::shared_ptr<Array> parsed_non_struct;
|
255 |
+
RETURN_NOT_OK(ParseFromString(options, src_str, &parsed_non_struct));
|
256 |
+
*parsed = internal::checked_pointer_cast<StructArray>(parsed_non_struct);
|
257 |
+
return Status::OK();
|
258 |
+
}
|
259 |
+
|
260 |
+
static inline std::string PrettyPrint(string_view one_line) {
|
261 |
+
rj::Document document;
|
262 |
+
|
263 |
+
// Must pass size to avoid ASAN issues.
|
264 |
+
document.Parse(one_line.data(), one_line.size());
|
265 |
+
rj::StringBuffer sb;
|
266 |
+
rj::PrettyWriter<rj::StringBuffer> writer(sb);
|
267 |
+
document.Accept(writer);
|
268 |
+
return sb.GetString();
|
269 |
+
}
|
270 |
+
|
271 |
+
template <typename T>
|
272 |
+
std::string RowsOfOneColumn(std::string_view name, std::initializer_list<T> values,
|
273 |
+
decltype(std::to_string(*values.begin()))* = nullptr) {
|
274 |
+
std::stringstream ss;
|
275 |
+
for (auto value : values) {
|
276 |
+
ss << R"({")" << name << R"(":)" << std::to_string(value) << "}\n";
|
277 |
+
}
|
278 |
+
return ss.str();
|
279 |
+
}
|
280 |
+
|
281 |
+
inline std::string RowsOfOneColumn(std::string_view name,
|
282 |
+
std::initializer_list<std::string> values) {
|
283 |
+
std::stringstream ss;
|
284 |
+
for (auto value : values) {
|
285 |
+
ss << R"({")" << name << R"(":)" << value << "}\n";
|
286 |
+
}
|
287 |
+
return ss.str();
|
288 |
+
}
|
289 |
+
|
290 |
+
inline static std::string scalars_only_src() {
|
291 |
+
return R"(
|
292 |
+
{ "hello": 3.5, "world": false, "yo": "thing" }
|
293 |
+
{ "hello": 3.25, "world": null }
|
294 |
+
{ "hello": 3.125, "world": null, "yo": "\u5fcd" }
|
295 |
+
{ "hello": 0.0, "world": true, "yo": null }
|
296 |
+
)";
|
297 |
+
}
|
298 |
+
|
299 |
+
inline static std::string nested_src() {
|
300 |
+
return R"(
|
301 |
+
{ "hello": 3.5, "world": false, "yo": "thing", "arr": [1, 2, 3], "nuf": {} }
|
302 |
+
{ "hello": 3.25, "world": null, "arr": [2], "nuf": null }
|
303 |
+
{ "hello": 3.125, "world": null, "yo": "\u5fcd", "arr": [], "nuf": { "ps": 78 } }
|
304 |
+
{ "hello": 0.0, "world": true, "yo": null, "arr": null, "nuf": { "ps": 90 } }
|
305 |
+
)";
|
306 |
+
}
|
307 |
+
|
308 |
+
inline static std::string null_src() {
|
309 |
+
return R"(
|
310 |
+
{ "plain": null, "list1": [], "list2": [], "struct": { "plain": null } }
|
311 |
+
{ "plain": null, "list1": [], "list2": [null], "struct": {} }
|
312 |
+
)";
|
313 |
+
}
|
314 |
+
|
315 |
+
inline static std::string unquoted_decimal_src() {
|
316 |
+
return R"(
|
317 |
+
{ "price": 30.04, "cost":30.001 }
|
318 |
+
{ "price": 1.23, "cost":1.229 }
|
319 |
+
)";
|
320 |
+
}
|
321 |
+
|
322 |
+
inline static std::string mixed_decimal_src() {
|
323 |
+
return R"(
|
324 |
+
{ "price": 30.04, "cost": 30.001 }
|
325 |
+
{ "price": "1.23", "cost": "1.229" }
|
326 |
+
)";
|
327 |
+
}
|
328 |
+
|
329 |
+
} // namespace json
|
330 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/json/type_fwd.h
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
namespace arrow {
|
19 |
+
namespace json {
|
20 |
+
|
21 |
+
class TableReader;
|
22 |
+
struct ReadOptions;
|
23 |
+
struct ParseOptions;
|
24 |
+
|
25 |
+
} // namespace json
|
26 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/async_test_util.h
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <atomic>
|
21 |
+
#include <memory>
|
22 |
+
|
23 |
+
#include "arrow/testing/gtest_util.h"
|
24 |
+
#include "arrow/util/async_generator.h"
|
25 |
+
#include "arrow/util/future.h"
|
26 |
+
|
27 |
+
namespace arrow {
|
28 |
+
namespace util {
|
29 |
+
|
30 |
+
template <typename T>
|
31 |
+
AsyncGenerator<T> AsyncVectorIt(std::vector<T> v) {
|
32 |
+
return MakeVectorGenerator(std::move(v));
|
33 |
+
}
|
34 |
+
|
35 |
+
template <typename T>
|
36 |
+
AsyncGenerator<T> FailAt(AsyncGenerator<T> src, int failing_index) {
|
37 |
+
auto index = std::make_shared<std::atomic<int>>(0);
|
38 |
+
return [src, index, failing_index]() {
|
39 |
+
auto idx = index->fetch_add(1);
|
40 |
+
if (idx >= failing_index) {
|
41 |
+
return Future<T>::MakeFinished(Status::Invalid("XYZ"));
|
42 |
+
}
|
43 |
+
return src();
|
44 |
+
};
|
45 |
+
}
|
46 |
+
|
47 |
+
template <typename T>
|
48 |
+
AsyncGenerator<T> SlowdownABit(AsyncGenerator<T> source) {
|
49 |
+
return MakeMappedGenerator(std::move(source), [](const T& res) {
|
50 |
+
return SleepABitAsync().Then([res]() { return res; });
|
51 |
+
});
|
52 |
+
}
|
53 |
+
|
54 |
+
template <typename T>
|
55 |
+
class TrackingGenerator {
|
56 |
+
public:
|
57 |
+
explicit TrackingGenerator(AsyncGenerator<T> source)
|
58 |
+
: state_(std::make_shared<State>(std::move(source))) {}
|
59 |
+
|
60 |
+
Future<T> operator()() {
|
61 |
+
state_->num_read++;
|
62 |
+
return state_->source();
|
63 |
+
}
|
64 |
+
|
65 |
+
int num_read() { return state_->num_read.load(); }
|
66 |
+
|
67 |
+
private:
|
68 |
+
struct State {
|
69 |
+
explicit State(AsyncGenerator<T> source) : source(std::move(source)), num_read(0) {}
|
70 |
+
|
71 |
+
AsyncGenerator<T> source;
|
72 |
+
std::atomic<int> num_read;
|
73 |
+
};
|
74 |
+
|
75 |
+
std::shared_ptr<State> state_;
|
76 |
+
};
|
77 |
+
|
78 |
+
} // namespace util
|
79 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/builder.h
ADDED
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <type_traits>
|
23 |
+
#include <vector>
|
24 |
+
|
25 |
+
#include "arrow/array.h"
|
26 |
+
#include "arrow/array/builder_binary.h"
|
27 |
+
#include "arrow/array/builder_primitive.h"
|
28 |
+
#include "arrow/array/builder_time.h"
|
29 |
+
#include "arrow/buffer.h"
|
30 |
+
#include "arrow/testing/gtest_util.h"
|
31 |
+
#include "arrow/type_fwd.h"
|
32 |
+
#include "arrow/util/bit_util.h"
|
33 |
+
#include "arrow/visit_type_inline.h"
|
34 |
+
|
35 |
+
namespace arrow {
|
36 |
+
|
37 |
+
// ArrayFromVector: construct an Array from vectors of C values
|
38 |
+
|
39 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
40 |
+
void ArrayFromVector(const std::shared_ptr<DataType>& type,
|
41 |
+
const std::vector<bool>& is_valid, const std::vector<C_TYPE>& values,
|
42 |
+
std::shared_ptr<Array>* out) {
|
43 |
+
auto type_id = TYPE::type_id;
|
44 |
+
ASSERT_EQ(type_id, type->id())
|
45 |
+
<< "template parameter and concrete DataType instance don't agree";
|
46 |
+
|
47 |
+
std::unique_ptr<ArrayBuilder> builder_ptr;
|
48 |
+
ASSERT_OK(MakeBuilder(default_memory_pool(), type, &builder_ptr));
|
49 |
+
// Get the concrete builder class to access its Append() specializations
|
50 |
+
auto& builder = dynamic_cast<typename TypeTraits<TYPE>::BuilderType&>(*builder_ptr);
|
51 |
+
|
52 |
+
for (size_t i = 0; i < values.size(); ++i) {
|
53 |
+
if (is_valid[i]) {
|
54 |
+
ASSERT_OK(builder.Append(values[i]));
|
55 |
+
} else {
|
56 |
+
ASSERT_OK(builder.AppendNull());
|
57 |
+
}
|
58 |
+
}
|
59 |
+
ASSERT_OK(builder.Finish(out));
|
60 |
+
}
|
61 |
+
|
62 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
63 |
+
void ArrayFromVector(const std::shared_ptr<DataType>& type,
|
64 |
+
const std::vector<C_TYPE>& values, std::shared_ptr<Array>* out) {
|
65 |
+
auto type_id = TYPE::type_id;
|
66 |
+
ASSERT_EQ(type_id, type->id())
|
67 |
+
<< "template parameter and concrete DataType instance don't agree";
|
68 |
+
|
69 |
+
std::unique_ptr<ArrayBuilder> builder_ptr;
|
70 |
+
ASSERT_OK(MakeBuilder(default_memory_pool(), type, &builder_ptr));
|
71 |
+
// Get the concrete builder class to access its Append() specializations
|
72 |
+
auto& builder = dynamic_cast<typename TypeTraits<TYPE>::BuilderType&>(*builder_ptr);
|
73 |
+
|
74 |
+
for (size_t i = 0; i < values.size(); ++i) {
|
75 |
+
ASSERT_OK(builder.Append(values[i]));
|
76 |
+
}
|
77 |
+
ASSERT_OK(builder.Finish(out));
|
78 |
+
}
|
79 |
+
|
80 |
+
// Overloads without a DataType argument, for parameterless types
|
81 |
+
|
82 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
83 |
+
void ArrayFromVector(const std::vector<bool>& is_valid, const std::vector<C_TYPE>& values,
|
84 |
+
std::shared_ptr<Array>* out) {
|
85 |
+
auto type = TypeTraits<TYPE>::type_singleton();
|
86 |
+
ArrayFromVector<TYPE, C_TYPE>(type, is_valid, values, out);
|
87 |
+
}
|
88 |
+
|
89 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
90 |
+
void ArrayFromVector(const std::vector<C_TYPE>& values, std::shared_ptr<Array>* out) {
|
91 |
+
auto type = TypeTraits<TYPE>::type_singleton();
|
92 |
+
ArrayFromVector<TYPE, C_TYPE>(type, values, out);
|
93 |
+
}
|
94 |
+
|
95 |
+
// ChunkedArrayFromVector: construct a ChunkedArray from vectors of C values
|
96 |
+
|
97 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
98 |
+
void ChunkedArrayFromVector(const std::shared_ptr<DataType>& type,
|
99 |
+
const std::vector<std::vector<bool>>& is_valid,
|
100 |
+
const std::vector<std::vector<C_TYPE>>& values,
|
101 |
+
std::shared_ptr<ChunkedArray>* out) {
|
102 |
+
ArrayVector chunks;
|
103 |
+
ASSERT_EQ(is_valid.size(), values.size());
|
104 |
+
for (size_t i = 0; i < values.size(); ++i) {
|
105 |
+
std::shared_ptr<Array> array;
|
106 |
+
ArrayFromVector<TYPE, C_TYPE>(type, is_valid[i], values[i], &array);
|
107 |
+
chunks.push_back(array);
|
108 |
+
}
|
109 |
+
*out = std::make_shared<ChunkedArray>(chunks);
|
110 |
+
}
|
111 |
+
|
112 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
113 |
+
void ChunkedArrayFromVector(const std::shared_ptr<DataType>& type,
|
114 |
+
const std::vector<std::vector<C_TYPE>>& values,
|
115 |
+
std::shared_ptr<ChunkedArray>* out) {
|
116 |
+
ArrayVector chunks;
|
117 |
+
for (size_t i = 0; i < values.size(); ++i) {
|
118 |
+
std::shared_ptr<Array> array;
|
119 |
+
ArrayFromVector<TYPE, C_TYPE>(type, values[i], &array);
|
120 |
+
chunks.push_back(array);
|
121 |
+
}
|
122 |
+
*out = std::make_shared<ChunkedArray>(chunks);
|
123 |
+
}
|
124 |
+
|
125 |
+
// Overloads without a DataType argument, for parameterless types
|
126 |
+
|
127 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
128 |
+
void ChunkedArrayFromVector(const std::vector<std::vector<bool>>& is_valid,
|
129 |
+
const std::vector<std::vector<C_TYPE>>& values,
|
130 |
+
std::shared_ptr<ChunkedArray>* out) {
|
131 |
+
auto type = TypeTraits<TYPE>::type_singleton();
|
132 |
+
ChunkedArrayFromVector<TYPE, C_TYPE>(type, is_valid, values, out);
|
133 |
+
}
|
134 |
+
|
135 |
+
template <typename TYPE, typename C_TYPE = typename TYPE::c_type>
|
136 |
+
void ChunkedArrayFromVector(const std::vector<std::vector<C_TYPE>>& values,
|
137 |
+
std::shared_ptr<ChunkedArray>* out) {
|
138 |
+
auto type = TypeTraits<TYPE>::type_singleton();
|
139 |
+
ChunkedArrayFromVector<TYPE, C_TYPE>(type, values, out);
|
140 |
+
}
|
141 |
+
|
142 |
+
template <typename BuilderType>
|
143 |
+
void FinishAndCheckPadding(BuilderType* builder, std::shared_ptr<Array>* out) {
|
144 |
+
ASSERT_OK_AND_ASSIGN(*out, builder->Finish());
|
145 |
+
AssertZeroPadded(**out);
|
146 |
+
TestInitialized(**out);
|
147 |
+
}
|
148 |
+
|
149 |
+
template <class T, class Builder>
|
150 |
+
Status MakeArray(const std::vector<uint8_t>& valid_bytes, const std::vector<T>& values,
|
151 |
+
int64_t size, Builder* builder, std::shared_ptr<Array>* out) {
|
152 |
+
// Append the first 1000
|
153 |
+
for (int64_t i = 0; i < size; ++i) {
|
154 |
+
if (valid_bytes[i] > 0) {
|
155 |
+
RETURN_NOT_OK(builder->Append(values[i]));
|
156 |
+
} else {
|
157 |
+
RETURN_NOT_OK(builder->AppendNull());
|
158 |
+
}
|
159 |
+
}
|
160 |
+
return builder->Finish(out);
|
161 |
+
}
|
162 |
+
|
163 |
+
template <typename Fn>
|
164 |
+
struct VisitBuilder {
|
165 |
+
template <typename T, typename BuilderType = typename TypeTraits<T>::BuilderType,
|
166 |
+
// need to let SFINAE drop this Visit when it would result in
|
167 |
+
// [](NullBuilder*){}(double_builder)
|
168 |
+
typename = decltype(std::declval<Fn>()(std::declval<BuilderType*>()))>
|
169 |
+
Status Visit(const T&, ArrayBuilder* builder, Fn&& fn) {
|
170 |
+
fn(internal::checked_cast<BuilderType*>(builder));
|
171 |
+
return Status::OK();
|
172 |
+
}
|
173 |
+
|
174 |
+
Status Visit(const DataType& t, ArrayBuilder* builder, Fn&& fn) {
|
175 |
+
return Status::NotImplemented("visiting builders of type ", t);
|
176 |
+
}
|
177 |
+
};
|
178 |
+
|
179 |
+
template <typename Fn>
|
180 |
+
Result<std::shared_ptr<Array>> ArrayFromBuilderVisitor(
|
181 |
+
const std::shared_ptr<DataType>& type, int64_t initial_capacity,
|
182 |
+
int64_t visitor_repetitions, Fn&& fn) {
|
183 |
+
std::unique_ptr<ArrayBuilder> builder;
|
184 |
+
RETURN_NOT_OK(MakeBuilder(default_memory_pool(), type, &builder));
|
185 |
+
|
186 |
+
if (initial_capacity != 0) {
|
187 |
+
RETURN_NOT_OK(builder->Resize(initial_capacity));
|
188 |
+
}
|
189 |
+
|
190 |
+
VisitBuilder<Fn> visitor;
|
191 |
+
for (int64_t i = 0; i < visitor_repetitions; ++i) {
|
192 |
+
RETURN_NOT_OK(
|
193 |
+
VisitTypeInline(*builder->type(), &visitor, builder.get(), std::forward<Fn>(fn)));
|
194 |
+
}
|
195 |
+
|
196 |
+
std::shared_ptr<Array> out;
|
197 |
+
RETURN_NOT_OK(builder->Finish(&out));
|
198 |
+
return std::move(out);
|
199 |
+
}
|
200 |
+
|
201 |
+
template <typename Fn>
|
202 |
+
Result<std::shared_ptr<Array>> ArrayFromBuilderVisitor(
|
203 |
+
const std::shared_ptr<DataType>& type, int64_t length, Fn&& fn) {
|
204 |
+
return ArrayFromBuilderVisitor(type, length, length, std::forward<Fn>(fn));
|
205 |
+
}
|
206 |
+
|
207 |
+
template <typename T>
|
208 |
+
static inline Status GetBitmapFromVector(const std::vector<T>& is_valid,
|
209 |
+
std::shared_ptr<Buffer>* result) {
|
210 |
+
size_t length = is_valid.size();
|
211 |
+
|
212 |
+
ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateEmptyBitmap(length));
|
213 |
+
|
214 |
+
uint8_t* bitmap = buffer->mutable_data();
|
215 |
+
for (size_t i = 0; i < static_cast<size_t>(length); ++i) {
|
216 |
+
if (is_valid[i]) {
|
217 |
+
bit_util::SetBit(bitmap, i);
|
218 |
+
}
|
219 |
+
}
|
220 |
+
|
221 |
+
*result = buffer;
|
222 |
+
return Status::OK();
|
223 |
+
}
|
224 |
+
|
225 |
+
template <typename T>
|
226 |
+
inline void BitmapFromVector(const std::vector<T>& is_valid,
|
227 |
+
std::shared_ptr<Buffer>* out) {
|
228 |
+
ASSERT_OK(GetBitmapFromVector(is_valid, out));
|
229 |
+
}
|
230 |
+
|
231 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/executor_util.h
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include "arrow/util/thread_pool.h"
|
21 |
+
|
22 |
+
namespace arrow {
|
23 |
+
|
24 |
+
/// An executor which synchronously runs the task as part of the SpawnReal call.
|
25 |
+
class MockExecutor : public internal::Executor {
|
26 |
+
public:
|
27 |
+
int GetCapacity() override { return 0; }
|
28 |
+
|
29 |
+
Status SpawnReal(internal::TaskHints hints, internal::FnOnce<void()> task, StopToken,
|
30 |
+
StopCallback&&) override {
|
31 |
+
spawn_count++;
|
32 |
+
std::move(task)();
|
33 |
+
return Status::OK();
|
34 |
+
}
|
35 |
+
|
36 |
+
int spawn_count = 0;
|
37 |
+
};
|
38 |
+
|
39 |
+
/// An executor which does not actually run the task. Can be used to simulate situations
|
40 |
+
/// where the executor schedules a task in a long queue and doesn't get around to running
|
41 |
+
/// it for a while
|
42 |
+
class DelayedExecutor : public internal::Executor {
|
43 |
+
public:
|
44 |
+
int GetCapacity() override { return 0; }
|
45 |
+
|
46 |
+
Status SpawnReal(internal::TaskHints hints, internal::FnOnce<void()> task, StopToken,
|
47 |
+
StopCallback&&) override {
|
48 |
+
captured_tasks.push_back(std::move(task));
|
49 |
+
return Status::OK();
|
50 |
+
}
|
51 |
+
|
52 |
+
std::vector<internal::FnOnce<void()>> captured_tasks;
|
53 |
+
};
|
54 |
+
|
55 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/generator.h
ADDED
@@ -0,0 +1,321 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
#include <string>
|
23 |
+
#include <vector>
|
24 |
+
|
25 |
+
#include "arrow/array/array_base.h"
|
26 |
+
#include "arrow/compute/type_fwd.h"
|
27 |
+
#include "arrow/testing/gtest_util.h"
|
28 |
+
#include "arrow/testing/visibility.h"
|
29 |
+
#include "arrow/type_fwd.h"
|
30 |
+
|
31 |
+
namespace arrow {
|
32 |
+
|
33 |
+
class ARROW_TESTING_EXPORT ConstantArrayGenerator {
|
34 |
+
public:
|
35 |
+
/// \brief Generates a constant BooleanArray
|
36 |
+
///
|
37 |
+
/// \param[in] size the size of the array to generate
|
38 |
+
/// \param[in] value to repeat
|
39 |
+
///
|
40 |
+
/// \return a generated Array
|
41 |
+
static std::shared_ptr<Array> Boolean(int64_t size, bool value = false);
|
42 |
+
|
43 |
+
/// \brief Generates a constant UInt8Array
|
44 |
+
///
|
45 |
+
/// \param[in] size the size of the array to generate
|
46 |
+
/// \param[in] value to repeat
|
47 |
+
///
|
48 |
+
/// \return a generated Array
|
49 |
+
static std::shared_ptr<Array> UInt8(int64_t size, uint8_t value = 0);
|
50 |
+
|
51 |
+
/// \brief Generates a constant Int8Array
|
52 |
+
///
|
53 |
+
/// \param[in] size the size of the array to generate
|
54 |
+
/// \param[in] value to repeat
|
55 |
+
///
|
56 |
+
/// \return a generated Array
|
57 |
+
static std::shared_ptr<Array> Int8(int64_t size, int8_t value = 0);
|
58 |
+
|
59 |
+
/// \brief Generates a constant UInt16Array
|
60 |
+
///
|
61 |
+
/// \param[in] size the size of the array to generate
|
62 |
+
/// \param[in] value to repeat
|
63 |
+
///
|
64 |
+
/// \return a generated Array
|
65 |
+
static std::shared_ptr<Array> UInt16(int64_t size, uint16_t value = 0);
|
66 |
+
|
67 |
+
/// \brief Generates a constant UInt16Array
|
68 |
+
///
|
69 |
+
/// \param[in] size the size of the array to generate
|
70 |
+
/// \param[in] value to repeat
|
71 |
+
///
|
72 |
+
/// \return a generated Array
|
73 |
+
static std::shared_ptr<Array> Int16(int64_t size, int16_t value = 0);
|
74 |
+
|
75 |
+
/// \brief Generates a constant UInt32Array
|
76 |
+
///
|
77 |
+
/// \param[in] size the size of the array to generate
|
78 |
+
/// \param[in] value to repeat
|
79 |
+
///
|
80 |
+
/// \return a generated Array
|
81 |
+
static std::shared_ptr<Array> UInt32(int64_t size, uint32_t value = 0);
|
82 |
+
|
83 |
+
/// \brief Generates a constant UInt32Array
|
84 |
+
///
|
85 |
+
/// \param[in] size the size of the array to generate
|
86 |
+
/// \param[in] value to repeat
|
87 |
+
///
|
88 |
+
/// \return a generated Array
|
89 |
+
static std::shared_ptr<Array> Int32(int64_t size, int32_t value = 0);
|
90 |
+
|
91 |
+
/// \brief Generates a constant UInt64Array
|
92 |
+
///
|
93 |
+
/// \param[in] size the size of the array to generate
|
94 |
+
/// \param[in] value to repeat
|
95 |
+
///
|
96 |
+
/// \return a generated Array
|
97 |
+
static std::shared_ptr<Array> UInt64(int64_t size, uint64_t value = 0);
|
98 |
+
|
99 |
+
/// \brief Generates a constant UInt64Array
|
100 |
+
///
|
101 |
+
/// \param[in] size the size of the array to generate
|
102 |
+
/// \param[in] value to repeat
|
103 |
+
///
|
104 |
+
/// \return a generated Array
|
105 |
+
static std::shared_ptr<Array> Int64(int64_t size, int64_t value = 0);
|
106 |
+
|
107 |
+
/// \brief Generates a constant Float32Array
|
108 |
+
///
|
109 |
+
/// \param[in] size the size of the array to generate
|
110 |
+
/// \param[in] value to repeat
|
111 |
+
///
|
112 |
+
/// \return a generated Array
|
113 |
+
static std::shared_ptr<Array> Float32(int64_t size, float value = 0);
|
114 |
+
|
115 |
+
/// \brief Generates a constant Float64Array
|
116 |
+
///
|
117 |
+
/// \param[in] size the size of the array to generate
|
118 |
+
/// \param[in] value to repeat
|
119 |
+
///
|
120 |
+
/// \return a generated Array
|
121 |
+
static std::shared_ptr<Array> Float64(int64_t size, double value = 0);
|
122 |
+
|
123 |
+
/// \brief Generates a constant StringArray
|
124 |
+
///
|
125 |
+
/// \param[in] size the size of the array to generate
|
126 |
+
/// \param[in] value to repeat
|
127 |
+
///
|
128 |
+
/// \return a generated Array
|
129 |
+
static std::shared_ptr<Array> String(int64_t size, std::string value = "");
|
130 |
+
|
131 |
+
template <typename ArrowType, typename CType = typename ArrowType::c_type>
|
132 |
+
static std::shared_ptr<Array> Numeric(int64_t size, CType value = 0) {
|
133 |
+
switch (ArrowType::type_id) {
|
134 |
+
case Type::BOOL:
|
135 |
+
return Boolean(size, static_cast<bool>(value));
|
136 |
+
case Type::UINT8:
|
137 |
+
return UInt8(size, static_cast<uint8_t>(value));
|
138 |
+
case Type::INT8:
|
139 |
+
return Int8(size, static_cast<int8_t>(value));
|
140 |
+
case Type::UINT16:
|
141 |
+
return UInt16(size, static_cast<uint16_t>(value));
|
142 |
+
case Type::INT16:
|
143 |
+
return Int16(size, static_cast<int16_t>(value));
|
144 |
+
case Type::UINT32:
|
145 |
+
return UInt32(size, static_cast<uint32_t>(value));
|
146 |
+
case Type::INT32:
|
147 |
+
return Int32(size, static_cast<int32_t>(value));
|
148 |
+
case Type::UINT64:
|
149 |
+
return UInt64(size, static_cast<uint64_t>(value));
|
150 |
+
case Type::INT64:
|
151 |
+
return Int64(size, static_cast<int64_t>(value));
|
152 |
+
case Type::FLOAT:
|
153 |
+
return Float32(size, static_cast<float>(value));
|
154 |
+
case Type::DOUBLE:
|
155 |
+
return Float64(size, static_cast<double>(value));
|
156 |
+
case Type::INTERVAL_DAY_TIME:
|
157 |
+
case Type::DATE32: {
|
158 |
+
EXPECT_OK_AND_ASSIGN(auto viewed,
|
159 |
+
Int32(size, static_cast<uint32_t>(value))->View(date32()));
|
160 |
+
return viewed;
|
161 |
+
}
|
162 |
+
case Type::INTERVAL_MONTHS: {
|
163 |
+
EXPECT_OK_AND_ASSIGN(auto viewed,
|
164 |
+
Int32(size, static_cast<uint32_t>(value))
|
165 |
+
->View(std::make_shared<MonthIntervalType>()));
|
166 |
+
return viewed;
|
167 |
+
}
|
168 |
+
case Type::TIME32: {
|
169 |
+
EXPECT_OK_AND_ASSIGN(auto viewed,
|
170 |
+
Int32(size, static_cast<uint32_t>(value))
|
171 |
+
->View(std::make_shared<Time32Type>(TimeUnit::SECOND)));
|
172 |
+
return viewed;
|
173 |
+
}
|
174 |
+
case Type::TIME64: {
|
175 |
+
EXPECT_OK_AND_ASSIGN(auto viewed, Int64(size, static_cast<uint64_t>(value))
|
176 |
+
->View(std::make_shared<Time64Type>()));
|
177 |
+
return viewed;
|
178 |
+
}
|
179 |
+
case Type::DATE64: {
|
180 |
+
EXPECT_OK_AND_ASSIGN(auto viewed,
|
181 |
+
Int64(size, static_cast<uint64_t>(value))->View(date64()));
|
182 |
+
return viewed;
|
183 |
+
}
|
184 |
+
case Type::TIMESTAMP: {
|
185 |
+
EXPECT_OK_AND_ASSIGN(
|
186 |
+
auto viewed, Int64(size, static_cast<int64_t>(value))
|
187 |
+
->View(std::make_shared<TimestampType>(TimeUnit::SECOND)));
|
188 |
+
return viewed;
|
189 |
+
}
|
190 |
+
default:
|
191 |
+
return nullptr;
|
192 |
+
}
|
193 |
+
}
|
194 |
+
|
195 |
+
/// \brief Generates a constant Array of zeroes
|
196 |
+
///
|
197 |
+
/// \param[in] size the size of the array to generate
|
198 |
+
/// \param[in] type the type of the Array
|
199 |
+
///
|
200 |
+
/// \return a generated Array
|
201 |
+
static std::shared_ptr<Array> Zeroes(int64_t size,
|
202 |
+
const std::shared_ptr<DataType>& type);
|
203 |
+
|
204 |
+
/// \brief Generates a RecordBatch of zeroes
|
205 |
+
///
|
206 |
+
/// \param[in] size the size of the array to generate
|
207 |
+
/// \param[in] schema to conform to
|
208 |
+
///
|
209 |
+
/// This function is handy to return of RecordBatch of a desired shape.
|
210 |
+
///
|
211 |
+
/// \return a generated RecordBatch
|
212 |
+
static std::shared_ptr<RecordBatch> Zeroes(int64_t size,
|
213 |
+
const std::shared_ptr<Schema>& schema);
|
214 |
+
|
215 |
+
/// \brief Generates a RecordBatchReader by repeating a RecordBatch
|
216 |
+
///
|
217 |
+
/// \param[in] n_batch the number of times it repeats batch
|
218 |
+
/// \param[in] batch the RecordBatch to repeat
|
219 |
+
///
|
220 |
+
/// \return a generated RecordBatchReader
|
221 |
+
static std::shared_ptr<RecordBatchReader> Repeat(
|
222 |
+
int64_t n_batch, const std::shared_ptr<RecordBatch> batch);
|
223 |
+
|
224 |
+
/// \brief Generates a RecordBatchReader of zeroes batches
|
225 |
+
///
|
226 |
+
/// \param[in] n_batch the number of RecordBatch
|
227 |
+
/// \param[in] batch_size the size of each RecordBatch
|
228 |
+
/// \param[in] schema to conform to
|
229 |
+
///
|
230 |
+
/// \return a generated RecordBatchReader
|
231 |
+
static std::shared_ptr<RecordBatchReader> Zeroes(int64_t n_batch, int64_t batch_size,
|
232 |
+
const std::shared_ptr<Schema>& schema);
|
233 |
+
};
|
234 |
+
|
235 |
+
ARROW_TESTING_EXPORT
|
236 |
+
Result<std::shared_ptr<Array>> ScalarVectorToArray(const ScalarVector& scalars);
|
237 |
+
|
238 |
+
namespace gen {
|
239 |
+
|
240 |
+
class ARROW_TESTING_EXPORT ArrayGenerator {
|
241 |
+
public:
|
242 |
+
virtual ~ArrayGenerator() = default;
|
243 |
+
virtual Result<std::shared_ptr<Array>> Generate(int64_t num_rows) = 0;
|
244 |
+
virtual std::shared_ptr<DataType> type() const = 0;
|
245 |
+
};
|
246 |
+
|
247 |
+
// Same as DataGenerator below but instead of returning Result an ok status is EXPECT'd
|
248 |
+
class ARROW_TESTING_EXPORT GTestDataGenerator {
|
249 |
+
public:
|
250 |
+
virtual ~GTestDataGenerator() = default;
|
251 |
+
virtual std::shared_ptr<::arrow::RecordBatch> RecordBatch(int64_t num_rows) = 0;
|
252 |
+
virtual std::vector<std::shared_ptr<::arrow::RecordBatch>> RecordBatches(
|
253 |
+
int64_t rows_per_batch, int num_batches) = 0;
|
254 |
+
|
255 |
+
virtual ::arrow::compute::ExecBatch ExecBatch(int64_t num_rows) = 0;
|
256 |
+
virtual std::vector<::arrow::compute::ExecBatch> ExecBatches(int64_t rows_per_batch,
|
257 |
+
int num_batches) = 0;
|
258 |
+
|
259 |
+
virtual std::shared_ptr<::arrow::Table> Table(int64_t rows_per_chunk,
|
260 |
+
int num_chunks = 1) = 0;
|
261 |
+
virtual std::shared_ptr<::arrow::Schema> Schema() = 0;
|
262 |
+
};
|
263 |
+
|
264 |
+
class ARROW_TESTING_EXPORT DataGenerator {
|
265 |
+
public:
|
266 |
+
virtual ~DataGenerator() = default;
|
267 |
+
virtual Result<std::shared_ptr<::arrow::RecordBatch>> RecordBatch(int64_t num_rows) = 0;
|
268 |
+
virtual Result<std::vector<std::shared_ptr<::arrow::RecordBatch>>> RecordBatches(
|
269 |
+
int64_t rows_per_batch, int num_batches) = 0;
|
270 |
+
|
271 |
+
virtual Result<::arrow::compute::ExecBatch> ExecBatch(int64_t num_rows) = 0;
|
272 |
+
virtual Result<std::vector<::arrow::compute::ExecBatch>> ExecBatches(
|
273 |
+
int64_t rows_per_batch, int num_batches) = 0;
|
274 |
+
|
275 |
+
virtual Result<std::shared_ptr<::arrow::Table>> Table(int64_t rows_per_chunk,
|
276 |
+
int num_chunks = 1) = 0;
|
277 |
+
virtual std::shared_ptr<::arrow::Schema> Schema() = 0;
|
278 |
+
/// @brief Converts this generator to a variant that fails (in a googletest sense)
|
279 |
+
/// if any error is encountered.
|
280 |
+
virtual std::unique_ptr<GTestDataGenerator> FailOnError() = 0;
|
281 |
+
};
|
282 |
+
|
283 |
+
/// @brief A potentially named field
|
284 |
+
///
|
285 |
+
/// If name is not specified then a name will be generated automatically (e.g. f0, f1)
|
286 |
+
struct ARROW_TESTING_EXPORT GeneratorField {
|
287 |
+
public:
|
288 |
+
GeneratorField(std::shared_ptr<ArrayGenerator> gen) // NOLINT implicit conversion
|
289 |
+
: name(), gen(std::move(gen)) {}
|
290 |
+
GeneratorField(std::string name, std::shared_ptr<ArrayGenerator> gen)
|
291 |
+
: name(std::move(name)), gen(std::move(gen)) {}
|
292 |
+
|
293 |
+
std::optional<std::string> name;
|
294 |
+
std::shared_ptr<ArrayGenerator> gen;
|
295 |
+
};
|
296 |
+
|
297 |
+
/// Create a table generator with the given fields
|
298 |
+
ARROW_TESTING_EXPORT std::shared_ptr<DataGenerator> Gen(
|
299 |
+
std::vector<GeneratorField> column_gens);
|
300 |
+
|
301 |
+
/// make a generator that returns a constant value
|
302 |
+
ARROW_TESTING_EXPORT std::shared_ptr<ArrayGenerator> Constant(
|
303 |
+
std::shared_ptr<Scalar> value);
|
304 |
+
/// make a generator that returns an incrementing value
|
305 |
+
///
|
306 |
+
/// Note: overflow is not prevented standard unsigned integer overflow applies
|
307 |
+
ARROW_TESTING_EXPORT std::shared_ptr<ArrayGenerator> Step(uint32_t start = 0,
|
308 |
+
uint32_t step = 1,
|
309 |
+
bool signed_int = false);
|
310 |
+
/// make a generator that returns a random value
|
311 |
+
ARROW_TESTING_EXPORT std::shared_ptr<ArrayGenerator> Random(
|
312 |
+
std::shared_ptr<DataType> type);
|
313 |
+
/// TODO(if-needed) could add a repeat-scalars generator, e.g. Repeat({1, 2, 3}) for
|
314 |
+
/// 1,2,3,1,2,3,1
|
315 |
+
///
|
316 |
+
/// TODO(if-needed) could add a repeat-from-json generator e.g. Repeat(int32(), "[1, 2,
|
317 |
+
/// 3]")), same behavior as repeat-scalars
|
318 |
+
|
319 |
+
} // namespace gen
|
320 |
+
|
321 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/gtest_compat.h
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <gtest/gtest.h>
|
21 |
+
|
22 |
+
// GTest < 1.11
|
23 |
+
#ifndef GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST
|
24 |
+
#define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(A)
|
25 |
+
#endif
|
26 |
+
// GTest < 1.10
|
27 |
+
#ifndef TYPED_TEST_SUITE
|
28 |
+
#define TYPED_TEST_SUITE TYPED_TEST_CASE
|
29 |
+
#define TYPED_TEST_SUITE_P TYPED_TEST_CASE_P
|
30 |
+
#define INSTANTIATE_TEST_SUITE_P INSTANTIATE_TEST_CASE_P
|
31 |
+
#define REGISTER_TYPED_TEST_SUITE_P REGISTER_TYPED_TEST_CASE_P
|
32 |
+
#define INSTANTIATE_TYPED_TEST_SUITE_P INSTANTIATE_TYPED_TEST_CASE_P
|
33 |
+
#endif
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/random.h
ADDED
@@ -0,0 +1,698 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm>
|
21 |
+
#include <cassert>
|
22 |
+
#include <cstdint>
|
23 |
+
#include <limits>
|
24 |
+
#include <memory>
|
25 |
+
#include <random>
|
26 |
+
#include <vector>
|
27 |
+
|
28 |
+
#include "arrow/testing/uniform_real.h"
|
29 |
+
#include "arrow/testing/visibility.h"
|
30 |
+
#include "arrow/type.h"
|
31 |
+
|
32 |
+
namespace arrow {
|
33 |
+
|
34 |
+
class Array;
|
35 |
+
|
36 |
+
namespace random {
|
37 |
+
|
38 |
+
using SeedType = int32_t;
|
39 |
+
constexpr SeedType kSeedMax = std::numeric_limits<SeedType>::max();
|
40 |
+
|
41 |
+
class ARROW_TESTING_EXPORT RandomArrayGenerator {
|
42 |
+
public:
|
43 |
+
explicit RandomArrayGenerator(SeedType seed)
|
44 |
+
: seed_distribution_(static_cast<SeedType>(1), kSeedMax), seed_rng_(seed) {}
|
45 |
+
|
46 |
+
/// \brief Generate a null bitmap
|
47 |
+
///
|
48 |
+
/// \param[in] size the size of the bitmap to generate
|
49 |
+
/// \param[in] null_probability the probability of a bit being zero
|
50 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
51 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
52 |
+
///
|
53 |
+
/// \return a generated Buffer
|
54 |
+
std::shared_ptr<Buffer> NullBitmap(int64_t size, double null_probability = 0,
|
55 |
+
int64_t alignment = kDefaultBufferAlignment,
|
56 |
+
MemoryPool* memory_pool = default_memory_pool());
|
57 |
+
|
58 |
+
/// \brief Generate a random BooleanArray
|
59 |
+
///
|
60 |
+
/// \param[in] size the size of the array to generate
|
61 |
+
/// \param[in] true_probability the probability of a value being 1 / bit-set
|
62 |
+
/// \param[in] null_probability the probability of a value being null
|
63 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
64 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
65 |
+
///
|
66 |
+
/// \return a generated Array
|
67 |
+
std::shared_ptr<Array> Boolean(int64_t size, double true_probability,
|
68 |
+
double null_probability = 0,
|
69 |
+
int64_t alignment = kDefaultBufferAlignment,
|
70 |
+
MemoryPool* memory_pool = default_memory_pool());
|
71 |
+
/// \brief Generate a random UInt8Array
|
72 |
+
///
|
73 |
+
/// \param[in] size the size of the array to generate
|
74 |
+
/// \param[in] min the lower bound of the uniform distribution
|
75 |
+
/// \param[in] max the upper bound of the uniform distribution
|
76 |
+
/// \param[in] null_probability the probability of a value being null
|
77 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
78 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
79 |
+
///
|
80 |
+
/// \return a generated Array
|
81 |
+
std::shared_ptr<Array> UInt8(int64_t size, uint8_t min, uint8_t max,
|
82 |
+
double null_probability = 0,
|
83 |
+
int64_t alignment = kDefaultBufferAlignment,
|
84 |
+
MemoryPool* memory_pool = default_memory_pool());
|
85 |
+
|
86 |
+
/// \brief Generate a random Int8Array
|
87 |
+
///
|
88 |
+
/// \param[in] size the size of the array to generate
|
89 |
+
/// \param[in] min the lower bound of the uniform distribution
|
90 |
+
/// \param[in] max the upper bound of the uniform distribution
|
91 |
+
/// \param[in] null_probability the probability of a value being null
|
92 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
93 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
94 |
+
///
|
95 |
+
/// \return a generated Array
|
96 |
+
std::shared_ptr<Array> Int8(int64_t size, int8_t min, int8_t max,
|
97 |
+
double null_probability = 0,
|
98 |
+
int64_t alignment = kDefaultBufferAlignment,
|
99 |
+
MemoryPool* memory_pool = default_memory_pool());
|
100 |
+
|
101 |
+
/// \brief Generate a random UInt16Array
|
102 |
+
///
|
103 |
+
/// \param[in] size the size of the array to generate
|
104 |
+
/// \param[in] min the lower bound of the uniform distribution
|
105 |
+
/// \param[in] max the upper bound of the uniform distribution
|
106 |
+
/// \param[in] null_probability the probability of a value being null
|
107 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
108 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
109 |
+
///
|
110 |
+
/// \return a generated Array
|
111 |
+
std::shared_ptr<Array> UInt16(int64_t size, uint16_t min, uint16_t max,
|
112 |
+
double null_probability = 0,
|
113 |
+
int64_t alignment = kDefaultBufferAlignment,
|
114 |
+
MemoryPool* memory_pool = default_memory_pool());
|
115 |
+
|
116 |
+
/// \brief Generate a random Int16Array
|
117 |
+
///
|
118 |
+
/// \param[in] size the size of the array to generate
|
119 |
+
/// \param[in] min the lower bound of the uniform distribution
|
120 |
+
/// \param[in] max the upper bound of the uniform distribution
|
121 |
+
/// \param[in] null_probability the probability of a value being null
|
122 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
123 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
124 |
+
///
|
125 |
+
/// \return a generated Array
|
126 |
+
std::shared_ptr<Array> Int16(int64_t size, int16_t min, int16_t max,
|
127 |
+
double null_probability = 0,
|
128 |
+
int64_t alignment = kDefaultBufferAlignment,
|
129 |
+
MemoryPool* memory_pool = default_memory_pool());
|
130 |
+
|
131 |
+
/// \brief Generate a random UInt32Array
|
132 |
+
///
|
133 |
+
/// \param[in] size the size of the array to generate
|
134 |
+
/// \param[in] min the lower bound of the uniform distribution
|
135 |
+
/// \param[in] max the upper bound of the uniform distribution
|
136 |
+
/// \param[in] null_probability the probability of a value being null
|
137 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
138 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
139 |
+
///
|
140 |
+
/// \return a generated Array
|
141 |
+
std::shared_ptr<Array> UInt32(int64_t size, uint32_t min, uint32_t max,
|
142 |
+
double null_probability = 0,
|
143 |
+
int64_t alignment = kDefaultBufferAlignment,
|
144 |
+
MemoryPool* memory_pool = default_memory_pool());
|
145 |
+
|
146 |
+
/// \brief Generate a random Int32Array
|
147 |
+
///
|
148 |
+
/// \param[in] size the size of the array to generate
|
149 |
+
/// \param[in] min the lower bound of the uniform distribution
|
150 |
+
/// \param[in] max the upper bound of the uniform distribution
|
151 |
+
/// \param[in] null_probability the probability of a value being null
|
152 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
153 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
154 |
+
///
|
155 |
+
/// \return a generated Array
|
156 |
+
std::shared_ptr<Array> Int32(int64_t size, int32_t min, int32_t max,
|
157 |
+
double null_probability = 0,
|
158 |
+
int64_t alignment = kDefaultBufferAlignment,
|
159 |
+
MemoryPool* memory_pool = default_memory_pool());
|
160 |
+
|
161 |
+
/// \brief Generate a random UInt64Array
|
162 |
+
///
|
163 |
+
/// \param[in] size the size of the array to generate
|
164 |
+
/// \param[in] min the lower bound of the uniform distribution
|
165 |
+
/// \param[in] max the upper bound of the uniform distribution
|
166 |
+
/// \param[in] null_probability the probability of a value being null
|
167 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
168 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
169 |
+
///
|
170 |
+
/// \return a generated Array
|
171 |
+
std::shared_ptr<Array> UInt64(int64_t size, uint64_t min, uint64_t max,
|
172 |
+
double null_probability = 0,
|
173 |
+
int64_t alignment = kDefaultBufferAlignment,
|
174 |
+
MemoryPool* memory_pool = default_memory_pool());
|
175 |
+
|
176 |
+
/// \brief Generate a random Int64Array
|
177 |
+
///
|
178 |
+
/// \param[in] size the size of the array to generate
|
179 |
+
/// \param[in] min the lower bound of the uniform distribution
|
180 |
+
/// \param[in] max the upper bound of the uniform distribution
|
181 |
+
/// \param[in] null_probability the probability of a value being null
|
182 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
183 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
184 |
+
///
|
185 |
+
/// \return a generated Array
|
186 |
+
std::shared_ptr<Array> Int64(int64_t size, int64_t min, int64_t max,
|
187 |
+
double null_probability = 0,
|
188 |
+
int64_t alignment = kDefaultBufferAlignment,
|
189 |
+
MemoryPool* memory_pool = default_memory_pool());
|
190 |
+
|
191 |
+
/// \brief Generate a random HalfFloatArray
|
192 |
+
///
|
193 |
+
/// \param[in] size the size of the array to generate
|
194 |
+
/// \param[in] min the lower bound of the distribution
|
195 |
+
/// \param[in] max the upper bound of the distribution
|
196 |
+
/// \param[in] null_probability the probability of a value being null
|
197 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
198 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
199 |
+
///
|
200 |
+
/// \return a generated Array
|
201 |
+
std::shared_ptr<Array> Float16(int64_t size, int16_t min, int16_t max,
|
202 |
+
double null_probability = 0,
|
203 |
+
int64_t alignment = kDefaultBufferAlignment,
|
204 |
+
MemoryPool* memory_pool = default_memory_pool());
|
205 |
+
|
206 |
+
/// \brief Generate a random FloatArray
|
207 |
+
///
|
208 |
+
/// \param[in] size the size of the array to generate
|
209 |
+
/// \param[in] min the lower bound of the uniform distribution
|
210 |
+
/// \param[in] max the upper bound of the uniform distribution
|
211 |
+
/// \param[in] null_probability the probability of a value being null
|
212 |
+
/// \param[in] nan_probability the probability of a value being NaN
|
213 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
214 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
215 |
+
///
|
216 |
+
/// \return a generated Array
|
217 |
+
std::shared_ptr<Array> Float32(int64_t size, float min, float max,
|
218 |
+
double null_probability = 0, double nan_probability = 0,
|
219 |
+
int64_t alignment = kDefaultBufferAlignment,
|
220 |
+
MemoryPool* memory_pool = default_memory_pool());
|
221 |
+
|
222 |
+
/// \brief Generate a random DoubleArray
|
223 |
+
///
|
224 |
+
/// \param[in] size the size of the array to generate
|
225 |
+
/// \param[in] min the lower bound of the uniform distribution
|
226 |
+
/// \param[in] max the upper bound of the uniform distribution
|
227 |
+
/// \param[in] null_probability the probability of a value being null
|
228 |
+
/// \param[in] nan_probability the probability of a value being NaN
|
229 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
230 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
231 |
+
///
|
232 |
+
/// \return a generated Array
|
233 |
+
std::shared_ptr<Array> Float64(int64_t size, double min, double max,
|
234 |
+
double null_probability = 0, double nan_probability = 0,
|
235 |
+
int64_t alignment = kDefaultBufferAlignment,
|
236 |
+
MemoryPool* memory_pool = default_memory_pool());
|
237 |
+
|
238 |
+
/// \brief Generate a random Date64Array
|
239 |
+
///
|
240 |
+
/// \param[in] size the size of the array to generate
|
241 |
+
/// \param[in] min the lower bound of the uniform distribution
|
242 |
+
/// \param[in] max the upper bound of the uniform distribution
|
243 |
+
/// \param[in] null_probability the probability of a value being null
|
244 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
245 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
246 |
+
///
|
247 |
+
/// \return a generated Array
|
248 |
+
std::shared_ptr<Array> Date64(int64_t size, int64_t min, int64_t max,
|
249 |
+
double null_probability = 0,
|
250 |
+
int64_t alignment = kDefaultBufferAlignment,
|
251 |
+
MemoryPool* memory_pool = default_memory_pool());
|
252 |
+
|
253 |
+
template <typename ArrowType, typename CType = typename ArrowType::c_type>
|
254 |
+
std::shared_ptr<Array> Numeric(int64_t size, CType min, CType max,
|
255 |
+
double null_probability = 0,
|
256 |
+
int64_t alignment = kDefaultBufferAlignment,
|
257 |
+
MemoryPool* memory_pool = default_memory_pool()) {
|
258 |
+
switch (ArrowType::type_id) {
|
259 |
+
case Type::UINT8:
|
260 |
+
return UInt8(size, static_cast<uint8_t>(min), static_cast<uint8_t>(max),
|
261 |
+
null_probability, alignment, memory_pool);
|
262 |
+
case Type::INT8:
|
263 |
+
return Int8(size, static_cast<int8_t>(min), static_cast<int8_t>(max),
|
264 |
+
null_probability, alignment, memory_pool);
|
265 |
+
case Type::UINT16:
|
266 |
+
return UInt16(size, static_cast<uint16_t>(min), static_cast<uint16_t>(max),
|
267 |
+
null_probability, alignment, memory_pool);
|
268 |
+
case Type::INT16:
|
269 |
+
return Int16(size, static_cast<int16_t>(min), static_cast<int16_t>(max),
|
270 |
+
null_probability, alignment, memory_pool);
|
271 |
+
case Type::UINT32:
|
272 |
+
return UInt32(size, static_cast<uint32_t>(min), static_cast<uint32_t>(max),
|
273 |
+
null_probability, alignment, memory_pool);
|
274 |
+
case Type::INT32:
|
275 |
+
return Int32(size, static_cast<int32_t>(min), static_cast<int32_t>(max),
|
276 |
+
null_probability, alignment, memory_pool);
|
277 |
+
case Type::UINT64:
|
278 |
+
return UInt64(size, static_cast<uint64_t>(min), static_cast<uint64_t>(max),
|
279 |
+
null_probability, alignment, memory_pool);
|
280 |
+
case Type::INT64:
|
281 |
+
return Int64(size, static_cast<int64_t>(min), static_cast<int64_t>(max),
|
282 |
+
null_probability, alignment, memory_pool);
|
283 |
+
case Type::HALF_FLOAT:
|
284 |
+
return Float16(size, static_cast<int16_t>(min), static_cast<int16_t>(max),
|
285 |
+
null_probability, alignment, memory_pool);
|
286 |
+
case Type::FLOAT:
|
287 |
+
return Float32(size, static_cast<float>(min), static_cast<float>(max),
|
288 |
+
null_probability, /*nan_probability=*/0, alignment, memory_pool);
|
289 |
+
case Type::DOUBLE:
|
290 |
+
return Float64(size, static_cast<double>(min), static_cast<double>(max),
|
291 |
+
null_probability, /*nan_probability=*/0, alignment, memory_pool);
|
292 |
+
case Type::DATE64:
|
293 |
+
return Date64(size, static_cast<int64_t>(min), static_cast<int64_t>(max),
|
294 |
+
null_probability, alignment, memory_pool);
|
295 |
+
default:
|
296 |
+
return nullptr;
|
297 |
+
}
|
298 |
+
}
|
299 |
+
|
300 |
+
/// \brief Generate a random Decimal128Array
|
301 |
+
///
|
302 |
+
/// \param[in] type the type of the array to generate
|
303 |
+
/// (must be an instance of Decimal128Type)
|
304 |
+
/// \param[in] size the size of the array to generate
|
305 |
+
/// \param[in] null_probability the probability of a value being null
|
306 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
307 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
308 |
+
///
|
309 |
+
/// \return a generated Array
|
310 |
+
std::shared_ptr<Array> Decimal128(std::shared_ptr<DataType> type, int64_t size,
|
311 |
+
double null_probability = 0,
|
312 |
+
int64_t alignment = kDefaultBufferAlignment,
|
313 |
+
MemoryPool* memory_pool = default_memory_pool());
|
314 |
+
|
315 |
+
/// \brief Generate a random Decimal256Array
|
316 |
+
///
|
317 |
+
/// \param[in] type the type of the array to generate
|
318 |
+
/// (must be an instance of Decimal256Type)
|
319 |
+
/// \param[in] size the size of the array to generate
|
320 |
+
/// \param[in] null_probability the probability of a value being null
|
321 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
322 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
323 |
+
///
|
324 |
+
/// \return a generated Array
|
325 |
+
std::shared_ptr<Array> Decimal256(std::shared_ptr<DataType> type, int64_t size,
|
326 |
+
double null_probability = 0,
|
327 |
+
int64_t alignment = kDefaultBufferAlignment,
|
328 |
+
MemoryPool* memory_pool = default_memory_pool());
|
329 |
+
|
330 |
+
/// \brief Generate an array of offsets (for use in e.g. ListArray::FromArrays)
|
331 |
+
///
|
332 |
+
/// \param[in] size the size of the array to generate
|
333 |
+
/// \param[in] first_offset the first offset value (usually 0)
|
334 |
+
/// \param[in] last_offset the last offset value (usually the size of the child array)
|
335 |
+
/// \param[in] null_probability the probability of an offset being null
|
336 |
+
/// \param[in] force_empty_nulls if true, null offsets must have 0 "length"
|
337 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
338 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
339 |
+
///
|
340 |
+
/// \return a generated Array
|
341 |
+
std::shared_ptr<Array> Offsets(int64_t size, int32_t first_offset, int32_t last_offset,
|
342 |
+
double null_probability = 0,
|
343 |
+
bool force_empty_nulls = false,
|
344 |
+
int64_t alignment = kDefaultBufferAlignment,
|
345 |
+
MemoryPool* memory_pool = default_memory_pool());
|
346 |
+
|
347 |
+
std::shared_ptr<Array> LargeOffsets(int64_t size, int64_t first_offset,
|
348 |
+
int64_t last_offset, double null_probability = 0,
|
349 |
+
bool force_empty_nulls = false,
|
350 |
+
int64_t alignment = kDefaultBufferAlignment,
|
351 |
+
MemoryPool* memory_pool = default_memory_pool());
|
352 |
+
|
353 |
+
/// \brief Generate a random StringArray
|
354 |
+
///
|
355 |
+
/// \param[in] size the size of the array to generate
|
356 |
+
/// \param[in] min_length the lower bound of the string length
|
357 |
+
/// determined by the uniform distribution
|
358 |
+
/// \param[in] max_length the upper bound of the string length
|
359 |
+
/// determined by the uniform distribution
|
360 |
+
/// \param[in] null_probability the probability of a value being null
|
361 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
362 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
363 |
+
///
|
364 |
+
/// \return a generated Array
|
365 |
+
std::shared_ptr<Array> String(int64_t size, int32_t min_length, int32_t max_length,
|
366 |
+
double null_probability = 0,
|
367 |
+
int64_t alignment = kDefaultBufferAlignment,
|
368 |
+
MemoryPool* memory_pool = default_memory_pool());
|
369 |
+
|
370 |
+
/// \brief Generate a random StringViewArray
|
371 |
+
///
|
372 |
+
/// \param[in] size the size of the array to generate
|
373 |
+
/// \param[in] min_length the lower bound of the string length
|
374 |
+
/// determined by the uniform distribution
|
375 |
+
/// \param[in] max_length the upper bound of the string length
|
376 |
+
/// determined by the uniform distribution
|
377 |
+
/// \param[in] null_probability the probability of a value being null
|
378 |
+
/// \param[in] max_data_buffer_length the data buffer size at which
|
379 |
+
/// a new chunk will be generated
|
380 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
381 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
382 |
+
///
|
383 |
+
/// \return a generated Array
|
384 |
+
std::shared_ptr<Array> StringView(int64_t size, int32_t min_length, int32_t max_length,
|
385 |
+
double null_probability = 0,
|
386 |
+
std::optional<int64_t> max_data_buffer_length = {},
|
387 |
+
int64_t alignment = kDefaultBufferAlignment,
|
388 |
+
MemoryPool* memory_pool = default_memory_pool());
|
389 |
+
|
390 |
+
/// \brief Generate a random LargeStringArray
|
391 |
+
///
|
392 |
+
/// \param[in] size the size of the array to generate
|
393 |
+
/// \param[in] min_length the lower bound of the string length
|
394 |
+
/// determined by the uniform distribution
|
395 |
+
/// \param[in] max_length the upper bound of the string length
|
396 |
+
/// determined by the uniform distribution
|
397 |
+
/// \param[in] null_probability the probability of a value being null
|
398 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
399 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
400 |
+
///
|
401 |
+
/// \return a generated Array
|
402 |
+
std::shared_ptr<Array> LargeString(int64_t size, int32_t min_length, int32_t max_length,
|
403 |
+
double null_probability = 0,
|
404 |
+
int64_t alignment = kDefaultBufferAlignment,
|
405 |
+
MemoryPool* memory_pool = default_memory_pool());
|
406 |
+
|
407 |
+
/// \brief Generate a random StringArray with repeated values
|
408 |
+
///
|
409 |
+
/// \param[in] size the size of the array to generate
|
410 |
+
/// \param[in] unique the number of unique string values used
|
411 |
+
/// to populate the array
|
412 |
+
/// \param[in] min_length the lower bound of the string length
|
413 |
+
/// determined by the uniform distribution
|
414 |
+
/// \param[in] max_length the upper bound of the string length
|
415 |
+
/// determined by the uniform distribution
|
416 |
+
/// \param[in] null_probability the probability of a value being null
|
417 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
418 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
419 |
+
///
|
420 |
+
/// \return a generated Array
|
421 |
+
std::shared_ptr<Array> StringWithRepeats(
|
422 |
+
int64_t size, int64_t unique, int32_t min_length, int32_t max_length,
|
423 |
+
double null_probability = 0, int64_t alignment = kDefaultBufferAlignment,
|
424 |
+
MemoryPool* memory_pool = default_memory_pool());
|
425 |
+
|
426 |
+
/// \brief Like StringWithRepeats but return BinaryArray
|
427 |
+
std::shared_ptr<Array> BinaryWithRepeats(
|
428 |
+
int64_t size, int64_t unique, int32_t min_length, int32_t max_length,
|
429 |
+
double null_probability = 0, int64_t alignment = kDefaultBufferAlignment,
|
430 |
+
MemoryPool* memory_pool = default_memory_pool());
|
431 |
+
|
432 |
+
/// \brief Generate a random FixedSizeBinaryArray
|
433 |
+
///
|
434 |
+
/// \param[in] size the size of the array to generate
|
435 |
+
/// \param[in] byte_width the byte width of fixed-size binary items
|
436 |
+
/// \param[in] null_probability the probability of a value being null
|
437 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
438 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
439 |
+
///
|
440 |
+
/// \return a generated Array
|
441 |
+
std::shared_ptr<Array> FixedSizeBinary(int64_t size, int32_t byte_width,
|
442 |
+
double null_probability = 0,
|
443 |
+
int64_t alignment = kDefaultBufferAlignment,
|
444 |
+
MemoryPool* memory_pool = default_memory_pool());
|
445 |
+
|
446 |
+
/// \brief Generate a random ListArray
|
447 |
+
///
|
448 |
+
/// \param[in] values The underlying values array
|
449 |
+
/// \param[in] size The size of the generated list array
|
450 |
+
/// \param[in] null_probability the probability of a list value being null
|
451 |
+
/// \param[in] force_empty_nulls if true, null list entries must have 0 length
|
452 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
453 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
454 |
+
///
|
455 |
+
/// \return a generated Array
|
456 |
+
std::shared_ptr<Array> List(const Array& values, int64_t size,
|
457 |
+
double null_probability = 0, bool force_empty_nulls = false,
|
458 |
+
int64_t alignment = kDefaultBufferAlignment,
|
459 |
+
MemoryPool* memory_pool = default_memory_pool());
|
460 |
+
|
461 |
+
/// \brief Generate a random ListViewArray
|
462 |
+
///
|
463 |
+
/// \param[in] values The underlying values array
|
464 |
+
/// \param[in] size The size of the generated list array
|
465 |
+
/// \param[in] null_probability the probability of a list value being null
|
466 |
+
/// \param[in] force_empty_nulls if true, null list entries must have 0 length
|
467 |
+
/// must be set to 0
|
468 |
+
/// \param[in] coverage proportion of the values array covered by list-views
|
469 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
470 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
471 |
+
///
|
472 |
+
/// \return a generated Array
|
473 |
+
std::shared_ptr<Array> ListView(const Array& values, int64_t size,
|
474 |
+
double null_probability = 0,
|
475 |
+
bool force_empty_nulls = false, double coverage = 1.0,
|
476 |
+
int64_t alignment = kDefaultBufferAlignment,
|
477 |
+
MemoryPool* memory_pool = default_memory_pool());
|
478 |
+
|
479 |
+
/// \brief Generate a random LargeListViewArray
|
480 |
+
///
|
481 |
+
/// \param[in] values The underlying values array
|
482 |
+
/// \param[in] size The size of the generated list array
|
483 |
+
/// \param[in] null_probability the probability of a list value being null
|
484 |
+
/// \param[in] force_empty_nulls if true, null list entries must have 0 length
|
485 |
+
/// must be set to 0
|
486 |
+
/// \param[in] coverage proportion of the values array covered by list-views
|
487 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
488 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
489 |
+
///
|
490 |
+
/// \return a generated Array
|
491 |
+
std::shared_ptr<Array> LargeListView(const Array& values, int64_t size,
|
492 |
+
double null_probability = 0,
|
493 |
+
bool force_empty_nulls = false,
|
494 |
+
double coverage = 1.0,
|
495 |
+
int64_t alignment = kDefaultBufferAlignment,
|
496 |
+
MemoryPool* memory_pool = default_memory_pool());
|
497 |
+
|
498 |
+
/// \brief Generate a random MapArray
|
499 |
+
///
|
500 |
+
/// \param[in] keys The underlying keys array
|
501 |
+
/// \param[in] items The underlying items array
|
502 |
+
/// \param[in] size The size of the generated map array
|
503 |
+
/// \param[in] null_probability the probability of a map value being null
|
504 |
+
/// \param[in] force_empty_nulls if true, null map entries must have 0 length
|
505 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
506 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
507 |
+
///
|
508 |
+
/// \return a generated Array
|
509 |
+
std::shared_ptr<Array> Map(const std::shared_ptr<Array>& keys,
|
510 |
+
const std::shared_ptr<Array>& items, int64_t size,
|
511 |
+
double null_probability = 0, bool force_empty_nulls = false,
|
512 |
+
int64_t alignment = kDefaultBufferAlignment,
|
513 |
+
MemoryPool* memory_pool = default_memory_pool());
|
514 |
+
|
515 |
+
/// \brief Generate a random RunEndEncodedArray
|
516 |
+
///
|
517 |
+
/// \param[in] value_type The DataType of the encoded values
|
518 |
+
/// \param[in] logical_size The logical length of the generated array
|
519 |
+
/// \param[in] null_probability the probability of a value being null
|
520 |
+
///
|
521 |
+
/// \return a generated Array
|
522 |
+
std::shared_ptr<Array> RunEndEncoded(std::shared_ptr<DataType> value_type,
|
523 |
+
int64_t logical_size,
|
524 |
+
double null_probability = 0.0);
|
525 |
+
|
526 |
+
/// \brief Generate a random SparseUnionArray
|
527 |
+
///
|
528 |
+
/// The type ids are chosen randomly, according to a uniform distribution,
|
529 |
+
/// amongst the given child fields.
|
530 |
+
///
|
531 |
+
/// \param[in] fields Vector of Arrays containing the data for each union field
|
532 |
+
/// \param[in] size The size of the generated sparse union array
|
533 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
534 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
535 |
+
std::shared_ptr<Array> SparseUnion(const ArrayVector& fields, int64_t size,
|
536 |
+
int64_t alignment = kDefaultBufferAlignment,
|
537 |
+
MemoryPool* memory_pool = default_memory_pool());
|
538 |
+
|
539 |
+
/// \brief Generate a random DenseUnionArray
|
540 |
+
///
|
541 |
+
/// The type ids are chosen randomly, according to a uniform distribution,
|
542 |
+
/// amongst the given child fields. The offsets are incremented along
|
543 |
+
/// each child field.
|
544 |
+
///
|
545 |
+
/// \param[in] fields Vector of Arrays containing the data for each union field
|
546 |
+
/// \param[in] size The size of the generated sparse union array
|
547 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
548 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
549 |
+
std::shared_ptr<Array> DenseUnion(const ArrayVector& fields, int64_t size,
|
550 |
+
int64_t alignment = kDefaultBufferAlignment,
|
551 |
+
MemoryPool* memory_pool = default_memory_pool());
|
552 |
+
|
553 |
+
/// \brief Generate a random Array of the specified type, size, and null_probability.
|
554 |
+
///
|
555 |
+
/// Generation parameters other than size and null_probability are determined based on
|
556 |
+
/// the type of Array to be generated.
|
557 |
+
/// If boolean the probabilities of true,false values are 0.25,0.75 respectively.
|
558 |
+
/// If numeric min,max will be the least and greatest representable values.
|
559 |
+
/// If string min_length,max_length will be 0,sqrt(size) respectively.
|
560 |
+
///
|
561 |
+
/// \param[in] type the type of Array to generate
|
562 |
+
/// \param[in] size the size of the Array to generate
|
563 |
+
/// \param[in] null_probability the probability of a slot being null
|
564 |
+
/// \param[in] alignment alignment for memory allocations (in bytes)
|
565 |
+
/// \param[in] memory_pool memory pool to allocate memory from
|
566 |
+
/// \return a generated Array
|
567 |
+
std::shared_ptr<Array> ArrayOf(std::shared_ptr<DataType> type, int64_t size,
|
568 |
+
double null_probability = 0,
|
569 |
+
int64_t alignment = kDefaultBufferAlignment,
|
570 |
+
MemoryPool* memory_pool = default_memory_pool());
|
571 |
+
|
572 |
+
/// \brief Generate an array with random data based on the given field. See BatchOf
|
573 |
+
/// for usage info.
|
574 |
+
std::shared_ptr<Array> ArrayOf(const Field& field, int64_t size,
|
575 |
+
int64_t alignment = kDefaultBufferAlignment,
|
576 |
+
MemoryPool* memory_pool = default_memory_pool());
|
577 |
+
|
578 |
+
/// \brief Generate a record batch with random data of the specified length.
|
579 |
+
///
|
580 |
+
/// Generation options are read from key-value metadata for each field, and may be
|
581 |
+
/// specified at any nesting level. For example, generation options for the child
|
582 |
+
/// values of a list array can be specified by constructing the list type with
|
583 |
+
/// list(field("item", int8(), options_metadata))
|
584 |
+
///
|
585 |
+
/// The following options are supported:
|
586 |
+
///
|
587 |
+
/// For all types except NullType:
|
588 |
+
/// - null_probability (double): range [0.0, 1.0] the probability of a null value.
|
589 |
+
/// Default/value is 0.0 if the field is marked non-nullable, else it is 0.01
|
590 |
+
///
|
591 |
+
/// For all numeric types T:
|
592 |
+
/// - min (T::c_type): the minimum value to generate (inclusive), default
|
593 |
+
/// std::numeric_limits<T::c_type>::min()
|
594 |
+
/// - max (T::c_type): the maximum value to generate (inclusive), default
|
595 |
+
/// std::numeric_limits<T::c_type>::max()
|
596 |
+
/// Note this means that, for example, min/max are int16_t values for HalfFloatType.
|
597 |
+
///
|
598 |
+
/// For floating point types T for which is_physical_floating_type<T>:
|
599 |
+
/// - nan_probability (double): range [0.0, 1.0] the probability of a NaN value.
|
600 |
+
///
|
601 |
+
/// For BooleanType:
|
602 |
+
/// - true_probability (double): range [0.0, 1.0] the probability of a true.
|
603 |
+
///
|
604 |
+
/// For DictionaryType:
|
605 |
+
/// - values (int32_t): the size of the dictionary.
|
606 |
+
/// Other properties are passed to the generator for the dictionary indices. However,
|
607 |
+
/// min and max cannot be specified. Note it is not possible to otherwise customize
|
608 |
+
/// the generation of dictionary values.
|
609 |
+
///
|
610 |
+
/// For list, string, and binary types T, including their large variants:
|
611 |
+
/// - min_length (T::offset_type): the minimum length of the child to generate,
|
612 |
+
/// default 0
|
613 |
+
/// - max_length (T::offset_type): the minimum length of the child to generate,
|
614 |
+
/// default 1024
|
615 |
+
///
|
616 |
+
/// For string and binary types T (not including their large or view variants):
|
617 |
+
/// - unique (int32_t): if positive, this many distinct values will be generated
|
618 |
+
/// and all array values will be one of these values, default -1
|
619 |
+
///
|
620 |
+
/// For string and binary view types T:
|
621 |
+
/// - max_data_buffer_length (int64_t): the data buffer size at which a new chunk
|
622 |
+
/// will be generated, default 32KB
|
623 |
+
///
|
624 |
+
/// For MapType:
|
625 |
+
/// - values (int32_t): the number of key-value pairs to generate, which will be
|
626 |
+
/// partitioned among the array values.
|
627 |
+
///
|
628 |
+
/// For extension types:
|
629 |
+
/// - extension_allow_random_storage (bool): in general an extension array may have
|
630 |
+
/// invariants on its storage beyond those already imposed by the arrow format,
|
631 |
+
/// which may result in an invalid array if we just wrap randomly generated
|
632 |
+
/// storage. Set this flag to explicitly allow wrapping of randomly generated
|
633 |
+
/// storage.
|
634 |
+
std::shared_ptr<arrow::RecordBatch> BatchOf(
|
635 |
+
const FieldVector& fields, int64_t size,
|
636 |
+
int64_t alignment = kDefaultBufferAlignment,
|
637 |
+
MemoryPool* memory_pool = default_memory_pool());
|
638 |
+
|
639 |
+
SeedType seed() { return seed_distribution_(seed_rng_); }
|
640 |
+
|
641 |
+
private:
|
642 |
+
std::uniform_int_distribution<SeedType> seed_distribution_;
|
643 |
+
std::default_random_engine seed_rng_;
|
644 |
+
};
|
645 |
+
|
646 |
+
/// Generate a batch with random data. See RandomArrayGenerator::BatchOf.
|
647 |
+
ARROW_TESTING_EXPORT
|
648 |
+
std::shared_ptr<arrow::RecordBatch> GenerateBatch(
|
649 |
+
const FieldVector& fields, int64_t size, SeedType seed,
|
650 |
+
int64_t alignment = kDefaultBufferAlignment,
|
651 |
+
MemoryPool* memory_pool = default_memory_pool());
|
652 |
+
|
653 |
+
/// Generate an array with random data. See RandomArrayGenerator::BatchOf.
|
654 |
+
ARROW_TESTING_EXPORT
|
655 |
+
std::shared_ptr<arrow::Array> GenerateArray(
|
656 |
+
const Field& field, int64_t size, SeedType seed,
|
657 |
+
int64_t alignment = kDefaultBufferAlignment,
|
658 |
+
MemoryPool* memory_pool = default_memory_pool());
|
659 |
+
|
660 |
+
} // namespace random
|
661 |
+
|
662 |
+
//
|
663 |
+
// Assorted functions
|
664 |
+
//
|
665 |
+
|
666 |
+
ARROW_TESTING_EXPORT
|
667 |
+
void rand_day_millis(int64_t N, std::vector<DayTimeIntervalType::DayMilliseconds>* out);
|
668 |
+
ARROW_TESTING_EXPORT
|
669 |
+
void rand_month_day_nanos(int64_t N,
|
670 |
+
std::vector<MonthDayNanoIntervalType::MonthDayNanos>* out);
|
671 |
+
|
672 |
+
template <typename T, typename U>
|
673 |
+
void randint(int64_t N, T lower, T upper, std::vector<U>* out) {
|
674 |
+
const int random_seed = 0;
|
675 |
+
std::default_random_engine gen(random_seed);
|
676 |
+
std::uniform_int_distribution<T> d(lower, upper);
|
677 |
+
out->resize(N, static_cast<T>(0));
|
678 |
+
std::generate(out->begin(), out->end(), [&d, &gen] { return static_cast<U>(d(gen)); });
|
679 |
+
}
|
680 |
+
|
681 |
+
template <typename T, typename U>
|
682 |
+
void random_real(int64_t n, uint32_t seed, T min_value, T max_value,
|
683 |
+
std::vector<U>* out) {
|
684 |
+
std::default_random_engine gen(seed);
|
685 |
+
::arrow::random::uniform_real_distribution<T> d(min_value, max_value);
|
686 |
+
out->resize(n, static_cast<T>(0));
|
687 |
+
std::generate(out->begin(), out->end(), [&d, &gen] { return static_cast<U>(d(gen)); });
|
688 |
+
}
|
689 |
+
|
690 |
+
template <typename T, typename U>
|
691 |
+
void rand_uniform_int(int64_t n, uint32_t seed, T min_value, T max_value, U* out) {
|
692 |
+
assert(out || (n == 0));
|
693 |
+
std::default_random_engine gen(seed);
|
694 |
+
std::uniform_int_distribution<T> d(min_value, max_value);
|
695 |
+
std::generate(out, out + n, [&d, &gen] { return static_cast<U>(d(gen)); });
|
696 |
+
}
|
697 |
+
|
698 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/uniform_real.h
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
// Random real generation is very slow on Arm if built with clang + libstdc++
|
19 |
+
// due to software emulated long double arithmetic.
|
20 |
+
// This file ports some random real libs from llvm libc++ library, which are
|
21 |
+
// free from long double calculation.
|
22 |
+
// It improves performance significantly on both Arm (~100x) and x86 (~8x) in
|
23 |
+
// generating random reals when built with clang + gnu libstdc++.
|
24 |
+
// Based on: https://github.com/llvm/llvm-project/tree/main/libcxx
|
25 |
+
|
26 |
+
#pragma once
|
27 |
+
|
28 |
+
#include <limits>
|
29 |
+
|
30 |
+
#include <arrow/util/bit_util.h>
|
31 |
+
|
32 |
+
namespace arrow {
|
33 |
+
namespace random {
|
34 |
+
|
35 |
+
namespace detail {
|
36 |
+
|
37 |
+
// std::generate_canonical, simplified
|
38 |
+
// https://en.cppreference.com/w/cpp/numeric/random/generate_canonical
|
39 |
+
template <typename RealType, typename Rng>
|
40 |
+
RealType generate_canonical(Rng& rng) {
|
41 |
+
const size_t b = std::numeric_limits<RealType>::digits;
|
42 |
+
const size_t log2R = 63 - ::arrow::bit_util::CountLeadingZeros(
|
43 |
+
static_cast<uint64_t>(Rng::max() - Rng::min()) + 1);
|
44 |
+
const size_t k = b / log2R + (b % log2R != 0) + (b == 0);
|
45 |
+
const RealType r = static_cast<RealType>(Rng::max() - Rng::min()) + 1;
|
46 |
+
RealType base = r;
|
47 |
+
RealType sp = static_cast<RealType>(rng() - Rng::min());
|
48 |
+
for (size_t i = 1; i < k; ++i, base *= r) {
|
49 |
+
sp += (rng() - Rng::min()) * base;
|
50 |
+
}
|
51 |
+
return sp / base;
|
52 |
+
}
|
53 |
+
|
54 |
+
} // namespace detail
|
55 |
+
|
56 |
+
// std::uniform_real_distribution, simplified
|
57 |
+
// https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
|
58 |
+
template <typename RealType = double>
|
59 |
+
struct uniform_real_distribution {
|
60 |
+
const RealType a, b;
|
61 |
+
|
62 |
+
explicit uniform_real_distribution(RealType a = 0, RealType b = 1) : a(a), b(b) {}
|
63 |
+
|
64 |
+
template <typename Rng>
|
65 |
+
RealType operator()(Rng& rng) {
|
66 |
+
return (b - a) * detail::generate_canonical<RealType>(rng) + a;
|
67 |
+
}
|
68 |
+
};
|
69 |
+
|
70 |
+
// std::bernoulli_distribution, simplified
|
71 |
+
// https://en.cppreference.com/w/cpp/numeric/random/bernoulli_distribution
|
72 |
+
struct bernoulli_distribution {
|
73 |
+
const double p;
|
74 |
+
|
75 |
+
explicit bernoulli_distribution(double p = 0.5) : p(p) {}
|
76 |
+
|
77 |
+
template <class Rng>
|
78 |
+
bool operator()(Rng& rng) {
|
79 |
+
return detail::generate_canonical<double>(rng) < p;
|
80 |
+
}
|
81 |
+
};
|
82 |
+
|
83 |
+
} // namespace random
|
84 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/testing/util.h
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm>
|
21 |
+
#include <cstdint>
|
22 |
+
#include <cstdlib>
|
23 |
+
#include <cstring>
|
24 |
+
#include <limits>
|
25 |
+
#include <memory>
|
26 |
+
#include <optional>
|
27 |
+
#include <string>
|
28 |
+
#include <type_traits>
|
29 |
+
#include <utility>
|
30 |
+
#include <vector>
|
31 |
+
|
32 |
+
#include "arrow/buffer.h"
|
33 |
+
#include "arrow/record_batch.h"
|
34 |
+
#include "arrow/status.h"
|
35 |
+
#include "arrow/testing/visibility.h"
|
36 |
+
#include "arrow/type_fwd.h"
|
37 |
+
#include "arrow/util/macros.h"
|
38 |
+
|
39 |
+
namespace arrow {
|
40 |
+
|
41 |
+
template <typename T>
|
42 |
+
Status CopyBufferFromVector(const std::vector<T>& values, MemoryPool* pool,
|
43 |
+
std::shared_ptr<Buffer>* result) {
|
44 |
+
int64_t nbytes = static_cast<int>(values.size()) * sizeof(T);
|
45 |
+
|
46 |
+
ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(nbytes, pool));
|
47 |
+
auto immutable_data = reinterpret_cast<const uint8_t*>(values.data());
|
48 |
+
std::copy(immutable_data, immutable_data + nbytes, buffer->mutable_data());
|
49 |
+
memset(buffer->mutable_data() + nbytes, 0,
|
50 |
+
static_cast<size_t>(buffer->capacity() - nbytes));
|
51 |
+
|
52 |
+
*result = std::move(buffer);
|
53 |
+
return Status::OK();
|
54 |
+
}
|
55 |
+
|
56 |
+
// Sets approximately pct_null of the first n bytes in null_bytes to zero
|
57 |
+
// and the rest to non-zero (true) values.
|
58 |
+
ARROW_TESTING_EXPORT void random_null_bytes(int64_t n, double pct_null,
|
59 |
+
uint8_t* null_bytes);
|
60 |
+
ARROW_TESTING_EXPORT void random_is_valid(int64_t n, double pct_null,
|
61 |
+
std::vector<bool>* is_valid,
|
62 |
+
int random_seed = 0);
|
63 |
+
ARROW_TESTING_EXPORT void random_bytes(int64_t n, uint32_t seed, uint8_t* out);
|
64 |
+
ARROW_TESTING_EXPORT std::string random_string(int64_t n, uint32_t seed);
|
65 |
+
ARROW_TESTING_EXPORT int32_t DecimalSize(int32_t precision);
|
66 |
+
ARROW_TESTING_EXPORT void random_ascii(int64_t n, uint32_t seed, uint8_t* out);
|
67 |
+
ARROW_TESTING_EXPORT int64_t CountNulls(const std::vector<uint8_t>& valid_bytes);
|
68 |
+
|
69 |
+
ARROW_TESTING_EXPORT Status MakeRandomByteBuffer(int64_t length, MemoryPool* pool,
|
70 |
+
std::shared_ptr<ResizableBuffer>* out,
|
71 |
+
uint32_t seed = 0);
|
72 |
+
|
73 |
+
ARROW_TESTING_EXPORT uint64_t random_seed();
|
74 |
+
|
75 |
+
#define DECL_T() typedef typename TestFixture::T T;
|
76 |
+
|
77 |
+
#define DECL_TYPE() typedef typename TestFixture::Type Type;
|
78 |
+
|
79 |
+
// ----------------------------------------------------------------------
|
80 |
+
// A RecordBatchReader for serving a sequence of in-memory record batches
|
81 |
+
|
82 |
+
class BatchIterator : public RecordBatchReader {
|
83 |
+
public:
|
84 |
+
BatchIterator(const std::shared_ptr<Schema>& schema,
|
85 |
+
const std::vector<std::shared_ptr<RecordBatch>>& batches)
|
86 |
+
: schema_(schema), batches_(batches), position_(0) {}
|
87 |
+
|
88 |
+
std::shared_ptr<Schema> schema() const override { return schema_; }
|
89 |
+
|
90 |
+
Status ReadNext(std::shared_ptr<RecordBatch>* out) override {
|
91 |
+
if (position_ >= batches_.size()) {
|
92 |
+
*out = nullptr;
|
93 |
+
} else {
|
94 |
+
*out = batches_[position_++];
|
95 |
+
}
|
96 |
+
return Status::OK();
|
97 |
+
}
|
98 |
+
|
99 |
+
private:
|
100 |
+
std::shared_ptr<Schema> schema_;
|
101 |
+
std::vector<std::shared_ptr<RecordBatch>> batches_;
|
102 |
+
size_t position_;
|
103 |
+
};
|
104 |
+
|
105 |
+
static inline std::vector<std::shared_ptr<DataType> (*)(FieldVector, std::vector<int8_t>)>
|
106 |
+
UnionTypeFactories() {
|
107 |
+
return {sparse_union, dense_union};
|
108 |
+
}
|
109 |
+
|
110 |
+
// Return the value of the ARROW_TEST_DATA environment variable or return error
|
111 |
+
// Status
|
112 |
+
ARROW_TESTING_EXPORT Status GetTestResourceRoot(std::string*);
|
113 |
+
|
114 |
+
// Return the value of the ARROW_TIMEZONE_DATABASE environment variable
|
115 |
+
ARROW_TESTING_EXPORT std::optional<std::string> GetTestTimezoneDatabaseRoot();
|
116 |
+
|
117 |
+
// Set the Timezone database based on the ARROW_TIMEZONE_DATABASE env variable
|
118 |
+
// This is only relevant on Windows, since other OSs have compatible databases built-in
|
119 |
+
ARROW_TESTING_EXPORT Status InitTestTimezoneDatabase();
|
120 |
+
|
121 |
+
// Get a TCP port number to listen on. This is a different number every time,
|
122 |
+
// as reusing the same port across tests can produce spurious bind errors on
|
123 |
+
// Windows.
|
124 |
+
ARROW_TESTING_EXPORT int GetListenPort();
|
125 |
+
|
126 |
+
// Get a IPv4 "address:port" to listen on. The address will be a loopback
|
127 |
+
// address. Compared to GetListenPort(), this will minimize the risk of
|
128 |
+
// port conflicts.
|
129 |
+
ARROW_TESTING_EXPORT std::string GetListenAddress();
|
130 |
+
|
131 |
+
ARROW_TESTING_EXPORT
|
132 |
+
const std::vector<std::shared_ptr<DataType>>& all_dictionary_index_types();
|
133 |
+
|
134 |
+
// Get a list of supported hardware flags from the given candidates.
|
135 |
+
// The result will always contain 0, meaning no optional CPU feature enabled at all.
|
136 |
+
ARROW_TESTING_EXPORT
|
137 |
+
std::vector<int64_t> GetSupportedHardwareFlags(
|
138 |
+
const std::vector<int64_t>& candidate_flags);
|
139 |
+
|
140 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/util/bitmap.h
ADDED
@@ -0,0 +1,466 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <algorithm>
|
21 |
+
#include <array>
|
22 |
+
#include <bitset>
|
23 |
+
#include <cassert>
|
24 |
+
#include <cstdint>
|
25 |
+
#include <cstring>
|
26 |
+
#include <memory>
|
27 |
+
#include <string>
|
28 |
+
#include <string_view>
|
29 |
+
#include <utility>
|
30 |
+
|
31 |
+
#include "arrow/buffer.h"
|
32 |
+
#include "arrow/util/bit_util.h"
|
33 |
+
#include "arrow/util/bitmap_ops.h"
|
34 |
+
#include "arrow/util/bitmap_reader.h"
|
35 |
+
#include "arrow/util/bitmap_writer.h"
|
36 |
+
#include "arrow/util/compare.h"
|
37 |
+
#include "arrow/util/endian.h"
|
38 |
+
#include "arrow/util/functional.h"
|
39 |
+
#include "arrow/util/span.h"
|
40 |
+
#include "arrow/util/string_builder.h"
|
41 |
+
#include "arrow/util/visibility.h"
|
42 |
+
|
43 |
+
namespace arrow {
|
44 |
+
|
45 |
+
class BooleanArray;
|
46 |
+
|
47 |
+
namespace internal {
|
48 |
+
|
49 |
+
class ARROW_EXPORT Bitmap : public util::ToStringOstreamable<Bitmap>,
|
50 |
+
public util::EqualityComparable<Bitmap> {
|
51 |
+
public:
|
52 |
+
Bitmap() = default;
|
53 |
+
|
54 |
+
Bitmap(const std::shared_ptr<Buffer>& buffer, int64_t offset, int64_t length)
|
55 |
+
: data_(buffer->data()), offset_(offset), length_(length) {
|
56 |
+
if (buffer->is_mutable()) {
|
57 |
+
mutable_data_ = buffer->mutable_data();
|
58 |
+
}
|
59 |
+
}
|
60 |
+
|
61 |
+
Bitmap(const void* data, int64_t offset, int64_t length)
|
62 |
+
: data_(reinterpret_cast<const uint8_t*>(data)), offset_(offset), length_(length) {}
|
63 |
+
|
64 |
+
Bitmap(void* data, int64_t offset, int64_t length)
|
65 |
+
: data_(reinterpret_cast<const uint8_t*>(data)),
|
66 |
+
mutable_data_(reinterpret_cast<uint8_t*>(data)),
|
67 |
+
offset_(offset),
|
68 |
+
length_(length) {}
|
69 |
+
|
70 |
+
Bitmap Slice(int64_t offset) const {
|
71 |
+
if (mutable_data_ != NULLPTR) {
|
72 |
+
return {mutable_data_, offset_ + offset, length_ - offset};
|
73 |
+
} else {
|
74 |
+
return {data_, offset_ + offset, length_ - offset};
|
75 |
+
}
|
76 |
+
}
|
77 |
+
|
78 |
+
Bitmap Slice(int64_t offset, int64_t length) const {
|
79 |
+
if (mutable_data_ != NULLPTR) {
|
80 |
+
return {mutable_data_, offset_ + offset, length};
|
81 |
+
} else {
|
82 |
+
return {data_, offset_ + offset, length};
|
83 |
+
}
|
84 |
+
}
|
85 |
+
|
86 |
+
std::string ToString() const;
|
87 |
+
|
88 |
+
bool Equals(const Bitmap& other) const;
|
89 |
+
|
90 |
+
std::string Diff(const Bitmap& other) const;
|
91 |
+
|
92 |
+
bool GetBit(int64_t i) const { return bit_util::GetBit(data_, i + offset_); }
|
93 |
+
|
94 |
+
bool operator[](int64_t i) const { return GetBit(i); }
|
95 |
+
|
96 |
+
void SetBitTo(int64_t i, bool v) const {
|
97 |
+
bit_util::SetBitTo(mutable_data_, i + offset_, v);
|
98 |
+
}
|
99 |
+
|
100 |
+
void SetBitsTo(bool v) { bit_util::SetBitsTo(mutable_data_, offset_, length_, v); }
|
101 |
+
|
102 |
+
void CopyFrom(const Bitmap& other);
|
103 |
+
void CopyFromInverted(const Bitmap& other);
|
104 |
+
|
105 |
+
/// \brief Visit bits from each bitmap as bitset<N>
|
106 |
+
///
|
107 |
+
/// All bitmaps must have identical length.
|
108 |
+
template <size_t N, typename Visitor>
|
109 |
+
static void VisitBits(const Bitmap (&bitmaps)[N], Visitor&& visitor) {
|
110 |
+
int64_t bit_length = BitLength(bitmaps, N);
|
111 |
+
std::bitset<N> bits;
|
112 |
+
for (int64_t bit_i = 0; bit_i < bit_length; ++bit_i) {
|
113 |
+
for (size_t i = 0; i < N; ++i) {
|
114 |
+
bits[i] = bitmaps[i].GetBit(bit_i);
|
115 |
+
}
|
116 |
+
visitor(bits);
|
117 |
+
}
|
118 |
+
}
|
119 |
+
|
120 |
+
/// \brief Visit bits from each bitmap as bitset<N>
|
121 |
+
///
|
122 |
+
/// All bitmaps must have identical length.
|
123 |
+
template <size_t N, typename Visitor>
|
124 |
+
static void VisitBits(const std::array<Bitmap, N>& bitmaps, Visitor&& visitor) {
|
125 |
+
int64_t bit_length = BitLength(bitmaps);
|
126 |
+
std::bitset<N> bits;
|
127 |
+
for (int64_t bit_i = 0; bit_i < bit_length; ++bit_i) {
|
128 |
+
for (size_t i = 0; i < N; ++i) {
|
129 |
+
bits[i] = bitmaps[i].GetBit(bit_i);
|
130 |
+
}
|
131 |
+
visitor(bits);
|
132 |
+
}
|
133 |
+
}
|
134 |
+
|
135 |
+
/// \brief Visit words of bits from each bitmap as array<Word, N>
|
136 |
+
///
|
137 |
+
/// All bitmaps must have identical length. The first bit in a visited bitmap
|
138 |
+
/// may be offset within the first visited word, but words will otherwise contain
|
139 |
+
/// densely packed bits loaded from the bitmap. That offset within the first word is
|
140 |
+
/// returned.
|
141 |
+
///
|
142 |
+
/// TODO(bkietz) allow for early termination
|
143 |
+
// NOTE: this function is efficient on 3+ sufficiently large bitmaps.
|
144 |
+
// It also has a large prolog / epilog overhead and should be used
|
145 |
+
// carefully in other cases.
|
146 |
+
// For 2 bitmaps or less, and/or smaller bitmaps, see also VisitTwoBitBlocksVoid
|
147 |
+
// and BitmapUInt64Reader.
|
148 |
+
template <size_t N, typename Visitor,
|
149 |
+
typename Word = typename std::decay<
|
150 |
+
internal::call_traits::argument_type<0, Visitor&&>>::type::value_type>
|
151 |
+
static int64_t VisitWords(const Bitmap (&bitmaps_arg)[N], Visitor&& visitor) {
|
152 |
+
constexpr int64_t kBitWidth = sizeof(Word) * 8;
|
153 |
+
|
154 |
+
// local, mutable variables which will be sliced/decremented to represent consumption:
|
155 |
+
Bitmap bitmaps[N];
|
156 |
+
int64_t offsets[N];
|
157 |
+
int64_t bit_length = BitLength(bitmaps_arg, N);
|
158 |
+
util::span<const Word> words[N];
|
159 |
+
for (size_t i = 0; i < N; ++i) {
|
160 |
+
bitmaps[i] = bitmaps_arg[i];
|
161 |
+
offsets[i] = bitmaps[i].template word_offset<Word>();
|
162 |
+
assert(offsets[i] >= 0 && offsets[i] < kBitWidth);
|
163 |
+
words[i] = bitmaps[i].template words<Word>();
|
164 |
+
}
|
165 |
+
|
166 |
+
auto consume = [&](int64_t consumed_bits) {
|
167 |
+
for (size_t i = 0; i < N; ++i) {
|
168 |
+
bitmaps[i] = bitmaps[i].Slice(consumed_bits, bit_length - consumed_bits);
|
169 |
+
offsets[i] = bitmaps[i].template word_offset<Word>();
|
170 |
+
assert(offsets[i] >= 0 && offsets[i] < kBitWidth);
|
171 |
+
words[i] = bitmaps[i].template words<Word>();
|
172 |
+
}
|
173 |
+
bit_length -= consumed_bits;
|
174 |
+
};
|
175 |
+
|
176 |
+
std::array<Word, N> visited_words;
|
177 |
+
visited_words.fill(0);
|
178 |
+
|
179 |
+
if (bit_length <= kBitWidth * 2) {
|
180 |
+
// bitmaps fit into one or two words so don't bother with optimization
|
181 |
+
while (bit_length > 0) {
|
182 |
+
auto leading_bits = std::min(bit_length, kBitWidth);
|
183 |
+
SafeLoadWords(bitmaps, 0, leading_bits, false, &visited_words);
|
184 |
+
visitor(visited_words);
|
185 |
+
consume(leading_bits);
|
186 |
+
}
|
187 |
+
return 0;
|
188 |
+
}
|
189 |
+
|
190 |
+
int64_t max_offset = *std::max_element(offsets, offsets + N);
|
191 |
+
int64_t min_offset = *std::min_element(offsets, offsets + N);
|
192 |
+
if (max_offset > 0) {
|
193 |
+
// consume leading bits
|
194 |
+
auto leading_bits = kBitWidth - min_offset;
|
195 |
+
SafeLoadWords(bitmaps, 0, leading_bits, true, &visited_words);
|
196 |
+
visitor(visited_words);
|
197 |
+
consume(leading_bits);
|
198 |
+
}
|
199 |
+
assert(*std::min_element(offsets, offsets + N) == 0);
|
200 |
+
|
201 |
+
int64_t whole_word_count = bit_length / kBitWidth;
|
202 |
+
assert(whole_word_count >= 1);
|
203 |
+
|
204 |
+
if (min_offset == max_offset) {
|
205 |
+
// all offsets were identical, all leading bits have been consumed
|
206 |
+
assert(
|
207 |
+
std::all_of(offsets, offsets + N, [](int64_t offset) { return offset == 0; }));
|
208 |
+
|
209 |
+
for (int64_t word_i = 0; word_i < whole_word_count; ++word_i) {
|
210 |
+
for (size_t i = 0; i < N; ++i) {
|
211 |
+
visited_words[i] = words[i][word_i];
|
212 |
+
}
|
213 |
+
visitor(visited_words);
|
214 |
+
}
|
215 |
+
consume(whole_word_count * kBitWidth);
|
216 |
+
} else {
|
217 |
+
// leading bits from potentially incomplete words have been consumed
|
218 |
+
|
219 |
+
// word_i such that words[i][word_i] and words[i][word_i + 1] are lie entirely
|
220 |
+
// within the bitmap for all i
|
221 |
+
for (int64_t word_i = 0; word_i < whole_word_count - 1; ++word_i) {
|
222 |
+
for (size_t i = 0; i < N; ++i) {
|
223 |
+
if (offsets[i] == 0) {
|
224 |
+
visited_words[i] = words[i][word_i];
|
225 |
+
} else {
|
226 |
+
auto words0 = bit_util::ToLittleEndian(words[i][word_i]);
|
227 |
+
auto words1 = bit_util::ToLittleEndian(words[i][word_i + 1]);
|
228 |
+
visited_words[i] = bit_util::FromLittleEndian(
|
229 |
+
(words0 >> offsets[i]) | (words1 << (kBitWidth - offsets[i])));
|
230 |
+
}
|
231 |
+
}
|
232 |
+
visitor(visited_words);
|
233 |
+
}
|
234 |
+
consume((whole_word_count - 1) * kBitWidth);
|
235 |
+
|
236 |
+
SafeLoadWords(bitmaps, 0, kBitWidth, false, &visited_words);
|
237 |
+
|
238 |
+
visitor(visited_words);
|
239 |
+
consume(kBitWidth);
|
240 |
+
}
|
241 |
+
|
242 |
+
// load remaining bits
|
243 |
+
if (bit_length > 0) {
|
244 |
+
SafeLoadWords(bitmaps, 0, bit_length, false, &visited_words);
|
245 |
+
visitor(visited_words);
|
246 |
+
}
|
247 |
+
|
248 |
+
return min_offset;
|
249 |
+
}
|
250 |
+
|
251 |
+
template <size_t N, size_t M, typename ReaderT, typename WriterT, typename Visitor,
|
252 |
+
typename Word = typename std::decay<
|
253 |
+
internal::call_traits::argument_type<0, Visitor&&>>::type::value_type>
|
254 |
+
static void RunVisitWordsAndWriteLoop(int64_t bit_length,
|
255 |
+
std::array<ReaderT, N>& readers,
|
256 |
+
std::array<WriterT, M>& writers,
|
257 |
+
Visitor&& visitor) {
|
258 |
+
constexpr int64_t kBitWidth = sizeof(Word) * 8;
|
259 |
+
|
260 |
+
std::array<Word, N> visited_words;
|
261 |
+
std::array<Word, M> output_words;
|
262 |
+
|
263 |
+
// every reader will have same number of words, since they are same length'ed
|
264 |
+
// TODO($JIRA) this will be inefficient in some cases. When there are offsets beyond
|
265 |
+
// Word boundary, every Word would have to be created from 2 adjoining Words
|
266 |
+
auto n_words = readers[0].words();
|
267 |
+
bit_length -= n_words * kBitWidth;
|
268 |
+
while (n_words--) {
|
269 |
+
// first collect all words to visited_words array
|
270 |
+
for (size_t i = 0; i < N; i++) {
|
271 |
+
visited_words[i] = readers[i].NextWord();
|
272 |
+
}
|
273 |
+
visitor(visited_words, &output_words);
|
274 |
+
for (size_t i = 0; i < M; i++) {
|
275 |
+
writers[i].PutNextWord(output_words[i]);
|
276 |
+
}
|
277 |
+
}
|
278 |
+
|
279 |
+
// every reader will have same number of trailing bytes, because of the above reason
|
280 |
+
// tailing portion could be more than one word! (ref: BitmapWordReader constructor)
|
281 |
+
// remaining full/ partial words to write
|
282 |
+
|
283 |
+
if (bit_length) {
|
284 |
+
// convert the word visitor lambda to a byte_visitor
|
285 |
+
auto byte_visitor = [&](const std::array<uint8_t, N>& in,
|
286 |
+
std::array<uint8_t, M>* out) {
|
287 |
+
std::array<Word, N> in_words;
|
288 |
+
std::array<Word, M> out_words;
|
289 |
+
std::copy(in.begin(), in.end(), in_words.begin());
|
290 |
+
visitor(in_words, &out_words);
|
291 |
+
for (size_t i = 0; i < M; i++) {
|
292 |
+
out->at(i) = static_cast<uint8_t>(out_words[i]);
|
293 |
+
}
|
294 |
+
};
|
295 |
+
|
296 |
+
std::array<uint8_t, N> visited_bytes;
|
297 |
+
std::array<uint8_t, M> output_bytes;
|
298 |
+
int n_bytes = readers[0].trailing_bytes();
|
299 |
+
while (n_bytes--) {
|
300 |
+
visited_bytes.fill(0);
|
301 |
+
output_bytes.fill(0);
|
302 |
+
int valid_bits;
|
303 |
+
for (size_t i = 0; i < N; i++) {
|
304 |
+
visited_bytes[i] = readers[i].NextTrailingByte(valid_bits);
|
305 |
+
}
|
306 |
+
byte_visitor(visited_bytes, &output_bytes);
|
307 |
+
for (size_t i = 0; i < M; i++) {
|
308 |
+
writers[i].PutNextTrailingByte(output_bytes[i], valid_bits);
|
309 |
+
}
|
310 |
+
}
|
311 |
+
}
|
312 |
+
}
|
313 |
+
|
314 |
+
/// \brief Visit words of bits from each input bitmap as array<Word, N> and collects
|
315 |
+
/// outputs to an array<Word, M>, to be written into the output bitmaps accordingly.
|
316 |
+
///
|
317 |
+
/// All bitmaps must have identical length. The first bit in a visited bitmap
|
318 |
+
/// may be offset within the first visited word, but words will otherwise contain
|
319 |
+
/// densely packed bits loaded from the bitmap. That offset within the first word is
|
320 |
+
/// returned.
|
321 |
+
/// Visitor is expected to have the following signature
|
322 |
+
/// [](const std::array<Word, N>& in_words, std::array<Word, M>* out_words){...}
|
323 |
+
///
|
324 |
+
// NOTE: this function is efficient on 3+ sufficiently large bitmaps.
|
325 |
+
// It also has a large prolog / epilog overhead and should be used
|
326 |
+
// carefully in other cases.
|
327 |
+
// For 2 bitmaps or less, and/or smaller bitmaps, see also VisitTwoBitBlocksVoid
|
328 |
+
// and BitmapUInt64Reader.
|
329 |
+
template <size_t N, size_t M, typename Visitor,
|
330 |
+
typename Word = typename std::decay<
|
331 |
+
internal::call_traits::argument_type<0, Visitor&&>>::type::value_type>
|
332 |
+
static void VisitWordsAndWrite(const std::array<Bitmap, N>& bitmaps_arg,
|
333 |
+
std::array<Bitmap, M>* out_bitmaps_arg,
|
334 |
+
Visitor&& visitor) {
|
335 |
+
int64_t bit_length = BitLength(bitmaps_arg);
|
336 |
+
assert(bit_length == BitLength(*out_bitmaps_arg));
|
337 |
+
|
338 |
+
// if both input and output bitmaps have no byte offset, then use special template
|
339 |
+
if (std::all_of(bitmaps_arg.begin(), bitmaps_arg.end(),
|
340 |
+
[](const Bitmap& b) { return b.offset_ % 8 == 0; }) &&
|
341 |
+
std::all_of(out_bitmaps_arg->begin(), out_bitmaps_arg->end(),
|
342 |
+
[](const Bitmap& b) { return b.offset_ % 8 == 0; })) {
|
343 |
+
std::array<BitmapWordReader<Word, /*may_have_byte_offset=*/false>, N> readers;
|
344 |
+
for (size_t i = 0; i < N; ++i) {
|
345 |
+
const Bitmap& in_bitmap = bitmaps_arg[i];
|
346 |
+
readers[i] = BitmapWordReader<Word, /*may_have_byte_offset=*/false>(
|
347 |
+
in_bitmap.data_, in_bitmap.offset_, in_bitmap.length_);
|
348 |
+
}
|
349 |
+
|
350 |
+
std::array<BitmapWordWriter<Word, /*may_have_byte_offset=*/false>, M> writers;
|
351 |
+
for (size_t i = 0; i < M; ++i) {
|
352 |
+
const Bitmap& out_bitmap = out_bitmaps_arg->at(i);
|
353 |
+
writers[i] = BitmapWordWriter<Word, /*may_have_byte_offset=*/false>(
|
354 |
+
out_bitmap.mutable_data_, out_bitmap.offset_, out_bitmap.length_);
|
355 |
+
}
|
356 |
+
|
357 |
+
RunVisitWordsAndWriteLoop(bit_length, readers, writers, visitor);
|
358 |
+
} else {
|
359 |
+
std::array<BitmapWordReader<Word>, N> readers;
|
360 |
+
for (size_t i = 0; i < N; ++i) {
|
361 |
+
const Bitmap& in_bitmap = bitmaps_arg[i];
|
362 |
+
readers[i] =
|
363 |
+
BitmapWordReader<Word>(in_bitmap.data_, in_bitmap.offset_, in_bitmap.length_);
|
364 |
+
}
|
365 |
+
|
366 |
+
std::array<BitmapWordWriter<Word>, M> writers;
|
367 |
+
for (size_t i = 0; i < M; ++i) {
|
368 |
+
const Bitmap& out_bitmap = out_bitmaps_arg->at(i);
|
369 |
+
writers[i] = BitmapWordWriter<Word>(out_bitmap.mutable_data_, out_bitmap.offset_,
|
370 |
+
out_bitmap.length_);
|
371 |
+
}
|
372 |
+
|
373 |
+
RunVisitWordsAndWriteLoop(bit_length, readers, writers, visitor);
|
374 |
+
}
|
375 |
+
}
|
376 |
+
|
377 |
+
const uint8_t* data() const { return data_; }
|
378 |
+
uint8_t* mutable_data() { return mutable_data_; }
|
379 |
+
|
380 |
+
/// offset of first bit relative to buffer().data()
|
381 |
+
int64_t offset() const { return offset_; }
|
382 |
+
|
383 |
+
/// number of bits in this Bitmap
|
384 |
+
int64_t length() const { return length_; }
|
385 |
+
|
386 |
+
/// span of all bytes which contain any bit in this Bitmap
|
387 |
+
util::span<const uint8_t> bytes() const {
|
388 |
+
auto byte_offset = offset_ / 8;
|
389 |
+
auto byte_count = bit_util::CeilDiv(offset_ + length_, 8) - byte_offset;
|
390 |
+
return {data_ + byte_offset, static_cast<size_t>(byte_count)};
|
391 |
+
}
|
392 |
+
|
393 |
+
private:
|
394 |
+
/// span of all Words which contain any bit in this Bitmap
|
395 |
+
///
|
396 |
+
/// For example, given Word=uint16_t and a bitmap spanning bits [20, 36)
|
397 |
+
/// words() would span bits [16, 48).
|
398 |
+
///
|
399 |
+
/// 0 16 32 48 64
|
400 |
+
/// |-------|-------|------|------| (buffer)
|
401 |
+
/// [ ] (bitmap)
|
402 |
+
/// |-------|------| (returned words)
|
403 |
+
///
|
404 |
+
/// \warning The words may contain bytes which lie outside the buffer or are
|
405 |
+
/// uninitialized.
|
406 |
+
template <typename Word>
|
407 |
+
util::span<const Word> words() const {
|
408 |
+
auto bytes_addr = reinterpret_cast<intptr_t>(bytes().data());
|
409 |
+
auto words_addr = bytes_addr - bytes_addr % sizeof(Word);
|
410 |
+
auto word_byte_count =
|
411 |
+
bit_util::RoundUpToPowerOf2(static_cast<int64_t>(bytes_addr + bytes().size()),
|
412 |
+
static_cast<int64_t>(sizeof(Word))) -
|
413 |
+
words_addr;
|
414 |
+
return {reinterpret_cast<const Word*>(words_addr),
|
415 |
+
static_cast<size_t>(word_byte_count / sizeof(Word))};
|
416 |
+
}
|
417 |
+
|
418 |
+
/// offset of first bit relative to words<Word>().data()
|
419 |
+
template <typename Word>
|
420 |
+
int64_t word_offset() const {
|
421 |
+
return offset_ + 8 * (reinterpret_cast<intptr_t>(data_) -
|
422 |
+
reinterpret_cast<intptr_t>(words<Word>().data()));
|
423 |
+
}
|
424 |
+
|
425 |
+
/// load words from bitmaps bitwise
|
426 |
+
template <size_t N, typename Word>
|
427 |
+
static void SafeLoadWords(const Bitmap (&bitmaps)[N], int64_t offset,
|
428 |
+
int64_t out_length, bool set_trailing_bits,
|
429 |
+
std::array<Word, N>* out) {
|
430 |
+
out->fill(0);
|
431 |
+
|
432 |
+
int64_t out_offset = set_trailing_bits ? sizeof(Word) * 8 - out_length : 0;
|
433 |
+
|
434 |
+
Bitmap slices[N], out_bitmaps[N];
|
435 |
+
for (size_t i = 0; i < N; ++i) {
|
436 |
+
slices[i] = bitmaps[i].Slice(offset, out_length);
|
437 |
+
out_bitmaps[i] = Bitmap(&out->at(i), out_offset, out_length);
|
438 |
+
}
|
439 |
+
|
440 |
+
int64_t bit_i = 0;
|
441 |
+
Bitmap::VisitBits(slices, [&](std::bitset<N> bits) {
|
442 |
+
for (size_t i = 0; i < N; ++i) {
|
443 |
+
out_bitmaps[i].SetBitTo(bit_i, bits[i]);
|
444 |
+
}
|
445 |
+
++bit_i;
|
446 |
+
});
|
447 |
+
}
|
448 |
+
|
449 |
+
/// assert bitmaps have identical length and return that length
|
450 |
+
static int64_t BitLength(const Bitmap* bitmaps, size_t N);
|
451 |
+
|
452 |
+
template <size_t N>
|
453 |
+
static int64_t BitLength(const std::array<Bitmap, N>& bitmaps) {
|
454 |
+
for (size_t i = 1; i < N; ++i) {
|
455 |
+
assert(bitmaps[i].length() == bitmaps[0].length());
|
456 |
+
}
|
457 |
+
return bitmaps[0].length();
|
458 |
+
}
|
459 |
+
|
460 |
+
const uint8_t* data_ = NULLPTR;
|
461 |
+
uint8_t* mutable_data_ = NULLPTR;
|
462 |
+
int64_t offset_ = 0, length_ = 0;
|
463 |
+
};
|
464 |
+
|
465 |
+
} // namespace internal
|
466 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/util/bitmap_ops.h
ADDED
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <cstdint>
|
21 |
+
#include <memory>
|
22 |
+
|
23 |
+
#include "arrow/result.h"
|
24 |
+
#include "arrow/util/visibility.h"
|
25 |
+
|
26 |
+
namespace arrow {
|
27 |
+
|
28 |
+
class Buffer;
|
29 |
+
class MemoryPool;
|
30 |
+
|
31 |
+
namespace internal {
|
32 |
+
|
33 |
+
// ----------------------------------------------------------------------
|
34 |
+
// Bitmap utilities
|
35 |
+
|
36 |
+
/// Copy a bit range of an existing bitmap
|
37 |
+
///
|
38 |
+
/// \param[in] pool memory pool to allocate memory from
|
39 |
+
/// \param[in] bitmap source data
|
40 |
+
/// \param[in] offset bit offset into the source data
|
41 |
+
/// \param[in] length number of bits to copy
|
42 |
+
///
|
43 |
+
/// \return Status message
|
44 |
+
ARROW_EXPORT
|
45 |
+
Result<std::shared_ptr<Buffer>> CopyBitmap(MemoryPool* pool, const uint8_t* bitmap,
|
46 |
+
int64_t offset, int64_t length);
|
47 |
+
|
48 |
+
/// Copy a bit range of an existing bitmap into an existing bitmap
|
49 |
+
///
|
50 |
+
/// \param[in] bitmap source data
|
51 |
+
/// \param[in] offset bit offset into the source data
|
52 |
+
/// \param[in] length number of bits to copy
|
53 |
+
/// \param[in] dest_offset bit offset into the destination
|
54 |
+
/// \param[out] dest the destination buffer, must have at least space for
|
55 |
+
/// (offset + length) bits
|
56 |
+
ARROW_EXPORT
|
57 |
+
void CopyBitmap(const uint8_t* bitmap, int64_t offset, int64_t length, uint8_t* dest,
|
58 |
+
int64_t dest_offset);
|
59 |
+
|
60 |
+
/// Invert a bit range of an existing bitmap into an existing bitmap
|
61 |
+
///
|
62 |
+
/// \param[in] bitmap source data
|
63 |
+
/// \param[in] offset bit offset into the source data
|
64 |
+
/// \param[in] length number of bits to copy
|
65 |
+
/// \param[in] dest_offset bit offset into the destination
|
66 |
+
/// \param[out] dest the destination buffer, must have at least space for
|
67 |
+
/// (offset + length) bits
|
68 |
+
ARROW_EXPORT
|
69 |
+
void InvertBitmap(const uint8_t* bitmap, int64_t offset, int64_t length, uint8_t* dest,
|
70 |
+
int64_t dest_offset);
|
71 |
+
|
72 |
+
/// Invert a bit range of an existing bitmap
|
73 |
+
///
|
74 |
+
/// \param[in] pool memory pool to allocate memory from
|
75 |
+
/// \param[in] bitmap source data
|
76 |
+
/// \param[in] offset bit offset into the source data
|
77 |
+
/// \param[in] length number of bits to copy
|
78 |
+
///
|
79 |
+
/// \return Status message
|
80 |
+
ARROW_EXPORT
|
81 |
+
Result<std::shared_ptr<Buffer>> InvertBitmap(MemoryPool* pool, const uint8_t* bitmap,
|
82 |
+
int64_t offset, int64_t length);
|
83 |
+
|
84 |
+
/// Reverse a bit range of an existing bitmap into an existing bitmap
|
85 |
+
///
|
86 |
+
/// \param[in] bitmap source data
|
87 |
+
/// \param[in] offset bit offset into the source data
|
88 |
+
/// \param[in] length number of bits to reverse
|
89 |
+
/// \param[in] dest_offset bit offset into the destination
|
90 |
+
/// \param[out] dest the destination buffer, must have at least space for
|
91 |
+
/// (offset + length) bits
|
92 |
+
ARROW_EXPORT
|
93 |
+
void ReverseBitmap(const uint8_t* bitmap, int64_t offset, int64_t length, uint8_t* dest,
|
94 |
+
int64_t dest_offset);
|
95 |
+
|
96 |
+
/// Reverse a bit range of an existing bitmap
|
97 |
+
///
|
98 |
+
/// \param[in] pool memory pool to allocate memory from
|
99 |
+
/// \param[in] bitmap source data
|
100 |
+
/// \param[in] offset bit offset into the source data
|
101 |
+
/// \param[in] length number of bits to reverse
|
102 |
+
///
|
103 |
+
/// \return Status message
|
104 |
+
ARROW_EXPORT
|
105 |
+
Result<std::shared_ptr<Buffer>> ReverseBitmap(MemoryPool* pool, const uint8_t* bitmap,
|
106 |
+
int64_t offset, int64_t length);
|
107 |
+
|
108 |
+
/// Compute the number of 1's in the given data array
|
109 |
+
///
|
110 |
+
/// \param[in] data a packed LSB-ordered bitmap as a byte array
|
111 |
+
/// \param[in] bit_offset a bitwise offset into the bitmap
|
112 |
+
/// \param[in] length the number of bits to inspect in the bitmap relative to
|
113 |
+
/// the offset
|
114 |
+
///
|
115 |
+
/// \return The number of set (1) bits in the range
|
116 |
+
ARROW_EXPORT
|
117 |
+
int64_t CountSetBits(const uint8_t* data, int64_t bit_offset, int64_t length);
|
118 |
+
|
119 |
+
/// Compute the number of 1's in the result of an "and" (&) of two bitmaps
|
120 |
+
///
|
121 |
+
/// \param[in] left_bitmap a packed LSB-ordered bitmap as a byte array
|
122 |
+
/// \param[in] left_offset a bitwise offset into the left bitmap
|
123 |
+
/// \param[in] right_bitmap a packed LSB-ordered bitmap as a byte array
|
124 |
+
/// \param[in] right_offset a bitwise offset into the right bitmap
|
125 |
+
/// \param[in] length the length of the bitmaps (must be the same)
|
126 |
+
///
|
127 |
+
/// \return The number of set (1) bits in the "and" of the two bitmaps
|
128 |
+
ARROW_EXPORT
|
129 |
+
int64_t CountAndSetBits(const uint8_t* left_bitmap, int64_t left_offset,
|
130 |
+
const uint8_t* right_bitmap, int64_t right_offset,
|
131 |
+
int64_t length);
|
132 |
+
|
133 |
+
ARROW_EXPORT
|
134 |
+
bool BitmapEquals(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
135 |
+
int64_t right_offset, int64_t length);
|
136 |
+
|
137 |
+
// Same as BitmapEquals, but considers a NULL bitmap pointer the same as an
|
138 |
+
// all-ones bitmap.
|
139 |
+
ARROW_EXPORT
|
140 |
+
bool OptionalBitmapEquals(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
141 |
+
int64_t right_offset, int64_t length);
|
142 |
+
|
143 |
+
ARROW_EXPORT
|
144 |
+
bool OptionalBitmapEquals(const std::shared_ptr<Buffer>& left, int64_t left_offset,
|
145 |
+
const std::shared_ptr<Buffer>& right, int64_t right_offset,
|
146 |
+
int64_t length);
|
147 |
+
|
148 |
+
/// \brief Do a "bitmap and" on right and left buffers starting at
|
149 |
+
/// their respective bit-offsets for the given bit-length and put
|
150 |
+
/// the results in out_buffer starting at the given bit-offset.
|
151 |
+
///
|
152 |
+
/// out_buffer will be allocated and initialized to zeros using pool before
|
153 |
+
/// the operation.
|
154 |
+
ARROW_EXPORT
|
155 |
+
Result<std::shared_ptr<Buffer>> BitmapAnd(MemoryPool* pool, const uint8_t* left,
|
156 |
+
int64_t left_offset, const uint8_t* right,
|
157 |
+
int64_t right_offset, int64_t length,
|
158 |
+
int64_t out_offset);
|
159 |
+
|
160 |
+
/// \brief Do a "bitmap and" on right and left buffers starting at
|
161 |
+
/// their respective bit-offsets for the given bit-length and put
|
162 |
+
/// the results in out starting at the given bit-offset.
|
163 |
+
ARROW_EXPORT
|
164 |
+
void BitmapAnd(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
165 |
+
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);
|
166 |
+
|
167 |
+
/// \brief Do a "bitmap or" for the given bit length on right and left buffers
|
168 |
+
/// starting at their respective bit-offsets and put the results in out_buffer
|
169 |
+
/// starting at the given bit-offset.
|
170 |
+
///
|
171 |
+
/// out_buffer will be allocated and initialized to zeros using pool before
|
172 |
+
/// the operation.
|
173 |
+
ARROW_EXPORT
|
174 |
+
Result<std::shared_ptr<Buffer>> BitmapOr(MemoryPool* pool, const uint8_t* left,
|
175 |
+
int64_t left_offset, const uint8_t* right,
|
176 |
+
int64_t right_offset, int64_t length,
|
177 |
+
int64_t out_offset);
|
178 |
+
|
179 |
+
/// \brief Do a "bitmap or" for the given bit length on right and left buffers
|
180 |
+
/// starting at their respective bit-offsets and put the results in out
|
181 |
+
/// starting at the given bit-offset.
|
182 |
+
ARROW_EXPORT
|
183 |
+
void BitmapOr(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
184 |
+
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);
|
185 |
+
|
186 |
+
/// \brief Do a "bitmap xor" for the given bit-length on right and left
|
187 |
+
/// buffers starting at their respective bit-offsets and put the results in
|
188 |
+
/// out_buffer starting at the given bit offset.
|
189 |
+
///
|
190 |
+
/// out_buffer will be allocated and initialized to zeros using pool before
|
191 |
+
/// the operation.
|
192 |
+
ARROW_EXPORT
|
193 |
+
Result<std::shared_ptr<Buffer>> BitmapXor(MemoryPool* pool, const uint8_t* left,
|
194 |
+
int64_t left_offset, const uint8_t* right,
|
195 |
+
int64_t right_offset, int64_t length,
|
196 |
+
int64_t out_offset);
|
197 |
+
|
198 |
+
/// \brief Do a "bitmap xor" for the given bit-length on right and left
|
199 |
+
/// buffers starting at their respective bit-offsets and put the results in
|
200 |
+
/// out starting at the given bit offset.
|
201 |
+
ARROW_EXPORT
|
202 |
+
void BitmapXor(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
203 |
+
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);
|
204 |
+
|
205 |
+
/// \brief Do a "bitmap and not" on right and left buffers starting at
|
206 |
+
/// their respective bit-offsets for the given bit-length and put
|
207 |
+
/// the results in out_buffer starting at the given bit-offset.
|
208 |
+
///
|
209 |
+
/// out_buffer will be allocated and initialized to zeros using pool before
|
210 |
+
/// the operation.
|
211 |
+
ARROW_EXPORT
|
212 |
+
Result<std::shared_ptr<Buffer>> BitmapAndNot(MemoryPool* pool, const uint8_t* left,
|
213 |
+
int64_t left_offset, const uint8_t* right,
|
214 |
+
int64_t right_offset, int64_t length,
|
215 |
+
int64_t out_offset);
|
216 |
+
|
217 |
+
/// \brief Do a "bitmap and not" on right and left buffers starting at
|
218 |
+
/// their respective bit-offsets for the given bit-length and put
|
219 |
+
/// the results in out starting at the given bit-offset.
|
220 |
+
ARROW_EXPORT
|
221 |
+
void BitmapAndNot(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
222 |
+
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);
|
223 |
+
|
224 |
+
/// \brief Do a "bitmap or not" on right and left buffers starting at
|
225 |
+
/// their respective bit-offsets for the given bit-length and put
|
226 |
+
/// the results in out_buffer starting at the given bit-offset.
|
227 |
+
///
|
228 |
+
/// out_buffer will be allocated and initialized to zeros using pool before
|
229 |
+
/// the operation.
|
230 |
+
ARROW_EXPORT
|
231 |
+
Result<std::shared_ptr<Buffer>> BitmapOrNot(MemoryPool* pool, const uint8_t* left,
|
232 |
+
int64_t left_offset, const uint8_t* right,
|
233 |
+
int64_t right_offset, int64_t length,
|
234 |
+
int64_t out_offset);
|
235 |
+
|
236 |
+
/// \brief Do a "bitmap or not" on right and left buffers starting at
|
237 |
+
/// their respective bit-offsets for the given bit-length and put
|
238 |
+
/// the results in out starting at the given bit-offset.
|
239 |
+
ARROW_EXPORT
|
240 |
+
void BitmapOrNot(const uint8_t* left, int64_t left_offset, const uint8_t* right,
|
241 |
+
int64_t right_offset, int64_t length, int64_t out_offset, uint8_t* out);
|
242 |
+
|
243 |
+
} // namespace internal
|
244 |
+
} // namespace arrow
|
venv/lib/python3.10/site-packages/pyarrow/include/arrow/util/checked_cast.h
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Licensed to the Apache Software Foundation (ASF) under one
|
2 |
+
// or more contributor license agreements. See the NOTICE file
|
3 |
+
// distributed with this work for additional information
|
4 |
+
// regarding copyright ownership. The ASF licenses this file
|
5 |
+
// to you under the Apache License, Version 2.0 (the
|
6 |
+
// "License"); you may not use this file except in compliance
|
7 |
+
// with the License. You may obtain a copy of the License at
|
8 |
+
//
|
9 |
+
// http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
//
|
11 |
+
// Unless required by applicable law or agreed to in writing,
|
12 |
+
// software distributed under the License is distributed on an
|
13 |
+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14 |
+
// KIND, either express or implied. See the License for the
|
15 |
+
// specific language governing permissions and limitations
|
16 |
+
// under the License.
|
17 |
+
|
18 |
+
#pragma once
|
19 |
+
|
20 |
+
#include <memory>
|
21 |
+
#include <type_traits>
|
22 |
+
#include <utility>
|
23 |
+
|
24 |
+
namespace arrow {
|
25 |
+
namespace internal {
|
26 |
+
|
27 |
+
template <typename OutputType, typename InputType>
|
28 |
+
inline OutputType checked_cast(InputType&& value) {
|
29 |
+
static_assert(std::is_class<typename std::remove_pointer<
|
30 |
+
typename std::remove_reference<InputType>::type>::type>::value,
|
31 |
+
"checked_cast input type must be a class");
|
32 |
+
static_assert(std::is_class<typename std::remove_pointer<
|
33 |
+
typename std::remove_reference<OutputType>::type>::type>::value,
|
34 |
+
"checked_cast output type must be a class");
|
35 |
+
#ifdef NDEBUG
|
36 |
+
return static_cast<OutputType>(value);
|
37 |
+
#else
|
38 |
+
return dynamic_cast<OutputType>(value);
|
39 |
+
#endif
|
40 |
+
}
|
41 |
+
|
42 |
+
template <class T, class U>
|
43 |
+
std::shared_ptr<T> checked_pointer_cast(std::shared_ptr<U> r) noexcept {
|
44 |
+
#ifdef NDEBUG
|
45 |
+
return std::static_pointer_cast<T>(std::move(r));
|
46 |
+
#else
|
47 |
+
return std::dynamic_pointer_cast<T>(std::move(r));
|
48 |
+
#endif
|
49 |
+
}
|
50 |
+
|
51 |
+
template <class T, class U>
|
52 |
+
std::unique_ptr<T> checked_pointer_cast(std::unique_ptr<U> r) noexcept {
|
53 |
+
#ifdef NDEBUG
|
54 |
+
return std::unique_ptr<T>(static_cast<T*>(r.release()));
|
55 |
+
#else
|
56 |
+
return std::unique_ptr<T>(dynamic_cast<T*>(r.release()));
|
57 |
+
#endif
|
58 |
+
}
|
59 |
+
|
60 |
+
} // namespace internal
|
61 |
+
} // namespace arrow
|