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