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