forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projector.cpp
202 lines (178 loc) · 6.08 KB
/
projector.cpp
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
/*
* 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.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <arrow-glib/basic-array.hpp>
#include <arrow-glib/record-batch.hpp>
#include <arrow-glib/schema.hpp>
#include <arrow-glib/error.hpp>
#include <gandiva-glib/expression.hpp>
#include <gandiva-glib/projector.hpp>
G_BEGIN_DECLS
/**
* SECTION: projector
* @title: Projector classes
* @include: gandiva-glib/gandiva-glib.h
*
* #GGandivaProjector is a class for building a specific schema
* and vector of expressions.
*
* Since: 0.12.0
*/
typedef struct GGandivaProjectorPrivate_ {
std::shared_ptr<gandiva::Projector> projector;
} GGandivaProjectorPrivate;
enum {
PROP_0,
PROP_PROJECTOR
};
G_DEFINE_TYPE_WITH_PRIVATE(GGandivaProjector,
ggandiva_projector,
G_TYPE_OBJECT)
#define GGANDIVA_PROJECTOR_GET_PRIVATE(obj) \
static_cast<GGandivaProjectorPrivate *>( \
ggandiva_projector_get_instance_private( \
GGANDIVA_PROJECTOR(obj)))
static void
ggandiva_projector_finalize(GObject *object)
{
auto priv = GGANDIVA_PROJECTOR_GET_PRIVATE(object);
priv->projector = nullptr;
G_OBJECT_CLASS(ggandiva_projector_parent_class)->finalize(object);
}
static void
ggandiva_projector_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
auto priv = GGANDIVA_PROJECTOR_GET_PRIVATE(object);
switch (prop_id) {
case PROP_PROJECTOR:
priv->projector =
*static_cast<std::shared_ptr<gandiva::Projector> *>(g_value_get_pointer(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
ggandiva_projector_init(GGandivaProjector *object)
{
}
static void
ggandiva_projector_class_init(GGandivaProjectorClass *klass)
{
GParamSpec *spec;
auto gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = ggandiva_projector_finalize;
gobject_class->set_property = ggandiva_projector_set_property;
spec = g_param_spec_pointer("projector",
"Projector",
"The raw std::shared<gandiva::Projector> *",
static_cast<GParamFlags>(G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_PROJECTOR, spec);
}
/**
* ggandiva_projector_new:
* @schema: A #GArrowSchema.
* @expressions: (element-type GGandivaExpression): The built expressions.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): A newly created #GGandivaProjector on success,
* %NULL on error.
*
* Since: 0.12.0
*/
GGandivaProjector *
ggandiva_projector_new(GArrowSchema *schema,
GList *expressions,
GError **error)
{
auto arrow_schema = garrow_schema_get_raw(schema);
std::vector<std::shared_ptr<gandiva::Expression>> gandiva_expressions;
for (auto node = expressions; node; node = g_list_next(node)) {
auto expression = GGANDIVA_EXPRESSION(node->data);
auto gandiva_expression = ggandiva_expression_get_raw(expression);
gandiva_expressions.push_back(gandiva_expression);
}
std::shared_ptr<gandiva::Projector> gandiva_projector;
auto status =
gandiva_projector->Make(arrow_schema,
gandiva_expressions,
&gandiva_projector);
if (garrow_error_check(error, status, "[gandiva][projector][new]")) {
return ggandiva_projector_new_raw(&gandiva_projector);
} else {
return NULL;
}
}
/**
* ggandiva_projector_evaluate:
* @projector: A #GGandivaProjector.
* @record_batch: A #GArrowRecordBatch.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (element-type GArrowArray) (nullable) (transfer full):
* The #GArrowArray as the result evaluated on success, %NULL on error.
*
* Since: 0.12.0
*/
GList *
ggandiva_projector_evaluate(GGandivaProjector *projector,
GArrowRecordBatch *record_batch,
GError **error)
{
auto gandiva_projector = ggandiva_projector_get_raw(projector);
auto arrow_record_batch = garrow_record_batch_get_raw(record_batch);
auto memory_pool = arrow::default_memory_pool();
arrow::ArrayVector arrow_arrays;
auto status =
gandiva_projector->Evaluate(*arrow_record_batch,
memory_pool,
&arrow_arrays);
if (garrow_error_check(error, status, "[gandiva][projector][evaluate]")) {
GList *arrays = NULL;
for (auto arrow_array : arrow_arrays) {
auto array = garrow_array_new_raw(&arrow_array);
arrays = g_list_prepend(arrays, array);
}
return g_list_reverse(arrays);
} else {
return NULL;
}
}
G_END_DECLS
GGandivaProjector *
ggandiva_projector_new_raw(std::shared_ptr<gandiva::Projector> *gandiva_projector)
{
auto projector = g_object_new(GGANDIVA_TYPE_PROJECTOR,
"projector", gandiva_projector,
NULL);
return GGANDIVA_PROJECTOR(projector);
}
std::shared_ptr<gandiva::Projector>
ggandiva_projector_get_raw(GGandivaProjector *projector)
{
auto priv = GGANDIVA_PROJECTOR_GET_PRIVATE(projector);
return priv->projector;
}