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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #include <arrow-glib/compute.hpp>
21 #include <arrow-glib/datum.hpp>
22 #include <arrow-glib/expression.hpp>
23 #include <arrow-glib/error.hpp>
29 * @section_id: expression
31 * @include: arrow-glib/arrow-glib.h
33 * #GArrowExpression is a base class for all expression classes such
34 * as #GArrowLiteralExpression.
36 * #GArrowLiteralExpression is a class for literal value.
38 * #GArrowFieldExpression is a class for field reference.
40 * #GArrowCallExpression is a class for function call.
45 typedef struct GArrowExpressionPrivate_
{
46 arrow::compute::Expression expression
;
47 } GArrowExpressionPrivate
;
49 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(GArrowExpression
,
53 #define GARROW_EXPRESSION_GET_PRIVATE(object) \
54 static_cast<GArrowExpressionPrivate *>( \
55 garrow_expression_get_instance_private( \
56 GARROW_EXPRESSION(object)))
59 garrow_expression_finalize(GObject
*object
)
61 auto priv
= GARROW_EXPRESSION_GET_PRIVATE(object
);
62 priv
->expression
.~Expression();
63 G_OBJECT_CLASS(garrow_expression_parent_class
)->finalize(object
);
67 garrow_expression_init(GArrowExpression
*object
)
69 auto priv
= GARROW_EXPRESSION_GET_PRIVATE(object
);
70 new(&priv
->expression
) arrow::compute::Expression();
74 garrow_expression_class_init(GArrowExpressionClass
*klass
)
76 auto gobject_class
= G_OBJECT_CLASS(klass
);
78 gobject_class
->finalize
= garrow_expression_finalize
;
82 * garrow_expression_to_string:
83 * @expression: A #GArrowExpression.
85 * Returns: The formatted expression.
87 * It should be freed with g_free() when no longer needed.
92 garrow_expression_to_string(GArrowExpression
*expression
)
94 auto priv
= GARROW_EXPRESSION_GET_PRIVATE(expression
);
95 auto string
= priv
->expression
.ToString();
96 return g_strndup(string
.data(), string
.size());
100 * garrow_expression_equal:
101 * @expression: A #GArrowExpression.
102 * @other_expression: A #GArrowExpression.
104 * Returns: %TRUE if both of them have the same content, %FALSE
110 garrow_expression_equal(GArrowExpression
*expression
,
111 GArrowExpression
*other_expression
)
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
);
119 G_DEFINE_TYPE(GArrowLiteralExpression
,
120 garrow_literal_expression
,
121 GARROW_TYPE_EXPRESSION
)
124 garrow_literal_expression_init(GArrowLiteralExpression
*object
)
129 garrow_literal_expression_class_init(GArrowLiteralExpressionClass
*klass
)
134 * garrow_literal_expression_new:
135 * @datum: A #GArrowDatum.
137 * Returns: A newly created #GArrowLiteralExpression.
141 GArrowLiteralExpression
*
142 garrow_literal_expression_new(GArrowDatum
*datum
)
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
));
150 G_DEFINE_TYPE(GArrowFieldExpression
,
151 garrow_field_expression
,
152 GARROW_TYPE_EXPRESSION
)
155 garrow_field_expression_init(GArrowFieldExpression
*object
)
160 garrow_field_expression_class_init(GArrowFieldExpressionClass
*klass
)
165 * garrow_field_expression_new:
166 * @reference: A field name or dot path.
167 * @error: (nullable): Return location for a #GError or %NULL.
169 * Returns: A newly created #GArrowFieldExpression on sucess, %NULL on
174 GArrowFieldExpression
*
175 garrow_field_expression_new(const gchar
*reference
,
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]")) {
185 auto arrow_expression
= arrow::compute::field_ref(*arrow_reference_result
);
186 return GARROW_FIELD_EXPRESSION(garrow_expression_new_raw(arrow_expression
));
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
));
195 G_DEFINE_TYPE(GArrowCallExpression
,
196 garrow_call_expression
,
197 GARROW_TYPE_EXPRESSION
)
200 garrow_call_expression_init(GArrowCallExpression
*object
)
205 garrow_call_expression_class_init(GArrowCallExpressionClass
*klass
)
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.
215 * Returns: A newly created #GArrowCallExpression.
219 GArrowCallExpression
*
220 garrow_call_expression_new(const gchar
*function
,
222 GArrowFunctionOptions
*options
)
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
);
230 std::shared_ptr
<arrow::compute::FunctionOptions
> arrow_options
;
232 arrow_options
.reset(garrow_function_options_get_raw(options
));
234 auto arrow_expression
= arrow::compute::call(function
,
237 return GARROW_CALL_EXPRESSION(garrow_expression_new_raw(arrow_expression
));
244 garrow_expression_new_raw(const arrow::compute::Expression
&arrow_expression
)
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
;
254 auto expression
= GARROW_EXPRESSION(g_object_new(gtype
, NULL
));
255 auto priv
= GARROW_EXPRESSION_GET_PRIVATE(expression
);
256 priv
->expression
= arrow_expression
;
260 arrow::compute::Expression
*
261 garrow_expression_get_raw(GArrowExpression
*expression
)
263 auto priv
= GARROW_EXPRESSION_GET_PRIVATE(expression
);
264 return &(priv
->expression
);