]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/c_glib/arrow-glib/expression.cpp
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / c_glib / arrow-glib / expression.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 <arrow-glib/compute.hpp>
21#include <arrow-glib/datum.hpp>
22#include <arrow-glib/expression.hpp>
23#include <arrow-glib/error.hpp>
24
25G_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
45typedef struct GArrowExpressionPrivate_ {
46 arrow::compute::Expression expression;
47} GArrowExpressionPrivate;
48
49G_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
58static void
59garrow_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
66static void
67garrow_expression_init(GArrowExpression *object)
68{
69 auto priv = GARROW_EXPRESSION_GET_PRIVATE(object);
70 new(&priv->expression) arrow::compute::Expression();
71}
72
73static void
74garrow_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 */
91gchar *
92garrow_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 */
109gboolean
110garrow_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
119G_DEFINE_TYPE(GArrowLiteralExpression,
120 garrow_literal_expression,
121 GARROW_TYPE_EXPRESSION)
122
123static void
124garrow_literal_expression_init(GArrowLiteralExpression *object)
125{
126}
127
128static void
129garrow_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 */
141GArrowLiteralExpression *
142garrow_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
150G_DEFINE_TYPE(GArrowFieldExpression,
151 garrow_field_expression,
152 GARROW_TYPE_EXPRESSION)
153
154static void
155garrow_field_expression_init(GArrowFieldExpression *object)
156{
157}
158
159static void
160garrow_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 */
174GArrowFieldExpression *
175garrow_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
195G_DEFINE_TYPE(GArrowCallExpression,
196 garrow_call_expression,
197 GARROW_TYPE_EXPRESSION)
198
199static void
200garrow_call_expression_init(GArrowCallExpression *object)
201{
202}
203
204static void
205garrow_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 */
219GArrowCallExpression *
220garrow_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
241G_END_DECLS
242
243GArrowExpression *
244garrow_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
260arrow::compute::Expression *
261garrow_expression_get_raw(GArrowExpression *expression)
262{
263 auto priv = GARROW_EXPRESSION_GET_PRIVATE(expression);
264 return &(priv->expression);
265}