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