]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/cpp/src/gandiva/jni/expression_registry_helper.cc
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / gandiva / jni / expression_registry_helper.cc
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#include "jni/org_apache_arrow_gandiva_evaluator_ExpressionRegistryJniHelper.h"
19
20#include <memory>
21
22#include "Types.pb.h"
23#include "arrow/util/logging.h"
24#include "gandiva/arrow.h"
25#include "gandiva/expression_registry.h"
26
27using gandiva::DataTypePtr;
28using gandiva::ExpressionRegistry;
29
30types::TimeUnit MapTimeUnit(arrow::TimeUnit::type& unit) {
31 switch (unit) {
32 case arrow::TimeUnit::MILLI:
33 return types::TimeUnit::MILLISEC;
34 case arrow::TimeUnit::SECOND:
35 return types::TimeUnit::SEC;
36 case arrow::TimeUnit::MICRO:
37 return types::TimeUnit::MICROSEC;
38 case arrow::TimeUnit::NANO:
39 return types::TimeUnit::NANOSEC;
40 }
41 // satisfy gcc. should be unreachable.
42 return types::TimeUnit::SEC;
43}
44
45void ArrowToProtobuf(DataTypePtr type, types::ExtGandivaType* gandiva_data_type) {
46 switch (type->id()) {
47 case arrow::Type::BOOL:
48 gandiva_data_type->set_type(types::GandivaType::BOOL);
49 break;
50 case arrow::Type::UINT8:
51 gandiva_data_type->set_type(types::GandivaType::UINT8);
52 break;
53 case arrow::Type::INT8:
54 gandiva_data_type->set_type(types::GandivaType::INT8);
55 break;
56 case arrow::Type::UINT16:
57 gandiva_data_type->set_type(types::GandivaType::UINT16);
58 break;
59 case arrow::Type::INT16:
60 gandiva_data_type->set_type(types::GandivaType::INT16);
61 break;
62 case arrow::Type::UINT32:
63 gandiva_data_type->set_type(types::GandivaType::UINT32);
64 break;
65 case arrow::Type::INT32:
66 gandiva_data_type->set_type(types::GandivaType::INT32);
67 break;
68 case arrow::Type::UINT64:
69 gandiva_data_type->set_type(types::GandivaType::UINT64);
70 break;
71 case arrow::Type::INT64:
72 gandiva_data_type->set_type(types::GandivaType::INT64);
73 break;
74 case arrow::Type::HALF_FLOAT:
75 gandiva_data_type->set_type(types::GandivaType::HALF_FLOAT);
76 break;
77 case arrow::Type::FLOAT:
78 gandiva_data_type->set_type(types::GandivaType::FLOAT);
79 break;
80 case arrow::Type::DOUBLE:
81 gandiva_data_type->set_type(types::GandivaType::DOUBLE);
82 break;
83 case arrow::Type::STRING:
84 gandiva_data_type->set_type(types::GandivaType::UTF8);
85 break;
86 case arrow::Type::BINARY:
87 gandiva_data_type->set_type(types::GandivaType::BINARY);
88 break;
89 case arrow::Type::DATE32:
90 gandiva_data_type->set_type(types::GandivaType::DATE32);
91 break;
92 case arrow::Type::DATE64:
93 gandiva_data_type->set_type(types::GandivaType::DATE64);
94 break;
95 case arrow::Type::TIMESTAMP: {
96 gandiva_data_type->set_type(types::GandivaType::TIMESTAMP);
97 std::shared_ptr<arrow::TimestampType> cast_time_stamp_type =
98 std::dynamic_pointer_cast<arrow::TimestampType>(type);
99 arrow::TimeUnit::type unit = cast_time_stamp_type->unit();
100 types::TimeUnit time_unit = MapTimeUnit(unit);
101 gandiva_data_type->set_timeunit(time_unit);
102 break;
103 }
104 case arrow::Type::TIME32: {
105 gandiva_data_type->set_type(types::GandivaType::TIME32);
106 std::shared_ptr<arrow::Time32Type> cast_time_32_type =
107 std::dynamic_pointer_cast<arrow::Time32Type>(type);
108 arrow::TimeUnit::type unit = cast_time_32_type->unit();
109 types::TimeUnit time_unit = MapTimeUnit(unit);
110 gandiva_data_type->set_timeunit(time_unit);
111 break;
112 }
113 case arrow::Type::TIME64: {
114 gandiva_data_type->set_type(types::GandivaType::TIME32);
115 std::shared_ptr<arrow::Time64Type> cast_time_64_type =
116 std::dynamic_pointer_cast<arrow::Time64Type>(type);
117 arrow::TimeUnit::type unit = cast_time_64_type->unit();
118 types::TimeUnit time_unit = MapTimeUnit(unit);
119 gandiva_data_type->set_timeunit(time_unit);
120 break;
121 }
122 case arrow::Type::NA:
123 gandiva_data_type->set_type(types::GandivaType::NONE);
124 break;
125 case arrow::Type::DECIMAL: {
126 gandiva_data_type->set_type(types::GandivaType::DECIMAL);
127 gandiva_data_type->set_precision(0);
128 gandiva_data_type->set_scale(0);
129 break;
130 }
131 case arrow::Type::INTERVAL_MONTHS:
132 gandiva_data_type->set_type(types::GandivaType::INTERVAL);
133 gandiva_data_type->set_intervaltype(types::IntervalType::YEAR_MONTH);
134 break;
135 case arrow::Type::INTERVAL_DAY_TIME:
136 gandiva_data_type->set_type(types::GandivaType::INTERVAL);
137 gandiva_data_type->set_intervaltype(types::IntervalType::DAY_TIME);
138 break;
139 default:
140 // un-supported types. test ensures that
141 // when one of these are added build breaks.
142 DCHECK(false);
143 }
144}
145
146JNIEXPORT jbyteArray JNICALL
147Java_org_apache_arrow_gandiva_evaluator_ExpressionRegistryJniHelper_getGandivaSupportedDataTypes( // NOLINT
148 JNIEnv* env, jobject types_helper) {
149 types::GandivaDataTypes gandiva_data_types;
150 auto supported_types = ExpressionRegistry::supported_types();
151 for (auto const& type : supported_types) {
152 types::ExtGandivaType* gandiva_data_type = gandiva_data_types.add_datatype();
153 ArrowToProtobuf(type, gandiva_data_type);
154 }
155 auto size = gandiva_data_types.ByteSizeLong();
156 std::unique_ptr<jbyte[]> buffer{new jbyte[size]};
157 gandiva_data_types.SerializeToArray(reinterpret_cast<void*>(buffer.get()), size);
158 jbyteArray ret = env->NewByteArray(size);
159 env->SetByteArrayRegion(ret, 0, size, buffer.get());
160 return ret;
161}
162
163/*
164 * Class: org_apache_arrow_gandiva_types_ExpressionRegistryJniHelper
165 * Method: getGandivaSupportedFunctions
166 * Signature: ()[B
167 */
168JNIEXPORT jbyteArray JNICALL
169Java_org_apache_arrow_gandiva_evaluator_ExpressionRegistryJniHelper_getGandivaSupportedFunctions( // NOLINT
170 JNIEnv* env, jobject types_helper) {
171 ExpressionRegistry expr_registry;
172 types::GandivaFunctions gandiva_functions;
173 for (auto function = expr_registry.function_signature_begin();
174 function != expr_registry.function_signature_end(); function++) {
175 types::FunctionSignature* function_signature = gandiva_functions.add_function();
176 function_signature->set_name((*function).base_name());
177 types::ExtGandivaType* return_type = function_signature->mutable_returntype();
178 ArrowToProtobuf((*function).ret_type(), return_type);
179 for (auto& param_type : (*function).param_types()) {
180 types::ExtGandivaType* proto_param_type = function_signature->add_paramtypes();
181 ArrowToProtobuf(param_type, proto_param_type);
182 }
183 }
184 auto size = gandiva_functions.ByteSizeLong();
185 std::unique_ptr<jbyte[]> buffer{new jbyte[size]};
186 gandiva_functions.SerializeToArray(reinterpret_cast<void*>(buffer.get()), size);
187 jbyteArray ret = env->NewByteArray(size);
188 env->SetByteArrayRegion(ret, 0, size, buffer.get());
189 return ret;
190}