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