OmniSciDB  95562058bd
 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.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
38  final static Logger MAPDLOGGER =
39  LoggerFactory.getLogger(ExtensionFunctionSignatureParser.class);
40 
41  static Map<String, ExtensionFunction> parse(final String file_path) throws IOException {
42  File file = new File(file_path);
43  FileReader fileReader = new FileReader(file);
44  BufferedReader bufferedReader = new BufferedReader(fileReader);
45  String line;
46  Pattern s = Pattern.compile("\\| ([\\` ]|used)+ ([\\w]+) '([\\w<>]+) \\((.*)\\)'");
47  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
48  while ((line = bufferedReader.readLine()) != null) {
49  Matcher m = s.matcher(line);
50  if (m.find()) {
51  final String name = m.group(2);
52  final String ret = m.group(3);
53  final String cs_param_list = m.group(4);
54  sigs.put(name, toSignature(ret, cs_param_list, false));
55  }
56  }
57  return sigs;
58  }
59 
60  static Map<String, ExtensionFunction> parseUdfAst(final String file_path)
61  throws IOException {
62  File file = new File(file_path);
63  FileReader fileReader = new FileReader(file);
64  BufferedReader bufferedReader = new BufferedReader(fileReader);
65  String line;
66  Pattern s = Pattern.compile("([<>:\\w]+) ([:\\w]+)(?:\\(\\))?\\((.*)\\)");
67  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
68  while ((line = bufferedReader.readLine()) != null) {
69  Matcher m = s.matcher(line);
70  if (m.find()) {
71  final String name = m.group(2);
72  final String ret = m.group(1);
73  final String cs_param_list = m.group(3);
74  if (cs_param_list.isEmpty()) {
75  continue;
76  }
77  sigs.put(name, toSignature(ret, cs_param_list, true));
78  }
79  }
80  return sigs;
81  }
82 
83  static Map<String, ExtensionFunction> parseFromString(final String udf_string)
84  throws IOException {
85  return parseFromString(udf_string, true);
86  }
87 
88  static Map<String, ExtensionFunction> parseFromString(
89  final String udf_string, final boolean is_row_func) throws IOException {
90  StringReader stringReader = new StringReader(udf_string);
91  BufferedReader bufferedReader = new BufferedReader(stringReader);
92  String line;
93  Pattern r = Pattern.compile("([\\w]+)\\s+'([\\w]+)\\s*\\((.*)\\)'");
94  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
95  while ((line = bufferedReader.readLine()) != null) {
96  Matcher m = r.matcher(line);
97  if (m.find()) {
98  final String name = m.group(1);
99  final String ret = m.group(2);
100  final String cs_param_list = m.group(3);
101  sigs.put(name, toSignature(ret, cs_param_list, is_row_func));
102  }
103  }
104  return sigs;
105  }
106  static String signaturesToJson(final Map<String, ExtensionFunction> sigs) {
107  List<String> json_sigs = new ArrayList<String>();
108  if (sigs != null) {
109  for (Map.Entry<String, ExtensionFunction> sig : sigs.entrySet()) {
110  if (sig.getValue().isRowUdf()) {
111  json_sigs.add(sig.getValue().toJson(sig.getKey()));
112  }
113  }
114  }
115  return "[" + join(json_sigs, ",") + "]";
116  }
117 
119  final String ret, final String cs_param_list, final boolean has_variable_name) {
120  return toSignature(ret, cs_param_list, has_variable_name, true);
121  }
122 
123  private static ExtensionFunction toSignature(final String ret,
124  final String cs_param_list,
125  final boolean has_variable_name,
126  final boolean is_row_func) {
127  String[] params = cs_param_list.split(",");
128  List<ExtensionFunction.ExtArgumentType> args =
129  new ArrayList<ExtensionFunction.ExtArgumentType>();
130  for (final String param : params) {
131  ExtensionFunction.ExtArgumentType arg_type;
132  if (has_variable_name) {
133  String[] full_param = param.trim().split("\\s+");
134  if (full_param.length > 0) {
135  if (full_param[0].trim().compareTo("const") == 0) {
136  assert full_param.length > 1;
137  arg_type = deserializeType((full_param[1]).trim());
138  } else {
139  arg_type = deserializeType((full_param[0]).trim());
140  }
141  } else {
142  arg_type = deserializeType(full_param[0]);
143  }
144  } else {
145  arg_type = deserializeType(param.trim());
146  }
147  if (arg_type != ExtensionFunction.ExtArgumentType.Void) {
148  args.add(arg_type);
149  }
150  }
151  assert is_row_func;
152  return new ExtensionFunction(args, deserializeType(ret));
153  }
154  private static ExtensionFunction.ExtArgumentType deserializeType(
155  final String type_name) {
156  final String const_prefix = "const ";
157  final String std_namespace_prefix = "std::";
158 
159  if (type_name.startsWith(const_prefix)) {
160  return deserializeType(type_name.substring(const_prefix.length()));
161  }
162  if (type_name.startsWith(std_namespace_prefix)) {
163  return deserializeType(type_name.substring(std_namespace_prefix.length()));
164  }
165 
166  if (type_name.equals("bool") || type_name.equals("_Bool")) {
167  return ExtensionFunction.ExtArgumentType.Bool;
168  }
169  if (type_name.equals("int8_t") || type_name.equals("char")
170  || type_name.equals("int8")) {
171  return ExtensionFunction.ExtArgumentType.Int8;
172  }
173  if (type_name.equals("int16_t") || type_name.equals("short")
174  || type_name.equals("int16")) {
175  return ExtensionFunction.ExtArgumentType.Int16;
176  }
177  if (type_name.equals("int32_t") || type_name.equals("int")
178  || type_name.equals("int32")) {
179  return ExtensionFunction.ExtArgumentType.Int32;
180  }
181  if (type_name.equals("int64_t") || type_name.equals("size_t")
182  || type_name.equals("int64") || type_name.equals("long")) {
183  return ExtensionFunction.ExtArgumentType.Int64;
184  }
185  if (type_name.equals("float") || type_name.equals("float32")) {
186  return ExtensionFunction.ExtArgumentType.Float;
187  }
188  if (type_name.equals("double") || type_name.equals("float64")) {
190  }
191  if (type_name.isEmpty() || type_name.equals("void")) {
192  return ExtensionFunction.ExtArgumentType.Void;
193  }
194  if (type_name.endsWith(" *")) {
195  return pointerType(deserializeType(type_name.substring(0, type_name.length() - 2)));
196  }
197  if (type_name.endsWith("*")) {
198  return pointerType(deserializeType(type_name.substring(0, type_name.length() - 1)));
199  }
200  if (type_name.equals("Array<bool>")) {
201  return ExtensionFunction.ExtArgumentType.ArrayBool;
202  }
203  if (type_name.equals("Array<int8_t>") || type_name.equals("Array<char>")) {
204  return ExtensionFunction.ExtArgumentType.ArrayInt8;
205  }
206  if (type_name.equals("Array<int16_t>") || type_name.equals("Array<short>")) {
207  return ExtensionFunction.ExtArgumentType.ArrayInt16;
208  }
209  if (type_name.equals("Array<int32_t>") || type_name.equals("Array<int>")) {
210  return ExtensionFunction.ExtArgumentType.ArrayInt32;
211  }
212  if (type_name.equals("Array<int64_t>") || type_name.equals("Array<size_t>")
213  || type_name.equals("Array<long>")) {
214  return ExtensionFunction.ExtArgumentType.ArrayInt64;
215  }
216  if (type_name.equals("Array<float>")) {
217  return ExtensionFunction.ExtArgumentType.ArrayFloat;
218  }
219  if (type_name.equals("Array<double>")) {
220  return ExtensionFunction.ExtArgumentType.ArrayDouble;
221  }
222  if (type_name.equals("Array<bool>")) {
223  return ExtensionFunction.ExtArgumentType.ArrayBool;
224  }
225  if (type_name.equals("Column<int8_t>") || type_name.equals("Column<char>")) {
226  return ExtensionFunction.ExtArgumentType.ColumnInt8;
227  }
228  if (type_name.equals("Column<int16_t>") || type_name.equals("Column<short>")) {
229  return ExtensionFunction.ExtArgumentType.ColumnInt16;
230  }
231  if (type_name.equals("Column<int32_t>") || type_name.equals("Column<int>")) {
232  return ExtensionFunction.ExtArgumentType.ColumnInt32;
233  }
234  if (type_name.equals("Column<int64_t>") || type_name.equals("Column<size_t>")
235  || type_name.equals("Column<long>")) {
236  return ExtensionFunction.ExtArgumentType.ColumnInt64;
237  }
238  if (type_name.equals("Column<float>")) {
239  return ExtensionFunction.ExtArgumentType.ColumnFloat;
240  }
241  if (type_name.equals("Column<double>")) {
242  return ExtensionFunction.ExtArgumentType.ColumnDouble;
243  }
244  if (type_name.equals("Cursor")) {
245  return ExtensionFunction.ExtArgumentType.Cursor;
246  }
247  if (type_name.equals("GeoPoint")) {
248  return ExtensionFunction.ExtArgumentType.GeoPoint;
249  }
250  if (type_name.equals("GeoLineString")) {
251  return ExtensionFunction.ExtArgumentType.GeoLineString;
252  }
253  if (type_name.equals("GeoPolygon")) {
254  return ExtensionFunction.ExtArgumentType.GeoPolygon;
255  }
256  if (type_name.equals("GeoMultiPolygon")) {
257  return ExtensionFunction.ExtArgumentType.GeoMultiPolygon;
258  }
259 
260  MAPDLOGGER.info(
261  "ExtensionfunctionSignatureParser::deserializeType: unknown type_name=`"
262  + type_name + "`");
263  // TODO: Return void for convenience. Consider sanitizing functions for supported
264  // types before they reach Calcite
265  return ExtensionFunction.ExtArgumentType.Void;
266  }
267 
268  private static ExtensionFunction.ExtArgumentType pointerType(
269  final ExtensionFunction.ExtArgumentType targetType) {
270  switch (targetType) {
271  case Bool:
272  return ExtensionFunction.ExtArgumentType.PBool;
273  case Int8:
274  return ExtensionFunction.ExtArgumentType.PInt8;
275  case Int16:
276  return ExtensionFunction.ExtArgumentType.PInt16;
277  case Int32:
278  return ExtensionFunction.ExtArgumentType.PInt32;
279  case Int64:
280  return ExtensionFunction.ExtArgumentType.PInt64;
281  case Float:
282  return ExtensionFunction.ExtArgumentType.PFloat;
283  case Double:
284  return ExtensionFunction.ExtArgumentType.PDouble;
285  default:
286  assert false;
287  return null;
288  }
289  }
290 
291  static String join(final List<String> strs, final String sep) {
292  StringBuilder sb = new StringBuilder();
293  if (strs.isEmpty()) {
294  return "";
295  }
296  sb.append(strs.get(0));
297  for (int i = 1; i < strs.size(); ++i) {
298  sb.append(sep).append(strs.get(i));
299  }
300  return sb.toString();
301  }
302 }
static Map< String, ExtensionFunction > parseFromString(final String udf_string)
tuple line
Definition: parse_ast.py:10
static ExtensionFunction.ExtArgumentType pointerType(final ExtensionFunction.ExtArgumentType targetType)
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
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)
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:140
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 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)
string name
Definition: setup.py:35