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