17 package com.mapd.parser.server;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.StringReader;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
38 LoggerFactory.getLogger(ExtensionFunctionSignatureParser.class);
40 private static String
OS = System.getProperty(
"os.name").toLowerCase();
42 return (
OS.indexOf(
"win") >= 0);
45 static Map<String, ExtensionFunction>
parse(
final String file_path)
throws IOException {
46 File file =
new File(file_path);
47 FileReader fileReader =
new FileReader(file);
48 BufferedReader bufferedReader =
new BufferedReader(fileReader);
50 Pattern s = Pattern.compile(
"\\| ([\\` ]|used)+ ([\\w]+) '([\\w<>]+) \\((.*)\\)'");
51 Map<String, ExtensionFunction> sigs =
new HashMap<String, ExtensionFunction>();
52 while ((line = bufferedReader.readLine()) != null) {
53 Matcher m = s.matcher(
line);
55 final String
name = m.group(2);
56 final String ret = m.group(3);
57 final String cs_param_list = m.group(4);
64 static Map<String, ExtensionFunction>
parseUdfAst(
final String file_path)
66 File file =
new File(file_path);
67 FileReader fileReader =
new FileReader(file);
68 BufferedReader bufferedReader =
new BufferedReader(fileReader);
70 Pattern s = Pattern.compile(
"([<>:\\w]+) ([:\\w]+)(?:\\(\\))?\\((.*)\\)");
71 Map<String, ExtensionFunction> sigs =
new HashMap<String, ExtensionFunction>();
72 while ((line = bufferedReader.readLine()) != null) {
73 Matcher m = s.matcher(
line);
75 final String
name = m.group(2);
76 final String ret = m.group(1);
77 final String cs_param_list = m.group(3);
78 if (cs_param_list.isEmpty()) {
93 final String udf_string,
final boolean is_row_func)
throws IOException {
94 StringReader stringReader =
new StringReader(udf_string);
95 BufferedReader bufferedReader =
new BufferedReader(stringReader);
97 Pattern r = Pattern.compile(
"([\\w]+)\\s+'([\\w]+)\\s*\\((.*)\\)'");
98 Map<String, ExtensionFunction> sigs =
new HashMap<String, ExtensionFunction>();
99 while ((line = bufferedReader.readLine()) != null) {
100 Matcher m = r.matcher(
line);
102 final String
name = m.group(1);
103 final String ret = m.group(2);
104 final String cs_param_list = m.group(3);
111 List<String> json_sigs =
new ArrayList<String>();
114 if (sig.getValue().isRowUdf()) {
115 json_sigs.add(sig.getValue().toJson(sig.getKey()));
119 return "[" +
join(json_sigs,
",") +
"]";
123 final String ret,
final String cs_param_list,
final boolean has_variable_name) {
124 return toSignature(ret, cs_param_list, has_variable_name,
true);
128 final String cs_param_list,
129 final boolean has_variable_name,
130 final boolean is_row_func) {
131 String[]
params = cs_param_list.split(
",");
132 List<ExtensionFunction.ExtArgumentType>
args =
133 new ArrayList<ExtensionFunction.ExtArgumentType>();
135 List<Map<String, String>> annotations =
new ArrayList<Map<String, String>>();
136 for (
final String param : params) {
137 ExtensionFunction.ExtArgumentType arg_type;
138 if (param.contains(
"RowFunctionManager")) {
139 uses_manager =
"true";
142 if (has_variable_name) {
143 String[] full_param = param.trim().
split(
"\\s+");
144 if (full_param.length > 0) {
145 if (full_param[0].trim().compareTo(
"const") == 0) {
146 assert full_param.length > 1;
157 annotations.add(Collections.EMPTY_MAP);
163 annotations.add(Collections.singletonMap(
"uses_manager",
uses_manager));
168 final String const_prefix =
"const ";
169 final String std_namespace_prefix =
"std::";
171 if (type_name.startsWith(const_prefix)) {
174 if (type_name.startsWith(std_namespace_prefix)) {
175 return deserializeType(type_name.substring(std_namespace_prefix.length()));
178 if (type_name.equals(
"bool") || type_name.equals(
"_Bool")) {
179 return ExtensionFunction.ExtArgumentType.Bool;
181 if (type_name.equals(
"int8_t") || type_name.equals(
"char")
182 || type_name.equals(
"int8")) {
183 return ExtensionFunction.ExtArgumentType.Int8;
185 if (type_name.equals(
"int16_t") || type_name.equals(
"short")
186 || type_name.equals(
"int16")) {
187 return ExtensionFunction.ExtArgumentType.Int16;
189 if (type_name.equals(
"int32_t") || type_name.equals(
"int")
190 || type_name.equals(
"int32") || (
isWindows() && type_name.equals(
"long"))) {
191 return ExtensionFunction.ExtArgumentType.Int32;
193 if (type_name.equals(
"int64_t") || type_name.equals(
"size_t")
194 || type_name.equals(
"int64")) {
195 return ExtensionFunction.ExtArgumentType.Int64;
197 if ((!
isWindows() && type_name.equals(
"long"))
198 || (
isWindows() && type_name.equals(
"long long"))) {
199 return ExtensionFunction.ExtArgumentType.Int64;
201 if (type_name.equals(
"float") || type_name.equals(
"float32")) {
202 return ExtensionFunction.ExtArgumentType.Float;
204 if (type_name.equals(
"double") || type_name.equals(
"float64")) {
207 if (type_name.isEmpty() || type_name.equals(
"void")) {
208 return ExtensionFunction.ExtArgumentType.Void;
210 if (type_name.endsWith(
" *")) {
213 if (type_name.endsWith(
"*")) {
216 if (type_name.endsWith(
"&")) {
217 return deserializeType(type_name.substring(0, type_name.length() - 1).trim());
219 if (type_name.equals(
"Array<bool>")) {
220 return ExtensionFunction.ExtArgumentType.ArrayBool;
222 if (type_name.equals(
"Array<int8_t>") || type_name.equals(
"Array<char>")) {
223 return ExtensionFunction.ExtArgumentType.ArrayInt8;
225 if (type_name.equals(
"Array<int16_t>") || type_name.equals(
"Array<short>")) {
226 return ExtensionFunction.ExtArgumentType.ArrayInt16;
228 if (type_name.equals(
"Array<int32_t>") || type_name.equals(
"Array<int>")) {
229 return ExtensionFunction.ExtArgumentType.ArrayInt32;
231 if (type_name.equals(
"Array<int64_t>") || type_name.equals(
"Array<size_t>")
232 || type_name.equals(
"Array<long>")) {
233 return ExtensionFunction.ExtArgumentType.ArrayInt64;
235 if (type_name.equals(
"Array<float>")) {
236 return ExtensionFunction.ExtArgumentType.ArrayFloat;
238 if (type_name.equals(
"Array<double>")) {
239 return ExtensionFunction.ExtArgumentType.ArrayDouble;
241 if (type_name.equals(
"Array<bool>")) {
242 return ExtensionFunction.ExtArgumentType.ArrayBool;
244 if (type_name.equals(
"Array<TextEncodingDict>")) {
245 return ExtensionFunction.ExtArgumentType.ArrayTextEncodingDict;
247 if (type_name.equals(
"Array<TextEncodingNone>")) {
248 return ExtensionFunction.ExtArgumentType.ArrayTextEncodingNone;
250 if (type_name.equals(
"TextEncodingDict")) {
251 return ExtensionFunction.ExtArgumentType.TextEncodingDict;
253 if (type_name.equals(
"RowFunctionManager")) {
257 return ExtensionFunction.ExtArgumentType.Void;
259 if (type_name.equals(
"Timestamp")) {
260 return ExtensionFunction.ExtArgumentType.Timestamp;
262 if (type_name.equals(
"TextEncodingNone")) {
263 return ExtensionFunction.ExtArgumentType.TextEncodingNone;
265 if (type_name.equals(
"Column<int8_t>") || type_name.equals(
"Column<char>")) {
266 return ExtensionFunction.ExtArgumentType.ColumnInt8;
268 if (type_name.equals(
"Column<int16_t>") || type_name.equals(
"Column<short>")) {
269 return ExtensionFunction.ExtArgumentType.ColumnInt16;
271 if (type_name.equals(
"Column<int32_t>") || type_name.equals(
"Column<int>")) {
272 return ExtensionFunction.ExtArgumentType.ColumnInt32;
274 if (type_name.equals(
"Column<int64_t>") || type_name.equals(
"Column<size_t>")
275 || type_name.equals(
"Column<long>")) {
276 return ExtensionFunction.ExtArgumentType.ColumnInt64;
278 if (type_name.equals(
"Column<float>")) {
279 return ExtensionFunction.ExtArgumentType.ColumnFloat;
281 if (type_name.equals(
"Column<double>")) {
282 return ExtensionFunction.ExtArgumentType.ColumnDouble;
284 if (type_name.equals(
"Column<TextEncodingDict>")) {
285 return ExtensionFunction.ExtArgumentType.ColumnTextEncodingDict;
287 if (type_name.equals(
"Column<Timestamp>")) {
288 return ExtensionFunction.ExtArgumentType.ColumnTimestamp;
290 if (type_name.equals(
"Cursor")) {
291 return ExtensionFunction.ExtArgumentType.Cursor;
293 if (type_name.equals(
"GeoPoint")) {
296 if (type_name.equals(
"GeoMultiPoint")) {
299 if (type_name.equals(
"GeoLineString")) {
302 if (type_name.equals(
"GeoMultiLineString")) {
305 if (type_name.equals(
"GeoPolygon")) {
308 if (type_name.equals(
"GeoMultiPolygon")) {
311 if (type_name.equals(
"ColumnList<int8_t>") || type_name.equals(
"ColumnList<char>")) {
312 return ExtensionFunction.ExtArgumentType.ColumnListInt8;
314 if (type_name.equals(
"ColumnList<int16_t>")
315 || type_name.equals(
"ColumnList<short>")) {
316 return ExtensionFunction.ExtArgumentType.ColumnListInt16;
318 if (type_name.equals(
"ColumnList<int32_t>") || type_name.equals(
"ColumnList<int>")) {
319 return ExtensionFunction.ExtArgumentType.ColumnListInt32;
321 if (type_name.equals(
"ColumnList<int64_t>") || type_name.equals(
"ColumnList<size_t>")
322 || type_name.equals(
"ColumnList<long>")) {
323 return ExtensionFunction.ExtArgumentType.ColumnListInt64;
325 if (type_name.equals(
"ColumnList<float>")) {
326 return ExtensionFunction.ExtArgumentType.ColumnListFloat;
328 if (type_name.equals(
"ColumnList<double>")) {
329 return ExtensionFunction.ExtArgumentType.ColumnListDouble;
331 if (type_name.equals(
"ColumnList<TextEncodingDict>")) {
332 return ExtensionFunction.ExtArgumentType.ColumnListTextEncodingDict;
335 "ExtensionfunctionSignatureParser::deserializeType: unknown type_name=`"
339 return ExtensionFunction.ExtArgumentType.Void;
344 switch (targetType) {
346 return ExtensionFunction.ExtArgumentType.PBool;
348 return ExtensionFunction.ExtArgumentType.PInt8;
350 return ExtensionFunction.ExtArgumentType.PInt16;
352 return ExtensionFunction.ExtArgumentType.PInt32;
354 return ExtensionFunction.ExtArgumentType.PInt64;
356 return ExtensionFunction.ExtArgumentType.PFloat;
358 return ExtensionFunction.ExtArgumentType.PDouble;
365 static String
join(
final List<String> strs,
final String sep) {
366 StringBuilder sb =
new StringBuilder();
367 if (strs.isEmpty()) {
370 sb.append(strs.get(0));
371 for (
int i = 1; i < strs.size(); ++i) {
372 sb.append(sep).
append(strs.get(i));
374 return sb.toString();
static Map< String, ExtensionFunction > parseFromString(final String udf_string)
size_t append(FILE *f, const size_t size, const int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
struct GeoLineStringStruct GeoLineString
struct GeoPointStruct GeoPoint
static ExtensionFunction.ExtArgumentType pointerType(final ExtensionFunction.ExtArgumentType targetType)
static final Logger HEAVYDBLOGGER
static boolean isWindows()
static Map< String, ExtensionFunction > parseFromString(final String udf_string, final boolean is_row_func)
static ExtensionFunction toSignature(final String ret, final String cs_param_list, final boolean has_variable_name, final boolean is_row_func)
static Map< String, ExtensionFunction > parse(final String file_path)
struct GeoMultiPointStruct GeoMultiPoint
struct GeoMultiLineStringStruct GeoMultiLineString
static String join(final List< String > strs, final String sep)
static ExtensionFunction toSignature(final String ret, final String cs_param_list, final boolean has_variable_name)
static String signaturesToJson(final Map< String, ExtensionFunction > sigs)
static ExtensionFunction.ExtArgumentType deserializeType(final String type_name)
static Map< String, ExtensionFunction > parseUdfAst(final String file_path)
struct GeoMultiPolygonStruct GeoMultiPolygon
struct GeoPolygonStruct GeoPolygon
constexpr auto type_name() noexcept