OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyDBSqlOperatorTable.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
17 package com.mapd.calcite.parser;
18 
19 import com.google.common.base.Predicate;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.Multimap;
24 
25 import org.apache.calcite.rel.metadata.RelColumnMapping;
26 import org.apache.calcite.rel.type.RelDataType;
27 import org.apache.calcite.rel.type.RelDataTypeFactory;
28 import org.apache.calcite.rel.type.RelDataTypeFactory.FieldInfoBuilder;
29 import org.apache.calcite.schema.FunctionParameter;
30 import org.apache.calcite.schema.TranslatableTable;
31 import org.apache.calcite.sql.SqlAggFunction;
32 import org.apache.calcite.sql.SqlCall;
33 import org.apache.calcite.sql.SqlCallBinding;
34 import org.apache.calcite.sql.SqlFunction;
35 import org.apache.calcite.sql.SqlFunctionCategory;
36 import org.apache.calcite.sql.SqlIdentifier;
37 import org.apache.calcite.sql.SqlKind;
38 import org.apache.calcite.sql.SqlLiteral;
39 import org.apache.calcite.sql.SqlNode;
41 import org.apache.calcite.sql.SqlOperatorBinding;
42 import org.apache.calcite.sql.SqlOperatorTable;
43 import org.apache.calcite.sql.SqlSyntax;
44 import org.apache.calcite.sql.SqlTableFunction;
45 import org.apache.calcite.sql.fun.SqlArrayValueConstructor;
46 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
47 import org.apache.calcite.sql.parser.SqlParserPos;
48 import org.apache.calcite.sql.type.OperandTypes;
49 import org.apache.calcite.sql.type.ReturnTypes;
50 import org.apache.calcite.sql.type.SameOperandTypeChecker;
51 import org.apache.calcite.sql.type.SqlReturnTypeInference;
52 import org.apache.calcite.sql.type.SqlTypeFamily;
53 import org.apache.calcite.sql.type.SqlTypeName;
54 import org.apache.calcite.sql.type.SqlTypeTransforms;
55 import org.apache.calcite.sql.type.SqlTypeUtil;
56 import org.apache.calcite.sql.util.ChainedSqlOperatorTable;
57 import org.apache.calcite.sql.util.ListSqlOperatorTable;
58 import org.apache.calcite.sql.util.ReflectiveSqlOperatorTable;
59 import org.apache.calcite.sql.validate.SqlNameMatcher;
60 import org.apache.calcite.util.Optionality;
61 import org.checkerframework.checker.nullness.qual.Nullable;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64 
65 import java.lang.reflect.Field;
66 import java.util.Arrays;
67 import java.util.HashSet;
68 import java.util.Iterator;
69 import java.util.List;
70 import java.util.Locale;
71 import java.util.Map;
72 import java.util.Set;
73 import java.util.stream.Collectors;
74 
75 class CaseInsensitiveListSqlOperatorTable extends ListSqlOperatorTable {
76  @Override
77  public void lookupOperatorOverloads(SqlIdentifier opName,
78  SqlFunctionCategory category,
79  SqlSyntax syntax,
80  List<SqlOperator> operatorList,
81  SqlNameMatcher nameMatcher) {
82  for (SqlOperator operator : this.getOperatorList()) {
83  if (operator.getSyntax() != syntax) {
84  continue;
85  }
86  if (!opName.isSimple()
87  || !nameMatcher.matches(operator.getName(), opName.getSimple())) {
88  continue;
89  }
90  SqlFunctionCategory functionCategory;
91  if (operator instanceof SqlFunction) {
92  functionCategory = ((SqlFunction) operator).getFunctionType();
93  } else {
94  functionCategory = SqlFunctionCategory.SYSTEM;
95  }
96  if (category != functionCategory
97  && category != SqlFunctionCategory.USER_DEFINED_FUNCTION) {
98  continue;
99  }
100  operatorList.add(operator);
101  }
102  }
103 }
104 
105 public class HeavyDBSqlOperatorTable extends ChainedSqlOperatorTable {
108 
109  static {
110  try {
111  // some nasty bit to remove the std APPROX_COUNT_DISTINCT function definition
112  {
113  Field f = ReflectiveSqlOperatorTable.class.getDeclaredField(
114  "caseSensitiveOperators");
115  f.setAccessible(true);
116  Multimap operators = (Multimap) f.get(SqlStdOperatorTable.instance());
117  for (Iterator i = operators.entries().iterator(); i.hasNext();) {
118  Map.Entry entry = (Map.Entry) i.next();
119  if (entry.getValue() == SqlStdOperatorTable.APPROX_COUNT_DISTINCT
120  || entry.getValue() == SqlStdOperatorTable.AVG
121  || entry.getValue() == SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR) {
122  i.remove();
123  }
124  }
125  }
126 
127  {
128  Field f = ReflectiveSqlOperatorTable.class.getDeclaredField(
129  "caseInsensitiveOperators");
130  f.setAccessible(true);
131  Multimap operators = (Multimap) f.get(SqlStdOperatorTable.instance());
132  for (Iterator i = operators.entries().iterator(); i.hasNext();) {
133  Map.Entry entry = (Map.Entry) i.next();
134  if (entry.getValue() == SqlStdOperatorTable.APPROX_COUNT_DISTINCT
135  || entry.getValue() == SqlStdOperatorTable.AVG
136  || entry.getValue() == SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR) {
137  i.remove();
138  }
139  }
140  }
141 
142  SqlStdOperatorTable.instance().register(ARRAY_VALUE_CONSTRUCTOR);
143 
144  } catch (Exception e) {
145  throw new RuntimeException(e);
146  }
147 
148  // register our approx count distinct against std table
149  // SqlStdOperatorTable.instance().register(new ApproxCountDistinct());
150  }
151 
152  final static Logger HEAVYDBLOGGER =
153  LoggerFactory.getLogger(HeavyDBSqlOperatorTable.class);
154 
159  // ~ Instance fields --------------------------------------------------------
160  private final ListSqlOperatorTable listOpTab;
161 
162  // ~ Constructors -----------------------------------------------------------
163  public HeavyDBSqlOperatorTable(SqlOperatorTable parentTable) {
164  super(ImmutableList.of(parentTable, new CaseInsensitiveListSqlOperatorTable()));
165  listOpTab = (ListSqlOperatorTable) tableList.get(1);
166  }
167 
173  public void addOperator(SqlOperator op) {
174  listOpTab.add(op);
175  }
176 
177  public void addUDF(final Map<String, ExtensionFunction> extSigs) {
178  // Don't use anonymous inner classes. They can't be instantiated
179  // using reflection when we are deserializing from JSON.
180  // addOperator(new RampFunction());
181  // addOperator(new DedupFunction());
182  addOperator(new MyUDFFunction());
183  addOperator(new PgUnnest());
184  addOperator(new Any());
185  addOperator(new All());
186  addOperator(new Now());
187  addOperator(new Datetime());
188  addOperator(new PgExtract());
189  addOperator(new Dateadd());
190  addOperator(new Datediff());
191  addOperator(new Datepart());
192  addOperator(new PgDateTrunc());
193  addOperator(new Length());
194  addOperator(new CharLength());
195  addOperator(new KeyForString());
196  addOperator(new SampleRatio());
197  addOperator(new WidthBucket());
198  addOperator(new LagInFrame());
199  addOperator(new LeadInFrame());
200  addOperator(new ArrayLength());
201  addOperator(new PgILike());
202  addOperator(new LTrim());
203  addOperator(new RTrim());
204  addOperator(new LPad());
205  addOperator(new RPad());
206  addOperator(new Replace());
207  addOperator(new Reverse());
208  addOperator(new Repeat());
209  addOperator(new SplitPart());
210  addOperator(new RegexpLike());
211  addOperator(new RegexpReplace());
212  addOperator(new RegexpSubstr());
213  addOperator(new RegexpMatch());
214  addOperator(new Base64Encode());
215  addOperator(new Base64Decode());
216  addOperator(new Likely());
217  addOperator(new Unlikely());
218  addOperator(new Sign());
219  addOperator(new Truncate());
220  addOperator(new ST_IsEmpty());
221  addOperator(new ST_IsValid());
222  addOperator(new ST_Contains());
223  addOperator(new ST_Equals());
224  addOperator(new ST_Intersects());
225  addOperator(new ST_Overlaps());
227  addOperator(new ST_Disjoint());
228  addOperator(new ST_Within());
229  addOperator(new ST_DWithin());
231  addOperator(new ST_Distance());
235  addOperator(new ST_Transform());
236  addOperator(new ST_X());
237  addOperator(new ST_Y());
238  addOperator(new ST_XMin());
239  addOperator(new ST_XMax());
240  addOperator(new ST_YMin());
241  addOperator(new ST_YMax());
242  addOperator(new ST_PointN());
243  addOperator(new ST_StartPoint());
244  addOperator(new ST_EndPoint());
245  addOperator(new ST_Length());
246  addOperator(new ST_Perimeter());
247  addOperator(new ST_Area());
248  addOperator(new ST_NPoints());
249  addOperator(new ST_NRings());
250  addOperator(new ST_SRID());
251  addOperator(new ST_SetSRID());
252  addOperator(new ST_Point());
253  addOperator(new ST_Centroid());
254  addOperator(new ST_Buffer());
256  addOperator(new ST_ConvexHull());
258  addOperator(new ST_Union());
259  addOperator(new ST_Difference());
261  addOperator(new EncodeText());
264  addOperator(new ApproxMedian());
267  addOperator(new MapDAvg());
268  addOperator(new Sample());
269  addOperator(new LastSample());
276  addOperator(new usTimestamp());
277  addOperator(new nsTimestamp());
278  if (extSigs == null) {
279  return;
280  }
281  HashSet<String> demangledNames = new HashSet<String>();
282  for (Map.Entry<String, ExtensionFunction> extSig : extSigs.entrySet()) {
283  final String demangledName = dropSuffix(extSig.getKey());
284  final String demangledNameArity = extSig.getValue().isTableUdf()
285  ? String.format("%s-%s-%s",
286  demangledName,
287  extSig.getValue().getArgs(),
288  extSig.getValue().getCursorFieldTypes())
289  : String.format("%s-%d", demangledName, extSig.getValue().getArgs().size());
290  if (demangledNames.contains(demangledNameArity)) {
291  continue;
292  }
293  demangledNames.add(demangledNameArity);
294  if (extSig.getValue().isRowUdf()) {
295  addOperator(new ExtFunction(demangledName, extSig.getValue()));
296  } else {
297  addOperator(new ExtTableFunction(demangledName, extSig.getValue()));
298  }
299  }
300  }
301 
302  private static String dropSuffix(final String str) {
303  int suffix_idx = str.indexOf("__");
304  if (suffix_idx == -1) {
305  return str;
306  }
307  assert suffix_idx > 0;
308  return str.substring(0, suffix_idx);
309  }
310 
311  //@Deprecated // to be removed before 2.0
312  // public static final SqlFunction LTRIM = SqlLibraryOperators.LTRIM;
313 
314  //@Deprecated // to be removed before 2.0
315  // public static final SqlFunction RTRIM = SqlLibraryOperators.RTRIM;
316 
318  extends SqlArrayValueConstructor {
319  @Override
320  protected RelDataType getComponentType(
321  RelDataTypeFactory typeFactory, List<RelDataType> argTypes) {
322  if (argTypes.isEmpty()) {
323  return typeFactory.createSqlType(SqlTypeName.NULL);
324  }
325  return super.getComponentType(typeFactory, argTypes);
326  }
327 
328  @Override
329  public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
330  if (callBinding.operands().isEmpty()) {
331  return true;
332  }
333  return super.checkOperandTypes(callBinding, throwOnFailure);
334  }
335  }
336 
340  public static class RampFunction extends SqlFunction {
341  public RampFunction() {
342  super("RAMP",
343  SqlKind.OTHER_FUNCTION,
344  null,
345  null,
346  OperandTypes.NUMERIC,
347  SqlFunctionCategory.USER_DEFINED_FUNCTION);
348  }
349 
350  @Override
351  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
352  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
353  return typeFactory.builder().add("I", SqlTypeName.INTEGER).build();
354  }
355  }
356 
360  public static class DedupFunction extends SqlFunction {
361  public DedupFunction() {
362  super("DEDUP",
363  SqlKind.OTHER_FUNCTION,
364  null,
365  null,
366  OperandTypes.VARIADIC,
367  SqlFunctionCategory.USER_DEFINED_FUNCTION);
368  }
369 
370  @Override
371  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
372  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
373  return typeFactory.builder().add("NAME", SqlTypeName.VARCHAR, 1024).build();
374  }
375  }
376 
381  public static class MyUDFFunction extends SqlFunction {
382  public MyUDFFunction() {
383  super("MyUDF",
384  SqlKind.OTHER_FUNCTION,
385  null,
386  null,
387  OperandTypes.STRING_STRING,
388  SqlFunctionCategory.SYSTEM);
389  }
390 
391  @Override
392  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
393  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
394  return typeFactory.createSqlType(SqlTypeName.BIGINT);
395  }
396  }
397 
398  /* Postgres-style UNNEST */
399  public static class PgUnnest extends SqlFunction {
400  public PgUnnest() {
401  super("PG_UNNEST",
402  SqlKind.OTHER_FUNCTION,
403  null,
404  null,
405  OperandTypes.ARRAY,
406  SqlFunctionCategory.SYSTEM);
407  }
408 
409  @Override
410  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
411  assert opBinding.getOperandCount() == 1;
412  RelDataType elem_type = opBinding.getOperandType(0).getComponentType();
413  assert elem_type != null;
414  return elem_type;
415  }
416  }
417 
418  /* ANY qualifier */
419  public static class Any extends SqlFunction {
420  public Any() {
421  super("PG_ANY",
422  SqlKind.OTHER_FUNCTION,
423  null,
424  null,
425  OperandTypes.ARRAY,
426  SqlFunctionCategory.SYSTEM);
427  }
428 
429  @Override
430  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
431  assert opBinding.getOperandCount() == 1;
432  RelDataType elem_type = opBinding.getOperandType(0).getComponentType();
433  assert elem_type != null;
434  return elem_type;
435  }
436  }
437 
438  /* ALL qualifier */
439  public static class All extends SqlFunction {
440  public All() {
441  super("PG_ALL",
442  SqlKind.OTHER_FUNCTION,
443  null,
444  null,
445  OperandTypes.ARRAY,
446  SqlFunctionCategory.SYSTEM);
447  }
448 
449  @Override
450  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
451  assert opBinding.getOperandCount() == 1;
452  RelDataType elem_type = opBinding.getOperandType(0).getComponentType();
453  assert elem_type != null;
454  return elem_type;
455  }
456  }
457 
458  /* NOW() */
459  public static class Now extends SqlFunction {
460  public Now() {
461  super("NOW",
462  SqlKind.OTHER_FUNCTION,
463  null,
464  null,
465  OperandTypes.NILADIC,
466  SqlFunctionCategory.SYSTEM);
467  }
468 
469  @Override
470  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
471  assert opBinding.getOperandCount() == 0;
472  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
473  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP);
474  }
475  }
476 
477  /* DATETIME */
478  public static class Datetime extends SqlFunction {
479  public Datetime() {
480  super("DATETIME",
481  SqlKind.OTHER_FUNCTION,
482  null,
483  null,
484  OperandTypes.STRING,
485  SqlFunctionCategory.SYSTEM);
486  }
487 
488  @Override
489  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
490  assert opBinding.getOperandCount() == 1;
491  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
492  return typeFactory.createSqlType(
493  SqlTypeName.TIMESTAMP, opBinding.getOperandType(0).getPrecision());
494  }
495  }
496 
497  /* Postgres-style EXTRACT */
498  public static class PgExtract extends SqlFunction {
499  public PgExtract() {
500  super("PG_EXTRACT",
501  SqlKind.OTHER_FUNCTION,
502  null,
503  null,
504  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.DATETIME),
505  SqlFunctionCategory.SYSTEM);
506  }
507 
508  @Override
509  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
510  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
511  return typeFactory.createTypeWithNullability(
512  typeFactory.createSqlType(SqlTypeName.BIGINT),
513  opBinding.getOperandType(1).isNullable());
514  }
515  }
516 
517  public static class Datepart extends SqlFunction {
518  public Datepart() {
519  super("DATEPART",
520  SqlKind.OTHER_FUNCTION,
521  null,
522  null,
523  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.DATETIME),
524  SqlFunctionCategory.TIMEDATE);
525  }
526 
527  @Override
528  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
529  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
530  return typeFactory.createTypeWithNullability(
531  typeFactory.createSqlType(SqlTypeName.BIGINT),
532  opBinding.getOperandType(1).isNullable());
533  }
534  }
535 
536  public static class Dateadd extends SqlFunction {
537  public Dateadd() {
538  super("DATEADD",
539  SqlKind.OTHER_FUNCTION,
540  null,
541  null,
542  OperandTypes.family(SqlTypeFamily.STRING,
543  SqlTypeFamily.INTEGER,
544  SqlTypeFamily.DATETIME),
545  SqlFunctionCategory.TIMEDATE);
546  }
547 
548  @Override
549  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
550  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
551  return typeFactory.createTypeWithNullability(
552  typeFactory.createSqlType(
553  SqlTypeName.TIMESTAMP, opBinding.getOperandType(2).getPrecision()),
554  opBinding.getOperandType(2).isNullable());
555  }
556  }
557 
558  public static class Datediff extends SqlFunction {
559  public Datediff() {
560  super("DATEDIFF",
561  SqlKind.OTHER_FUNCTION,
562  null,
563  null,
564  OperandTypes.family(SqlTypeFamily.STRING,
565  SqlTypeFamily.DATETIME,
566  SqlTypeFamily.DATETIME),
567  SqlFunctionCategory.TIMEDATE);
568  }
569 
570  @Override
571  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
572  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
573  return typeFactory.createTypeWithNullability(
574  typeFactory.createSqlType(SqlTypeName.BIGINT),
575  opBinding.getOperandType(1).isNullable()
576  || opBinding.getOperandType(2).isNullable());
577  }
578  }
579 
580  /* Postgres-style DATE_TRUNC */
581  public static class PgDateTrunc extends SqlFunction {
582  public PgDateTrunc() {
583  super("PG_DATE_TRUNC",
584  SqlKind.OTHER_FUNCTION,
585  null,
586  null,
587  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.DATETIME),
588  SqlFunctionCategory.SYSTEM);
589  }
590 
591  @Override
592  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
593  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
594  return typeFactory.createTypeWithNullability(
595  typeFactory.createSqlType(
596  SqlTypeName.TIMESTAMP, opBinding.getOperandType(1).getPrecision()),
597  opBinding.getOperandType(1).isNullable());
598  }
599  }
600 
601  public static class Length extends SqlFunction {
602  public Length() {
603  super("LENGTH",
604  SqlKind.OTHER_FUNCTION,
605  null,
606  null,
607  OperandTypes.STRING,
608  SqlFunctionCategory.SYSTEM);
609  }
610 
611  @Override
612  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
613  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
614  return typeFactory.createSqlType(SqlTypeName.INTEGER);
615  }
616  }
617 
618  public static class CharLength extends SqlFunction {
619  public CharLength() {
620  super("CHAR_LENGTH",
621  SqlKind.OTHER_FUNCTION,
622  null,
623  null,
624  OperandTypes.STRING,
625  SqlFunctionCategory.SYSTEM);
626  }
627 
628  @Override
629  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
630  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
631  return typeFactory.createSqlType(SqlTypeName.INTEGER);
632  }
633  }
634 
635  public static class KeyForString extends SqlFunction {
636  public KeyForString() {
637  super("KEY_FOR_STRING",
638  SqlKind.OTHER_FUNCTION,
639  null,
640  null,
641  OperandTypes.STRING,
642  SqlFunctionCategory.SYSTEM);
643  }
644 
645  @Override
646  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
647  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
648  return typeFactory.createTypeWithNullability(
649  typeFactory.createSqlType(SqlTypeName.INTEGER),
650  opBinding.getOperandType(0).isNullable());
651  }
652  }
653 
654  public static class SampleRatio extends SqlFunction {
655  public SampleRatio() {
656  super("SAMPLE_RATIO",
657  SqlKind.OTHER_FUNCTION,
658  null,
659  null,
660  OperandTypes.family(signature()),
661  SqlFunctionCategory.SYSTEM);
662  }
663 
664  @Override
665  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
666  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
667  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
668  }
669 
670  private static java.util.List<SqlTypeFamily> signature() {
671  java.util.ArrayList<SqlTypeFamily> families =
672  new java.util.ArrayList<SqlTypeFamily>();
673  families.add(SqlTypeFamily.NUMERIC);
674  return families;
675  }
676  }
677 
678  public static class WidthBucket extends SqlFunction {
679  public WidthBucket() {
680  super("WIDTH_BUCKET",
681  SqlKind.OTHER_FUNCTION,
682  null,
683  null,
684  OperandTypes.family(signature()),
685  SqlFunctionCategory.SYSTEM);
686  }
687 
688  @Override
689  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
690  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
691  return typeFactory.createTypeWithNullability(
692  typeFactory.createSqlType(SqlTypeName.INTEGER), /*nullable=*/true);
693  }
694 
695  private static java.util.List<SqlTypeFamily> signature() {
696  java.util.ArrayList<SqlTypeFamily> families =
697  new java.util.ArrayList<SqlTypeFamily>();
698  families.add(SqlTypeFamily.NUMERIC);
699  families.add(SqlTypeFamily.NUMERIC);
700  families.add(SqlTypeFamily.NUMERIC);
701  families.add(SqlTypeFamily.INTEGER);
702  return families;
703  }
704  }
705 
706  public static class LeadInFrame extends SqlLeadLag {
707  public LeadInFrame() {
708  super("LEAD_IN_FRAME", SqlKind.LEAD);
709  }
710  }
711 
712  public static class LagInFrame extends SqlLeadLag {
713  public LagInFrame() {
714  super("LAG_IN_FRAME", SqlKind.LAG);
715  }
716  }
717 
718  public static class ArrayLength extends SqlFunction {
719  public ArrayLength() {
720  super("ARRAY_LENGTH",
721  SqlKind.OTHER_FUNCTION,
722  null,
723  null,
724  OperandTypes.ARRAY,
725  SqlFunctionCategory.SYSTEM);
726  }
727 
728  @Override
729  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
730  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
731  return typeFactory.createSqlType(SqlTypeName.INTEGER);
732  }
733  }
734 
735  public static class PgILike extends SqlFunction {
736  public PgILike() {
737  super("PG_ILIKE",
738  SqlKind.OTHER_FUNCTION,
739  null,
740  null,
741  OperandTypes.family(getSignatureFamilies(), new EscapeOptional()),
742  SqlFunctionCategory.SYSTEM);
743  }
744 
745  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
746  java.util.ArrayList<SqlTypeFamily> families =
747  new java.util.ArrayList<SqlTypeFamily>();
748  families.add(SqlTypeFamily.STRING);
749  families.add(SqlTypeFamily.STRING);
750  families.add(SqlTypeFamily.STRING);
751  return families;
752  }
753 
754  private static class EscapeOptional
755  implements java.util.function.Predicate<Integer>, Predicate<Integer> {
756  @Override
757  public boolean test(Integer t) {
758  return apply(t);
759  }
760 
761  @Override
762  public boolean apply(Integer t) {
763  return t == 2;
764  }
765  }
766 
767  @Override
768  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
769  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
770  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
771  }
772  }
773 
774  public static class RegexpLike extends SqlFunction {
775  public RegexpLike() {
776  super("REGEXP_LIKE",
777  SqlKind.OTHER_FUNCTION,
778  null,
779  null,
780  OperandTypes.family(getSignatureFamilies(), new EscapeOptional()),
781  SqlFunctionCategory.SYSTEM);
782  }
783 
784  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
785  java.util.ArrayList<SqlTypeFamily> families =
786  new java.util.ArrayList<SqlTypeFamily>();
787  families.add(SqlTypeFamily.STRING);
788  families.add(SqlTypeFamily.STRING);
789  families.add(SqlTypeFamily.STRING);
790  return families;
791  }
792 
793  private static class EscapeOptional
794  implements java.util.function.Predicate<Integer>, Predicate<Integer> {
795  @Override
796  public boolean test(Integer t) {
797  return apply(t);
798  }
799 
800  public boolean apply(Integer t) {
801  return t == 2;
802  }
803  }
804 
805  @Override
806  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
807  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
808  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
809  }
810  }
811 
812  public static class LeftRightTrim extends SqlFunction {
813  public LeftRightTrim(final String name, final SqlKind kind) {
814  super(name,
815  kind,
816  ReturnTypes.ARG0.andThen(SqlTypeTransforms.TO_NULLABLE)
817  .andThen(SqlTypeTransforms.TO_VARYING),
818  null,
819  OperandTypes.and(
820  OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING),
821  new SameOperandTypeChecker(2) {
822  @Override
823  protected List<Integer> getOperandList(int operandCount) {
824  return ImmutableList.of(0, 1);
825  }
826  }),
827  SqlFunctionCategory.STRING);
828  }
829 
830  @Override
831  public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
832  SqlParserPos pos,
833  @Nullable SqlNode... operands) {
834  assert functionQualifier == null;
835  switch (operands.length) {
836  case 1:
837  operands = new SqlNode[] {operands[0], SqlLiteral.createCharString(" ", pos)};
838  break;
839  case 2:
840  if (operands[1] == null) {
841  operands[1] = SqlLiteral.createCharString(" ", pos);
842  }
843  operands = new SqlNode[] {operands[0], operands[1]};
844  break;
845  default:
846  throw new IllegalArgumentException(
847  "Invalid operand count " + Arrays.toString(operands));
848  }
849  return super.createCall(functionQualifier, pos, operands);
850  }
851 
852  @Override
853  public boolean requiresCreate(List<SqlNode> operands) {
854  // if there is only 1 Operand, the code will be creating 'defaults'
855  return (operands.size() == 1);
856  }
857  }
858 
859  public static class LTrim extends LeftRightTrim {
860  public LTrim() {
861  super("LTRIM", SqlKind.LTRIM);
862  }
863  }
864  public static class RTrim extends LeftRightTrim {
865  public RTrim() {
866  super("RTRIM", SqlKind.RTRIM);
867  }
868  }
869  public static class LeftRightPad extends SqlFunction {
870  public LeftRightPad(final String name) {
871  super(name,
872  SqlKind.OTHER_FUNCTION,
873  ReturnTypes.ARG0.andThen(SqlTypeTransforms.TO_NULLABLE)
874  .andThen(SqlTypeTransforms.TO_VARYING),
875  null,
876  OperandTypes.and(OperandTypes.family(SqlTypeFamily.STRING,
877  SqlTypeFamily.INTEGER,
878  SqlTypeFamily.STRING),
879  new SameOperandTypeChecker(3) {
880  @Override
881  protected List<Integer> getOperandList(int operandCount) {
882  return ImmutableList.of(0, 2);
883  }
884  }),
885  SqlFunctionCategory.STRING);
886  }
887 
888  @Override
889  public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
890  SqlParserPos pos,
891  @Nullable SqlNode... operands) {
892  assert functionQualifier == null;
893  switch (operands.length) {
894  case 2:
895  operands = new SqlNode[] {
896  operands[0], operands[1], SqlLiteral.createCharString(" ", pos)};
897  break;
898  case 3:
899  if (operands[2] == null) {
900  operands[2] = SqlLiteral.createCharString(" ", pos);
901  }
902  operands = new SqlNode[] {operands[0], operands[1], operands[2]};
903  break;
904  default:
905  throw new IllegalArgumentException(
906  "Invalid operand count " + Arrays.toString(operands));
907  }
908  return super.createCall(functionQualifier, pos, operands);
909  }
910 
911  @Override
912  public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
913  if (!super.checkOperandTypes(callBinding, throwOnFailure)) {
914  return false;
915  }
916  switch (kind) {
917  case TRIM:
918  return SqlTypeUtil.isCharTypeComparable(callBinding,
919  ImmutableList.of(callBinding.operand(0), callBinding.operand(2)),
920  throwOnFailure);
921  default:
922  return true;
923  }
924  }
925 
926  @Override
927  public boolean requiresCreate(List<SqlNode> operands) {
928  // if there are only 2 Operands, the code will be creating 'defaults'
929  return (operands.size() == 2);
930  }
931  }
932 
933  public static class LPad extends LeftRightPad {
934  public LPad() {
935  super("LPAD");
936  }
937  }
938  public static class RPad extends LeftRightPad {
939  public RPad() {
940  super("RPAD");
941  }
942  }
943 
944  public static class SplitPart extends SqlFunction {
945  public SplitPart() {
946  super("SPLIT_PART",
947  SqlKind.OTHER_FUNCTION,
948  null,
949  null,
950  OperandTypes.family(getSignatureFamilies()),
952  }
953 
954  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
955  java.util.ArrayList<SqlTypeFamily> families =
956  new java.util.ArrayList<SqlTypeFamily>();
957  families.add(SqlTypeFamily.STRING);
958  families.add(SqlTypeFamily.STRING);
959  families.add(SqlTypeFamily.INTEGER);
960  return families;
961  }
962 
963  @Override
964  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
965  return opBinding.getOperandType(0);
966  }
967  }
968 
969  public static class Replace extends SqlFunction {
970  public Replace() {
971  super("REPLACE",
972  SqlKind.OTHER_FUNCTION,
973  null,
974  null,
975  OperandTypes.family(getSignatureFamilies()),
977  }
978 
979  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
980  java.util.ArrayList<SqlTypeFamily> families =
981  new java.util.ArrayList<SqlTypeFamily>();
982  families.add(SqlTypeFamily.STRING);
983  families.add(SqlTypeFamily.STRING);
984  families.add(SqlTypeFamily.STRING);
985  return families;
986  }
987 
988  @Override
989  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
990  return opBinding.getOperandType(0);
991  }
992 
993  @Override
994  public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
995  SqlParserPos pos,
996  @Nullable SqlNode... operands) {
997  assert functionQualifier == null;
998  switch (operands.length) {
999  case 2:
1000  operands = new SqlNode[] {
1001  operands[0], operands[1], SqlLiteral.createCharString("", pos)};
1002  break;
1003  case 3:
1004  break;
1005  default:
1006  throw new IllegalArgumentException(
1007  "Invalid operand count " + Arrays.toString(operands));
1008  }
1009  return super.createCall(functionQualifier, pos, operands);
1010  }
1011 
1012  @Override
1013  public boolean requiresCreate(List<SqlNode> operands) {
1014  // if there are only 2 Operands, the code will be creating 'defaults'
1015  return (operands.size() == 2);
1016  }
1017  }
1018  public static class Reverse extends SqlFunction {
1019  public Reverse() {
1020  super("REVERSE",
1021  SqlKind.OTHER_FUNCTION,
1022  null,
1023  null,
1024  OperandTypes.family(getSignatureFamilies()),
1026  }
1027 
1028  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
1029  java.util.ArrayList<SqlTypeFamily> families =
1030  new java.util.ArrayList<SqlTypeFamily>();
1031  families.add(SqlTypeFamily.STRING);
1032  return families;
1033  }
1034 
1035  @Override
1036  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1037  return opBinding.getOperandType(0);
1038  }
1039  }
1040  public static class Repeat extends SqlFunction {
1041  public Repeat() {
1042  super("REPEAT",
1043  SqlKind.OTHER_FUNCTION,
1044  null,
1045  null,
1046  OperandTypes.family(getSignatureFamilies()),
1048  }
1049 
1050  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
1051  java.util.ArrayList<SqlTypeFamily> families =
1052  new java.util.ArrayList<SqlTypeFamily>();
1053  families.add(SqlTypeFamily.STRING);
1054  families.add(SqlTypeFamily.INTEGER);
1055  return families;
1056  }
1057 
1058  @Override
1059  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1060  return opBinding.getOperandType(0);
1061  }
1062  }
1063 
1064  public static class RegexpReplace extends SqlFunction {
1065  public RegexpReplace() {
1066  super("REGEXP_REPLACE",
1067  SqlKind.OTHER_FUNCTION,
1068  null,
1069  null,
1070  OperandTypes.family(getSignatureFamilies()),
1072  }
1073 
1074  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
1075  java.util.ArrayList<SqlTypeFamily> families =
1076  new java.util.ArrayList<SqlTypeFamily>();
1077  families.add(SqlTypeFamily.STRING);
1078  families.add(SqlTypeFamily.STRING);
1079  families.add(SqlTypeFamily.STRING);
1080  families.add(SqlTypeFamily.INTEGER);
1081  families.add(SqlTypeFamily.INTEGER);
1082  families.add(SqlTypeFamily.STRING);
1083  return families;
1084  }
1085 
1086  @Override
1087  public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
1088  SqlParserPos pos,
1089  @Nullable SqlNode... operands) {
1090  assert functionQualifier == null;
1091  final int num_operands = operands.length;
1092  if (num_operands < 2 || num_operands > 6) {
1093  throw new IllegalArgumentException(
1094  "Invalid operand count " + Arrays.toString(operands));
1095  }
1096  SqlNode[] new_operands = new SqlNode[6];
1097  // operand string
1098  new_operands[0] = operands[0];
1099  // pattern
1100  new_operands[1] = operands[1];
1101  // replacement
1102  if (num_operands < 3 || operands[2] == null) {
1103  new_operands[2] = SqlLiteral.createCharString("", pos);
1104  } else {
1105  new_operands[2] = operands[2];
1106  }
1107  // position
1108  if (num_operands < 4 || operands[3] == null) {
1109  new_operands[3] = SqlLiteral.createExactNumeric("1", pos);
1110  } else {
1111  new_operands[3] = operands[3];
1112  }
1113  // occurrence
1114  if (num_operands < 5 || operands[4] == null) {
1115  new_operands[4] = SqlLiteral.createExactNumeric("0", pos);
1116  } else {
1117  new_operands[4] = operands[4];
1118  }
1119  // parameters
1120  if (num_operands < 6 || operands[5] == null) {
1121  new_operands[5] = SqlLiteral.createCharString("c", pos);
1122  } else {
1123  new_operands[5] = operands[5];
1124  }
1125  return super.createCall(functionQualifier, pos, new_operands);
1126  }
1127 
1128  @Override
1129  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1130  return opBinding.getOperandType(0);
1131  }
1132  }
1133 
1134  public static class RegexpSubstr extends SqlFunction {
1135  public RegexpSubstr() {
1136  super("REGEXP_SUBSTR",
1137  SqlKind.OTHER_FUNCTION,
1138  null,
1139  null,
1140  OperandTypes.family(getSignatureFamilies()),
1142  }
1143 
1144  public RegexpSubstr(final String alias) {
1145  super(alias,
1146  SqlKind.OTHER_FUNCTION,
1147  null,
1148  null,
1149  OperandTypes.family(getSignatureFamilies()),
1150  SqlFunctionCategory.SYSTEM);
1151  }
1152 
1153  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
1154  java.util.ArrayList<SqlTypeFamily> families =
1155  new java.util.ArrayList<SqlTypeFamily>();
1156  families.add(SqlTypeFamily.STRING);
1157  families.add(SqlTypeFamily.STRING);
1158  families.add(SqlTypeFamily.INTEGER);
1159  families.add(SqlTypeFamily.INTEGER);
1160  families.add(SqlTypeFamily.STRING);
1161  families.add(SqlTypeFamily.INTEGER);
1162  return families;
1163  }
1164 
1165  @Override
1166  public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
1167  SqlParserPos pos,
1168  @Nullable SqlNode... operands) {
1169  assert functionQualifier == null;
1170  final int num_operands = operands.length;
1171  if (num_operands < 2 || num_operands > 6) {
1172  throw new IllegalArgumentException(
1173  "Invalid operand count " + Arrays.toString(operands));
1174  }
1175  SqlNode[] new_operands = new SqlNode[6];
1176 
1177  // operand string (required)
1178  new_operands[0] = operands[0];
1179  // pattern (required)
1180  new_operands[1] = operands[1];
1181  // start position
1182  if (num_operands < 3 || operands[2] == null) {
1183  new_operands[2] = SqlLiteral.createExactNumeric("1", pos);
1184  } else {
1185  new_operands[2] = operands[2];
1186  }
1187  // match occurrence
1188  if (num_operands < 4 || operands[3] == null) {
1189  new_operands[3] = SqlLiteral.createExactNumeric("1", pos);
1190  } else {
1191  new_operands[3] = operands[3];
1192  }
1193  // regex params (default 'c' = case sensitive)
1194  if (num_operands < 5 || operands[4] == null) {
1195  new_operands[4] = SqlLiteral.createCharString("c", pos);
1196  } else {
1197  new_operands[4] = operands[4];
1198  }
1199  // Sub-match occurrence, valid with regex param 'e'
1200  if (num_operands < 6 || operands[5] == null) {
1201  new_operands[5] = SqlLiteral.createExactNumeric("1", pos);
1202  } else {
1203  new_operands[5] = operands[5];
1204  }
1205  return super.createCall(functionQualifier, pos, new_operands);
1206  }
1207 
1208  @Override
1209  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1210  return opBinding.getOperandType(0);
1211  }
1212  }
1213 
1214  public static class RegexpMatch extends RegexpSubstr {
1215  public RegexpMatch() {
1216  super("REGEXP_MATCH");
1217  }
1218  }
1219  public static class Base64Encode extends SqlFunction {
1220  public Base64Encode() {
1221  super("BASE64_ENCODE",
1222  SqlKind.OTHER_FUNCTION,
1223  null,
1224  null,
1225  OperandTypes.family(getSignatureFamilies()),
1227  }
1228 
1229  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
1230  java.util.ArrayList<SqlTypeFamily> families =
1231  new java.util.ArrayList<SqlTypeFamily>();
1232  families.add(SqlTypeFamily.STRING);
1233  return families;
1234  }
1235 
1236  @Override
1237  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1238  return opBinding.getOperandType(0);
1239  }
1240  }
1241 
1242  public static class Base64Decode extends SqlFunction {
1243  public Base64Decode() {
1244  super("BASE64_DECODE",
1245  SqlKind.OTHER_FUNCTION,
1246  null,
1247  null,
1248  OperandTypes.family(getSignatureFamilies()),
1250  }
1251 
1252  private static java.util.List<SqlTypeFamily> getSignatureFamilies() {
1253  java.util.ArrayList<SqlTypeFamily> families =
1254  new java.util.ArrayList<SqlTypeFamily>();
1255  families.add(SqlTypeFamily.STRING);
1256  return families;
1257  }
1258 
1259  @Override
1260  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1261  return opBinding.getOperandType(0);
1262  }
1263  }
1264 
1265  public static class Likely extends SqlFunction {
1266  public Likely() {
1267  super("LIKELY",
1268  SqlKind.OTHER_FUNCTION,
1269  null,
1270  null,
1271  OperandTypes.BOOLEAN,
1272  SqlFunctionCategory.SYSTEM);
1273  }
1274 
1275  @Override
1276  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1277  return opBinding.getOperandType(0);
1278  }
1279  }
1280 
1281  public static class Unlikely extends SqlFunction {
1282  public Unlikely() {
1283  super("UNLIKELY",
1284  SqlKind.OTHER_FUNCTION,
1285  null,
1286  null,
1287  OperandTypes.BOOLEAN,
1288  SqlFunctionCategory.SYSTEM);
1289  }
1290 
1291  @Override
1292  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1293  return opBinding.getOperandType(0);
1294  }
1295  }
1296 
1297  public static class Sign extends SqlFunction {
1298  public Sign() {
1299  super("SIGN",
1300  SqlKind.OTHER_FUNCTION,
1301  null,
1302  null,
1303  OperandTypes.NUMERIC,
1304  SqlFunctionCategory.NUMERIC);
1305  }
1306 
1307  @Override
1308  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1309  return opBinding.getOperandType(0);
1310  }
1311  }
1312 
1313  static class Truncate extends SqlFunction {
1315  super("TRUNCATE",
1316  SqlKind.OTHER_FUNCTION,
1317  null,
1318  null,
1319  OperandTypes.family(signature()),
1320  SqlFunctionCategory.NUMERIC);
1321  }
1322 
1323  @Override
1324  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1325  assert opBinding.getOperandCount() == 2;
1326  return opBinding.getOperandType(0);
1327  }
1328 
1329  private static java.util.List<SqlTypeFamily> signature() {
1330  java.util.List<SqlTypeFamily> truncate_sig =
1331  new java.util.ArrayList<SqlTypeFamily>();
1332  truncate_sig.add(SqlTypeFamily.NUMERIC);
1333  truncate_sig.add(SqlTypeFamily.INTEGER);
1334  return truncate_sig;
1335  }
1336  }
1337 
1338  static class ST_IsEmpty extends SqlFunction {
1340  super("ST_IsEmpty",
1341  SqlKind.OTHER_FUNCTION,
1342  null,
1343  null,
1344  OperandTypes.family(signature()),
1345  SqlFunctionCategory.SYSTEM);
1346  }
1347 
1348  @Override
1349  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1350  assert opBinding.getOperandCount() == 1;
1351  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1352  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1353  }
1354 
1355  private static java.util.List<SqlTypeFamily> signature() {
1356  java.util.List<SqlTypeFamily> st_isempty_sig =
1357  new java.util.ArrayList<SqlTypeFamily>();
1358  st_isempty_sig.add(SqlTypeFamily.ANY);
1359  return st_isempty_sig;
1360  }
1361  }
1362 
1363  static class ST_IsValid extends SqlFunction {
1365  super("ST_IsValid",
1366  SqlKind.OTHER_FUNCTION,
1367  null,
1368  null,
1369  OperandTypes.family(signature()),
1370  SqlFunctionCategory.SYSTEM);
1371  }
1372 
1373  @Override
1374  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1375  assert opBinding.getOperandCount() == 1;
1376  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1377  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1378  }
1379 
1380  private static java.util.List<SqlTypeFamily> signature() {
1381  java.util.List<SqlTypeFamily> st_isvalid_sig =
1382  new java.util.ArrayList<SqlTypeFamily>();
1383  st_isvalid_sig.add(SqlTypeFamily.ANY);
1384  return st_isvalid_sig;
1385  }
1386  }
1387 
1388  static class ST_Contains extends SqlFunction {
1390  super("ST_Contains",
1391  SqlKind.OTHER_FUNCTION,
1392  null,
1393  null,
1394  OperandTypes.family(signature()),
1395  SqlFunctionCategory.SYSTEM);
1396  }
1397 
1398  @Override
1399  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1400  assert opBinding.getOperandCount() == 2;
1401  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1402  return typeFactory.createTypeWithNullability(
1403  typeFactory.createSqlType(SqlTypeName.BOOLEAN),
1404  opBinding.getOperandType(0).isNullable()
1405  || opBinding.getOperandType(1).isNullable());
1406  }
1407 
1408  private static java.util.List<SqlTypeFamily> signature() {
1409  java.util.List<SqlTypeFamily> st_contains_sig =
1410  new java.util.ArrayList<SqlTypeFamily>();
1411  st_contains_sig.add(SqlTypeFamily.ANY);
1412  st_contains_sig.add(SqlTypeFamily.ANY);
1413  return st_contains_sig;
1414  }
1415  }
1416 
1417  static class ST_Equals extends SqlFunction {
1419  super("ST_Equals",
1420  SqlKind.OTHER_FUNCTION,
1421  null,
1422  null,
1423  OperandTypes.family(signature()),
1424  SqlFunctionCategory.SYSTEM);
1425  }
1426 
1427  @Override
1428  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1429  assert opBinding.getOperandCount() == 2;
1430  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1431  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1432  }
1433 
1434  private static java.util.List<SqlTypeFamily> signature() {
1435  java.util.List<SqlTypeFamily> st_equals_sig =
1436  new java.util.ArrayList<SqlTypeFamily>();
1437  st_equals_sig.add(SqlTypeFamily.ANY);
1438  st_equals_sig.add(SqlTypeFamily.ANY);
1439  return st_equals_sig;
1440  }
1441  }
1442 
1443  static class ST_Intersects extends SqlFunction {
1445  super("ST_Intersects",
1446  SqlKind.OTHER_FUNCTION,
1447  null,
1448  null,
1449  OperandTypes.family(signature()),
1450  SqlFunctionCategory.SYSTEM);
1451  }
1452 
1453  @Override
1454  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1455  assert opBinding.getOperandCount() == 2;
1456  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1457  return typeFactory.createTypeWithNullability(
1458  typeFactory.createSqlType(SqlTypeName.BOOLEAN),
1459  opBinding.getOperandType(0).isNullable()
1460  || opBinding.getOperandType(1).isNullable());
1461  }
1462 
1463  private static java.util.List<SqlTypeFamily> signature() {
1464  java.util.List<SqlTypeFamily> st_intersects_sig =
1465  new java.util.ArrayList<SqlTypeFamily>();
1466  st_intersects_sig.add(SqlTypeFamily.ANY);
1467  st_intersects_sig.add(SqlTypeFamily.ANY);
1468  return st_intersects_sig;
1469  }
1470  }
1471 
1472  static class ST_Overlaps extends SqlFunction {
1474  super("ST_Overlaps",
1475  SqlKind.OTHER_FUNCTION,
1476  null,
1477  null,
1478  OperandTypes.family(signature()),
1479  SqlFunctionCategory.SYSTEM);
1480  }
1481 
1482  @Override
1483  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1484  assert opBinding.getOperandCount() == 2;
1485  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1486  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1487  }
1488 
1489  private static java.util.List<SqlTypeFamily> signature() {
1490  java.util.List<SqlTypeFamily> st_overlaps_sig =
1491  new java.util.ArrayList<SqlTypeFamily>();
1492  st_overlaps_sig.add(SqlTypeFamily.ANY);
1493  st_overlaps_sig.add(SqlTypeFamily.ANY);
1494  return st_overlaps_sig;
1495  }
1496  }
1497 
1498  static class ST_Approx_Overlaps extends SqlFunction {
1500  super("ST_Approx_Overlaps",
1501  SqlKind.OTHER_FUNCTION,
1502  null,
1503  null,
1504  OperandTypes.family(signature()),
1505  SqlFunctionCategory.SYSTEM);
1506  }
1507 
1508  @Override
1509  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1510  assert opBinding.getOperandCount() == 2;
1511  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1512  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
1513  }
1514 
1515  private static java.util.List<SqlTypeFamily> signature() {
1516  java.util.List<SqlTypeFamily> st_overlaps_sig =
1517  new java.util.ArrayList<SqlTypeFamily>();
1518  st_overlaps_sig.add(SqlTypeFamily.ANY);
1519  st_overlaps_sig.add(SqlTypeFamily.ANY);
1520  return st_overlaps_sig;
1521  }
1522  }
1523 
1524  static class ST_Disjoint extends SqlFunction {
1526  super("ST_Disjoint",
1527  SqlKind.OTHER_FUNCTION,
1528  null,
1529  null,
1530  OperandTypes.family(signature()),
1531  SqlFunctionCategory.SYSTEM);
1532  }
1533 
1534  @Override
1535  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1536  assert opBinding.getOperandCount() == 2;
1537  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1538  return typeFactory.createTypeWithNullability(
1539  typeFactory.createSqlType(SqlTypeName.BOOLEAN),
1540  opBinding.getOperandType(0).isNullable()
1541  || opBinding.getOperandType(1).isNullable());
1542  }
1543 
1544  private static java.util.List<SqlTypeFamily> signature() {
1545  java.util.List<SqlTypeFamily> st_disjoint_sig =
1546  new java.util.ArrayList<SqlTypeFamily>();
1547  st_disjoint_sig.add(SqlTypeFamily.ANY);
1548  st_disjoint_sig.add(SqlTypeFamily.ANY);
1549  return st_disjoint_sig;
1550  }
1551  }
1552 
1553  static class ST_Within extends SqlFunction {
1555  super("ST_Within",
1556  SqlKind.OTHER_FUNCTION,
1557  null,
1558  null,
1559  OperandTypes.family(signature()),
1560  SqlFunctionCategory.SYSTEM);
1561  }
1562 
1563  @Override
1564  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1565  assert opBinding.getOperandCount() == 2;
1566  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1567  return typeFactory.createTypeWithNullability(
1568  typeFactory.createSqlType(SqlTypeName.BOOLEAN),
1569  opBinding.getOperandType(0).isNullable()
1570  || opBinding.getOperandType(1).isNullable());
1571  }
1572 
1573  private static java.util.List<SqlTypeFamily> signature() {
1574  java.util.List<SqlTypeFamily> st_within_sig =
1575  new java.util.ArrayList<SqlTypeFamily>();
1576  st_within_sig.add(SqlTypeFamily.ANY);
1577  st_within_sig.add(SqlTypeFamily.ANY);
1578  return st_within_sig;
1579  }
1580  }
1581 
1582  static class ST_DWithin extends SqlFunction {
1584  super("ST_DWithin",
1585  SqlKind.OTHER_FUNCTION,
1586  null,
1587  null,
1588  OperandTypes.family(signature()),
1589  SqlFunctionCategory.SYSTEM);
1590  }
1591 
1592  @Override
1593  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1594  assert opBinding.getOperandCount() == 3;
1595  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1596  return typeFactory.createTypeWithNullability(
1597  typeFactory.createSqlType(SqlTypeName.BOOLEAN),
1598  opBinding.getOperandType(0).isNullable()
1599  || opBinding.getOperandType(1).isNullable()
1600  || opBinding.getOperandType(2).isNullable());
1601  }
1602 
1603  private static java.util.List<SqlTypeFamily> signature() {
1604  java.util.List<SqlTypeFamily> st_dwithin_sig =
1605  new java.util.ArrayList<SqlTypeFamily>();
1606  st_dwithin_sig.add(SqlTypeFamily.ANY);
1607  st_dwithin_sig.add(SqlTypeFamily.ANY);
1608  st_dwithin_sig.add(SqlTypeFamily.NUMERIC);
1609  return st_dwithin_sig;
1610  }
1611  }
1612 
1613  static class ST_DFullyWithin extends SqlFunction {
1615  super("ST_DFullyWithin",
1616  SqlKind.OTHER_FUNCTION,
1617  null,
1618  null,
1619  OperandTypes.family(signature()),
1620  SqlFunctionCategory.SYSTEM);
1621  }
1622 
1623  @Override
1624  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1625  assert opBinding.getOperandCount() == 3;
1626  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1627  return typeFactory.createTypeWithNullability(
1628  typeFactory.createSqlType(SqlTypeName.BOOLEAN),
1629  opBinding.getOperandType(0).isNullable()
1630  || opBinding.getOperandType(1).isNullable()
1631  || opBinding.getOperandType(2).isNullable());
1632  }
1633 
1634  private static java.util.List<SqlTypeFamily> signature() {
1635  java.util.List<SqlTypeFamily> st_dwithin_sig =
1636  new java.util.ArrayList<SqlTypeFamily>();
1637  st_dwithin_sig.add(SqlTypeFamily.ANY);
1638  st_dwithin_sig.add(SqlTypeFamily.ANY);
1639  st_dwithin_sig.add(SqlTypeFamily.NUMERIC);
1640  return st_dwithin_sig;
1641  }
1642  }
1643 
1644  static class ST_Distance extends SqlFunction {
1646  super("ST_Distance",
1647  SqlKind.OTHER_FUNCTION,
1648  null,
1649  null,
1650  OperandTypes.family(signature()),
1651  SqlFunctionCategory.SYSTEM);
1652  }
1653 
1654  @Override
1655  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1656  assert opBinding.getOperandCount() == 2;
1657  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1658  return typeFactory.createTypeWithNullability(
1659  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1660  opBinding.getOperandType(0).isNullable()
1661  || opBinding.getOperandType(1).isNullable());
1662  }
1663 
1664  private static java.util.List<SqlTypeFamily> signature() {
1665  java.util.List<SqlTypeFamily> st_distance_sig =
1666  new java.util.ArrayList<SqlTypeFamily>();
1667  st_distance_sig.add(SqlTypeFamily.ANY);
1668  st_distance_sig.add(SqlTypeFamily.ANY);
1669  return st_distance_sig;
1670  }
1671  }
1672 
1673  static class ST_MaxDistance extends SqlFunction {
1675  super("ST_MaxDistance",
1676  SqlKind.OTHER_FUNCTION,
1677  null,
1678  null,
1679  OperandTypes.family(signature()),
1680  SqlFunctionCategory.SYSTEM);
1681  }
1682 
1683  @Override
1684  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1685  assert opBinding.getOperandCount() == 2;
1686  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1687  return typeFactory.createTypeWithNullability(
1688  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1689  opBinding.getOperandType(0).isNullable()
1690  || opBinding.getOperandType(1).isNullable());
1691  }
1692 
1693  private static java.util.List<SqlTypeFamily> signature() {
1694  java.util.List<SqlTypeFamily> st_maxdistance_sig =
1695  new java.util.ArrayList<SqlTypeFamily>();
1696  st_maxdistance_sig.add(SqlTypeFamily.ANY);
1697  st_maxdistance_sig.add(SqlTypeFamily.ANY);
1698  return st_maxdistance_sig;
1699  }
1700  }
1701 
1702  static class ST_GeogFromText extends SqlFunction {
1704  super("ST_GeogFromText",
1705  SqlKind.OTHER_FUNCTION,
1706  null,
1707  null,
1708  OperandTypes.or(OperandTypes.family(SqlTypeFamily.ANY),
1709  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER)),
1710  SqlFunctionCategory.SYSTEM);
1711  }
1712 
1713  @Override
1714  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1715  assert opBinding.getOperandCount() == 1;
1716  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1717  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1718  }
1719  }
1720 
1721  static class ST_GeomFromText extends SqlFunction {
1723  super("ST_GeomFromText",
1724  SqlKind.OTHER_FUNCTION,
1725  null,
1726  null,
1727  OperandTypes.or(OperandTypes.family(SqlTypeFamily.ANY),
1728  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER)),
1729  SqlFunctionCategory.SYSTEM);
1730  }
1731 
1732  @Override
1733  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1734  assert opBinding.getOperandCount() == 1;
1735  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1736  return typeFactory.createSqlType(SqlTypeName.INTEGER);
1737  }
1738  }
1739 
1740  static class ST_Transform extends SqlFunction {
1742  super("ST_Transform",
1743  SqlKind.OTHER_FUNCTION,
1744  null,
1745  null,
1746  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER),
1747  SqlFunctionCategory.SYSTEM);
1748  }
1749 
1750  @Override
1751  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1752  assert opBinding.getOperandCount() == 1;
1753  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1754  return typeFactory.createTypeWithNullability(
1755  typeFactory.createSqlType(SqlTypeName.INTEGER),
1756  opBinding.getOperandType(0).isNullable());
1757  }
1758  }
1759 
1760  static class ST_X extends SqlFunction {
1761  ST_X() {
1762  super("ST_X",
1763  SqlKind.OTHER_FUNCTION,
1764  null,
1765  null,
1766  OperandTypes.family(SqlTypeFamily.ANY),
1767  SqlFunctionCategory.SYSTEM);
1768  }
1769 
1770  @Override
1771  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1772  assert opBinding.getOperandCount() == 1;
1773  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1774  return typeFactory.createTypeWithNullability(
1775  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1776  opBinding.getOperandType(0).isNullable());
1777  }
1778  }
1779 
1780  static class ST_Y extends SqlFunction {
1781  ST_Y() {
1782  super("ST_Y",
1783  SqlKind.OTHER_FUNCTION,
1784  null,
1785  null,
1786  OperandTypes.family(SqlTypeFamily.ANY),
1787  SqlFunctionCategory.SYSTEM);
1788  }
1789 
1790  @Override
1791  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1792  assert opBinding.getOperandCount() == 1;
1793  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1794  return typeFactory.createTypeWithNullability(
1795  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1796  opBinding.getOperandType(0).isNullable());
1797  }
1798  }
1799 
1800  static class ST_XMin extends SqlFunction {
1802  super("ST_XMin",
1803  SqlKind.OTHER_FUNCTION,
1804  null,
1805  null,
1806  OperandTypes.family(SqlTypeFamily.ANY),
1807  SqlFunctionCategory.SYSTEM);
1808  }
1809 
1810  @Override
1811  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1812  assert opBinding.getOperandCount() == 1;
1813  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1814  return typeFactory.createTypeWithNullability(
1815  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1816  opBinding.getOperandType(0).isNullable());
1817  }
1818  }
1819 
1820  static class ST_XMax extends SqlFunction {
1822  super("ST_XMax",
1823  SqlKind.OTHER_FUNCTION,
1824  null,
1825  null,
1826  OperandTypes.family(SqlTypeFamily.ANY),
1827  SqlFunctionCategory.SYSTEM);
1828  }
1829 
1830  @Override
1831  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1832  assert opBinding.getOperandCount() == 1;
1833  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1834  return typeFactory.createTypeWithNullability(
1835  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1836  opBinding.getOperandType(0).isNullable());
1837  }
1838  }
1839 
1840  static class ST_YMin extends SqlFunction {
1842  super("ST_YMin",
1843  SqlKind.OTHER_FUNCTION,
1844  null,
1845  null,
1846  OperandTypes.family(SqlTypeFamily.ANY),
1847  SqlFunctionCategory.SYSTEM);
1848  }
1849 
1850  @Override
1851  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1852  assert opBinding.getOperandCount() == 1;
1853  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1854  return typeFactory.createTypeWithNullability(
1855  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1856  opBinding.getOperandType(0).isNullable());
1857  }
1858  }
1859 
1860  static class ST_YMax extends SqlFunction {
1862  super("ST_YMax",
1863  SqlKind.OTHER_FUNCTION,
1864  null,
1865  null,
1866  OperandTypes.family(SqlTypeFamily.ANY),
1867  SqlFunctionCategory.SYSTEM);
1868  }
1869 
1870  @Override
1871  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1872  assert opBinding.getOperandCount() == 1;
1873  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1874  return typeFactory.createTypeWithNullability(
1875  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1876  opBinding.getOperandType(0).isNullable());
1877  }
1878  }
1879 
1880  static class ST_PointN extends SqlFunction {
1882  super("ST_PointN",
1883  SqlKind.OTHER_FUNCTION,
1884  null,
1885  null,
1886  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER),
1887  SqlFunctionCategory.SYSTEM);
1888  }
1889 
1890  @Override
1891  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1892  assert opBinding.getOperandCount() == 1;
1893  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1894  return typeFactory.createTypeWithNullability(
1895  typeFactory.createSqlType(SqlTypeName.INTEGER),
1896  opBinding.getOperandType(0).isNullable());
1897  }
1898  }
1899 
1900  static class ST_EndPoint extends SqlFunction {
1902  super("ST_EndPoint",
1903  SqlKind.OTHER_FUNCTION,
1904  null,
1905  null,
1906  OperandTypes.family(SqlTypeFamily.ANY),
1907  SqlFunctionCategory.SYSTEM);
1908  }
1909 
1910  @Override
1911  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1912  assert opBinding.getOperandCount() == 1;
1913  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1914  return typeFactory.createTypeWithNullability(
1915  typeFactory.createSqlType(SqlTypeName.INTEGER),
1916  opBinding.getOperandType(0).isNullable());
1917  }
1918  }
1919 
1920  static class ST_StartPoint extends SqlFunction {
1922  super("ST_StartPoint",
1923  SqlKind.OTHER_FUNCTION,
1924  null,
1925  null,
1926  OperandTypes.family(SqlTypeFamily.ANY),
1927  SqlFunctionCategory.SYSTEM);
1928  }
1929 
1930  @Override
1931  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1932  assert opBinding.getOperandCount() == 1;
1933  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1934  return typeFactory.createTypeWithNullability(
1935  typeFactory.createSqlType(SqlTypeName.INTEGER),
1936  opBinding.getOperandType(0).isNullable());
1937  }
1938  }
1939 
1940  static class ST_Length extends SqlFunction {
1942  super("ST_Length",
1943  SqlKind.OTHER_FUNCTION,
1944  null,
1945  null,
1946  OperandTypes.family(SqlTypeFamily.ANY),
1947  SqlFunctionCategory.SYSTEM);
1948  }
1949 
1950  @Override
1951  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1952  assert opBinding.getOperandCount() == 1;
1953  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1954  return typeFactory.createTypeWithNullability(
1955  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1956  opBinding.getOperandType(0).isNullable());
1957  }
1958  }
1959 
1960  static class ST_Perimeter extends SqlFunction {
1962  super("ST_Perimeter",
1963  SqlKind.OTHER_FUNCTION,
1964  null,
1965  null,
1966  OperandTypes.family(SqlTypeFamily.ANY),
1967  SqlFunctionCategory.SYSTEM);
1968  }
1969 
1970  @Override
1971  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1972  assert opBinding.getOperandCount() == 1;
1973  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1974  return typeFactory.createTypeWithNullability(
1975  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1976  opBinding.getOperandType(0).isNullable());
1977  }
1978  }
1979 
1980  static class ST_Area extends SqlFunction {
1982  super("ST_Area",
1983  SqlKind.OTHER_FUNCTION,
1984  null,
1985  null,
1986  OperandTypes.family(SqlTypeFamily.ANY),
1987  SqlFunctionCategory.SYSTEM);
1988  }
1989 
1990  @Override
1991  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
1992  assert opBinding.getOperandCount() == 1;
1993  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
1994  return typeFactory.createTypeWithNullability(
1995  typeFactory.createSqlType(SqlTypeName.DOUBLE),
1996  opBinding.getOperandType(0).isNullable());
1997  }
1998  }
1999 
2000  static class ST_NPoints extends SqlFunction {
2002  super("ST_NPoints",
2003  SqlKind.OTHER_FUNCTION,
2004  null,
2005  null,
2006  OperandTypes.family(SqlTypeFamily.ANY),
2007  SqlFunctionCategory.SYSTEM);
2008  }
2009 
2010  @Override
2011  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2012  assert opBinding.getOperandCount() == 1;
2013  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2014  return typeFactory.createTypeWithNullability(
2015  typeFactory.createSqlType(SqlTypeName.INTEGER),
2016  opBinding.getOperandType(0).isNullable());
2017  }
2018  }
2019 
2020  static class ST_NRings extends SqlFunction {
2022  super("ST_NRings",
2023  SqlKind.OTHER_FUNCTION,
2024  null,
2025  null,
2026  OperandTypes.family(SqlTypeFamily.ANY),
2027  SqlFunctionCategory.SYSTEM);
2028  }
2029 
2030  @Override
2031  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2032  assert opBinding.getOperandCount() == 1;
2033  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2034  return typeFactory.createTypeWithNullability(
2035  typeFactory.createSqlType(SqlTypeName.INTEGER),
2036  opBinding.getOperandType(0).isNullable());
2037  }
2038  }
2039 
2040  static class ST_SRID extends SqlFunction {
2042  super("ST_SRID",
2043  SqlKind.OTHER_FUNCTION,
2044  null,
2045  null,
2046  OperandTypes.family(SqlTypeFamily.ANY),
2047  SqlFunctionCategory.SYSTEM);
2048  }
2049 
2050  @Override
2051  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2052  assert opBinding.getOperandCount() == 1;
2053  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2054  return typeFactory.createTypeWithNullability(
2055  typeFactory.createSqlType(SqlTypeName.INTEGER),
2056  opBinding.getOperandType(0).isNullable());
2057  }
2058  }
2059 
2060  static class ST_SetSRID extends SqlFunction {
2062  super("ST_SetSRID",
2063  SqlKind.OTHER_FUNCTION,
2064  null,
2065  null,
2066  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER),
2067  SqlFunctionCategory.SYSTEM);
2068  }
2069 
2070  @Override
2071  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2072  assert opBinding.getOperandCount() == 1;
2073  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2074  return typeFactory.createTypeWithNullability(
2075  typeFactory.createSqlType(SqlTypeName.INTEGER),
2076  opBinding.getOperandType(0).isNullable());
2077  }
2078  }
2079 
2080  static class ST_Point extends SqlFunction {
2082  super("ST_Point",
2083  SqlKind.OTHER_FUNCTION,
2084  null,
2085  null,
2086  OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.NUMERIC),
2087  SqlFunctionCategory.SYSTEM);
2088  }
2089 
2090  @Override
2091  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2092  assert opBinding.getOperandCount() == 2;
2093  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2094  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2095  }
2096  }
2097 
2098  static class ST_Centroid extends SqlFunction {
2100  super("ST_Centroid",
2101  SqlKind.OTHER_FUNCTION,
2102  null,
2103  null,
2104  OperandTypes.family(signature()),
2105  SqlFunctionCategory.SYSTEM);
2106  }
2107 
2108  @Override
2109  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2110  assert opBinding.getOperandCount() == 1;
2111  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2112  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2113  }
2114 
2115  private static java.util.List<SqlTypeFamily> signature() {
2116  java.util.List<SqlTypeFamily> st_centroid_sig =
2117  new java.util.ArrayList<SqlTypeFamily>();
2118  st_centroid_sig.add(SqlTypeFamily.ANY);
2119  return st_centroid_sig;
2120  }
2121  }
2122 
2123  static class ST_Buffer extends SqlFunction {
2125  super("ST_Buffer",
2126  SqlKind.OTHER_FUNCTION,
2127  null,
2128  null,
2129  OperandTypes.family(signature()),
2130  SqlFunctionCategory.SYSTEM);
2131  }
2132 
2133  @Override
2134  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2135  assert opBinding.getOperandCount() == 2;
2136  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2137  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2138  }
2139 
2140  private static java.util.List<SqlTypeFamily> signature() {
2141  java.util.List<SqlTypeFamily> st_buffer_sig =
2142  new java.util.ArrayList<SqlTypeFamily>();
2143  st_buffer_sig.add(SqlTypeFamily.ANY);
2144  st_buffer_sig.add(SqlTypeFamily.NUMERIC);
2145  return st_buffer_sig;
2146  }
2147  }
2148 
2149  static class ST_ConcaveHull extends SqlFunction {
2151  super("ST_ConcaveHull",
2152  SqlKind.OTHER_FUNCTION,
2153  null,
2154  null,
2155  OperandTypes.family(signature()),
2156  SqlFunctionCategory.SYSTEM);
2157  }
2158 
2159  @Override
2160  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2161  assert opBinding.getOperandCount() == 2;
2162  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2163  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2164  }
2165 
2166  private static java.util.List<SqlTypeFamily> signature() {
2167  java.util.List<SqlTypeFamily> st_concavehull_sig =
2168  new java.util.ArrayList<SqlTypeFamily>();
2169  st_concavehull_sig.add(SqlTypeFamily.ANY);
2170  st_concavehull_sig.add(SqlTypeFamily.NUMERIC);
2171  return st_concavehull_sig;
2172  }
2173  }
2174 
2175  static class ST_ConvexHull extends SqlFunction {
2177  super("ST_ConvexHull",
2178  SqlKind.OTHER_FUNCTION,
2179  null,
2180  null,
2181  OperandTypes.family(signature()),
2182  SqlFunctionCategory.SYSTEM);
2183  }
2184 
2185  @Override
2186  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2187  assert opBinding.getOperandCount() == 1;
2188  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2189  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2190  }
2191 
2192  private static java.util.List<SqlTypeFamily> signature() {
2193  java.util.List<SqlTypeFamily> st_convexhull_sig =
2194  new java.util.ArrayList<SqlTypeFamily>();
2195  st_convexhull_sig.add(SqlTypeFamily.ANY);
2196  return st_convexhull_sig;
2197  }
2198  }
2199 
2200  static class ST_Intersection extends SqlFunction {
2202  super("ST_Intersection",
2203  SqlKind.OTHER_FUNCTION,
2204  null,
2205  null,
2206  OperandTypes.family(signature()),
2207  SqlFunctionCategory.SYSTEM);
2208  }
2209 
2210  @Override
2211  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2212  assert opBinding.getOperandCount() == 2;
2213  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2214  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2215  }
2216 
2217  private static java.util.List<SqlTypeFamily> signature() {
2218  java.util.List<SqlTypeFamily> st_intersection_sig =
2219  new java.util.ArrayList<SqlTypeFamily>();
2220  st_intersection_sig.add(SqlTypeFamily.ANY);
2221  st_intersection_sig.add(SqlTypeFamily.ANY);
2222  return st_intersection_sig;
2223  }
2224  }
2225 
2226  static class ST_Union extends SqlFunction {
2228  super("ST_Union",
2229  SqlKind.OTHER_FUNCTION,
2230  null,
2231  null,
2232  OperandTypes.family(signature()),
2233  SqlFunctionCategory.SYSTEM);
2234  }
2235 
2236  @Override
2237  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2238  assert opBinding.getOperandCount() == 2;
2239  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2240  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2241  }
2242 
2243  private static java.util.List<SqlTypeFamily> signature() {
2244  java.util.List<SqlTypeFamily> st_union_sig =
2245  new java.util.ArrayList<SqlTypeFamily>();
2246  st_union_sig.add(SqlTypeFamily.ANY);
2247  st_union_sig.add(SqlTypeFamily.ANY);
2248  return st_union_sig;
2249  }
2250  }
2251 
2252  static class ST_Difference extends SqlFunction {
2254  super("ST_Difference",
2255  SqlKind.OTHER_FUNCTION,
2256  null,
2257  null,
2258  OperandTypes.family(signature()),
2259  SqlFunctionCategory.SYSTEM);
2260  }
2261 
2262  @Override
2263  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2264  assert opBinding.getOperandCount() == 2;
2265  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2266  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2267  }
2268 
2269  private static java.util.List<SqlTypeFamily> signature() {
2270  java.util.List<SqlTypeFamily> st_difference_sig =
2271  new java.util.ArrayList<SqlTypeFamily>();
2272  st_difference_sig.add(SqlTypeFamily.ANY);
2273  st_difference_sig.add(SqlTypeFamily.ANY);
2274  return st_difference_sig;
2275  }
2276  }
2277 
2278  static class CastToGeography extends SqlFunction {
2280  super("CastToGeography",
2281  SqlKind.OTHER_FUNCTION,
2282  null,
2283  null,
2284  OperandTypes.family(SqlTypeFamily.ANY),
2285  SqlFunctionCategory.SYSTEM);
2286  }
2287 
2288  @Override
2289  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2290  assert opBinding.getOperandCount() == 1;
2291  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2292  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2293  }
2294  }
2295 
2296  static class EncodeText extends SqlFunction {
2298  super("ENCODE_TEXT",
2299  SqlKind.OTHER_FUNCTION,
2300  null,
2301  null,
2302  OperandTypes.family(SqlTypeFamily.STRING),
2303  SqlFunctionCategory.SYSTEM);
2304  }
2305 
2306  @Override
2307  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2308  assert opBinding.getOperandCount() == 1;
2309  return opBinding.getOperandType(0);
2310  }
2311  }
2312 
2313  /* OFFSET_IN_FRAGMENT() */
2314  public static class OffsetInFragment extends SqlFunction {
2315  public OffsetInFragment() {
2316  super("OFFSET_IN_FRAGMENT",
2317  SqlKind.OTHER_FUNCTION,
2318  null,
2319  null,
2320  OperandTypes.NILADIC,
2321  SqlFunctionCategory.SYSTEM);
2322  }
2323 
2324  @Override
2325  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2326  assert opBinding.getOperandCount() == 0;
2327  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2328  return typeFactory.createSqlType(SqlTypeName.BIGINT);
2329  }
2330  }
2331 
2332  static class ApproxCountDistinct extends SqlAggFunction {
2334  super("APPROX_COUNT_DISTINCT",
2335  null,
2336  SqlKind.OTHER_FUNCTION,
2337  null,
2338  null,
2339  OperandTypes.or(OperandTypes.family(SqlTypeFamily.ANY),
2340  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.INTEGER)),
2341  SqlFunctionCategory.SYSTEM,
2342  false,
2343  false,
2344  Optionality.FORBIDDEN);
2345  }
2346 
2347  @Override
2348  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2349  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2350  return typeFactory.createSqlType(SqlTypeName.BIGINT);
2351  }
2352  }
2353 
2354  static class ApproxMedian extends SqlAggFunction {
2356  super("APPROX_MEDIAN",
2357  null,
2358  SqlKind.OTHER_FUNCTION,
2359  null,
2360  null,
2361  OperandTypes.family(SqlTypeFamily.NUMERIC),
2362  SqlFunctionCategory.SYSTEM,
2363  false,
2364  false,
2365  Optionality.FORBIDDEN);
2366  }
2367 
2368  @Override
2369  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2370  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2371  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
2372  }
2373  }
2374 
2375  static class ApproxPercentile extends SqlAggFunction {
2377  super("APPROX_PERCENTILE",
2378  null,
2379  SqlKind.OTHER_FUNCTION,
2380  null,
2381  null,
2382  OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.NUMERIC),
2383  SqlFunctionCategory.SYSTEM,
2384  false,
2385  false,
2386  Optionality.FORBIDDEN);
2387  }
2388 
2389  @Override
2390  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2391  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2392  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
2393  }
2394  }
2395 
2396  static class ApproxQuantile extends SqlAggFunction {
2398  super("APPROX_QUANTILE",
2399  null,
2400  SqlKind.OTHER_FUNCTION,
2401  null,
2402  null,
2403  OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.NUMERIC),
2404  SqlFunctionCategory.SYSTEM,
2405  false,
2406  false,
2407  Optionality.FORBIDDEN);
2408  }
2409 
2410  @Override
2411  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2412  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2413  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
2414  }
2415  }
2416 
2417  static class MapDAvg extends SqlAggFunction {
2419  super("AVG",
2420  null,
2421  SqlKind.OTHER_FUNCTION,
2422  null,
2423  null,
2424  OperandTypes.family(SqlTypeFamily.NUMERIC),
2425  SqlFunctionCategory.SYSTEM);
2426  }
2427 
2428  @Override
2429  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2430  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2431  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
2432  }
2433  }
2434 
2435  public static class Sample extends SqlAggFunction {
2436  public Sample() {
2437  super("SAMPLE",
2438  null,
2439  SqlKind.OTHER_FUNCTION,
2440  null,
2441  null,
2442  OperandTypes.ANY,
2443  SqlFunctionCategory.SYSTEM,
2444  false,
2445  false,
2446  Optionality.FORBIDDEN);
2447  }
2448 
2449  @Override
2450  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2451  return opBinding.getOperandType(0);
2452  }
2453  }
2454 
2455  // for backwards compatibility
2456  public static class LastSample extends SqlAggFunction {
2457  public LastSample() {
2458  super("LAST_SAMPLE",
2459  null,
2460  SqlKind.OTHER_FUNCTION,
2461  null,
2462  null,
2463  OperandTypes.ANY,
2464  SqlFunctionCategory.SYSTEM,
2465  false,
2466  false,
2467  Optionality.FORBIDDEN);
2468  }
2469 
2470  @Override
2471  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2472  return opBinding.getOperandType(0);
2473  }
2474  }
2475 
2476  static class ExtFunction extends SqlFunction {
2477  ExtFunction(final String name, final ExtensionFunction sig) {
2478  super(name,
2479  SqlKind.OTHER_FUNCTION,
2480  null,
2481  null,
2482  OperandTypes.family(sig.toSqlSignature()),
2483  SqlFunctionCategory.SYSTEM);
2484  ret = sig.getSqlRet();
2485  arg_names = sig.getArgNames();
2486  }
2487 
2488  @Override
2489  public List<String> getParamNames() {
2490  return arg_names;
2491  }
2492 
2493  @Override
2494  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2495  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2496  return typeFactory.createTypeWithNullability(typeFactory.createSqlType(ret), true);
2497  }
2498 
2499  private final SqlTypeName ret;
2500  private final List<String> arg_names;
2501  }
2502 
2503  public class ExtTableFunction extends SqlFunction implements SqlTableFunction {
2504  ExtTableFunction(final String name, final ExtensionFunction sig) {
2505  super(name,
2506  SqlKind.OTHER_FUNCTION,
2507  ReturnTypes.CURSOR,
2508  null,
2510  SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
2511  arg_types = sig.toSqlSignature();
2512  outs = sig.getSqlOuts();
2513  out_names = sig.getOutNames();
2514  arg_names = sig.getArgNames();
2515  // pretty_arg_names will be same as arg_names, except with
2516  // arg names for cursors stripped of array elements, i.e
2517  // my_cursor_input[x, y, z] => my_cursor_input
2518  pretty_arg_names = sig.getPrettyArgNames();
2519  options = sig.getOptions();
2520  cursor_field_types = sig.getCursorFieldTypes();
2521  }
2522 
2523  // The following method allows for parameter annotation
2524  // i.e. my_param => 3.
2525  // Note this method is deprecated, and it appears that
2526  // getParameters() is the correct method to use, but
2527  // need to troubleshoot why it's not being called
2528  @Override
2529  public List<String> getParamNames() {
2530  return pretty_arg_names;
2531  }
2532 
2533  // Per the above, this appears to be the non-deprecated way
2534  // to allow for parameter annotation, and it also allows
2535  // for specifying optionality.
2536  // However it currently is not being picked up/called,
2537  // so need to troubleshot. Leaving in for now as scaffolding
2538  // for the correct approach forward.
2539 
2540  // Note that OperandTypeInference seems another route
2541  // to implementing optionality.
2542 
2543  public List<FunctionParameter> getParameters() {
2544  final Boolean has_names = this.pretty_arg_names != null
2545  && this.pretty_arg_names.size() == this.arg_types.size();
2546  final List<FunctionParameter> parameters = new java.util.ArrayList<>();
2547  for (int i = 0; i < this.arg_types.size(); i++) {
2548  final int arg_idx = i;
2549  parameters.add(new FunctionParameter() {
2550  public int getOrdinal() {
2551  return arg_idx;
2552  }
2553 
2554  public String getName() {
2555  if (has_names) {
2556  return pretty_arg_names.get(arg_idx);
2557  }
2558  return "arg" + arg_idx;
2559  }
2560 
2561  public RelDataType getType(RelDataTypeFactory typeFactory) {
2562  return arg_types.get(arg_idx).getDefaultConcreteType(typeFactory);
2563  }
2564 
2565  public boolean isOptional() {
2566  return false;
2567  }
2568  });
2569  }
2570  return parameters;
2571  }
2572 
2573  @Override
2574  public SqlReturnTypeInference getRowTypeInference() {
2575  return opBinding -> {
2576  FieldInfoBuilder ret = opBinding.getTypeFactory().builder();
2577  for (int out_idx = 0; out_idx < outs.size(); ++out_idx) {
2578  ret = ret.add(out_names.get(out_idx), outs.get(out_idx));
2579  ret = ret.nullable(true);
2580  }
2581  return ret.build();
2582  };
2583  }
2584 
2585  private void debugPrint(String msg, Boolean debugMode) {
2586  if (debugMode) {
2587  System.out.println(msg);
2588  }
2589  }
2590 
2591  public Set<RelColumnMapping> getColumnMappings() {
2592  final Boolean debugMode = false;
2593  Set<RelColumnMapping> s = new HashSet<RelColumnMapping>();
2594  debugPrint("getNameAsId() -> " + getNameAsId() + ", arg_names=" + arg_names
2595  + ", out_names=" + out_names,
2596  debugMode);
2597  if (Integer.valueOf(options.getOrDefault("filter_table_function_transpose", "0"))
2598  == 1) {
2599  debugPrint("getNameAsId() -> " + getNameAsId(), debugMode);
2600  int rel_idx = -1;
2601  for (int arg_idx = 0; arg_idx < arg_names.size(); ++arg_idx) {
2602  String arg_name = arg_names.get(arg_idx);
2603  String[] fields;
2604  int start = arg_name.indexOf("[");
2605  if (start != -1) {
2606  rel_idx += 1;
2607  int end = arg_name.lastIndexOf("]");
2608  fields = arg_name.substring(start + 1, end)
2609  .replaceAll("\\s+", "")
2610  .split(",", 0);
2611  } else {
2612  fields = new String[] {arg_name};
2613  }
2614  debugPrint("fields=" + Arrays.toString(fields), debugMode);
2615  for (int field_idx = 0; field_idx < fields.length; ++field_idx) {
2616  int out_idx = out_names.indexOf(fields[field_idx]);
2617  if (out_idx >= 0) {
2618  s.add(new RelColumnMapping(out_idx, rel_idx, field_idx, false));
2619  debugPrint("out_idx, arg_idx/rel_idx, field_idx=" + out_idx + ", " + arg_idx
2620  + "/" + rel_idx + ", " + field_idx,
2621  debugMode);
2622  }
2623  }
2624  }
2625  }
2626  return s;
2627  }
2628 
2629  // Returns the table function's signature, with extended operand
2630  // type information, including CURSOR field types and operand
2631  // names.
2632 
2633  // E.g. for the following UDTF:
2634  // my_udtf(Column<int64_t> input_col) -> Column<int64_t>
2635  // it'll build the signature:
2636  // 'my_udtf(input_col => <CURSOR>[BIGINT])'
2637 
2638  public String getExtendedSignature() {
2639  StringBuilder ret = new StringBuilder();
2640  ret.append("'");
2641  ret.append(this.getName());
2642  ret.append("(");
2643 
2644  for (int i = 0; i < this.arg_types.size(); i++) {
2645  if (i > 0) {
2646  ret.append(", ");
2647  }
2648 
2649  SqlTypeFamily type = arg_types.get(i);
2650  String paramName = arg_names.get(i);
2651  ret.append(paramName).append(" => ");
2652 
2653  final String t = type.toString().toUpperCase(Locale.ROOT);
2654  ret.append("<").append(t).append(">");
2655  if (type == SqlTypeFamily.CURSOR) {
2656  List<ExtensionFunction.ExtArgumentType> field_types =
2657  cursor_field_types.get(paramName);
2658  ret.append("[");
2659  for (int j = 0; j < field_types.size(); j++) {
2660  if (j > 0) {
2661  ret.append(",");
2662  }
2663  ExtensionFunction.ExtArgumentType field_type = field_types.get(j);
2664  ret.append(ExtensionFunction.toSqlTypeName(field_type));
2665  if (ExtensionFunction.isColumnListType(field_type)) {
2666  ExtensionFunction.ExtArgumentType subtype =
2667  ExtensionFunction.getValueType(field_type);
2668  ret.append("[");
2669  ret.append(ExtensionFunction.toSqlTypeName(subtype));
2670  ret.append("]");
2671  } else if (ExtensionFunction.isColumnArrayType(field_type)) {
2672  }
2673  }
2674  ret.append("]");
2675  }
2676  }
2677  ret.append(")'");
2678 
2679  return ret.toString();
2680  }
2681 
2682  // This returns the original arg names, with CURSOR field names included.
2683  // Is used to map arguments to their input annotations.
2684  public List<String> getExtendedParamNames() {
2685  return this.arg_names;
2686  }
2687 
2688  // Required to store ExtTableFunctions in Collections
2689  @Override
2690  public int hashCode() {
2691  return this.getExtendedSignature().hashCode();
2692  }
2693 
2694  // Required to store ExtTableFunctions in Collections
2695  // We disambiguate ExtTableFunctions by their names and input arg
2696  // types, including CURSOR field types.
2697  @Override
2698  public boolean equals(final Object obj) {
2699  if (obj == null) {
2700  return false;
2701  }
2702 
2703  if (getClass() != obj.getClass()) {
2704  return false;
2705  }
2706 
2707  if (this == obj) {
2708  return true;
2709  }
2710 
2711  final ExtTableFunction other = (ExtTableFunction) obj;
2712  if (!this.getName().equals(other.getName())) {
2713  return false;
2714  }
2715  if (arg_types.size() != other.arg_types.size()) {
2716  return false;
2717  }
2718 
2719  for (int i = 0; i < arg_types.size(); i++) {
2720  if (arg_types.get(i) != other.arg_types.get(i)) {
2721  return false;
2722  }
2723  if (arg_types.get(i) == SqlTypeFamily.CURSOR) {
2724  String paramName = this.arg_names.get(i);
2725  String otherParamName = other.getExtendedParamNames().get(i);
2726  if (!paramName.equals(otherParamName)) {
2727  return false;
2728  }
2729 
2730  List<ExtensionFunction.ExtArgumentType> field_types =
2731  this.getCursorFieldTypes().get(paramName);
2732  List<ExtensionFunction.ExtArgumentType> other_field_types =
2733  other.getCursorFieldTypes().get(paramName);
2734  if (field_types.size() != other_field_types.size()) {
2735  return false;
2736  }
2737  for (int j = 0; j < field_types.size(); j++) {
2738  if (field_types.get(j) != other_field_types.get(j)) {
2739  return false;
2740  }
2741  }
2742  }
2743  }
2744  return true;
2745  }
2746 
2747  @Override
2748  public String toString() {
2749  return new String(getName() + "("
2750  + String.join(",",
2751  this.arg_types.stream()
2752  .map(s -> s.toString())
2753  .collect(Collectors.toList()))
2754  + ")");
2755  }
2756 
2757  Map<String, List<ExtensionFunction.ExtArgumentType>> getCursorFieldTypes() {
2758  return cursor_field_types;
2759  }
2760 
2761  List<SqlTypeFamily> getArgTypes() {
2762  return arg_types;
2763  }
2764 
2765  private final List<SqlTypeFamily> arg_types;
2766  private final List<SqlTypeName> outs;
2767  private final List<String> arg_names;
2768  private final List<String> pretty_arg_names;
2769  private final List<String> out_names;
2770  private final Map<String, String> options;
2771  private final Map<String, List<ExtensionFunction.ExtArgumentType>> cursor_field_types;
2772  }
2773 
2774  //
2775  // Internal accessors for in-situ poly render queries
2776  //
2777 
2778  static class HeavyDB_Geo_PolyBoundsPtr extends SqlFunction {
2780  super("HeavyDB_Geo_PolyBoundsPtr",
2781  SqlKind.OTHER_FUNCTION,
2782  null,
2783  null,
2784  OperandTypes.family(SqlTypeFamily.ANY),
2785  SqlFunctionCategory.SYSTEM);
2786  }
2787 
2788  @Override
2789  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2790  assert opBinding.getOperandCount() == 1;
2791  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2792  return typeFactory.createSqlType(SqlTypeName.BIGINT);
2793  }
2794  }
2795 
2796  static class HeavyDB_Geo_PolyRenderGroup extends SqlFunction {
2798  super("HeavyDB_Geo_PolyRenderGroup",
2799  SqlKind.OTHER_FUNCTION,
2800  null,
2801  null,
2802  OperandTypes.family(SqlTypeFamily.ANY),
2803  SqlFunctionCategory.SYSTEM);
2804  }
2805 
2806  @Override
2807  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2808  assert opBinding.getOperandCount() == 1;
2809  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2810  return typeFactory.createSqlType(SqlTypeName.INTEGER);
2811  }
2812  }
2813 
2814  static class convert_meters_to_pixel_width extends SqlFunction {
2816  super("convert_meters_to_pixel_width",
2817  SqlKind.OTHER_FUNCTION,
2818  null,
2819  null,
2820  OperandTypes.family(SqlTypeFamily.NUMERIC,
2821  SqlTypeFamily.ANY,
2822  SqlTypeFamily.NUMERIC,
2823  SqlTypeFamily.NUMERIC,
2824  SqlTypeFamily.NUMERIC,
2825  SqlTypeFamily.NUMERIC),
2826  SqlFunctionCategory.SYSTEM);
2827  }
2828 
2829  @Override
2830  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2831  assert opBinding.getOperandCount() == 6;
2832  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2833  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
2834  }
2835  }
2836 
2837  static class convert_meters_to_pixel_height extends SqlFunction {
2839  super("convert_meters_to_pixel_height",
2840  SqlKind.OTHER_FUNCTION,
2841  null,
2842  null,
2843  OperandTypes.family(SqlTypeFamily.NUMERIC,
2844  SqlTypeFamily.ANY,
2845  SqlTypeFamily.NUMERIC,
2846  SqlTypeFamily.NUMERIC,
2847  SqlTypeFamily.NUMERIC,
2848  SqlTypeFamily.NUMERIC),
2849  SqlFunctionCategory.SYSTEM);
2850  }
2851 
2852  @Override
2853  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2854  assert opBinding.getOperandCount() == 6;
2855  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2856  return typeFactory.createSqlType(SqlTypeName.DOUBLE);
2857  }
2858  }
2859 
2860  static class is_point_in_view extends SqlFunction {
2862  super("is_point_in_view",
2863  SqlKind.OTHER_FUNCTION,
2864  null,
2865  null,
2866  OperandTypes.family(SqlTypeFamily.ANY,
2867  SqlTypeFamily.NUMERIC,
2868  SqlTypeFamily.NUMERIC,
2869  SqlTypeFamily.NUMERIC,
2870  SqlTypeFamily.NUMERIC),
2871  SqlFunctionCategory.SYSTEM);
2872  }
2873 
2874  @Override
2875  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2876  assert opBinding.getOperandCount() == 5;
2877  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2878  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
2879  }
2880  }
2881 
2882  static class is_point_size_in_view extends SqlFunction {
2884  super("is_point_size_in_view",
2885  SqlKind.OTHER_FUNCTION,
2886  null,
2887  null,
2888  OperandTypes.family(SqlTypeFamily.ANY,
2889  SqlTypeFamily.NUMERIC,
2890  SqlTypeFamily.NUMERIC,
2891  SqlTypeFamily.NUMERIC,
2892  SqlTypeFamily.NUMERIC,
2893  SqlTypeFamily.NUMERIC),
2894  SqlFunctionCategory.SYSTEM);
2895  }
2896 
2897  @Override
2898  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2899  assert opBinding.getOperandCount() == 6;
2900  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2901  return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
2902  }
2903  }
2904 
2905  public static class usTimestamp extends SqlFunction {
2906  public usTimestamp() {
2907  super("usTIMESTAMP",
2908  SqlKind.OTHER_FUNCTION,
2909  null,
2910  null,
2911  OperandTypes.STRING,
2912  SqlFunctionCategory.SYSTEM);
2913  }
2914 
2915  @Override
2916  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2917  assert opBinding.getOperandCount() == 1;
2918  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2919  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 6);
2920  }
2921  }
2922 
2923  public static class nsTimestamp extends SqlFunction {
2924  public nsTimestamp() {
2925  super("nsTIMESTAMP",
2926  SqlKind.OTHER_FUNCTION,
2927  null,
2928  null,
2929  OperandTypes.STRING,
2930  SqlFunctionCategory.SYSTEM);
2931  }
2932 
2933  @Override
2934  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
2935  assert opBinding.getOperandCount() == 1;
2936  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
2937  return typeFactory.createSqlType(SqlTypeName.TIMESTAMP, 9);
2938  }
2939  }
2940 }
2941 
2942 // End HeavyDBSqlOperatorTable.java
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)
EXTENSION_INLINE int64_t HeavyDB_Geo_PolyBoundsPtr(double *bounds, int64_t size)
final java.util.List< SqlTypeFamily > toSqlSignature()
size_t append(FILE *f, const size_t size, const int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
Definition: File.cpp:168
ExtTableFunction(final String name, final ExtensionFunction sig)
EXTENSION_NOINLINE double ST_XMax(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
Map< String, List< ExtensionFunction.ExtArgumentType > > getCursorFieldTypes()
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)
SqlCall createCall(@Nullable SqlLiteral functionQualifier, SqlParserPos pos,@Nullable SqlNode...operands)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_NOINLINE double ST_XMin(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
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)
tuple STRING
Definition: dtypes.py:31
constexpr double f
Definition: Utm.h:31
boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static boolean isColumnListType(final ExtArgumentType type)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static java.util.List< SqlTypeFamily > getSignatureFamilies()
RelDataType inferReturnType(SqlOperatorBinding opBinding)
RelDataType getComponentType(RelDataTypeFactory typeFactory, List< RelDataType > argTypes)
EXTENSION_NOINLINE double ST_YMin(int8_t *coords, int64_t size, int32_t ic, int32_t isr, int32_t osr)
SqlCall createCall(@Nullable SqlLiteral functionQualifier, SqlParserPos pos,@Nullable SqlNode...operands)
static java.util.List< SqlTypeFamily > getSignatureFamilies()
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static boolean isColumnArrayType(final ExtArgumentType type)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
EXTENSION_INLINE int32_t HeavyDB_Geo_PolyRenderGroup(int32_t render_group)
void addUDF(final Map< String, ExtensionFunction > extSigs)
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)
ExtFunction(final String name, final ExtensionFunction sig)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static java.util.List< SqlTypeFamily > getSignatureFamilies()
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static java.util.List< SqlTypeFamily > getSignatureFamilies()
SqlCall createCall(@Nullable SqlLiteral functionQualifier, SqlParserPos pos,@Nullable SqlNode...operands)
SqlCall createCall(@Nullable SqlLiteral functionQualifier, SqlParserPos pos,@Nullable SqlNode...operands)
EXTENSION_NOINLINE double Truncate(const double x, const int32_t y)
SqlCall createCall(@Nullable SqlLiteral functionQualifier, SqlParserPos pos,@Nullable SqlNode...operands)
string name
Definition: setup.in.py:72
RelDataType inferReturnType(SqlOperatorBinding opBinding)
static final SqlArrayValueConstructorAllowingEmpty ARRAY_VALUE_CONSTRUCTOR
RelDataType inferReturnType(SqlOperatorBinding opBinding)
final Map< String, List< ExtensionFunction.ExtArgumentType > > cursor_field_types
void lookupOperatorOverloads(SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax, List< SqlOperator > operatorList, SqlNameMatcher nameMatcher)
RelDataType inferReturnType(SqlOperatorBinding opBinding)