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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "converters.hpp"
namespace red_arrow {
namespace {
class RawRecordsBuilder : private Converter, public arrow::ArrayVisitor {
public:
explicit RawRecordsBuilder(VALUE records, int n_columns)
: Converter(),
records_(records),
n_columns_(n_columns) {
}
void build(const arrow::RecordBatch& record_batch) {
rb::protect([&] {
const auto n_rows = record_batch.num_rows();
for (int64_t i = 0; i < n_rows; ++i) {
auto record = rb_ary_new_capa(n_columns_);
rb_ary_push(records_, record);
}
row_offset_ = 0;
for (int i = 0; i < n_columns_; ++i) {
const auto array = record_batch.column(i).get();
column_index_ = i;
check_status(array->Accept(this),
"[record-batch][raw-records]");
}
return Qnil;
});
}
void build(const arrow::Table& table) {
rb::protect([&] {
const auto n_rows = table.num_rows();
for (int64_t i = 0; i < n_rows; ++i) {
auto record = rb_ary_new_capa(n_columns_);
rb_ary_push(records_, record);
}
for (int i = 0; i < n_columns_; ++i) {
const auto& chunked_array = table.column(i).get();
column_index_ = i;
row_offset_ = 0;
for (const auto array : chunked_array->chunks()) {
check_status(array->Accept(this),
"[table][raw-records]");
row_offset_ += array->length();
}
}
return Qnil;
});
}
#define VISIT(TYPE) \
arrow::Status Visit(const arrow::TYPE ## Array& array) override { \
convert(array); \
return arrow::Status::OK(); \
}
VISIT(Null)
VISIT(Boolean)
VISIT(Int8)
VISIT(Int16)
VISIT(Int32)
VISIT(Int64)
VISIT(UInt8)
VISIT(UInt16)
VISIT(UInt32)
VISIT(UInt64)
VISIT(HalfFloat)
VISIT(Float)
VISIT(Double)
VISIT(Binary)
VISIT(String)
VISIT(FixedSizeBinary)
VISIT(Date32)
VISIT(Date64)
VISIT(Time32)
VISIT(Time64)
VISIT(Timestamp)
VISIT(MonthInterval)
VISIT(DayTimeInterval)
VISIT(MonthDayNanoInterval)
VISIT(List)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
VISIT(DenseUnion)
VISIT(Dictionary)
VISIT(Decimal128)
VISIT(Decimal256)
// TODO
// VISIT(Extension)
#undef VISIT
private:
template <typename ArrayType>
void convert(const ArrayType& array) {
const auto n = array.length();
if (array.null_count() > 0) {
for (int64_t i = 0, ii = row_offset_; i < n; ++i, ++ii) {
auto value = Qnil;
if (!array.IsNull(i)) {
value = convert_value(array, i);
}
auto record = rb_ary_entry(records_, ii);
rb_ary_store(record, column_index_, value);
}
} else {
for (int64_t i = 0, ii = row_offset_; i < n; ++i, ++ii) {
auto record = rb_ary_entry(records_, ii);
rb_ary_store(record, column_index_, convert_value(array, i));
}
}
}
// Destination for converted records.
VALUE records_;
// The current column index.
int column_index_;
// The current row offset.
int64_t row_offset_;
// The number of columns.
const int n_columns_;
};
class RawRecordsProducer : private Converter, public arrow::ArrayVisitor {
public:
explicit RawRecordsProducer()
: Converter(),
record_(Qnil),
column_index_(0),
row_offset_(0) {
}
void produce(const arrow::RecordBatch& record_batch) {
rb::protect([&] {
const auto n_columns = record_batch.num_columns();
const auto n_rows = record_batch.num_rows();
for (int64_t i = 0; i < n_rows; ++i) {
record_ = rb_ary_new_capa(n_columns);
row_offset_ = i;
for (int i = 0; i < n_columns; ++i) {
const auto array = record_batch.column(i).get();
column_index_ = i;
check_status(array->Accept(this),
"[record-batch][each-raw-record]");
}
rb_yield(record_);
}
return Qnil;
});
}
void produce(const arrow::Table& table) {
rb::protect([&] {
const auto n_columns = table.num_columns();
const auto n_rows = table.num_rows();
std::vector<int> chunk_indexes(n_columns);
std::vector<int64_t> row_offsets(n_columns);
for (int64_t i_row = 0; i_row < n_rows; ++i_row) {
record_ = rb_ary_new_capa(n_columns);
for (int i_column = 0; i_column < n_columns; ++i_column) {
column_index_ = i_column;
const auto chunked_array = table.column(i_column).get();
auto& chunk_index = chunk_indexes[i_column];
auto& row_offset = row_offsets[i_column];
auto array = chunked_array->chunk(chunk_index).get();
while (array->length() == row_offset) {
++chunk_index;
row_offset = 0;
array = chunked_array->chunk(chunk_index).get();
}
row_offset_ = row_offset;
check_status(array->Accept(this),
"[table][each-raw-record]");
++row_offset;
}
rb_yield(record_);
}
return Qnil;
});
}
#define VISIT(TYPE) \
arrow::Status Visit(const arrow::TYPE ## Array& array) override { \
convert(array); \
return arrow::Status::OK(); \
}
VISIT(Null)
VISIT(Boolean)
VISIT(Int8)
VISIT(Int16)
VISIT(Int32)
VISIT(Int64)
VISIT(UInt8)
VISIT(UInt16)
VISIT(UInt32)
VISIT(UInt64)
VISIT(HalfFloat)
VISIT(Float)
VISIT(Double)
VISIT(Binary)
VISIT(String)
VISIT(FixedSizeBinary)
VISIT(Date32)
VISIT(Date64)
VISIT(Time32)
VISIT(Time64)
VISIT(Timestamp)
VISIT(MonthInterval)
VISIT(DayTimeInterval)
VISIT(MonthDayNanoInterval)
VISIT(List)
VISIT(Struct)
VISIT(Map)
VISIT(SparseUnion)
VISIT(DenseUnion)
VISIT(Dictionary)
VISIT(Decimal128)
VISIT(Decimal256)
// TODO
// VISIT(Extension)
#undef VISIT
private:
template <typename ArrayType>
void convert(const ArrayType& array) {
auto value = Qnil;
if (!array.IsNull(row_offset_)) {
value = convert_value(array, row_offset_);
}
rb_ary_store(record_, column_index_, value);
}
// Destination for converted record.
VALUE record_;
// The current column index.
int column_index_;
// The current row offset.
int64_t row_offset_;
};
}
VALUE
record_batch_raw_records(VALUE rb_record_batch) {
auto garrow_record_batch = GARROW_RECORD_BATCH(RVAL2GOBJ(rb_record_batch));
auto record_batch = garrow_record_batch_get_raw(garrow_record_batch).get();
const auto n_rows = record_batch->num_rows();
const auto n_columns = record_batch->num_columns();
auto records = rb_ary_new_capa(n_rows);
try {
RawRecordsBuilder builder(records, n_columns);
builder.build(*record_batch);
} catch (rb::State& state) {
state.jump();
}
return records;
}
VALUE
table_raw_records(VALUE rb_table) {
auto garrow_table = GARROW_TABLE(RVAL2GOBJ(rb_table));
auto table = garrow_table_get_raw(garrow_table).get();
const auto n_rows = table->num_rows();
const auto n_columns = table->num_columns();
auto records = rb_ary_new_capa(n_rows);
try {
RawRecordsBuilder builder(records, n_columns);
builder.build(*table);
} catch (rb::State& state) {
state.jump();
}
return records;
}
VALUE
record_batch_each_raw_record(VALUE rb_record_batch) {
auto garrow_record_batch = GARROW_RECORD_BATCH(RVAL2GOBJ(rb_record_batch));
auto record_batch = garrow_record_batch_get_raw(garrow_record_batch).get();
RETURN_SIZED_ENUMERATOR(rb_record_batch, 0, nullptr, record_batch->num_rows());
try {
RawRecordsProducer producer;
producer.produce(*record_batch);
} catch (rb::State& state) {
state.jump();
}
return Qnil;
}
VALUE
table_each_raw_record(VALUE rb_table) {
auto garrow_table = GARROW_TABLE(RVAL2GOBJ(rb_table));
auto table = garrow_table_get_raw(garrow_table).get();
RETURN_SIZED_ENUMERATOR(rb_table, 0, nullptr, table->num_rows());
try {
RawRecordsProducer producer;
producer.produce(*table);
} catch (rb::State& state) {
state.jump();
}
return Qnil;
}
}
|