OmniSciDB  b28c0d5765
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlLeadLag.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.parser.extension.ddl;
18 
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableList;
21 
22 import org.apache.calcite.rel.type.RelDataType;
23 import org.apache.calcite.sql.SqlAggFunction;
24 import org.apache.calcite.sql.SqlFunctionCategory;
25 import org.apache.calcite.sql.SqlKind;
26 import org.apache.calcite.sql.SqlOperatorBinding;
27 import org.apache.calcite.sql.type.*;
28 import org.apache.calcite.util.Optionality;
29 
30 import java.util.List;
31 
32 // COPY of SqlLeadLagAggFunction in Calcite 1.25 code base with slight modifications
33 public class SqlLeadLag extends SqlAggFunction {
34  private static final SqlSingleOperandTypeChecker OPERAND_TYPES =
35  OperandTypes.or(OperandTypes.ANY,
36  OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.NUMERIC),
37  OperandTypes.and(OperandTypes.family(SqlTypeFamily.ANY,
38  SqlTypeFamily.NUMERIC,
39  SqlTypeFamily.ANY),
40  // Arguments 1 and 3 must have same type
41  new SameOperandTypeChecker(3) {
42  @Override
43  protected List<Integer> getOperandList(int operandCount) {
44  return ImmutableList.of(0, 2);
45  }
46  }));
47 
48  private static final SqlReturnTypeInference RETURN_TYPE =
49  ReturnTypes.ARG0.andThen(SqlLeadLag::transformType);
50 
51  public SqlLeadLag(String functionName, SqlKind kind) {
52  super(functionName,
53  null,
54  kind,
56  null,
58  SqlFunctionCategory.NUMERIC,
59  false,
60  true,
61  Optionality.FORBIDDEN);
62  Preconditions.checkArgument(kind == SqlKind.LEAD || kind == SqlKind.LAG);
63  }
64 
65  // Result is NOT NULL if NOT NULL default value is provided
66  private static RelDataType transformType(SqlOperatorBinding binding, RelDataType type) {
67  SqlTypeTransform transform =
68  binding.getOperandCount() < 3 || binding.getOperandType(2).isNullable()
69  ? SqlTypeTransforms.FORCE_NULLABLE
70  : SqlTypeTransforms.TO_NOT_NULLABLE;
71  return transform.transformType(binding, type);
72  }
73 
74  // we now want to allow lead and lag functions with framing
75  // i.e., LeadInFrame and LagInFrame
76  @Override
77  public boolean allowsFraming() {
78  return true;
79  }
80 
81  @Override
82  public boolean allowsNullTreatment() {
83  return true;
84  }
85 }
static final SqlSingleOperandTypeChecker OPERAND_TYPES
Definition: SqlLeadLag.java:34
OUTPUT transform(INPUT const &input, FUNC const &func)
Definition: misc.h:320
static final SqlReturnTypeInference RETURN_TYPE
Definition: SqlLeadLag.java:48
static RelDataType transformType(SqlOperatorBinding binding, RelDataType type)
Definition: SqlLeadLag.java:66
SqlLeadLag(String functionName, SqlKind kind)
Definition: SqlLeadLag.java:51