]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/ruby/red-arrow/ext/arrow/values.cpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / ruby / red-arrow / ext / arrow / values.cpp
CommitLineData
1d09f67e
TL
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include "converters.hpp"
21
22namespace red_arrow {
23 namespace {
24 class ValuesBuilder : private Converter, public arrow::ArrayVisitor {
25 public:
26 explicit ValuesBuilder(VALUE values)
27 : Converter(),
28 values_(values),
29 row_offset_(0) {
30 }
31
32 void build(const arrow::Array& array, VALUE rb_array) {
33 rb::protect([&] {
34 check_status(array.Accept(this),
35 "[array][values]");
36 return Qnil;
37 });
38 }
39
40 void build(const arrow::ChunkedArray& chunked_array,
41 VALUE rb_chunked_array) {
42 rb::protect([&] {
43 for (const auto& array : chunked_array.chunks()) {
44 check_status(array->Accept(this),
45 "[chunked-array][values]");
46 row_offset_ += array->length();
47 }
48 return Qnil;
49 });
50 }
51
52#define VISIT(TYPE) \
53 arrow::Status Visit(const arrow::TYPE ## Array& array) override { \
54 convert(array); \
55 return arrow::Status::OK(); \
56 }
57
58 VISIT(Null)
59 VISIT(Boolean)
60 VISIT(Int8)
61 VISIT(Int16)
62 VISIT(Int32)
63 VISIT(Int64)
64 VISIT(UInt8)
65 VISIT(UInt16)
66 VISIT(UInt32)
67 VISIT(UInt64)
68 // TODO
69 // VISIT(HalfFloat)
70 VISIT(Float)
71 VISIT(Double)
72 VISIT(Binary)
73 VISIT(String)
74 VISIT(FixedSizeBinary)
75 VISIT(Date32)
76 VISIT(Date64)
77 VISIT(Time32)
78 VISIT(Time64)
79 VISIT(Timestamp)
80 // TODO
81 // VISIT(Interval)
82 VISIT(List)
83 VISIT(Struct)
84 VISIT(Map)
85 VISIT(SparseUnion)
86 VISIT(DenseUnion)
87 VISIT(Dictionary)
88 VISIT(Decimal128)
89 VISIT(Decimal256)
90 // TODO
91 // VISIT(Extension)
92
93#undef VISIT
94
95 private:
96 template <typename ArrayType>
97 void convert(const ArrayType& array) {
98 const auto n = array.length();
99 if (array.null_count() > 0) {
100 for (int64_t i = 0, ii = row_offset_; i < n; ++i, ++ii) {
101 auto value = Qnil;
102 if (!array.IsNull(i)) {
103 value = convert_value(array, i);
104 }
105 rb_ary_store(values_, ii, value);
106 }
107 } else {
108 for (int64_t i = 0, ii = row_offset_; i < n; ++i, ++ii) {
109 rb_ary_store(values_, ii, convert_value(array, i));
110 }
111 }
112 }
113
114 // Destination for converted values.
115 VALUE values_;
116
117 // The current row offset.
118 int64_t row_offset_;
119 };
120 }
121
122 VALUE
123 array_values(VALUE rb_array) {
124 auto garrow_array = GARROW_ARRAY(RVAL2GOBJ(rb_array));
125 auto array = garrow_array_get_raw(garrow_array).get();
126 const auto n_rows = array->length();
127 auto values = rb_ary_new_capa(n_rows);
128
129 try {
130 ValuesBuilder builder(values);
131 builder.build(*array, rb_array);
132 } catch (rb::State& state) {
133 state.jump();
134 }
135
136 return values;
137 }
138
139 VALUE
140 chunked_array_values(VALUE rb_chunked_array) {
141 auto garrow_chunked_array =
142 GARROW_CHUNKED_ARRAY(RVAL2GOBJ(rb_chunked_array));
143 auto chunked_array =
144 garrow_chunked_array_get_raw(garrow_chunked_array).get();
145 const auto n_rows = chunked_array->length();
146 auto values = rb_ary_new_capa(n_rows);
147
148 try {
149 ValuesBuilder builder(values);
150 builder.build(*chunked_array, rb_chunked_array);
151 } catch (rb::State& state) {
152 state.jump();
153 }
154
155 return values;
156 }
157}