]>
Commit | Line | Data |
---|---|---|
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 <arrow-glib/compute.hpp> | |
21 | #include <arrow-glib/datum.hpp> | |
22 | #include <arrow-glib/expression.hpp> | |
23 | #include <arrow-glib/error.hpp> | |
24 | ||
25 | G_BEGIN_DECLS | |
26 | ||
27 | /** | |
28 | * SECTION: expression | |
29 | * @section_id: expression | |
30 | * @title: Expression | |
31 | * @include: arrow-glib/arrow-glib.h | |
32 | * | |
33 | * #GArrowExpression is a base class for all expression classes such | |
34 | * as #GArrowLiteralExpression. | |
35 | * | |
36 | * #GArrowLiteralExpression is a class for literal value. | |
37 | * | |
38 | * #GArrowFieldExpression is a class for field reference. | |
39 | * | |
40 | * #GArrowCallExpression is a class for function call. | |
41 | * | |
42 | * Since: 6.0.0 | |
43 | */ | |
44 | ||
45 | typedef struct GArrowExpressionPrivate_ { | |
46 | arrow::compute::Expression expression; | |
47 | } GArrowExpressionPrivate; | |
48 | ||
49 | G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowExpression, | |
50 | garrow_expression, | |
51 | G_TYPE_OBJECT) | |
52 | ||
53 | #define GARROW_EXPRESSION_GET_PRIVATE(object) \ | |
54 | static_cast<GArrowExpressionPrivate *>( \ | |
55 | garrow_expression_get_instance_private( \ | |
56 | GARROW_EXPRESSION(object))) | |
57 | ||
58 | static void | |
59 | garrow_expression_finalize(GObject *object) | |
60 | { | |
61 | auto priv = GARROW_EXPRESSION_GET_PRIVATE(object); | |
62 | priv->expression.~Expression(); | |
63 | G_OBJECT_CLASS(garrow_expression_parent_class)->finalize(object); | |
64 | } | |
65 | ||
66 | static void | |
67 | garrow_expression_init(GArrowExpression *object) | |
68 | { | |
69 | auto priv = GARROW_EXPRESSION_GET_PRIVATE(object); | |
70 | new(&priv->expression) arrow::compute::Expression(); | |
71 | } | |
72 | ||
73 | static void | |
74 | garrow_expression_class_init(GArrowExpressionClass *klass) | |
75 | { | |
76 | auto gobject_class = G_OBJECT_CLASS(klass); | |
77 | ||
78 | gobject_class->finalize = garrow_expression_finalize; | |
79 | } | |
80 | ||
81 | /** | |
82 | * garrow_expression_to_string: | |
83 | * @expression: A #GArrowExpression. | |
84 | * | |
85 | * Returns: The formatted expression. | |
86 | * | |
87 | * It should be freed with g_free() when no longer needed. | |
88 | * | |
89 | * Since: 6.0.0 | |
90 | */ | |
91 | gchar * | |
92 | garrow_expression_to_string(GArrowExpression *expression) | |
93 | { | |
94 | auto priv = GARROW_EXPRESSION_GET_PRIVATE(expression); | |
95 | auto string = priv->expression.ToString(); | |
96 | return g_strndup(string.data(), string.size()); | |
97 | } | |
98 | ||
99 | /** | |
100 | * garrow_expression_equal: | |
101 | * @expression: A #GArrowExpression. | |
102 | * @other_expression: A #GArrowExpression. | |
103 | * | |
104 | * Returns: %TRUE if both of them have the same content, %FALSE | |
105 | * otherwise. | |
106 | * | |
107 | * Since: 6.0.0 | |
108 | */ | |
109 | gboolean | |
110 | garrow_expression_equal(GArrowExpression *expression, | |
111 | GArrowExpression *other_expression) | |
112 | { | |
113 | auto priv = GARROW_EXPRESSION_GET_PRIVATE(expression); | |
114 | auto other_priv = GARROW_EXPRESSION_GET_PRIVATE(other_expression); | |
115 | return priv->expression.Equals(other_priv->expression); | |
116 | } | |
117 | ||
118 | ||
119 | G_DEFINE_TYPE(GArrowLiteralExpression, | |
120 | garrow_literal_expression, | |
121 | GARROW_TYPE_EXPRESSION) | |
122 | ||
123 | static void | |
124 | garrow_literal_expression_init(GArrowLiteralExpression *object) | |
125 | { | |
126 | } | |
127 | ||
128 | static void | |
129 | garrow_literal_expression_class_init(GArrowLiteralExpressionClass *klass) | |
130 | { | |
131 | } | |
132 | ||
133 | /** | |
134 | * garrow_literal_expression_new: | |
135 | * @datum: A #GArrowDatum. | |
136 | * | |
137 | * Returns: A newly created #GArrowLiteralExpression. | |
138 | * | |
139 | * Since: 6.0.0 | |
140 | */ | |
141 | GArrowLiteralExpression * | |
142 | garrow_literal_expression_new(GArrowDatum *datum) | |
143 | { | |
144 | auto arrow_datum = garrow_datum_get_raw(datum); | |
145 | auto arrow_expression = arrow::compute::literal(arrow_datum); | |
146 | return GARROW_LITERAL_EXPRESSION(garrow_expression_new_raw(arrow_expression)); | |
147 | } | |
148 | ||
149 | ||
150 | G_DEFINE_TYPE(GArrowFieldExpression, | |
151 | garrow_field_expression, | |
152 | GARROW_TYPE_EXPRESSION) | |
153 | ||
154 | static void | |
155 | garrow_field_expression_init(GArrowFieldExpression *object) | |
156 | { | |
157 | } | |
158 | ||
159 | static void | |
160 | garrow_field_expression_class_init(GArrowFieldExpressionClass *klass) | |
161 | { | |
162 | } | |
163 | ||
164 | /** | |
165 | * garrow_field_expression_new: | |
166 | * @reference: A field name or dot path. | |
167 | * @error: (nullable): Return location for a #GError or %NULL. | |
168 | * | |
169 | * Returns: A newly created #GArrowFieldExpression on sucess, %NULL on | |
170 | * error. | |
171 | * | |
172 | * Since: 6.0.0 | |
173 | */ | |
174 | GArrowFieldExpression * | |
175 | garrow_field_expression_new(const gchar *reference, | |
176 | GError **error) | |
177 | { | |
178 | if (reference && reference[0] == '.') { | |
179 | auto arrow_reference_result = arrow::FieldRef::FromDotPath(reference); | |
180 | if (!garrow::check(error, | |
181 | arrow_reference_result, | |
182 | "[field-expression][new]")) { | |
183 | return NULL; | |
184 | } | |
185 | auto arrow_expression = arrow::compute::field_ref(*arrow_reference_result); | |
186 | return GARROW_FIELD_EXPRESSION(garrow_expression_new_raw(arrow_expression)); | |
187 | } else { | |
188 | arrow::FieldRef arrow_reference(reference); | |
189 | auto arrow_expression = arrow::compute::field_ref(arrow_reference); | |
190 | return GARROW_FIELD_EXPRESSION(garrow_expression_new_raw(arrow_expression)); | |
191 | } | |
192 | } | |
193 | ||
194 | ||
195 | G_DEFINE_TYPE(GArrowCallExpression, | |
196 | garrow_call_expression, | |
197 | GARROW_TYPE_EXPRESSION) | |
198 | ||
199 | static void | |
200 | garrow_call_expression_init(GArrowCallExpression *object) | |
201 | { | |
202 | } | |
203 | ||
204 | static void | |
205 | garrow_call_expression_class_init(GArrowCallExpressionClass *klass) | |
206 | { | |
207 | } | |
208 | ||
209 | /** | |
210 | * garrow_call_expression_new: | |
211 | * @function: A name of function to be called. | |
212 | * @arguments: (element-type GArrowExpression): Arguments of this call. | |
213 | * @options: (nullable): A #GArrowFunctionOptions for the called function. | |
214 | * | |
215 | * Returns: A newly created #GArrowCallExpression. | |
216 | * | |
217 | * Since: 6.0.0 | |
218 | */ | |
219 | GArrowCallExpression * | |
220 | garrow_call_expression_new(const gchar *function, | |
221 | GList *arguments, | |
222 | GArrowFunctionOptions *options) | |
223 | { | |
224 | std::vector<arrow::compute::Expression> arrow_arguments; | |
225 | for (GList *node = arguments; node; node = node->next) { | |
226 | auto argument = GARROW_EXPRESSION(node->data); | |
227 | auto arrow_argument = garrow_expression_get_raw(argument); | |
228 | arrow_arguments.push_back(*arrow_argument); | |
229 | } | |
230 | std::shared_ptr<arrow::compute::FunctionOptions> arrow_options; | |
231 | if (options) { | |
232 | arrow_options.reset(garrow_function_options_get_raw(options)); | |
233 | } | |
234 | auto arrow_expression = arrow::compute::call(function, | |
235 | arrow_arguments, | |
236 | arrow_options); | |
237 | return GARROW_CALL_EXPRESSION(garrow_expression_new_raw(arrow_expression)); | |
238 | } | |
239 | ||
240 | ||
241 | G_END_DECLS | |
242 | ||
243 | GArrowExpression * | |
244 | garrow_expression_new_raw(const arrow::compute::Expression &arrow_expression) | |
245 | { | |
246 | GType gtype = GARROW_TYPE_EXPRESSION; | |
247 | if (arrow_expression.literal()) { | |
248 | gtype = GARROW_TYPE_LITERAL_EXPRESSION; | |
249 | } else if (arrow_expression.parameter()) { | |
250 | gtype = GARROW_TYPE_FIELD_EXPRESSION; | |
251 | } else if (arrow_expression.call()) { | |
252 | gtype = GARROW_TYPE_CALL_EXPRESSION; | |
253 | } | |
254 | auto expression = GARROW_EXPRESSION(g_object_new(gtype, NULL)); | |
255 | auto priv = GARROW_EXPRESSION_GET_PRIVATE(expression); | |
256 | priv->expression = arrow_expression; | |
257 | return expression; | |
258 | } | |
259 | ||
260 | arrow::compute::Expression * | |
261 | garrow_expression_get_raw(GArrowExpression *expression) | |
262 | { | |
263 | auto priv = GARROW_EXPRESSION_GET_PRIVATE(expression); | |
264 | return &(priv->expression); | |
265 | } |