File indexing completed on 2025-08-28 08:26:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #pragma once
0019
0020 #include <cstdint>
0021 #include <iosfwd>
0022 #include <memory>
0023 #include <string>
0024 #include <vector>
0025
0026 #include "arrow/array/data.h"
0027 #include "arrow/buffer.h"
0028 #include "arrow/compare.h"
0029 #include "arrow/result.h"
0030 #include "arrow/status.h"
0031 #include "arrow/type.h"
0032 #include "arrow/util/bit_util.h"
0033 #include "arrow/util/macros.h"
0034 #include "arrow/util/visibility.h"
0035 #include "arrow/visitor.h"
0036
0037 namespace arrow {
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 class ARROW_EXPORT Array {
0054 public:
0055 virtual ~Array() = default;
0056
0057
0058 bool IsNull(int64_t i) const { return !IsValid(i); }
0059
0060
0061
0062 bool IsValid(int64_t i) const {
0063 if (null_bitmap_data_ != NULLPTR) {
0064 return bit_util::GetBit(null_bitmap_data_, i + data_->offset);
0065 }
0066
0067
0068
0069
0070 if (type_id() == Type::SPARSE_UNION) {
0071 return !internal::IsNullSparseUnion(*data_, i);
0072 }
0073 if (type_id() == Type::DENSE_UNION) {
0074 return !internal::IsNullDenseUnion(*data_, i);
0075 }
0076 if (type_id() == Type::RUN_END_ENCODED) {
0077 return !internal::IsNullRunEndEncoded(*data_, i);
0078 }
0079 return data_->null_count != data_->length;
0080 }
0081
0082
0083 Result<std::shared_ptr<Scalar>> GetScalar(int64_t i) const;
0084
0085
0086 int64_t length() const { return data_->length; }
0087
0088
0089
0090 int64_t offset() const { return data_->offset; }
0091
0092
0093
0094
0095
0096 int64_t null_count() const;
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 int64_t ComputeLogicalNullCount() const;
0108
0109 const std::shared_ptr<DataType>& type() const { return data_->type; }
0110 Type::type type_id() const { return data_->type->id(); }
0111
0112
0113
0114
0115
0116
0117 const std::shared_ptr<Buffer>& null_bitmap() const { return data_->buffers[0]; }
0118
0119
0120
0121
0122
0123 const uint8_t* null_bitmap_data() const { return null_bitmap_data_; }
0124
0125
0126 bool Equals(const Array& arr, const EqualOptions& = EqualOptions::Defaults()) const;
0127 bool Equals(const std::shared_ptr<Array>& arr,
0128 const EqualOptions& = EqualOptions::Defaults()) const;
0129
0130
0131
0132 std::string Diff(const Array& other) const;
0133
0134
0135
0136
0137 bool ApproxEquals(const std::shared_ptr<Array>& arr,
0138 const EqualOptions& = EqualOptions::Defaults()) const;
0139 bool ApproxEquals(const Array& arr,
0140 const EqualOptions& = EqualOptions::Defaults()) const;
0141
0142
0143
0144 bool RangeEquals(int64_t start_idx, int64_t end_idx, int64_t other_start_idx,
0145 const Array& other,
0146 const EqualOptions& = EqualOptions::Defaults()) const;
0147 bool RangeEquals(int64_t start_idx, int64_t end_idx, int64_t other_start_idx,
0148 const std::shared_ptr<Array>& other,
0149 const EqualOptions& = EqualOptions::Defaults()) const;
0150 bool RangeEquals(const Array& other, int64_t start_idx, int64_t end_idx,
0151 int64_t other_start_idx,
0152 const EqualOptions& = EqualOptions::Defaults()) const;
0153 bool RangeEquals(const std::shared_ptr<Array>& other, int64_t start_idx,
0154 int64_t end_idx, int64_t other_start_idx,
0155 const EqualOptions& = EqualOptions::Defaults()) const;
0156
0157
0158 Status Accept(ArrayVisitor* visitor) const;
0159
0160
0161
0162
0163
0164
0165
0166 Result<std::shared_ptr<Array>> View(const std::shared_ptr<DataType>& type) const;
0167
0168
0169
0170
0171
0172
0173 Result<std::shared_ptr<Array>> CopyTo(const std::shared_ptr<MemoryManager>& to) const;
0174
0175
0176
0177
0178
0179
0180
0181 Result<std::shared_ptr<Array>> ViewOrCopyTo(
0182 const std::shared_ptr<MemoryManager>& to) const;
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193 std::shared_ptr<Array> Slice(int64_t offset, int64_t length) const;
0194
0195
0196 std::shared_ptr<Array> Slice(int64_t offset) const;
0197
0198
0199 Result<std::shared_ptr<Array>> SliceSafe(int64_t offset, int64_t length) const;
0200
0201 Result<std::shared_ptr<Array>> SliceSafe(int64_t offset) const;
0202
0203 const std::shared_ptr<ArrayData>& data() const { return data_; }
0204
0205 int num_fields() const { return static_cast<int>(data_->child_data.size()); }
0206
0207
0208 std::string ToString() const;
0209
0210
0211
0212
0213
0214
0215
0216 Status Validate() const;
0217
0218
0219
0220
0221
0222
0223
0224
0225 Status ValidateFull() const;
0226
0227
0228
0229
0230
0231
0232
0233 DeviceAllocationType device_type() const { return data_->device_type(); }
0234
0235
0236
0237
0238
0239
0240
0241 const std::shared_ptr<ArrayStatistics>& statistics() const { return data_->statistics; }
0242
0243 protected:
0244 Array() = default;
0245 ARROW_DEFAULT_MOVE_AND_ASSIGN(Array);
0246
0247 std::shared_ptr<ArrayData> data_;
0248 const uint8_t* null_bitmap_data_ = NULLPTR;
0249
0250
0251 void SetData(const std::shared_ptr<ArrayData>& data) {
0252 if (data->buffers.size() > 0) {
0253 null_bitmap_data_ = data->GetValuesSafe<uint8_t>(0, 0);
0254 } else {
0255 null_bitmap_data_ = NULLPTR;
0256 }
0257 data_ = data;
0258 }
0259
0260 private:
0261 ARROW_DISALLOW_COPY_AND_ASSIGN(Array);
0262
0263 ARROW_FRIEND_EXPORT friend void PrintTo(const Array& x, std::ostream* os);
0264 };
0265
0266 static inline std::ostream& operator<<(std::ostream& os, const Array& x) {
0267 os << x.ToString();
0268 return os;
0269 }
0270
0271
0272 class ARROW_EXPORT FlatArray : public Array {
0273 protected:
0274 using Array::Array;
0275 };
0276
0277
0278 class ARROW_EXPORT PrimitiveArray : public FlatArray {
0279 public:
0280 PrimitiveArray(const std::shared_ptr<DataType>& type, int64_t length,
0281 const std::shared_ptr<Buffer>& data,
0282 const std::shared_ptr<Buffer>& null_bitmap = NULLPTR,
0283 int64_t null_count = kUnknownNullCount, int64_t offset = 0);
0284
0285
0286 const std::shared_ptr<Buffer>& values() const { return data_->buffers[1]; }
0287
0288 protected:
0289 PrimitiveArray() : raw_values_(NULLPTR) {}
0290
0291 void SetData(const std::shared_ptr<ArrayData>& data) {
0292 this->Array::SetData(data);
0293 raw_values_ = data->GetValuesSafe<uint8_t>(1, 0);
0294 }
0295
0296 explicit PrimitiveArray(const std::shared_ptr<ArrayData>& data) { SetData(data); }
0297
0298 const uint8_t* raw_values_;
0299 };
0300
0301
0302 class ARROW_EXPORT NullArray : public FlatArray {
0303 public:
0304 using TypeClass = NullType;
0305
0306 explicit NullArray(const std::shared_ptr<ArrayData>& data) { SetData(data); }
0307 explicit NullArray(int64_t length);
0308
0309 private:
0310 void SetData(const std::shared_ptr<ArrayData>& data) {
0311 null_bitmap_data_ = NULLPTR;
0312 data->null_count = data->length;
0313 data_ = data;
0314 }
0315 };
0316
0317 }