OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExtensionFunctionSignatureParser.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.mapd.parser.server;
17 
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
37  final static Logger MAPDLOGGER =
38  LoggerFactory.getLogger(ExtensionFunctionSignatureParser.class);
39 
40  static Map<String, ExtensionFunction> parse(final String file_path) throws IOException {
41  File file = new File(file_path);
42  FileReader fileReader = new FileReader(file);
43  BufferedReader bufferedReader = new BufferedReader(fileReader);
44  String line;
45  Pattern r = Pattern.compile("([\\w]+) '([\\w]+) \\((.*)\\)'");
46  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
47  while ((line = bufferedReader.readLine()) != null) {
48  Matcher m = r.matcher(line);
49  if (m.find()) {
50  final String name = m.group(1);
51  final String ret = m.group(2);
52  final String cs_param_list = m.group(3);
53  sigs.put(name, toSignature(ret, cs_param_list));
54  }
55  }
56  return sigs;
57  }
58 
59  static Map<String, ExtensionFunction> parseFromString(final String udf_string)
60  throws IOException {
61  return parseFromString(udf_string, true);
62  }
63 
64  static Map<String, ExtensionFunction> parseFromString(
65  final String udf_string, final boolean is_row_func) throws IOException {
66  StringReader stringReader = new StringReader(udf_string);
67  BufferedReader bufferedReader = new BufferedReader(stringReader);
68  String line;
69  Pattern r = Pattern.compile("([\\w]+)\\s+'([\\w]+)\\s*\\((.*)\\)'");
70  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
71  while ((line = bufferedReader.readLine()) != null) {
72  Matcher m = r.matcher(line);
73  if (m.find()) {
74  final String name = m.group(1);
75  final String ret = m.group(2);
76  final String cs_param_list = m.group(3);
77  sigs.put(name, toSignature(ret, cs_param_list, is_row_func));
78  }
79  }
80  return sigs;
81  }
82  static String signaturesToJson(final Map<String, ExtensionFunction> sigs) {
83  List<String> json_sigs = new ArrayList<String>();
84  if (sigs != null) {
85  for (Map.Entry<String, ExtensionFunction> sig : sigs.entrySet()) {
86  json_sigs.add(sig.getValue().toJson(sig.getKey()));
87  }
88  }
89  return "[" + join(json_sigs, ",") + "]";
90  }
91 
93  final String ret, final String cs_param_list) {
94  return toSignature(ret, cs_param_list, true);
95  }
96 
98  final String ret, final String cs_param_list, final boolean is_row_func) {
99  String[] params = cs_param_list.split(",");
100  List<ExtensionFunction.ExtArgumentType> args =
101  new ArrayList<ExtensionFunction.ExtArgumentType>();
102  for (final String param : params) {
103  final ExtensionFunction.ExtArgumentType arg_type = deserializeType(param.trim());
104  if (arg_type != ExtensionFunction.ExtArgumentType.Void) {
105  args.add(arg_type);
106  }
107  }
108  return new ExtensionFunction(args, deserializeType(ret), is_row_func);
109  }
110  private static ExtensionFunction.ExtArgumentType deserializeType(
111  final String type_name) {
112  final String const_prefix = "const ";
113  final String std_namespace_prefix = "std::";
114 
115  if (type_name.startsWith(const_prefix)) {
116  return deserializeType(type_name.substring(const_prefix.length()));
117  }
118  if (type_name.startsWith(std_namespace_prefix)) {
119  return deserializeType(type_name.substring(std_namespace_prefix.length()));
120  }
121 
122  if (type_name.equals("bool") || type_name.equals("_Bool")) {
123  return ExtensionFunction.ExtArgumentType.Int8; // bool is mapped to int8
124  }
125  if (type_name.equals("int8_t") || type_name.equals("char")
126  || type_name.equals("int8")) {
127  return ExtensionFunction.ExtArgumentType.Int8;
128  }
129  if (type_name.equals("int16_t") || type_name.equals("short")
130  || type_name.equals("int16")) {
131  return ExtensionFunction.ExtArgumentType.Int16;
132  }
133  if (type_name.equals("int32_t") || type_name.equals("int")
134  || type_name.equals("int32")) {
135  return ExtensionFunction.ExtArgumentType.Int32;
136  }
137  if (type_name.equals("int64_t") || type_name.equals("size_t")
138  || type_name.equals("int64") || type_name.equals("long")) {
139  return ExtensionFunction.ExtArgumentType.Int64;
140  }
141  if (type_name.equals("float") || type_name.equals("float32")) {
142  return ExtensionFunction.ExtArgumentType.Float;
143  }
144  if (type_name.equals("double") || type_name.equals("float64")) {
145  return ExtensionFunction.ExtArgumentType.Double;
146  }
147  if (type_name.isEmpty() || type_name.equals("void")) {
148  return ExtensionFunction.ExtArgumentType.Void;
149  }
150  if (type_name.endsWith(" *")) {
151  return pointerType(deserializeType(type_name.substring(0, type_name.length() - 2)));
152  }
153 
154  if (type_name.endsWith("*")) {
155  return pointerType(deserializeType(type_name.substring(0, type_name.length() - 1)));
156  }
157 
158  if (type_name.equals("Array<bool>")) {
159  return ExtensionFunction.ExtArgumentType.ArrayInt8;
160  }
161  if (type_name.equals("Array<int8_t>") || type_name.equals("Array<char>")) {
162  return ExtensionFunction.ExtArgumentType.ArrayInt8;
163  }
164  if (type_name.equals("Array<int16_t>") || type_name.equals("Array<short>")) {
165  return ExtensionFunction.ExtArgumentType.ArrayInt16;
166  }
167  if (type_name.equals("Array<int32_t>") || type_name.equals("Array<int>")) {
168  return ExtensionFunction.ExtArgumentType.ArrayInt32;
169  }
170  if (type_name.equals("Array<int64_t>") || type_name.equals("Array<size_t>")
171  || type_name.equals("Array<long>")) {
172  return ExtensionFunction.ExtArgumentType.ArrayInt64;
173  }
174  if (type_name.equals("Array<float>")) {
175  return ExtensionFunction.ExtArgumentType.ArrayFloat;
176  }
177  if (type_name.equals("Array<double>")) {
178  return ExtensionFunction.ExtArgumentType.ArrayDouble;
179  }
180  if (type_name.equals("Cursor")) {
181  return ExtensionFunction.ExtArgumentType.Cursor;
182  }
183  if (type_name.equals("GeoPoint")) {
184  return ExtensionFunction.ExtArgumentType.GeoPoint;
185  }
186  if (type_name.equals("GeoLineString")) {
187  return ExtensionFunction.ExtArgumentType.GeoLineString;
188  }
189  if (type_name.equals("GeoPolygon")) {
190  return ExtensionFunction.ExtArgumentType.GeoPolygon;
191  }
192 
193  MAPDLOGGER.info(
194  "ExtensionfunctionSignatureParser::deserializeType: unknown type_name=`"
195  + type_name + "`");
196  assert false;
197  return null;
198  }
199 
200  private static ExtensionFunction.ExtArgumentType pointerType(
201  final ExtensionFunction.ExtArgumentType targetType) {
202  switch (targetType) {
203  case Bool:
204  case Int8:
205  return ExtensionFunction.ExtArgumentType.PInt8;
206  case Int16:
207  return ExtensionFunction.ExtArgumentType.PInt16;
208  case Int32:
209  return ExtensionFunction.ExtArgumentType.PInt32;
210  case Int64:
211  return ExtensionFunction.ExtArgumentType.PInt64;
212  case Float:
213  return ExtensionFunction.ExtArgumentType.PFloat;
214  case Double:
215  return ExtensionFunction.ExtArgumentType.PDouble;
216  default:
217  assert false;
218  return null;
219  }
220  }
221 
222  static String join(final List<String> strs, final String sep) {
223  StringBuilder sb = new StringBuilder();
224  if (strs.isEmpty()) {
225  return "";
226  }
227  sb.append(strs.get(0));
228  for (int i = 1; i < strs.size(); ++i) {
229  sb.append(sep).append(strs.get(i));
230  }
231  return sb.toString();
232  }
233 }
static Map< String, ExtensionFunction > parseFromString(final String udf_string)
static ExtensionFunction.ExtArgumentType pointerType(final ExtensionFunction.ExtArgumentType targetType)
static ExtensionFunction toSignature(final String ret, final String cs_param_list)
static Map< String, ExtensionFunction > parseFromString(final String udf_string, final boolean is_row_func)
size_t append(FILE *f, const size_t size, int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
Definition: File.cpp:136
static Map< String, ExtensionFunction > parse(final String file_path)
int64_t const int32_t sz assert(dest)
static String join(final List< String > strs, final String sep)
static String signaturesToJson(final Map< String, ExtensionFunction > sigs)
static ExtensionFunction.ExtArgumentType deserializeType(final String type_name)
static ExtensionFunction toSignature(final String ret, final String cs_param_list, final boolean is_row_func)