summaryrefslogtreecommitdiffstats
path: root/src/arrow/c_glib/gandiva-glib/function-signature.cpp
blob: c344f3a923982dc500f35296b2c902ef75b328df (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * 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.
 */

#include <arrow-glib/basic-data-type.hpp>

#include <gandiva-glib/function-signature.hpp>

G_BEGIN_DECLS

/**
 * SECTION: function-signature
 * @short_description: FunctionSignature class
 * @title: FunctionSignature class
 * @include: gandiva-glib/gandiva-glib.h
 *
 * Since: 0.14.0
 */

typedef struct GGandivaFunctionSignaturePrivate_ {
  gandiva::FunctionSignature function_signature;
} GGandivaFunctionSignaturePrivate;

enum {
  PROP_FUNCTION_SIGNATURE = 1
};

G_DEFINE_TYPE_WITH_PRIVATE(GGandivaFunctionSignature,
                           ggandiva_function_signature,
                           G_TYPE_OBJECT)

#define GGANDIVA_FUNCTION_SIGNATURE_GET_PRIVATE(obj)      \
    static_cast<GGandivaFunctionSignaturePrivate *>(      \
        ggandiva_function_signature_get_instance_private( \
          GGANDIVA_FUNCTION_SIGNATURE(obj)))

static void
ggandiva_function_signature_set_property(GObject *object,
                                         guint prop_id,
                                         const GValue *value,
                                         GParamSpec *pspec)
{
  auto priv = GGANDIVA_FUNCTION_SIGNATURE_GET_PRIVATE(object);
  switch (prop_id) {
  case PROP_FUNCTION_SIGNATURE:
    priv->function_signature =
      *static_cast<const gandiva::FunctionSignature *>(g_value_get_pointer(value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
}

static void
ggandiva_function_signature_init(GGandivaFunctionSignature *object)
{
}

static void
ggandiva_function_signature_class_init(GGandivaFunctionSignatureClass *klass)
{
  auto gobject_class = G_OBJECT_CLASS(klass);

  gobject_class->set_property = ggandiva_function_signature_set_property;

  GParamSpec *spec;
  spec = g_param_spec_pointer("function-signature",
                              "FunctionSignature",
                              "The raw gandiva::FunctionSignature *",
                              static_cast<GParamFlags>(G_PARAM_WRITABLE |
                                                       G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property(gobject_class, PROP_FUNCTION_SIGNATURE, spec);
}

/**
 * ggandiva_function_signature_new:
 * @base_name: A base name of a function.
 * @parameter_types: (element-type GArrowDataType)
 *   A list of parameter data types.
 * @return_type: A return data type.
 *
 * Returns:
 *   A #GGandivaFunctionSignature.
 *
 * Since: 0.14.0
 */
GGandivaFunctionSignature *
ggandiva_function_signature_new(const gchar *base_name,
                                GList *parameter_types,
                                GArrowDataType *return_type)
{
  gandiva::DataTypeVector arrow_parameter_types;
  for (auto node = parameter_types; node; node = g_list_next(node)) {
    auto data_type = GARROW_DATA_TYPE(node->data);
    auto arrow_data_type = garrow_data_type_get_raw(data_type);
    arrow_parameter_types.push_back(arrow_data_type);
  }

  auto arrow_return_type = garrow_data_type_get_raw(return_type);

  gandiva::FunctionSignature gandiva_function_signature(base_name,
                                                        arrow_parameter_types,
                                                        arrow_return_type);
  return ggandiva_function_signature_new_raw(&gandiva_function_signature);
}

/**
 * ggandiva_function_signature_equal:
 * @function_signature: A #GGandivaFunctionSignature.
 * @other_function_signature: A #GGandivaFunctionSignature to be compared.
 *
 * Returns: %TRUE if both of them have the same data, %FALSE otherwise.
 *
 * Since: 0.14.0
 */
gboolean
ggandiva_function_signature_equal(GGandivaFunctionSignature *function_signature,
                                  GGandivaFunctionSignature *other_function_signature)
{
  auto gandiva_function_signature =
    ggandiva_function_signature_get_raw(function_signature);
  auto gandiva_other_function_signature =
    ggandiva_function_signature_get_raw(other_function_signature);

  return (*gandiva_function_signature) == (*gandiva_other_function_signature);
}

/**
 * ggandiva_function_signature_to_string:
 * @function_signature: A #GGandivaFunctionSignature
 *
 * Returns: (transfer full): The string representation of the function signature.
 *
 *   It should be freed with g_free() when no longer needed.
 *
 * Since: 0.14.0
 */
gchar *
ggandiva_function_signature_to_string(GGandivaFunctionSignature *function_signature)
{
  auto gandiva_function_signature =
    ggandiva_function_signature_get_raw(function_signature);
  return g_strdup(gandiva_function_signature->ToString().c_str());
}

/**
 * ggandiva_function_signature_get_return_type:
 * @function_signature: A #GGandivaFunctionSignature
 *
 * Returns: (transfer full):
 *   A #GArrowDataType of the return value of the function signature.
 *
 * Since: 0.14.0
 */
GArrowDataType *
ggandiva_function_signature_get_return_type(GGandivaFunctionSignature *function_signature)
{
  auto gandiva_function_signature =
    ggandiva_function_signature_get_raw(function_signature);
  auto arrow_data_type = gandiva_function_signature->ret_type();
  auto data_type = garrow_data_type_new_raw(&arrow_data_type);
  return data_type;
}

/**
 * ggandiva_function_signature_get_base_name:
 * @function_signature: A #GGandivaFunctionSignature
 *
 * Returns: (transfer full): A base name of the function signature.
 *
 *   It should be freed with g_free() when no longer needed.
 *
 * Since: 0.14.0
 */
gchar *
ggandiva_function_signature_get_base_name(GGandivaFunctionSignature *function_signature)
{
  auto gandiva_function_signature =
    ggandiva_function_signature_get_raw(function_signature);
  return g_strdup(gandiva_function_signature->base_name().c_str());
}

/**
 * ggandiva_function_signature_get_param_types:
 * @function_signature: A #GGandivaFunctionSignature
 *
 * Returns: (transfer full) (element-type GArrowDataType):
 *   A list of parameter data types of the function signature.
 *
 * Since: 0.14.0
 */
GList *
ggandiva_function_signature_get_param_types(GGandivaFunctionSignature *function_signature)
{
  auto gandiva_function_signature =
    ggandiva_function_signature_get_raw(function_signature);

  GList *param_type_list = nullptr;
  auto arrow_param_types = gandiva_function_signature->param_types();
  for (auto &arrow_param_type : arrow_param_types) {
    auto data_type = garrow_data_type_new_raw(&arrow_param_type);
    param_type_list = g_list_prepend(param_type_list, data_type);
  }
  param_type_list = g_list_reverse(param_type_list);

  return param_type_list;
}

G_END_DECLS

GGandivaFunctionSignature *
ggandiva_function_signature_new_raw(const gandiva::FunctionSignature *gandiva_function_signature)
{
  auto function_signature =
    GGANDIVA_FUNCTION_SIGNATURE(g_object_new(GGANDIVA_TYPE_FUNCTION_SIGNATURE,
                                             "function-signature",
                                             gandiva_function_signature,
                                             NULL));
  return function_signature;
}

const gandiva::FunctionSignature *
ggandiva_function_signature_get_raw(GGandivaFunctionSignature *function_signature)
{
  auto priv = GGANDIVA_FUNCTION_SIGNATURE_GET_PRIVATE(function_signature);
  return &priv->function_signature;
}