OmniSciDB  bf83d84833
 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 {
74  }
75  ;
76 
77  ExtensionFunction(final List<ExtArgumentType> args, final ExtArgumentType ret) {
78  this.args = args;
79  this.ret = ret;
80  this.outs = null;
81  this.isRowUdf = true;
82  }
83 
84  ExtensionFunction(final List<ExtArgumentType> args, final List<ExtArgumentType> outs) {
85  this.args = args;
86  this.ret = null;
87  this.outs = outs;
88  this.isRowUdf = false;
89  }
90 
91  public List<ExtArgumentType> getArgs() {
92  return this.args;
93  }
94 
95  public List<ExtArgumentType> getOuts() {
96  return this.outs;
97  }
98 
100  return this.ret;
101  }
102 
103  public SqlTypeName getSqlRet() {
104  assert this.isRowUdf();
105  return toSqlTypeName(this.ret);
106  }
107 
108  public List<SqlTypeName> getSqlOuts() {
109  assert this.isTableUdf();
110  List<SqlTypeName> sql_outs = new ArrayList<SqlTypeName>();
111  for (final ExtArgumentType otype : this.getOuts()) {
112  sql_outs.add(toSqlTypeName(getValueType(otype)));
113  }
114  return sql_outs;
115  }
116 
117  public boolean isRowUdf() {
118  return this.isRowUdf;
119  }
120 
121  public boolean isTableUdf() {
122  return !this.isRowUdf();
123  }
124 
125  public String toJson(final String name) {
126  MAPDLOGGER.debug("Extensionfunction::toJson: " + name);
127  StringBuilder json_cons = new StringBuilder();
128  json_cons.append("{");
129  json_cons.append("\"name\":").append(dq(name)).append(",");
130  if (isRowUdf) {
131  json_cons.append("\"ret\":").append(dq(typeName(ret))).append(",");
132  } else {
133  json_cons.append("\"outs\":");
134  json_cons.append("[");
135  List<String> param_list = new ArrayList<String>();
136  for (final ExtArgumentType out : outs) {
137  param_list.add(dq(typeName(out)));
138  }
139  json_cons.append(ExtensionFunctionSignatureParser.join(param_list, ","));
140  json_cons.append("],");
141  }
142  json_cons.append("\"args\":");
143  json_cons.append("[");
144  List<String> param_list = new ArrayList<String>();
145  for (final ExtArgumentType arg : args) {
146  param_list.add(dq(typeName(arg)));
147  }
148  json_cons.append(ExtensionFunctionSignatureParser.join(param_list, ","));
149  json_cons.append("]");
150  json_cons.append("}");
151  return json_cons.toString();
152  }
153 
154  private static String typeName(final ExtArgumentType type) {
155  switch (type) {
156  case Bool:
157  return "i1";
158  case Int8:
159  return "i8";
160  case Int16:
161  return "i16";
162  case Int32:
163  return "i32";
164  case Int64:
165  return "i64";
166  case Float:
167  return "float";
168  case Double:
169  return "double";
170  case Void:
171  return "void";
172  case PInt8:
173  return "i8*";
174  case PInt16:
175  return "i16*";
176  case PInt32:
177  return "i32*";
178  case PInt64:
179  return "i64*";
180  case PFloat:
181  return "float*";
182  case PDouble:
183  return "double*";
184  case PBool:
185  return "i1*";
186  case ArrayInt8:
187  return "{i8*, i64, i8}*";
188  case ArrayInt16:
189  return "{i16*, i64, i8}*";
190  case ArrayInt32:
191  return "{i32*, i64, i8}*";
192  case ArrayInt64:
193  return "{i64*, i64, i8}*";
194  case ArrayFloat:
195  return "{float*, i64, i8}*";
196  case ArrayDouble:
197  return "{double*, i64, i8}*";
198  case ArrayBool:
199  return "{i1*, i64, i8}*";
200  case ColumnInt8:
201  return "{i8*, i64}";
202  case ColumnInt16:
203  return "{i16*, i64}";
204  case ColumnInt32:
205  return "{i32*, i64}";
206  case ColumnInt64:
207  return "{i64*, i64}";
208  case ColumnFloat:
209  return "{float*, i64}";
210  case ColumnDouble:
211  return "{double*, i64}";
212  case ColumnBool:
213  return "{i1*, i64}";
214  case GeoPoint:
215  return "geo_point";
216  case Cursor:
217  return "cursor";
218  case GeoLineString:
219  return "geo_linestring";
220  case GeoPolygon:
221  return "geo_polygon";
222  case GeoMultiPolygon:
223  return "geo_multi_polygon";
224  case TextEncodingNone:
225  return "text_encoding_none";
226  case TextEncodingDict8:
227  return "text_encoding_dict8";
228  case TextEncodingDict16:
229  return "text_encoding_dict16";
230  case TextEncodingDict32:
231  return "text_encoding_dict32";
232  }
233  MAPDLOGGER.info("Extensionfunction::typeName: unknown type=`" + type + "`");
234  assert false;
235  return null;
236  }
237 
238  private static String dq(final String str) {
239  return "\"" + str + "\"";
240  }
241 
242  private final List<ExtArgumentType> args;
243  private final List<ExtArgumentType> outs; // only used by UDTFs
244  private final ExtArgumentType ret; // only used by UDFs
245  private final boolean isRowUdf;
246 
247  public final java.util.List<SqlTypeFamily> toSqlSignature() {
248  java.util.List<SqlTypeFamily> sql_sig = new java.util.ArrayList<SqlTypeFamily>();
249  boolean isRowUdf = this.isRowUdf();
250  for (int arg_idx = 0; arg_idx < this.getArgs().size(); ++arg_idx) {
251  final ExtArgumentType arg_type = this.getArgs().get(arg_idx);
252  if (isRowUdf) {
253  sql_sig.add(toSqlTypeName(arg_type).getFamily());
254  if (isPointerType(arg_type)) {
255  ++arg_idx;
256  }
257  } else {
258  sql_sig.add(toSqlTypeName(arg_type).getFamily());
259  }
260  }
261  return sql_sig;
262  }
263 
264  private static boolean isPointerType(final ExtArgumentType type) {
265  return type == ExtArgumentType.PInt8 || type == ExtArgumentType.PInt16
266  || type == ExtArgumentType.PInt32 || type == ExtArgumentType.PInt64
267  || type == ExtArgumentType.PFloat || type == ExtArgumentType.PDouble
268  || type == ExtArgumentType.PBool;
269  }
270 
271  private static boolean isColumnType(final ExtArgumentType type) {
272  return type == ExtArgumentType.ColumnInt8 || type == ExtArgumentType.ColumnInt16
273  || type == ExtArgumentType.ColumnInt32 || type == ExtArgumentType.ColumnInt64
274  || type == ExtArgumentType.ColumnFloat || type == ExtArgumentType.ColumnDouble
275  || type == ExtArgumentType.ColumnBool;
276  }
277 
279  switch (type) {
280  case PInt8:
281  case ColumnInt8:
282  case Int8:
283  return ExtArgumentType.Int8;
284  case ColumnInt16:
285  case PInt16:
286  case Int16:
287  return ExtArgumentType.Int16;
288  case PInt32:
289  case ColumnInt32:
290  case Int32:
291  return ExtArgumentType.Int32;
292  case PInt64:
293  case ColumnInt64:
294  case Int64:
295  return ExtArgumentType.Int64;
296  case PFloat:
297  case ColumnFloat:
298  case Float:
299  return ExtArgumentType.Float;
300  case PDouble:
301  case ColumnDouble:
302  case Double:
303  return ExtArgumentType.Double;
304  case PBool:
305  case ColumnBool:
306  case Bool:
307  return ExtArgumentType.Bool;
308  }
309  MAPDLOGGER.error("getValueType: no value for type " + type);
310  assert false;
311  return null;
312  }
313 
314  private static SqlTypeName toSqlTypeName(final ExtArgumentType type) {
315  switch (type) {
316  case Bool:
317  return SqlTypeName.BOOLEAN;
318  case Int8:
319  return SqlTypeName.TINYINT;
320  case Int16:
321  return SqlTypeName.SMALLINT;
322  case Int32:
323  return SqlTypeName.INTEGER;
324  case Int64:
325  return SqlTypeName.BIGINT;
326  case Float:
327  return SqlTypeName.FLOAT;
328  case Double:
329  return SqlTypeName.DOUBLE;
330  case PInt8:
331  case PInt16:
332  case PInt32:
333  case PInt64:
334  case PFloat:
335  case PDouble:
336  case PBool:
337  case ArrayInt8:
338  case ArrayInt16:
339  case ArrayInt32:
340  case ArrayInt64:
341  case ArrayFloat:
342  case ArrayDouble:
343  case ArrayBool:
344  return SqlTypeName.ARRAY;
345  case GeoPoint:
346  case GeoLineString:
347  case GeoPolygon:
348  case GeoMultiPolygon:
349  return SqlTypeName.GEOMETRY;
350  case Cursor:
351  return SqlTypeName.CURSOR;
352  case TextEncodingNone:
353  return SqlTypeName.VARCHAR;
354  case TextEncodingDict8:
355  case TextEncodingDict16:
356  case TextEncodingDict32:
357  return SqlTypeName.VARCHAR; // ?
358  }
359  Set<SqlTypeName> allSqlTypeNames = EnumSet.allOf(SqlTypeName.class);
360  MAPDLOGGER.error("toSqlTypeName: unknown type " + type + " to be mapped to {"
361  + allSqlTypeNames + "}");
362  assert false;
363  return null;
364  }
365 }
final java.util.List< SqlTypeFamily > toSqlSignature()
static SqlTypeName toSqlTypeName(final ExtArgumentType type)
static boolean isColumnType(final ExtArgumentType type)
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:141
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)
string name
Definition: setup.py:35
static boolean isPointerType(final ExtArgumentType type)
static ExtArgumentType getValueType(final ExtArgumentType type)