|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.cdc.runtime.functions; |
| 19 | + |
| 20 | +import org.apache.flink.annotation.Internal; |
| 21 | +import org.apache.flink.table.functions.BuiltInFunctionDefinition; |
| 22 | + |
| 23 | +import org.apache.calcite.sql.SqlFunction; |
| 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.SqlOperandTypeChecker; |
| 28 | +import org.apache.calcite.sql.type.SqlOperandTypeInference; |
| 29 | +import org.apache.calcite.sql.type.SqlReturnTypeInference; |
| 30 | +import org.apache.calcite.sql.validate.SqlMonotonicity; |
| 31 | + |
| 32 | +import javax.annotation.Nullable; |
| 33 | + |
| 34 | +import java.util.Optional; |
| 35 | +import java.util.function.Function; |
| 36 | + |
| 37 | +import static org.apache.flink.table.functions.BuiltInFunctionDefinition.DEFAULT_VERSION; |
| 38 | +import static org.apache.flink.table.functions.BuiltInFunctionDefinition.qualifyFunctionName; |
| 39 | +import static org.apache.flink.table.functions.BuiltInFunctionDefinition.validateFunction; |
| 40 | +import static org.apache.flink.util.Preconditions.checkNotNull; |
| 41 | + |
| 42 | +/** |
| 43 | + * SQL version of {@link BuiltInFunctionDefinition}. This is the case when the operator has a |
| 44 | + * special parsing syntax or uses other Calcite-specific features that are not exposed via {@link |
| 45 | + * BuiltInFunctionDefinition} yet. |
| 46 | + * |
| 47 | + * <p>Note: Try to keep usages of this class to a minimum and use Flink's {@link |
| 48 | + * BuiltInFunctionDefinition} stack instead. |
| 49 | + * |
| 50 | + * <p>For simple functions, use the provided builder. Otherwise, this class can also be extended. |
| 51 | + */ |
| 52 | +@Internal |
| 53 | +public class BuiltInSqlFunction extends SqlFunction { |
| 54 | + |
| 55 | + private final @Nullable Integer version; |
| 56 | + |
| 57 | + private final boolean isDeterministic; |
| 58 | + |
| 59 | + private final boolean isInternal; |
| 60 | + |
| 61 | + private final Function<SqlOperatorBinding, SqlMonotonicity> monotonicity; |
| 62 | + |
| 63 | + protected BuiltInSqlFunction( |
| 64 | + String name, |
| 65 | + int version, |
| 66 | + SqlKind kind, |
| 67 | + @Nullable SqlReturnTypeInference returnTypeInference, |
| 68 | + @Nullable SqlOperandTypeInference operandTypeInference, |
| 69 | + @Nullable SqlOperandTypeChecker operandTypeChecker, |
| 70 | + SqlFunctionCategory category, |
| 71 | + boolean isDeterministic, |
| 72 | + boolean isInternal, |
| 73 | + Function<SqlOperatorBinding, SqlMonotonicity> monotonicity) { |
| 74 | + super( |
| 75 | + checkNotNull(name), |
| 76 | + checkNotNull(kind), |
| 77 | + returnTypeInference, |
| 78 | + operandTypeInference, |
| 79 | + operandTypeChecker, |
| 80 | + checkNotNull(category)); |
| 81 | + this.version = isInternal ? null : version; |
| 82 | + this.isDeterministic = isDeterministic; |
| 83 | + this.isInternal = isInternal; |
| 84 | + this.monotonicity = monotonicity; |
| 85 | + validateFunction(name, version, isInternal); |
| 86 | + } |
| 87 | + |
| 88 | + protected BuiltInSqlFunction( |
| 89 | + String name, |
| 90 | + SqlKind kind, |
| 91 | + SqlReturnTypeInference returnTypeInference, |
| 92 | + SqlOperandTypeInference operandTypeInference, |
| 93 | + @Nullable SqlOperandTypeChecker operandTypeChecker, |
| 94 | + SqlFunctionCategory category) { |
| 95 | + this( |
| 96 | + name, |
| 97 | + DEFAULT_VERSION, |
| 98 | + kind, |
| 99 | + returnTypeInference, |
| 100 | + operandTypeInference, |
| 101 | + operandTypeChecker, |
| 102 | + category, |
| 103 | + true, |
| 104 | + false, |
| 105 | + call -> SqlMonotonicity.NOT_MONOTONIC); |
| 106 | + } |
| 107 | + |
| 108 | + /** Builder for configuring and creating instances of {@link BuiltInSqlFunction}. */ |
| 109 | + public static Builder newBuilder() { |
| 110 | + return new Builder(); |
| 111 | + } |
| 112 | + |
| 113 | + public final Optional<Integer> getVersion() { |
| 114 | + return Optional.ofNullable(version); |
| 115 | + } |
| 116 | + |
| 117 | + public String getQualifiedName() { |
| 118 | + if (isInternal) { |
| 119 | + return getName(); |
| 120 | + } |
| 121 | + assert version != null; |
| 122 | + return qualifyFunctionName(getName(), version); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean isDeterministic() { |
| 127 | + return isDeterministic; |
| 128 | + } |
| 129 | + |
| 130 | + public final boolean isInternal() { |
| 131 | + return isInternal; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) { |
| 136 | + return monotonicity.apply(call); |
| 137 | + } |
| 138 | + |
| 139 | + // -------------------------------------------------------------------------------------------- |
| 140 | + // Builder |
| 141 | + // -------------------------------------------------------------------------------------------- |
| 142 | + |
| 143 | + /** Builder for fluent definition of built-in functions. */ |
| 144 | + public static class Builder { |
| 145 | + |
| 146 | + private String name; |
| 147 | + |
| 148 | + private int version = DEFAULT_VERSION; |
| 149 | + |
| 150 | + private SqlKind kind = SqlKind.OTHER_FUNCTION; |
| 151 | + |
| 152 | + private SqlReturnTypeInference returnTypeInference; |
| 153 | + |
| 154 | + private SqlOperandTypeInference operandTypeInference; |
| 155 | + |
| 156 | + private SqlOperandTypeChecker operandTypeChecker; |
| 157 | + |
| 158 | + private SqlFunctionCategory category = SqlFunctionCategory.SYSTEM; |
| 159 | + |
| 160 | + private boolean isInternal = false; |
| 161 | + |
| 162 | + private boolean isDeterministic = true; |
| 163 | + |
| 164 | + private Function<SqlOperatorBinding, SqlMonotonicity> monotonicity = |
| 165 | + call -> SqlMonotonicity.NOT_MONOTONIC; |
| 166 | + |
| 167 | + /** @see BuiltInFunctionDefinition.Builder#name(String) */ |
| 168 | + public Builder name(String name) { |
| 169 | + this.name = name; |
| 170 | + return this; |
| 171 | + } |
| 172 | + |
| 173 | + /** @see BuiltInFunctionDefinition.Builder#version(int) */ |
| 174 | + public Builder version(int version) { |
| 175 | + this.version = version; |
| 176 | + return this; |
| 177 | + } |
| 178 | + |
| 179 | + public Builder kind(SqlKind kind) { |
| 180 | + this.kind = kind; |
| 181 | + return this; |
| 182 | + } |
| 183 | + |
| 184 | + public Builder returnType(SqlReturnTypeInference returnTypeInference) { |
| 185 | + this.returnTypeInference = returnTypeInference; |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + public Builder operandTypeInference(SqlOperandTypeInference operandTypeInference) { |
| 190 | + this.operandTypeInference = operandTypeInference; |
| 191 | + return this; |
| 192 | + } |
| 193 | + |
| 194 | + public Builder operandTypeChecker(SqlOperandTypeChecker operandTypeChecker) { |
| 195 | + this.operandTypeChecker = operandTypeChecker; |
| 196 | + return this; |
| 197 | + } |
| 198 | + |
| 199 | + public Builder category(SqlFunctionCategory category) { |
| 200 | + this.category = category; |
| 201 | + return this; |
| 202 | + } |
| 203 | + |
| 204 | + public Builder notDeterministic() { |
| 205 | + this.isDeterministic = false; |
| 206 | + return this; |
| 207 | + } |
| 208 | + |
| 209 | + /** @see BuiltInFunctionDefinition.Builder#internal() */ |
| 210 | + public Builder internal() { |
| 211 | + this.isInternal = true; |
| 212 | + return this; |
| 213 | + } |
| 214 | + |
| 215 | + public Builder monotonicity(SqlMonotonicity staticMonotonicity) { |
| 216 | + this.monotonicity = call -> staticMonotonicity; |
| 217 | + return this; |
| 218 | + } |
| 219 | + |
| 220 | + public Builder monotonicity(Function<SqlOperatorBinding, SqlMonotonicity> monotonicity) { |
| 221 | + this.monotonicity = monotonicity; |
| 222 | + return this; |
| 223 | + } |
| 224 | + |
| 225 | + public BuiltInSqlFunction build() { |
| 226 | + return new BuiltInSqlFunction( |
| 227 | + name, |
| 228 | + version, |
| 229 | + kind, |
| 230 | + returnTypeInference, |
| 231 | + operandTypeInference, |
| 232 | + operandTypeChecker, |
| 233 | + category, |
| 234 | + isDeterministic, |
| 235 | + isInternal, |
| 236 | + monotonicity); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments