OmniSciDB  8fa3bf436f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExtensionFunction.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.apache.calcite.sql.type.SqlTypeFamily;
19 import org.apache.calcite.sql.type.SqlTypeName;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 
23 import java.util.ArrayList;
24 import java.util.EnumSet;
25 import java.util.List;
26 import java.util.Set;
27 
32 public class ExtensionFunction {
33  final static Logger MAPDLOGGER = LoggerFactory.getLogger(ExtensionFunction.class);
34 
35  public enum ExtArgumentType {
81  }
82  ;
83 
84  ExtensionFunction(final List<ExtArgumentType> args, final ExtArgumentType ret) {
85  this.args = args;
86  this.ret = ret;
87  this.outs = null;
88  this.isRowUdf = true;
89  }
90 
91  ExtensionFunction(final List<ExtArgumentType> args, final List<ExtArgumentType> outs) {
92  this.args = args;
93  this.ret = null;
94  this.outs = outs;
95  this.isRowUdf = false;
96  }
97 
98  public List<ExtArgumentType> getArgs() {
99  return this.args;
100  }
101 
102  public List<ExtArgumentType> getOuts() {
103  return this.outs;
104  }
105 
107  return this.ret;
108  }
109 
110  public SqlTypeName getSqlRet() {
111  assert this.isRowUdf();
112  return toSqlTypeName(this.ret);
113  }
114 
115  public List<SqlTypeName> getSqlOuts() {
116  assert this.isTableUdf();
117  List<SqlTypeName> sql_outs = new ArrayList<SqlTypeName>();
118  for (final ExtArgumentType otype : this.getOuts()) {
119  sql_outs.add(toSqlTypeName(getValueType(otype)));
120  }
121  return sql_outs;
122  }
123 
124  public boolean isRowUdf() {
125  return this.isRowUdf;
126  }
127 
128  public boolean isTableUdf() {
129  return !this.isRowUdf();
130  }
131 
132  public String toJson(final String name) {
133  MAPDLOGGER.debug("Extensionfunction::toJson: " + name);
134  StringBuilder json_cons = new StringBuilder();
135  json_cons.append("{");
136  json_cons.append("\"name\":").append(dq(name)).append(",");
137  if (isRowUdf) {
138  json_cons.append("\"ret\":").append(dq(typeName(ret))).append(",");
139  } else {
140  json_cons.append("\"outs\":");
141  json_cons.append("[");
142  List<String> param_list = new ArrayList<String>();
143  for (final ExtArgumentType out : outs) {
144  param_list.add(dq(typeName(out)));
145  }
146  json_cons.append(ExtensionFunctionSignatureParser.join(param_list, ","));
147  json_cons.append("],");
148  }
149  json_cons.append("\"args\":");
150  json_cons.append("[");
151  List<String> param_list = new ArrayList<String>();
152  for (final ExtArgumentType arg : args) {
153  param_list.add(dq(typeName(arg)));
154  }
155  json_cons.append(ExtensionFunctionSignatureParser.join(param_list, ","));
156  json_cons.append("]");
157  json_cons.append("}");
158  return json_cons.toString();
159  }
160 
161  private static String typeName(final ExtArgumentType type) {
162  switch (type) {
163  case Bool:
164  return "i1";
165  case Int8:
166  return "i8";
167  case Int16:
168  return "i16";
169  case Int32:
170  return "i32";
171  case Int64:
172  return "i64";
173  case Float:
174  return "float";
175  case Double:
176  return "double";
177  case Void:
178  return "void";
179  case PInt8:
180  return "i8*";
181  case PInt16:
182  return "i16*";
183  case PInt32:
184  return "i32*";
185  case PInt64:
186  return "i64*";
187  case PFloat:
188  return "float*";
189  case PDouble:
190  return "double*";
191  case PBool:
192  return "i1*";
193  case ArrayInt8:
194  return "{i8*, i64, i8}*";
195  case ArrayInt16:
196  return "{i16*, i64, i8}*";
197  case ArrayInt32:
198  return "{i32*, i64, i8}*";
199  case ArrayInt64:
200  return "{i64*, i64, i8}*";
201  case ArrayFloat:
202  return "{float*, i64, i8}*";
203  case ArrayDouble:
204  return "{double*, i64, i8}*";
205  case ArrayBool:
206  return "{i1*, i64, i8}*";
207  case ColumnInt8:
208  return "{i8*, i64}";
209  case ColumnInt16:
210  return "{i16*, i64}";
211  case ColumnInt32:
212  return "{i32*, i64}";
213  case ColumnInt64:
214  return "{i64*, i64}";
215  case ColumnFloat:
216  return "{float*, i64}";
217  case ColumnDouble:
218  return "{double*, i64}";
219  case ColumnBool:
220  return "{i1*, i64}";
221  case GeoPoint:
222  return "geo_point";
223  case Cursor:
224  return "cursor";
225  case GeoLineString:
226  return "geo_linestring";
227  case GeoPolygon:
228  return "geo_polygon";
229  case GeoMultiPolygon:
230  return "geo_multi_polygon";
231  case TextEncodingNone:
232  return "text_encoding_none";
233  case TextEncodingDict8:
234  return "text_encoding_dict8";
235  case TextEncodingDict16:
236  return "text_encoding_dict16";
237  case TextEncodingDict32:
238  return "text_encoding_dict32";
239  case ColumnListInt8:
240  return "column_list_int8";
241  case ColumnListInt16:
242  return "column_list_int16";
243  case ColumnListInt32:
244  return "column_list_int32";
245  case ColumnListInt64:
246  return "column_list_int64";
247  case ColumnListFloat:
248  return "column_list_float";
249  case ColumnListDouble:
250  return "column_list_double";
251  case ColumnListBool:
252  return "column_list_bool";
253  }
254  MAPDLOGGER.info("Extensionfunction::typeName: unknown type=`" + type + "`");
255  assert false;
256  return null;
257  }
258 
259  private static String dq(final String str) {
260  return "\"" + str + "\"";
261  }
262 
263  private final List<ExtArgumentType> args;
264  private final List<ExtArgumentType> outs; // only used by UDTFs
265  private final ExtArgumentType ret; // only used by UDFs
266  private final boolean isRowUdf;
267 
268  public final java.util.List<SqlTypeFamily> toSqlSignature() {
269  java.util.List<SqlTypeFamily> sql_sig = new java.util.ArrayList<SqlTypeFamily>();
270  boolean isRowUdf = this.isRowUdf();
271  for (int arg_idx = 0; arg_idx < this.getArgs().size(); ++arg_idx) {
272  final ExtArgumentType arg_type = this.getArgs().get(arg_idx);
273  if (isRowUdf) {
274  sql_sig.add(toSqlTypeName(arg_type).getFamily());
275  if (isPointerType(arg_type)) {
276  ++arg_idx;
277  }
278  } else {
279  sql_sig.add(toSqlTypeName(arg_type).getFamily());
280  }
281  }
282  return sql_sig;
283  }
284 
285  private static boolean isPointerType(final ExtArgumentType type) {
286  return type == ExtArgumentType.PInt8 || type == ExtArgumentType.PInt16
287  || type == ExtArgumentType.PInt32 || type == ExtArgumentType.PInt64
288  || type == ExtArgumentType.PFloat || type == ExtArgumentType.PDouble
289  || type == ExtArgumentType.PBool;
290  }
291 
292  private static boolean isColumnType(final ExtArgumentType type) {
293  return type == ExtArgumentType.ColumnInt8 || type == ExtArgumentType.ColumnInt16
294  || type == ExtArgumentType.ColumnInt32 || type == ExtArgumentType.ColumnInt64
295  || type == ExtArgumentType.ColumnFloat || type == ExtArgumentType.ColumnDouble
296  || type == ExtArgumentType.ColumnBool;
297  }
298 
299  private static boolean isColumnListType(final ExtArgumentType type) {
300  return type == ExtArgumentType.ColumnListInt8
301  || type == ExtArgumentType.ColumnListInt16
302  || type == ExtArgumentType.ColumnListInt32
303  || type == ExtArgumentType.ColumnListInt64
304  || type == ExtArgumentType.ColumnListFloat
305  || type == ExtArgumentType.ColumnListDouble
307  }
308 
310  switch (type) {
311  case PInt8:
312  case ColumnInt8:
313  case ColumnListInt8:
314  case Int8:
315  return ExtArgumentType.Int8;
316  case PInt16:
317  case ColumnInt16:
318  case ColumnListInt16:
319  case Int16:
320  return ExtArgumentType.Int16;
321  case PInt32:
322  case ColumnInt32:
323  case ColumnListInt32:
324  case Int32:
325  return ExtArgumentType.Int32;
326  case PInt64:
327  case ColumnInt64:
328  case ColumnListInt64:
329  case Int64:
330  return ExtArgumentType.Int64;
331  case PFloat:
332  case ColumnFloat:
333  case ColumnListFloat:
334  case Float:
335  return ExtArgumentType.Float;
336  case PDouble:
337  case ColumnDouble:
338  case ColumnListDouble:
339  case Double:
340  return ExtArgumentType.Double;
341  case PBool:
342  case ColumnBool:
343  case ColumnListBool:
344  case Bool:
345  return ExtArgumentType.Bool;
346  }
347  MAPDLOGGER.error("getValueType: no value for type " + type);
348  assert false;
349  return null;
350  }
351 
352  private static SqlTypeName toSqlTypeName(final ExtArgumentType type) {
353  switch (type) {
354  case Bool:
355  return SqlTypeName.BOOLEAN;
356  case Int8:
357  return SqlTypeName.TINYINT;
358  case Int16:
359  return SqlTypeName.SMALLINT;
360  case Int32:
361  return SqlTypeName.INTEGER;
362  case Int64:
363  return SqlTypeName.BIGINT;
364  case Float:
365  return SqlTypeName.FLOAT;
366  case Double:
367  return SqlTypeName.DOUBLE;
368  case PInt8:
369  case PInt16:
370  case PInt32:
371  case PInt64:
372  case PFloat:
373  case PDouble:
374  case PBool:
375  case ArrayInt8:
376  case ArrayInt16:
377  case ArrayInt32:
378  case ArrayInt64:
379  case ArrayFloat:
380  case ArrayDouble:
381  case ArrayBool:
382  return SqlTypeName.ARRAY;
383  case GeoPoint:
384  case GeoLineString:
385  case GeoPolygon:
386  case GeoMultiPolygon:
387  return SqlTypeName.GEOMETRY;
388  case Cursor:
389  return SqlTypeName.CURSOR;
390  case TextEncodingNone:
391  return SqlTypeName.VARCHAR;
392  case TextEncodingDict8:
393  case TextEncodingDict16:
394  case TextEncodingDict32:
395  return SqlTypeName.VARCHAR; // ?
396  case ColumnListInt8:
397  case ColumnListInt16:
398  case ColumnListInt32:
399  case ColumnListInt64:
400  case ColumnListFloat:
401  case ColumnListDouble:
402  case ColumnListBool:
403  return SqlTypeName.COLUMN_LIST;
404  }
405  Set<SqlTypeName> allSqlTypeNames = EnumSet.allOf(SqlTypeName.class);
406  MAPDLOGGER.error("toSqlTypeName: unknown type " + type + " to be mapped to {"
407  + allSqlTypeNames + "}");
408  assert false;
409  return null;
410  }
411 }
final java.util.List< SqlTypeFamily > toSqlSignature()
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.
Definition: File.cpp:159
static SqlTypeName toSqlTypeName(final ExtArgumentType type)
#define GEOMETRY
#define CURSOR
#define SMALLINT
#define DOUBLE
string name
Definition: setup.in.py:72
#define BIGINT
static boolean isColumnListType(final ExtArgumentType type)
static boolean isColumnType(final ExtArgumentType type)
#define TINYINT
#define ARRAY
ExtensionFunction(final List< ExtArgumentType > args, final ExtArgumentType ret)
static String typeName(final ExtArgumentType type)
static String dq(final String str)
ExtensionFunction(final List< ExtArgumentType > args, final List< ExtArgumentType > outs)
#define FLOAT
static boolean isPointerType(final ExtArgumentType type)
static ExtArgumentType getValueType(final ExtArgumentType type)
#define BOOLEAN
#define INTEGER