arrow.tpp 6.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <LightGBM/arrow.h>

#ifndef ARROW_TPP_
#define ARROW_TPP_

namespace LightGBM {

/**
 * @brief Obtain a function to access an index from an Arrow array.
 *
 * @tparam T The return type of the function, must be a primitive type.
 * @param dtype The Arrow format string describing the datatype of the Arrow array.
 * @return std::function<T(const ArrowArray*, size_t)> The index accessor function.
 */
template <typename T>
std::function<T(const ArrowArray*, size_t)> get_index_accessor(const char* dtype);

/* ---------------------------------- ITERATOR INITIALIZATION ---------------------------------- */

template <typename T>
inline ArrowChunkedArray::Iterator<T> ArrowChunkedArray::begin() const {
  return ArrowChunkedArray::Iterator<T>(*this, get_index_accessor<T>(schema_->format), 0);
}

template <typename T>
inline ArrowChunkedArray::Iterator<T> ArrowChunkedArray::end() const {
  return ArrowChunkedArray::Iterator<T>(*this, get_index_accessor<T>(schema_->format),
                                        chunk_offsets_.size() - 1);
}

/* ---------------------------------- ITERATOR IMPLEMENTATION ---------------------------------- */

template <typename T>
34
ArrowChunkedArray::Iterator<T>::Iterator(const ArrowChunkedArray& array, getter_fn get,
35
36
37
38
39
40
41
42
                                         int64_t ptr_chunk)
    : array_(array), get_(get), ptr_chunk_(ptr_chunk) {
  this->ptr_offset_ = 0;
}

template <typename T>
T ArrowChunkedArray::Iterator<T>::operator*() const {
  auto chunk = array_.chunks_[ptr_chunk_];
43
  return get_(chunk, ptr_offset_);
44
45
46
47
48
49
50
51
52
53
54
55
}

template <typename T>
template <typename I>
T ArrowChunkedArray::Iterator<T>::operator[](I idx) const {
  auto it = std::lower_bound(array_.chunk_offsets_.begin(), array_.chunk_offsets_.end(), idx,
                             [](int64_t a, int64_t b) { return a <= b; });

  auto chunk_idx = std::distance(array_.chunk_offsets_.begin() + 1, it);
  auto chunk = array_.chunks_[chunk_idx];

  auto ptr_offset = static_cast<int64_t>(idx) - array_.chunk_offsets_[chunk_idx];
56
  return get_(chunk, ptr_offset);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}

template <typename T>
ArrowChunkedArray::Iterator<T>& ArrowChunkedArray::Iterator<T>::operator++() {
  if (ptr_offset_ + 1 >= array_.chunks_[ptr_chunk_]->length) {
    ptr_offset_ = 0;
    ptr_chunk_++;
  } else {
    ptr_offset_++;
  }
  return *this;
}

template <typename T>
ArrowChunkedArray::Iterator<T>& ArrowChunkedArray::Iterator<T>::operator--() {
  if (ptr_offset_ == 0) {
    ptr_chunk_--;
    ptr_offset_ = array_.chunks_[ptr_chunk_]->length - 1;
  } else {
    ptr_chunk_--;
  }
  return *this;
}

template <typename T>
ArrowChunkedArray::Iterator<T>& ArrowChunkedArray::Iterator<T>::operator+=(int64_t c) {
  while (ptr_offset_ + c >= array_.chunks_[ptr_chunk_]->length) {
    c -= array_.chunks_[ptr_chunk_]->length - ptr_offset_;
    ptr_offset_ = 0;
    ptr_chunk_++;
  }
  ptr_offset_ += c;
  return *this;
}

template <typename T>
bool operator==(const ArrowChunkedArray::Iterator<T>& a, const ArrowChunkedArray::Iterator<T>& b) {
  return a.ptr_chunk_ == b.ptr_chunk_ && a.ptr_offset_ == b.ptr_offset_;
}

template <typename T>
bool operator!=(const ArrowChunkedArray::Iterator<T>& a, const ArrowChunkedArray::Iterator<T>& b) {
  return a.ptr_chunk_ != b.ptr_chunk_ || a.ptr_offset_ != b.ptr_offset_;
}

template <typename T>
int64_t operator-(const ArrowChunkedArray::Iterator<T>& a,
                  const ArrowChunkedArray::Iterator<T>& b) {
  auto full_offset_a = a.array_.chunk_offsets_[a.ptr_chunk_] + a.ptr_offset_;
  auto full_offset_b = b.array_.chunk_offsets_[b.ptr_chunk_] + b.ptr_offset_;
  return full_offset_a - full_offset_b;
}

/* --------------------------------------- INDEX ACCESSOR -------------------------------------- */

/**
 * @brief The value of "no value" for a primitive type.
 *
 * @tparam T The type for which the missing value is defined.
 * @return T The missing value.
 */
template <typename T>
inline T arrow_primitive_missing_value() {
  return 0;
}

template <>
inline double arrow_primitive_missing_value() {
  return std::numeric_limits<double>::quiet_NaN();
}

template <>
inline float arrow_primitive_missing_value() {
  return std::numeric_limits<float>::quiet_NaN();
}

template <typename T, typename V>
struct ArrayIndexAccessor {
  V operator()(const ArrowArray* array, size_t idx) {
    auto buffer_idx = idx + array->offset;

    // For primitive types, buffer at idx 0 provides validity, buffer at idx 1 data, see:
    // https://arrow.apache.org/docs/format/Columnar.html#buffer-listing-for-each-layout
    auto validity = static_cast<const char*>(array->buffers[0]);

    // Take return value from data buffer conditional on the validity of the index:
    //  - The structure of validity bitmasks is taken from here:
    //    https://arrow.apache.org/docs/format/Columnar.html#validity-bitmaps
    //  - If the bitmask is NULL, all indices are valid
146
    if (validity == nullptr || (validity[buffer_idx / 8] & (1 << (buffer_idx % 8)))) {
147
148
      // In case the index is valid, we take it from the data buffer
      auto data = static_cast<const T*>(array->buffers[1]);
149
      return static_cast<V>(data[buffer_idx]);
150
151
152
    }

    // In case the index is not valid, we return a default value
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    return arrow_primitive_missing_value<V>();
  }
};

template <typename V>
struct ArrayIndexAccessor<bool, V> {
  V operator()(const ArrowArray* array, size_t idx) {
    // Custom implementation for booleans as values are bit-packed:
    // https://arrow.apache.org/docs/cpp/api/datatype.html#_CPPv4N5arrow4Type4type4BOOLE
    auto buffer_idx = idx + array->offset;
    auto validity = static_cast<const char*>(array->buffers[0]);
    if (validity == nullptr || (validity[buffer_idx / 8] & (1 << (buffer_idx % 8)))) {
      // In case the index is valid, we have to take the appropriate bit from the buffer
      auto data = static_cast<const char*>(array->buffers[1]);
      auto value = (data[buffer_idx / 8] & (1 << (buffer_idx % 8))) >> (buffer_idx % 8);
      return static_cast<V>(value);
    }
    return arrow_primitive_missing_value<V>();
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  }
};

template <typename T>
std::function<T(const ArrowArray*, size_t)> get_index_accessor(const char* dtype) {
  // Mapping obtained from:
  // https://arrow.apache.org/docs/format/CDataInterface.html#data-type-description-format-strings
  switch (dtype[0]) {
    case 'c':
      return ArrayIndexAccessor<int8_t, T>();
    case 'C':
      return ArrayIndexAccessor<uint8_t, T>();
    case 's':
      return ArrayIndexAccessor<int16_t, T>();
    case 'S':
      return ArrayIndexAccessor<uint16_t, T>();
    case 'i':
      return ArrayIndexAccessor<int32_t, T>();
    case 'I':
      return ArrayIndexAccessor<uint32_t, T>();
    case 'l':
      return ArrayIndexAccessor<int64_t, T>();
    case 'L':
      return ArrayIndexAccessor<uint64_t, T>();
    case 'f':
      return ArrayIndexAccessor<float, T>();
    case 'g':
      return ArrayIndexAccessor<double, T>();
199
200
    case 'b':
      return ArrayIndexAccessor<bool, T>();
201
202
203
204
205
206
207
208
    default:
      throw std::invalid_argument("unsupported Arrow datatype");
  }
}

}  // namespace LightGBM

#endif