OmniSciDB  addbbd5075
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MapDSqlOperatorTable.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.calcite.parser;
17 
18 import com.google.common.base.Predicate;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Multimap;
22 
23 import org.apache.calcite.rel.type.RelDataType;
24 import org.apache.calcite.rel.type.RelDataTypeFactory;
25 import org.apache.calcite.sql.SqlAggFunction;
26 import org.apache.calcite.sql.SqlCallBinding;
27 import org.apache.calcite.sql.SqlFunction;
28 import org.apache.calcite.sql.SqlFunctionCategory;
29 import org.apache.calcite.sql.SqlIdentifier;
30 import org.apache.calcite.sql.SqlKind;
31 import org.apache.calcite.sql.SqlOperator;
32 import org.apache.calcite.sql.SqlOperatorBinding;
33 import org.apache.calcite.sql.SqlOperatorTable;
34 import org.apache.calcite.sql.SqlSyntax;
35 import org.apache.calcite.sql.fun.SqlArrayValueConstructor;
36 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
37 import org.apache.calcite.sql.type.OperandTypes;
38 import org.apache.calcite.sql.type.SqlTypeFamily;
39 import org.apache.calcite.sql.type.SqlTypeName;
40 import org.apache.calcite.sql.util.ChainedSqlOperatorTable;
41 import org.apache.calcite.sql.util.ListSqlOperatorTable;
42 import org.apache.calcite.sql.util.ReflectiveSqlOperatorTable;
43 import org.apache.calcite.sql.validate.SqlNameMatcher;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 
47 import java.lang.reflect.Field;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Map;
52 
53 class CaseInsensitiveListSqlOperatorTable extends ListSqlOperatorTable {
54  @Override
55  public void lookupOperatorOverloads(SqlIdentifier opName,
56  SqlFunctionCategory category,
57  SqlSyntax syntax,
58  List<SqlOperator> operatorList,
59  SqlNameMatcher nameMatcher) {
60  for (SqlOperator operator : this.getOperatorList()) {
61  if (operator.getSyntax() != syntax) {
62  continue;
63  }
64  if (!opName.isSimple()
65  || !nameMatcher.matches(operator.getName(), opName.getSimple())) {
66  continue;
67  }
68  SqlFunctionCategory functionCategory;
69  if (operator instanceof SqlFunction) {
70  functionCategory = ((SqlFunction) operator).getFunctionType();
71  } else {
72  functionCategory = SqlFunctionCategory.SYSTEM;
73  }
74  if (category != functionCategory
75  && category != SqlFunctionCategory.USER_DEFINED_FUNCTION) {
76  continue;
77  }
78  operatorList.add(operator);
79  }
80  }
81 }
82 
87 public class MapDSqlOperatorTable extends ChainedSqlOperatorTable {
90 
91  static {
92  try {
93  // some nasty bit to remove the std APPROX_COUNT_DISTINCT function definition
94  {
95  Field f = ReflectiveSqlOperatorTable.class.getDeclaredField(
96  "caseSensitiveOperators");
97  f.setAccessible(true);
98  Multimap operators = (Multimap) f.get(SqlStdOperatorTable.instance());
99  for (Iterator i = operators.entries().iterator(); i.hasNext();) {
100  Map.Entry entry = (Map.Entry) i.next();
101  if (entry.getValue() == SqlStdOperatorTable.APPROX_COUNT_DISTINCT
102  || entry.getValue() == SqlStdOperatorTable.AVG
103  || entry.getValue() == SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR) {
104  i.remove();
105  }
106  }
107  }
108 
109  {
110  Field f = ReflectiveSqlOperatorTable.class.getDeclaredField(
111  "caseInsensitiveOperators");
112  f.setAccessible(true);
113  Multimap operators = (Multimap) f.get(SqlStdOperatorTable.instance());
114  for (Iterator i = operators.entries().iterator(); i.hasNext();) {
115  Map.Entry entry = (Map.Entry) i.next();
116  if (entry.getValue() == SqlStdOperatorTable.APPROX_COUNT_DISTINCT
117  || entry.getValue() == SqlStdOperatorTable.AVG
118  || entry.getValue() == SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR) {
119  i.remove();
120  }
121  }
122  }
123 
124  SqlStdOperatorTable.instance().register(ARRAY_VALUE_CONSTRUCTOR);
125 
126  } catch (Exception e) {
127  throw new RuntimeException(e);
128  }
129 
130  // register our approx count distinct against std table
131  // SqlStdOperatorTable.instance().register(new ApproxCountDistinct());
132  }
133 
134  final static Logger MAPDLOGGER = LoggerFactory.getLogger(MapDSqlOperatorTable.class);
135 
140  // ~ Instance fields --------------------------------------------------------
141  private final ListSqlOperatorTable listOpTab;
142 
143  // ~ Constructors -----------------------------------------------------------
144  public MapDSqlOperatorTable(SqlOperatorTable parentTable) {
145  super(ImmutableList.of(parentTable, new CaseInsensitiveListSqlOperatorTable()));
146  listOpTab = (ListSqlOperatorTable) tableList.get(1);
147  }
148 
149  // ~ Methods ----------------------------------------------------------------
155  public void addOperator(SqlOperator op) {
156  listOpTab.add(op);
157  }
158 
159  public static void addUDF(
160  MapDSqlOperatorTable opTab, final Map<String, ExtensionFunction> extSigs) {
161  // Don't use anonymous inner classes. They can't be instantiated
162  // using reflection when we are deserializing from JSON.
163  // opTab.addOperator(new RampFunction());
164  // opTab.addOperator(new DedupFunction());
165  opTab.addOperator(new RowCopier()); // Table UDF prototype
166  opTab.addOperator(new MyUDFFunction());
167  opTab.addOperator(new PgUnnest());
168  opTab.addOperator(new Any());
169  opTab.addOperator(new All());
170  opTab.addOperator(new Now());
171  opTab.addOperator(new Datetime());
172  opTab.addOperator(new PgExtract());
173  opTab.addOperator(new Dateadd());
174  opTab.addOperator(new Datediff());
175  opTab.addOperator(new Datepart());
176  opTab.addOperator(new PgDateTrunc());
177  opTab.addOperator(new Length());
178  opTab.addOperator(new CharLength());
179  opTab.addOperator(new KeyForString());
180  opTab.addOperator(new ArrayLength());
181  opTab.addOperator(new PgILike());
182  opTab.addOperator(new RegexpLike());
183  opTab.addOperator(new Likely());
184  opTab.addOperator(new Unlikely());
185  opTab.addOperator(new Sign());
186  opTab.addOperator(new Truncate());
187  opTab.addOperator(new ST_Contains());
188  opTab.addOperator(new ST_Intersects());
189  opTab.addOperator(new ST_Disjoint());
190  opTab.addOperator(new ST_Within());
191  opTab.addOperator(new ST_DWithin());
192  opTab.addOperator(new ST_DFullyWithin());
193  opTab.addOperator(new ST_Distance());
194  opTab.addOperator(new ST_MaxDistance());
195  opTab.addOperator(new ST_GeogFromText());
196  opTab.addOperator(new ST_GeomFromText());
197  opTab.addOperator(new ST_Transform());
198  opTab.addOperator(new ST_X());
199  opTab.addOperator(new ST_Y());
200  opTab.addOperator(new ST_XMin());
201  opTab.addOperator(new ST_XMax());
202  opTab.addOperator(new ST_YMin());
203  opTab.addOperator(new ST_YMax());
204  opTab.addOperator(new ST_PointN());
205  opTab.addOperator(new ST_StartPoint());
206  opTab.addOperator(new ST_EndPoint());
207  opTab.addOperator(new ST_Length());
208  opTab.addOperator(new ST_Perimeter());
209  opTab.addOperator(new ST_Area());
210  opTab.addOperator(new ST_NPoints());
211  opTab.addOperator(new ST_NRings());
212  opTab.addOperator(new ST_SRID());
213  opTab.addOperator(new ST_SetSRID());
214  opTab.addOperator(new ST_Point());
215  opTab.addOperator(new CastToGeography());
216  opTab.addOperator(new OffsetInFragment());
217  opTab.addOperator(new ApproxCountDistinct());
218  opTab.addOperator(new MapDAvg());
219  opTab.addOperator(new Sample());
220  opTab.addOperator(new LastSample());
221  // MapD_Geo* are deprecated in place of the OmniSci_Geo_ varietals
222  opTab.addOperator(new MapD_GeoPolyBoundsPtr());
223  opTab.addOperator(new MapD_GeoPolyRenderGroup());
224  opTab.addOperator(new OmniSci_Geo_PolyBoundsPtr());
225  opTab.addOperator(new OmniSci_Geo_PolyRenderGroup());
226  opTab.addOperator(new convert_meters_to_pixel_width());
227  opTab.addOperator(new convert_meters_to_pixel_height());
228  opTab.addOperator(new is_point_in_view());
229  opTab.addOperator(new is_point_size_in_view());
230  opTab.addOperator(new usTimestamp());
231  opTab.addOperator(new nsTimestamp());
232  if (extSigs == null) {
233  return;
234  }
235  HashSet<String> demangledNames = new HashSet<String>();
236  for (Map.Entry<String, ExtensionFunction> extSig : extSigs.entrySet()) {
237  final String demangledName = dropSuffix(extSig.getKey());
238  if (demangledNames.contains(demangledName)) {
239  continue;
240  }
241  demangledNames.add(demangledName);
242  opTab.addOperator(new ExtFunction(extSig.getKey(), extSig.getValue()));
243  }
244  }
245 
246  private static String dropSuffix(final String str) {
247  int suffix_idx = str.indexOf("__");
248  if (suffix_idx == -1) {
249  return str;
250  }
251  assert suffix_idx > 0;
252  return str.substring(0, suffix_idx - 1);
253  }
254 
256  extends SqlArrayValueConstructor {
257  @Override
258  protected RelDataType getComponentType(
259  RelDataTypeFactory typeFactory, List<RelDataType> argTypes) {
260  if (argTypes.isEmpty()) {
261  return typeFactory.createSqlType(SqlTypeName.NULL);
262  }
263  return super.getComponentType(typeFactory, argTypes);
264  }
265 
266  @Override
267  public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
268  if (callBinding.operands().isEmpty()) {
269  return true;
270  }
271  return super.checkOperandTypes(callBinding, throwOnFailure);
272  }
273  }
274 
278  public static class RampFunction extends SqlFunction {
279  public RampFunction() {
280  super("RAMP",
281  SqlKind.OTHER_FUNCTION,
282  null,
283  null,
284  OperandTypes.NUMERIC,
285  SqlFunctionCategory.USER_DEFINED_FUNCTION);
286  }
287 
288  @Override
289  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
290  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
291  return typeFactory.builder().add("I", SqlTypeName.INTEGER).build();
292  }
293  }
294 
298  public static class DedupFunction extends SqlFunction {
299  public DedupFunction() {
300  super("DEDUP",
301  SqlKind.OTHER_FUNCTION,
302  null,
303  null,
304  OperandTypes.VARIADIC,
305  SqlFunctionCategory.USER_DEFINED_FUNCTION);
306  }
307 
308  @Override
309  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
310  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
311  return typeFactory.builder().add("NAME", SqlTypeName.VARCHAR, 1024).build();
312  }
313  }
314 
319  public static class MyUDFFunction extends SqlFunction {
320  public MyUDFFunction() {
321  super("MyUDF",
322  SqlKind.OTHER_FUNCTION,
323  null,
324  null,
325  OperandTypes.STRING_STRING,
326  SqlFunctionCategory.SYSTEM);
327  }
328 
329  @Override
330  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
331  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
332  return typeFactory.createSqlType(SqlTypeName.BIGINT);
333  }
334  }
335 
339  public static class RowCopier extends SqlFunction {
340  public RowCopier() {
341  super("ROW_COPIER",
342  SqlKind.OTHER_FUNCTION,
343  null,
344  null,
345  OperandTypes.family(signature()),
346  SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
347  }
348 
349  @Override
350  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
351  assert opBinding.getOperandCount() == 2;
352  return opBinding.getCursorOperand(0);
353  }
354 
355  private static java.util.List<SqlTypeFamily> signature() {
356  java.util.List<SqlTypeFamily> sig_family = new java.util.ArrayList<SqlTypeFamily>();
357  sig_family.add(SqlTypeFamily.CURSOR);
358  sig_family.add(SqlTypeFamily.ANY);
359  return sig_family;
360  }
361  }
362 
363  /* Postgres-style UNNEST */
364  public static class PgUnnest extends SqlFunction {
365  public PgUnnest() {
366  super("PG_UNNEST",
367  SqlKind.OTHER_FUNCTION,
368  null,
369  null,
370  OperandTypes.ARRAY,
371  SqlFunctionCategory.SYSTEM);
372  }
373 
374  @Override
375  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
376  assert opBinding.getOperandCount() == 1;
377  RelDataType elem_type = opBinding.getOperandType(0).getComponentType();
378  assert elem_type != null;
379  return elem_type;
380  }
381  }
382 
383  /* ANY qualifier */
384  public static class Any extends SqlFunction {
385  public Any() {
386  super("PG_ANY",
387  SqlKind.OTHER_FUNCTION,
388  null,
389  null,
390  OperandTypes.ARRAY,
391  SqlFunctionCategory.SYSTEM);
392  }
393 
394  @Override
395  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
396  assert opBinding.getOperandCount() == 1;
397  RelDataType elem_type = opBinding.getOperandType(0).getComponentType();
398  assert elem_type != null;
399  return elem_type;
400  }
401  }
402 
403  /* ALL qualifier */
404  public static class All extends SqlFunction {
405  public All() {
406  super("PG_ALL",
407  SqlKind.OTHER_FUNCTION,
408  null,
409  null,
410  OperandTypes.ARRAY,
411  SqlFunctionCategory.SYSTEM);
412  }
413 
414  @Override
415  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
416  assert opBinding.getOperandCount() == 1;
417  RelDataType elem_type = opBinding.getOperandType(0).getComponentType();
418  assert elem_type != null;
419  return elem_type;
420  }
421  }
422 
423  /* NOW() */
424  public static class Now extends SqlFunction {
425  public Now() {
426  super("NOW",
427  SqlKind.OTHER_FUNCTION,
428  null,
429  null,
430  OperandTypes.NILADIC,
431  SqlFunctionCategory.SYSTEM);
432  }
433 
434  @Override
435  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
436  assert opBinding.getOperandCount() == 0;
437  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
438  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
439  }
440  }
441 
442  /* DATETIME */
443  public static class Datetime extends SqlFunction {
444  public Datetime() {
445  super("DATETIME",
446  SqlKind.OTHER_FUNCTION,
447  null,
448  null,
449  OperandTypes.STRING,
450  SqlFunctionCategory.SYSTEM);
451  }
452 
453  @Override
454  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
455  assert opBinding.getOperandCount() == 1;
456  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
457  return typeFactory.createSqlType(
458  SqlTypeName.TIMESTAMP, opBinding.getOperandType(0).getPrecision());
459  }
460  }
461 
462  /* Postgres-style EXTRACT */
463  public static class PgExtract extends SqlFunction {
464  public PgExtract() {
465  super("PG_EXTRACT",
466  SqlKind.OTHER_FUNCTION,
467  null,
468  null,
469  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.DATETIME),
470  SqlFunctionCategory.SYSTEM);
471  }
472 
473  @Override
474  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
475  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
476  return typeFactory.createTypeWithNullability(
477  typeFactory.createSqlType(SqlTypeName.BIGINT),
478  opBinding.getOperandType(1).isNullable());
479  }
480  }
481 
482  public static class Datepart extends SqlFunction {
483  public Datepart() {
484  super("DATEPART",
485  SqlKind.OTHER_FUNCTION,
486  null,
487  null,
488  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.DATETIME),
489  SqlFunctionCategory.TIMEDATE);
490  }
491 
492  @Override
493  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
494  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
495  return typeFactory.createTypeWithNullability(
496  typeFactory.createSqlType(SqlTypeName.BIGINT),
497  opBinding.getOperandType(1).isNullable());
498  }
499  }
500 
501  public static class Dateadd extends SqlFunction {
502  public Dateadd() {
503  super("DATEADD",
504  SqlKind.OTHER_FUNCTION,
505  null,
506  null,
507  OperandTypes.family(SqlTypeFamily.STRING,
508  SqlTypeFamily.INTEGER,
509  SqlTypeFamily.DATETIME),
510  SqlFunctionCategory.TIMEDATE);
511  }
512 
513  @Override
514  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
515  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
516  return typeFactory.createTypeWithNullability(
517  typeFactory.createSqlType(
518  SqlTypeName.TIMESTAMP, opBinding.getOperandType(2).getPrecision()),
519  opBinding.getOperandType(2).isNullable());
520  }
521  }
522 
523  public static class Datediff extends SqlFunction {
524  public Datediff() {
525  super("DATEDIFF",
526  SqlKind.OTHER_FUNCTION,
527  null,
528  null,
529  OperandTypes.family(SqlTypeFamily.STRING,
530  SqlTypeFamily.DATETIME,
531  SqlTypeFamily.DATETIME),
532  SqlFunctionCategory.TIMEDATE);
533  }
534 
535  @Override
536  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
537  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
538  return typeFactory.createTypeWithNullability(
539  typeFactory.createSqlType(SqlTypeName.BIGINT),
540  opBinding.getOperandType(1).isNullable()
541  || opBinding.getOperandType(2).isNullable());
542  }
543  }
544 
545  /* Postgres-style DATE_TRUNC */
546  public static class PgDateTrunc extends SqlFunction {
547  public PgDateTrunc() {
548  super("PG_DATE_TRUNC",
549  SqlKind.OTHER_FUNCTION,
550  null,
551  null,
552  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.DATETIME),
553  SqlFunctionCategory.SYSTEM);
554  }
555 
556  @Override
557  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
558  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
559  return typeFactory.createTypeWithNullability(
560  typeFactory.createSqlType(
561  SqlTypeName.TIMESTAMP, opBinding.getOperandType(1).getPrecision()),
562  opBinding.getOperandType(1).isNullable());
563  }
564  }
565 
566  public static class Length extends SqlFunction {
567  public Length() {
568  super("LENGTH",
569  SqlKind.OTHER_FUNCTION,
570  null,
571  null,
572  OperandTypes.STRING,
573  SqlFunctionCategory.SYSTEM);
574  }
575 
576  @Override
577  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
578  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
579  return typeFactory.createSqlType(SqlTypeName.INTEGER);
580  }
581  }
582 
583  public static class CharLength extends SqlFunction {
584  public CharLength() {
585  super("CHAR_LENGTH",
586  SqlKind.OTHER_FUNCTION,
587  null,
588  null,
589  OperandTypes.STRING,
590  SqlFunctionCategory.SYSTEM);
591  }
592 
593  @Override
594  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
595  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
596  return typeFactory.createSqlType(SqlTypeName.INTEGER);
597  }
598  }
599 
600  public static class KeyForString extends SqlFunction {
601  public KeyForString() {
602  super("KEY_FOR_STRING",
603  SqlKind.OTHER_FUNCTION,
604  null,
605  null,
606  OperandTypes.STRING,
607  SqlFunctionCategory.SYSTEM);
608  }
609 
610  @Override
611  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
612  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
613  return typeFactory.createTypeWithNullability(
614  typeFactory.createSqlType(SqlTypeName.INTEGER),
615  opBinding.getOperandType(0).isNullable());
616  }
617  }
618 
619  public static class ArrayLength extends SqlFunction {
620  public ArrayLength() {
621  super("ARRAY_LENGTH",
622  SqlKind.OTHER_FUNCTION,
623  null,
624  null,
625  OperandTypes.ARRAY,
626  SqlFunctionCategory.SYSTEM);
627  }
628 
629  @Override
630  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
631  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
632  return typeFactory.createSqlType(SqlTypeName.INTEGER);
633  }
634  }
635 
636  public static class PgILike extends SqlFunction {
637  public PgILike() {
638  super("PG_ILIKE",
639  SqlKind.OTHER_FUNCTION,
640  null,
641  null,
642  OperandTypes.family(getSignatureFamilies(), new EscapeOptional()),
643  SqlFunctionCategory.SYSTEM);
644  }
645 
646  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
647  java.util.ArrayList<SqlTypeFamily> families =
648  new java.util.ArrayList<SqlTypeFamily>();
649  families.add(SqlTypeFamily.STRING);
650  families.add(SqlTypeFamily.STRING);
651  families.add(SqlTypeFamily.STRING);
652  return families;
653  }
654 
655  private static class EscapeOptional
656  implements java.util.function.Predicate<Integer>, Predicate<Integer> {
657  @Override
658  public boolean test(Integer t) {
659  return apply(t);
660  }
661 
662  @Override
663  public boolean apply(Integer t) {
664  return t == 2;
665  }
666  }
667 
668  @Override
669  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
670  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
671  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
672  }
673  }
674 
675  public static class RegexpLike extends SqlFunction {
676  public RegexpLike() {
677  super("REGEXP_LIKE",
678  SqlKind.OTHER_FUNCTION,
679  null,
680  null,
681  OperandTypes.family(getSignatureFamilies(), new EscapeOptional()),
682  SqlFunctionCategory.SYSTEM);
683  }
684 
685  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
686  java.util.ArrayList<SqlTypeFamily> families =
687  new java.util.ArrayList<SqlTypeFamily>();
688  families.add(SqlTypeFamily.STRING);
689  families.add(SqlTypeFamily.STRING);
690  families.add(SqlTypeFamily.STRING);
691  return families;
692  }
693 
694  private static class EscapeOptional
695  implements java.util.function.Predicate<Integer>, Predicate<Integer> {
696  @Override
697  public boolean test(Integer t) {
698  return apply(t);
699  }
700 
701  public boolean apply(Integer t) {
702  return t == 2;
703  }
704  }
705 
706  @Override
707  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
708  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
709  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
710  }
711  }
712 
713  public static class Likely extends SqlFunction {
714  public Likely() {
715  super("LIKELY",
716  SqlKind.OTHER_FUNCTION,
717  null,
718  null,
719  OperandTypes.BOOLEAN,
720  SqlFunctionCategory.SYSTEM);
721  }
722 
723  @Override
724  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
725  return opBinding.getOperandType(0);
726  }
727  }
728 
729  public static class Unlikely extends SqlFunction {
730  public Unlikely() {
731  super("UNLIKELY",
732  SqlKind.OTHER_FUNCTION,
733  null,
734  null,
735  OperandTypes.BOOLEAN,
736  SqlFunctionCategory.SYSTEM);
737  }
738 
739  @Override
740  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
741  return opBinding.getOperandType(0);
742  }
743  }
744 
745  public static class Sign extends SqlFunction {
746  public Sign() {
747  super("SIGN",
748  SqlKind.OTHER_FUNCTION,
749  null,
750  null,
751  OperandTypes.NUMERIC,
752  SqlFunctionCategory.NUMERIC);
753  }
754 
755  @Override
756  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
757  return opBinding.getOperandType(0);
758  }
759  }
760 
761  static class Truncate extends SqlFunction {
763  super("TRUNCATE",
764  SqlKind.OTHER_FUNCTION,
765  null,
766  null,
767  OperandTypes.family(signature()),
768  SqlFunctionCategory.NUMERIC);
769  }
770 
771  @Override
772  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
773  assert opBinding.getOperandCount() == 2;
774  return opBinding.getOperandType(0);
775  }
776 
777  private static java.util.List<SqlTypeFamily> signature() {
778  java.util.List<SqlTypeFamily> truncate_sig =
779  new java.util.ArrayList<SqlTypeFamily>();
780  truncate_sig.add(SqlTypeFamily.NUMERIC);
781  truncate_sig.add(SqlTypeFamily.INTEGER);
782  return truncate_sig;
783  }
784  }
785 
786  static class ST_Contains extends SqlFunction {
788  super("ST_Contains",
789  SqlKind.OTHER_FUNCTION,
790  null,
791  null,
792  OperandTypes.family(signature()),
793  SqlFunctionCategory.SYSTEM);
794  }
795 
796  @Override
797  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
798  assert opBinding.getOperandCount() == 2;
799  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
800  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
801  }
802 
803  private static java.util.List<SqlTypeFamily> signature() {
804  java.util.List<SqlTypeFamily> st_contains_sig =
805  new java.util.ArrayList<SqlTypeFamily>();
806  st_contains_sig.add(SqlTypeFamily.ANY);
807  st_contains_sig.add(SqlTypeFamily.ANY);
808  return st_contains_sig;
809  }
810  }
811 
812  static class ST_Intersects extends SqlFunction {
814  super("ST_Intersects",
815  SqlKind.OTHER_FUNCTION,
816  null,
817  null,
818  OperandTypes.family(signature()),
819  SqlFunctionCategory.SYSTEM);
820  }
821 
822  @Override
823  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
824  assert opBinding.getOperandCount() == 2;
825  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
826  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
827  }
828 
829  private static java.util.List<SqlTypeFamily> signature() {
830  java.util.List<SqlTypeFamily> st_intersects_sig =
831  new java.util.ArrayList<SqlTypeFamily>();
832  st_intersects_sig.add(SqlTypeFamily.ANY);
833  st_intersects_sig.add(SqlTypeFamily.ANY);
834  return st_intersects_sig;
835  }
836  }
837 
838  static class ST_Disjoint extends SqlFunction {
840  super("ST_Disjoint",
841  SqlKind.OTHER_FUNCTION,
842  null,
843  null,
844  OperandTypes.family(signature()),
845  SqlFunctionCategory.SYSTEM);
846  }
847 
848  @Override
849  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
850  assert opBinding.getOperandCount() == 2;
851  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
852  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
853  }
854 
855  private static java.util.List<SqlTypeFamily> signature() {
856  java.util.List<SqlTypeFamily> st_disjoint_sig =
857  new java.util.ArrayList<SqlTypeFamily>();
858  st_disjoint_sig.add(SqlTypeFamily.ANY);
859  st_disjoint_sig.add(SqlTypeFamily.ANY);
860  return st_disjoint_sig;
861  }
862  }
863 
864  static class ST_Within extends SqlFunction {
866  super("ST_Within",
867  SqlKind.OTHER_FUNCTION,
868  null,
869  null,
870  OperandTypes.family(signature()),
871  SqlFunctionCategory.SYSTEM);
872  }
873 
874  @Override
875  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
876  assert opBinding.getOperandCount() == 2;
877  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
878  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
879  }
880 
881  private static java.util.List<SqlTypeFamily> signature() {
882  java.util.List<SqlTypeFamily> st_within_sig =
883  new java.util.ArrayList<SqlTypeFamily>();
884  st_within_sig.add(SqlTypeFamily.ANY);
885  st_within_sig.add(SqlTypeFamily.ANY);
886  return st_within_sig;
887  }
888  }
889 
890  static class ST_DWithin extends SqlFunction {
892  super("ST_DWithin",
893  SqlKind.OTHER_FUNCTION,
894  null,
895  null,
896  OperandTypes.family(signature()),
897  SqlFunctionCategory.SYSTEM);
898  }
899 
900  @Override
901  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
902  assert opBinding.getOperandCount() == 3;
903  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
904  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
905  }
906 
907  private static java.util.List<SqlTypeFamily> signature() {
908  java.util.List<SqlTypeFamily> st_dwithin_sig =
909  new java.util.ArrayList<SqlTypeFamily>();
910  st_dwithin_sig.add(SqlTypeFamily.ANY);
911  st_dwithin_sig.add(SqlTypeFamily.ANY);
912  st_dwithin_sig.add(SqlTypeFamily.NUMERIC);
913  return st_dwithin_sig;
914  }
915  }
916 
917  static class ST_DFullyWithin extends SqlFunction {
919  super("ST_DFullyWithin",
920  SqlKind.OTHER_FUNCTION,
921  null,
922  null,
923  OperandTypes.family(signature()),
924  SqlFunctionCategory.SYSTEM);
925  }
926 
927  @Override
928  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
929  assert opBinding.getOperandCount() == 3;
930  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
931  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
932  }
933 
934  private static java.util.List<SqlTypeFamily> signature() {
935  java.util.List<SqlTypeFamily> st_dwithin_sig =
936  new java.util.ArrayList<SqlTypeFamily>();
937  st_dwithin_sig.add(SqlTypeFamily.ANY);
938  st_dwithin_sig.add(SqlTypeFamily.ANY);
939  st_dwithin_sig.add(SqlTypeFamily.NUMERIC);
940  return st_dwithin_sig;
941  }
942  }
943 
944  static class ST_Distance extends SqlFunction {
946  super("ST_Distance",
947  SqlKind.OTHER_FUNCTION,
948  null,
949  null,
950  OperandTypes.family(signature()),
951  SqlFunctionCategory.SYSTEM);
952  }
953 
954  @Override
955  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
956  assert opBinding.getOperandCount() == 2;
957  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
958  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
959  }
960 
961  private static java.util.List<SqlTypeFamily> signature() {
962  java.util.List<SqlTypeFamily> st_distance_sig =
963  new java.util.ArrayList<SqlTypeFamily>();
964  st_distance_sig.add(SqlTypeFamily.ANY);
965  st_distance_sig.add(SqlTypeFamily.ANY);
966  return st_distance_sig;
967  }
968  }
969 
970  static class ST_MaxDistance extends SqlFunction {
972  super("ST_MaxDistance",
973  SqlKind.OTHER_FUNCTION,
974  null,
975  null,
976  OperandTypes.family(signature()),
977  SqlFunctionCategory.SYSTEM);
978  }
979 
980  @Override
981  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
982  assert opBinding.getOperandCount() == 2;
983  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
984  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
985  }
986 
987  private static java.util.List<SqlTypeFamily> signature() {
988  java.util.List<SqlTypeFamily> st_maxdistance_sig =
989  new java.util.ArrayList<SqlTypeFamily>();
990  st_maxdistance_sig.add(SqlTypeFamily.ANY);
991  st_maxdistance_sig.add(SqlTypeFamily.ANY);
992  return st_maxdistance_sig;
993  }
994  }
995 
996  static class ST_GeogFromText extends SqlFunction {
998  super("ST_GeogFromText",
999  SqlKind.OTHER_FUNCTION,
1000  null,
1001  null,
1002  OperandTypes.or(OperandTypes.family(SqlTypeFamily.ANY),
1003  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER)),
1004  SqlFunctionCategory.SYSTEM);
1005  }
1006 
1007  @Override
1008  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1009  assert opBinding.getOperandCount() == 1;
1010  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1011  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1012  }
1013  }
1014 
1015  static class ST_GeomFromText extends SqlFunction {
1017  super("ST_GeomFromText",
1018  SqlKind.OTHER_FUNCTION,
1019  null,
1020  null,
1021  OperandTypes.or(OperandTypes.family(SqlTypeFamily.ANY),
1022  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER)),
1023  SqlFunctionCategory.SYSTEM);
1024  }
1025 
1026  @Override
1027  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1028  assert opBinding.getOperandCount() == 1;
1029  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1030  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1031  }
1032  }
1033 
1034  static class ST_Transform extends SqlFunction {
1036  super("ST_Transform",
1037  SqlKind.OTHER_FUNCTION,
1038  null,
1039  null,
1040  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER),
1041  SqlFunctionCategory.SYSTEM);
1042  }
1043 
1044  @Override
1045  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1046  assert opBinding.getOperandCount() == 1;
1047  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1048  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1049  }
1050  }
1051 
1052  static class ST_X extends SqlFunction {
1053  ST_X() {
1054  super("ST_X",
1055  SqlKind.OTHER_FUNCTION,
1056  null,
1057  null,
1058  OperandTypes.family(SqlTypeFamily.ANY),
1059  SqlFunctionCategory.SYSTEM);
1060  }
1061 
1062  @Override
1063  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1064  assert opBinding.getOperandCount() == 1;
1065  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1066  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1067  }
1068  }
1069 
1070  static class ST_Y extends SqlFunction {
1071  ST_Y() {
1072  super("ST_Y",
1073  SqlKind.OTHER_FUNCTION,
1074  null,
1075  null,
1076  OperandTypes.family(SqlTypeFamily.ANY),
1077  SqlFunctionCategory.SYSTEM);
1078  }
1079 
1080  @Override
1081  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1082  assert opBinding.getOperandCount() == 1;
1083  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1084  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1085  }
1086  }
1087 
1088  static class ST_XMin extends SqlFunction {
1090  super("ST_XMin",
1091  SqlKind.OTHER_FUNCTION,
1092  null,
1093  null,
1094  OperandTypes.family(SqlTypeFamily.ANY),
1095  SqlFunctionCategory.SYSTEM);
1096  }
1097 
1098  @Override
1099  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1100  assert opBinding.getOperandCount() == 1;
1101  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1102  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1103  }
1104  }
1105 
1106  static class ST_XMax extends SqlFunction {
1108  super("ST_XMax",
1109  SqlKind.OTHER_FUNCTION,
1110  null,
1111  null,
1112  OperandTypes.family(SqlTypeFamily.ANY),
1113  SqlFunctionCategory.SYSTEM);
1114  }
1115 
1116  @Override
1117  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1118  assert opBinding.getOperandCount() == 1;
1119  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1120  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1121  }
1122  }
1123 
1124  static class ST_YMin extends SqlFunction {
1126  super("ST_YMin",
1127  SqlKind.OTHER_FUNCTION,
1128  null,
1129  null,
1130  OperandTypes.family(SqlTypeFamily.ANY),
1131  SqlFunctionCategory.SYSTEM);
1132  }
1133 
1134  @Override
1135  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1136  assert opBinding.getOperandCount() == 1;
1137  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1138  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1139  }
1140  }
1141 
1142  static class ST_YMax extends SqlFunction {
1144  super("ST_YMax",
1145  SqlKind.OTHER_FUNCTION,
1146  null,
1147  null,
1148  OperandTypes.family(SqlTypeFamily.ANY),
1149  SqlFunctionCategory.SYSTEM);
1150  }
1151 
1152  @Override
1153  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1154  assert opBinding.getOperandCount() == 1;
1155  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1156  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1157  }
1158  }
1159 
1160  static class ST_PointN extends SqlFunction {
1162  super("ST_PointN",
1163  SqlKind.OTHER_FUNCTION,
1164  null,
1165  null,
1166  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER),
1167  SqlFunctionCategory.SYSTEM);
1168  }
1169 
1170  @Override
1171  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1172  assert opBinding.getOperandCount() == 1;
1173  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1174  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1175  }
1176  }
1177 
1178  static class ST_EndPoint extends SqlFunction {
1180  super("ST_EndPoint",
1181  SqlKind.OTHER_FUNCTION,
1182  null,
1183  null,
1184  OperandTypes.family(SqlTypeFamily.ANY),
1185  SqlFunctionCategory.SYSTEM);
1186  }
1187 
1188  @Override
1189  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1190  assert opBinding.getOperandCount() == 1;
1191  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1192  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1193  }
1194  }
1195 
1196  static class ST_StartPoint extends SqlFunction {
1198  super("ST_StartPoint",
1199  SqlKind.OTHER_FUNCTION,
1200  null,
1201  null,
1202  OperandTypes.family(SqlTypeFamily.ANY),
1203  SqlFunctionCategory.SYSTEM);
1204  }
1205 
1206  @Override
1207  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1208  assert opBinding.getOperandCount() == 1;
1209  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1210  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1211  }
1212  }
1213 
1214  static class ST_Length extends SqlFunction {
1216  super("ST_Length",
1217  SqlKind.OTHER_FUNCTION,
1218  null,
1219  null,
1220  OperandTypes.family(SqlTypeFamily.ANY),
1221  SqlFunctionCategory.SYSTEM);
1222  }
1223 
1224  @Override
1225  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1226  assert opBinding.getOperandCount() == 1;
1227  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1228  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1229  }
1230  }
1231 
1232  static class ST_Perimeter extends SqlFunction {
1234  super("ST_Perimeter",
1235  SqlKind.OTHER_FUNCTION,
1236  null,
1237  null,
1238  OperandTypes.family(SqlTypeFamily.ANY),
1239  SqlFunctionCategory.SYSTEM);
1240  }
1241 
1242  @Override
1243  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1244  assert opBinding.getOperandCount() == 1;
1245  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1246  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1247  }
1248  }
1249 
1250  static class ST_Area extends SqlFunction {
1252  super("ST_Area",
1253  SqlKind.OTHER_FUNCTION,
1254  null,
1255  null,
1256  OperandTypes.family(SqlTypeFamily.ANY),
1257  SqlFunctionCategory.SYSTEM);
1258  }
1259 
1260  @Override
1261  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1262  assert opBinding.getOperandCount() == 1;
1263  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1264  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1265  }
1266  }
1267 
1268  static class ST_NPoints extends SqlFunction {
1270  super("ST_NPoints",
1271  SqlKind.OTHER_FUNCTION,
1272  null,
1273  null,
1274  OperandTypes.family(SqlTypeFamily.ANY),
1275  SqlFunctionCategory.SYSTEM);
1276  }
1277 
1278  @Override
1279  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1280  assert opBinding.getOperandCount() == 1;
1281  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1282  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1283  }
1284  }
1285 
1286  static class ST_NRings extends SqlFunction {
1288  super("ST_NRings",
1289  SqlKind.OTHER_FUNCTION,
1290  null,
1291  null,
1292  OperandTypes.family(SqlTypeFamily.ANY),
1293  SqlFunctionCategory.SYSTEM);
1294  }
1295 
1296  @Override
1297  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1298  assert opBinding.getOperandCount() == 1;
1299  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1300  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1301  }
1302  }
1303 
1304  static class ST_SRID extends SqlFunction {
1306  super("ST_SRID",
1307  SqlKind.OTHER_FUNCTION,
1308  null,
1309  null,
1310  OperandTypes.family(SqlTypeFamily.ANY),
1311  SqlFunctionCategory.SYSTEM);
1312  }
1313 
1314  @Override
1315  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1316  assert opBinding.getOperandCount() == 1;
1317  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1318  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1319  }
1320  }
1321 
1322  static class ST_SetSRID extends SqlFunction {
1324  super("ST_SetSRID",
1325  SqlKind.OTHER_FUNCTION,
1326  null,
1327  null,
1328  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER),
1329  SqlFunctionCategory.SYSTEM);
1330  }
1331 
1332  @Override
1333  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1334  assert opBinding.getOperandCount() == 1;
1335  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1336  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1337  }
1338  }
1339 
1340  static class ST_Point extends SqlFunction {
1342  super("ST_Point",
1343  SqlKind.OTHER_FUNCTION,
1344  null,
1345  null,
1346  OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.NUMERIC),
1347  SqlFunctionCategory.SYSTEM);
1348  }
1349 
1350  @Override
1351  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1352  assert opBinding.getOperandCount() == 2;
1353  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1354  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1355  }
1356  }
1357 
1358  static class CastToGeography extends SqlFunction {
1360  super("CastToGeography",
1361  SqlKind.OTHER_FUNCTION,
1362  null,
1363  null,
1364  OperandTypes.family(SqlTypeFamily.ANY),
1365  SqlFunctionCategory.SYSTEM);
1366  }
1367 
1368  @Override
1369  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1370  assert opBinding.getOperandCount() == 1;
1371  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1372  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1373  }
1374  }
1375 
1376  /* OFFSET_IN_FRAGMENT() */
1377  public static class OffsetInFragment extends SqlFunction {
1378  public OffsetInFragment() {
1379  super("OFFSET_IN_FRAGMENT",
1380  SqlKind.OTHER_FUNCTION,
1381  null,
1382  null,
1383  OperandTypes.NILADIC,
1384  SqlFunctionCategory.SYSTEM);
1385  }
1386 
1387  @Override
1388  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1389  assert opBinding.getOperandCount() == 0;
1390  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1391  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1392  }
1393  }
1394 
1395  static class ApproxCountDistinct extends SqlAggFunction {
1397  super("APPROX_COUNT_DISTINCT",
1398  null,
1399  SqlKind.OTHER_FUNCTION,
1400  null,
1401  null,
1402  OperandTypes.or(OperandTypes.family(SqlTypeFamily.ANY),
1403  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER)),
1404  SqlFunctionCategory.SYSTEM);
1405  }
1406 
1407  @Override
1408  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1409  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1410  return typeFactory.createSqlType(SqlTypeName.BIGINT);
1411  }
1412  }
1413 
1414  static class MapDAvg extends SqlAggFunction {
1416  super("AVG",
1417  null,
1418  SqlKind.OTHER_FUNCTION,
1419  null,
1420  null,
1421  OperandTypes.family(SqlTypeFamily.NUMERIC),
1422  SqlFunctionCategory.SYSTEM);
1423  }
1424 
1425  @Override
1426  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1427  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1428  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1429  }
1430  }
1431 
1432  public static class Sample extends SqlAggFunction {
1433  public Sample() {
1434  super("SAMPLE",
1435  SqlKind.OTHER_FUNCTION,
1436  null,
1437  null,
1438  OperandTypes.ANY,
1439  SqlFunctionCategory.SYSTEM);
1440  }
1441 
1442  @Override
1443  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1444  return opBinding.getOperandType(0);
1445  }
1446  }
1447 
1448  // for backwards compatibility
1449  public static class LastSample extends SqlAggFunction {
1450  public LastSample() {
1451  super("LAST_SAMPLE",
1452  SqlKind.OTHER_FUNCTION,
1453  null,
1454  null,
1455  OperandTypes.ANY,
1456  SqlFunctionCategory.SYSTEM);
1457  }
1458 
1459  @Override
1460  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1461  return opBinding.getOperandType(0);
1462  }
1463  }
1464 
1465  static class ExtFunction extends SqlFunction {
1466  ExtFunction(final String name, final ExtensionFunction sig) {
1467  super(name,
1468  SqlKind.OTHER_FUNCTION,
1469  null,
1470  null,
1471  OperandTypes.family(toSqlSignature(sig)),
1472  sig.isRowUdf() ? SqlFunctionCategory.SYSTEM
1473  : SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
1474  isRowUdf = sig.isRowUdf();
1475  if (isRowUdf) {
1476  ret = toSqlTypeName(sig.getRet());
1477  } else {
1478  ret = null;
1479  }
1480  }
1481 
1482  private static java.util.List<SqlTypeFamily> toSqlSignature(
1483  final ExtensionFunction sig) {
1484  java.util.List<SqlTypeFamily> sql_sig = new java.util.ArrayList<SqlTypeFamily>();
1485  boolean isRowUdf = sig.isRowUdf();
1486  for (int arg_idx = 0; arg_idx < sig.getArgs().size(); ++arg_idx) {
1487  final ExtensionFunction.ExtArgumentType arg_type = sig.getArgs().get(arg_idx);
1488  if (isRowUdf) {
1489  sql_sig.add(toSqlTypeName(arg_type).getFamily());
1490  if (isPointerType(arg_type)) {
1491  ++arg_idx;
1492  }
1493  } else {
1494  if (isPointerType(arg_type)) {
1495  /* TODO: eliminate using getValueType */
1496  sql_sig.add(toSqlTypeName(getValueType(arg_type)).getFamily());
1497  } else {
1498  sql_sig.add(toSqlTypeName(arg_type).getFamily());
1499  }
1500  }
1501  }
1502  return sql_sig;
1503  }
1504 
1505  private static boolean isPointerType(final ExtensionFunction.ExtArgumentType type) {
1506  return type == ExtensionFunction.ExtArgumentType.PInt8
1507  || type == ExtensionFunction.ExtArgumentType.PInt16
1508  || type == ExtensionFunction.ExtArgumentType.PInt32
1509  || type == ExtensionFunction.ExtArgumentType.PInt64
1510  || type == ExtensionFunction.ExtArgumentType.PFloat
1511  || type == ExtensionFunction.ExtArgumentType.PDouble;
1512  }
1513 
1514  @Override
1515  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1516  if (isRowUdf) {
1517  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1518  return typeFactory.createTypeWithNullability(
1519  typeFactory.createSqlType(ret), true);
1520  } else {
1521  assert opBinding.getOperandCount() == 2;
1522  return opBinding.getCursorOperand(0);
1523  }
1524  }
1525 
1526  private static ExtensionFunction.ExtArgumentType getValueType(
1527  final ExtensionFunction.ExtArgumentType type) {
1528  switch (type) {
1529  case PInt8:
1530  return ExtensionFunction.ExtArgumentType.Int8;
1531  case PInt16:
1532  return ExtensionFunction.ExtArgumentType.Int16;
1533  case PInt32:
1534  return ExtensionFunction.ExtArgumentType.Int32;
1535  case PInt64:
1536  return ExtensionFunction.ExtArgumentType.Int64;
1537  case PFloat:
1538  return ExtensionFunction.ExtArgumentType.Float;
1539  case PDouble:
1540  return ExtensionFunction.ExtArgumentType.Double;
1541  }
1542  MAPDLOGGER.error("getValueType: no value for type " + type);
1543  assert false;
1544  return null;
1545  }
1546 
1547  private static SqlTypeName toSqlTypeName(
1548  final ExtensionFunction.ExtArgumentType type) {
1549  switch (type) {
1550  case Bool:
1551  return SqlTypeName.BOOLEAN;
1552  case Int8:
1553  return SqlTypeName.TINYINT;
1554  case Int16:
1555  return SqlTypeName.SMALLINT;
1556  case Int32:
1557  return SqlTypeName.INTEGER;
1558  case Int64:
1559  return SqlTypeName.BIGINT;
1560  case Float:
1561  return SqlTypeName.FLOAT;
1562  case Double:
1563  return SqlTypeName.DOUBLE;
1564  case PInt8:
1565  case PInt16:
1566  case PInt32:
1567  case PInt64:
1568  case PFloat:
1569  case PDouble:
1570  case ArrayInt8:
1571  case ArrayInt16:
1572  case ArrayInt32:
1573  case ArrayInt64:
1574  case ArrayFloat:
1575  case ArrayDouble:
1576  return SqlTypeName.ARRAY;
1577  case GeoPoint:
1578  case GeoLineString:
1579  case GeoPolygon:
1580  case GeoMultiPolygon:
1581  return SqlTypeName.GEOMETRY;
1582  case Cursor:
1583  return SqlTypeName.CURSOR;
1584  }
1585  MAPDLOGGER.error("toSqlTypeName: unknown type " + type);
1586  assert false;
1587  return null;
1588  }
1589  private final boolean isRowUdf;
1590  private final SqlTypeName ret;
1591  }
1592 
1593  //
1594  // Internal accessors for in-situ poly render queries
1595  //
1596  // The MapD_* varietals are deprecated. The OmniSci_Geo_* ones should be used
1597  // instead
1598  //
1599 
1600  static class MapD_GeoPolyBoundsPtr extends SqlFunction {
1602  super("MapD_GeoPolyBoundsPtr",
1603  SqlKind.OTHER_FUNCTION,
1604  null,
1605  null,
1606  OperandTypes.family(SqlTypeFamily.ANY),
1607  SqlFunctionCategory.SYSTEM);
1608  }
1609 
1610  @Override
1611  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1612  assert opBinding.getOperandCount() == 1;
1613  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1614  return typeFactory.createSqlType(SqlTypeName.BIGINT);
1615  }
1616  }
1617 
1618  static class MapD_GeoPolyRenderGroup extends SqlFunction {
1620  super("MapD_GeoPolyRenderGroup",
1621  SqlKind.OTHER_FUNCTION,
1622  null,
1623  null,
1624  OperandTypes.family(SqlTypeFamily.ANY),
1625  SqlFunctionCategory.SYSTEM);
1626  }
1627 
1628  @Override
1629  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1630  assert opBinding.getOperandCount() == 1;
1631  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1632  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1633  }
1634  }
1635 
1636  static class OmniSci_Geo_PolyBoundsPtr extends SqlFunction {
1638  super("OmniSci_Geo_PolyBoundsPtr",
1639  SqlKind.OTHER_FUNCTION,
1640  null,
1641  null,
1642  OperandTypes.family(SqlTypeFamily.ANY),
1643  SqlFunctionCategory.SYSTEM);
1644  }
1645 
1646  @Override
1647  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1648  assert opBinding.getOperandCount() == 1;
1649  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1650  return typeFactory.createSqlType(SqlTypeName.BIGINT);
1651  }
1652  }
1653 
1654  static class OmniSci_Geo_PolyRenderGroup extends SqlFunction {
1656  super("OmniSci_Geo_PolyRenderGroup",
1657  SqlKind.OTHER_FUNCTION,
1658  null,
1659  null,
1660  OperandTypes.family(SqlTypeFamily.ANY),
1661  SqlFunctionCategory.SYSTEM);
1662  }
1663 
1664  @Override
1665  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1666  assert opBinding.getOperandCount() == 1;
1667  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1668  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1669  }
1670  }
1671 
1672  static class convert_meters_to_pixel_width extends SqlFunction {
1674  super("convert_meters_to_pixel_width",
1675  SqlKind.OTHER_FUNCTION,
1676  null,
1677  null,
1678  OperandTypes.family(SqlTypeFamily.NUMERIC,
1679  SqlTypeFamily.ANY,
1680  SqlTypeFamily.NUMERIC,
1681  SqlTypeFamily.NUMERIC,
1682  SqlTypeFamily.NUMERIC,
1683  SqlTypeFamily.NUMERIC),
1684  SqlFunctionCategory.SYSTEM);
1685  }
1686 
1687  @Override
1688  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1689  assert opBinding.getOperandCount() == 6;
1690  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1691  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1692  }
1693  }
1694 
1695  static class convert_meters_to_pixel_height extends SqlFunction {
1697  super("convert_meters_to_pixel_height",
1698  SqlKind.OTHER_FUNCTION,
1699  null,
1700  null,
1701  OperandTypes.family(SqlTypeFamily.NUMERIC,
1702  SqlTypeFamily.ANY,
1703  SqlTypeFamily.NUMERIC,
1704  SqlTypeFamily.NUMERIC,
1705  SqlTypeFamily.NUMERIC,
1706  SqlTypeFamily.NUMERIC),
1707  SqlFunctionCategory.SYSTEM);
1708  }
1709 
1710  @Override
1711  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1712  assert opBinding.getOperandCount() == 6;
1713  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1714  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
1715  }
1716  }
1717 
1718  static class is_point_in_view extends SqlFunction {
1720  super("is_point_in_view",
1721  SqlKind.OTHER_FUNCTION,
1722  null,
1723  null,
1724  OperandTypes.family(SqlTypeFamily.ANY,
1725  SqlTypeFamily.NUMERIC,
1726  SqlTypeFamily.NUMERIC,
1727  SqlTypeFamily.NUMERIC,
1728  SqlTypeFamily.NUMERIC),
1729  SqlFunctionCategory.SYSTEM);
1730  }
1731 
1732  @Override
1733  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1734  assert opBinding.getOperandCount() == 5;
1735  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1736  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1737  }
1738  }
1739 
1740  static class is_point_size_in_view extends SqlFunction {
1742  super("is_point_size_in_view",
1743  SqlKind.OTHER_FUNCTION,
1744  null,
1745  null,
1746  OperandTypes.family(SqlTypeFamily.ANY,
1747  SqlTypeFamily.NUMERIC,
1748  SqlTypeFamily.NUMERIC,
1749  SqlTypeFamily.NUMERIC,
1750  SqlTypeFamily.NUMERIC,
1751  SqlTypeFamily.NUMERIC),
1752  SqlFunctionCategory.SYSTEM);
1753  }
1754 
1755  @Override
1756  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1757  assert opBinding.getOperandCount() == 6;
1758  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1759  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1760  }
1761  }
1762 
1763  public static class usTimestamp extends SqlFunction {
1764  public usTimestamp() {
1765  super("usTIMESTAMP",
1766  SqlKind.OTHER_FUNCTION,
1767  null,
1768  null,
1769  OperandTypes.STRING,
1770  SqlFunctionCategory.SYSTEM);
1771  }
1772 
1773  @Override
1774  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1775  assert opBinding.getOperandCount() == 1;
1776  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1777  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 6);
1778  }
1779  }
1780 
1781  public static class nsTimestamp extends SqlFunction {
1782  public nsTimestamp() {
1783  super("nsTIMESTAMP",
1784  SqlKind.OTHER_FUNCTION,
1785  null,
1786  null,
1787  OperandTypes.STRING,
1788  SqlFunctionCategory.SYSTEM);
1789  }
1790 
1791  @Override
1792  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1793  assert opBinding.getOperandCount() == 1;
1794  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1795  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 9);
1796  }
1797  }
1798 }
1799 
1800 // End MapDSqlOperatorTable.java
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double convert_meters_to_pixel_width(const double meters, int8_t *p, const int64_t psize, const int32_t ic, const int32_t isr, const int32_t osr, const double min_lon, const double max_lon, const int32_t img_width, const double min_width)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static boolean isPointerType(final ExtensionFunction.ExtArgumentType type)
ExtFunction(final String name, final ExtensionFunction sig)
static java.util.List< SqlTypeFamily > signature()
EXTENSION_INLINE int32_t OmniSci_Geo_PolyRenderGroup(int32_t render_group)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double ST_XMax(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static java.util.List< SqlTypeFamily > signature()
static java.util.List< SqlTypeFamily > getSignatureFamilies()
EXTENSION_NOINLINE bool is_point_size_in_view(int8_t *p, const int64_t psize, const int32_t ic, const double meters, const double min_lon, const double max_lon, const double min_lat, const double max_lat)
EXTENSION_NOINLINE double ST_YMax(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double ST_XMin(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
static java.util.List< SqlTypeFamily > getSignatureFamilies()
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double convert_meters_to_pixel_height(const double meters, int8_t *p, const int64_t psize, const int32_t ic, const int32_t isr, const int32_t osr, const double min_lat, const double max_lat, const int32_t img_height, const double min_height)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_INLINE int32_t MapD_GeoPolyRenderGroup(int32_t render_group)
EXTENSION_INLINE int64_t OmniSci_Geo_PolyBoundsPtr(double *bounds, int64_t size)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static SqlTypeName toSqlTypeName(final ExtensionFunction.ExtArgumentType type)
static final SqlArrayValueConstructorAllowingEmpty ARRAY_VALUE_CONSTRUCTOR
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double ST_YMin(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_INLINE int64_t MapD_GeoPolyBoundsPtr(double *bounds, int64_t size)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE bool is_point_in_view(int8_t *p, const int64_t psize, const int32_t ic, const double min_lon, const double max_lon, const double min_lat, const double max_lat)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure)
RelDataType getComponentType(RelDataTypeFactory typeFactory, List< RelDataType > argTypes)
int64_t const int32_t sz assert(dest)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static java.util.List< SqlTypeFamily > signature()
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static java.util.List< SqlTypeFamily > toSqlSignature(final ExtensionFunction sig)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_INLINE int32_t ST_NRings(int32_t *poly_ring_sizes, int64_t poly_num_rings)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double Truncate(const double x, const int32_t y)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static void addUDF(MapDSqlOperatorTable opTab, final Map< String, ExtensionFunction > extSigs)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static ExtensionFunction.ExtArgumentType getValueType(final ExtensionFunction.ExtArgumentType type)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_INLINE int32_t ST_NPoints(int8_t *coords, int64_t coords_sz, int32_t ic)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
void lookupOperatorOverloads(SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax, List< SqlOperator > operatorList, SqlNameMatcher nameMatcher)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType inferReturnType(SqlOperatorBinding opBinding)