|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.api.spec; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.function.BiFunction; |
| 11 | +import javax.annotation.Nullable; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import org.apache.calcite.rel.type.RelDataType; |
| 14 | +import org.apache.calcite.rel.type.RelDataTypeFactory; |
| 15 | +import org.apache.calcite.rex.RexBuilder; |
| 16 | +import org.apache.calcite.rex.RexCall; |
| 17 | +import org.apache.calcite.rex.RexNode; |
| 18 | +import org.apache.calcite.sql.SqlCallBinding; |
| 19 | +import org.apache.calcite.sql.SqlFunction; |
| 20 | +import org.apache.calcite.sql.SqlFunctionCategory; |
| 21 | +import org.apache.calcite.sql.SqlIdentifier; |
| 22 | +import org.apache.calcite.sql.SqlKind; |
| 23 | +import org.apache.calcite.sql.SqlOperandCountRange; |
| 24 | +import org.apache.calcite.sql.SqlOperator; |
| 25 | +import org.apache.calcite.sql.parser.SqlParserPos; |
| 26 | +import org.apache.calcite.sql.type.InferTypes; |
| 27 | +import org.apache.calcite.sql.type.OperandTypes; |
| 28 | +import org.apache.calcite.sql.type.SqlOperandCountRanges; |
| 29 | +import org.apache.calcite.sql.type.SqlOperandMetadata; |
| 30 | +import org.apache.calcite.sql.type.SqlReturnTypeInference; |
| 31 | +import org.apache.calcite.sql.type.SqlTypeFamily; |
| 32 | +import org.apache.calcite.sql.validate.SqlUserDefinedFunction; |
| 33 | + |
| 34 | +/** Fluent DSL for building {@link UnifiedFunctionSpec} instances. */ |
| 35 | +@RequiredArgsConstructor |
| 36 | +class FunctionSpecBuilder { |
| 37 | + /** Function name to register. */ |
| 38 | + private final String name; |
| 39 | + |
| 40 | + /** |
| 41 | + * Wraps an existing Calcite operator, preserving its native type system and RexImpTable |
| 42 | + * implementation for in-memory execution. |
| 43 | + * |
| 44 | + * @param op the Calcite operator to delegate to |
| 45 | + * @return a builder that produces the spec on {@code build()} |
| 46 | + */ |
| 47 | + DelegateFunctionBuilder delegateTo(SqlOperator op) { |
| 48 | + return new DelegateFunctionBuilder(name, op); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Builds a pushdown-only UDF with relaxed type checking. The resulting function has no local |
| 53 | + * implementation and delegates execution to the data source via pushdown. |
| 54 | + * |
| 55 | + * @param paramNames required parameter names for signature display |
| 56 | + * @return a builder that produces the spec on {@code build()} |
| 57 | + */ |
| 58 | + CatalogFunctionBuilder vararg(String... paramNames) { |
| 59 | + return new CatalogFunctionBuilder(name, List.of(paramNames)); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Builds a typed SqlFunction with strict operand type checking. Optionally accepts a late-binding |
| 64 | + * {@code impl} that rewrites the function into executable Calcite expressions at compilation |
| 65 | + * time. |
| 66 | + * |
| 67 | + * @param families operand type families for validation |
| 68 | + * @return a builder that produces the spec on {@code build()} |
| 69 | + */ |
| 70 | + DefaultFunctionBuilder operands(SqlTypeFamily... families) { |
| 71 | + return new DefaultFunctionBuilder(name, families); |
| 72 | + } |
| 73 | + |
| 74 | + @RequiredArgsConstructor |
| 75 | + static class DefaultFunctionBuilder { |
| 76 | + private final String name; |
| 77 | + private final SqlTypeFamily[] operandFamilies; |
| 78 | + private SqlReturnTypeInference returnType; |
| 79 | + private SqlFunctionCategory category = SqlFunctionCategory.USER_DEFINED_FUNCTION; |
| 80 | + private @Nullable BiFunction<RexBuilder, RexCall, RexNode> impl; |
| 81 | + |
| 82 | + DefaultFunctionBuilder returns(SqlReturnTypeInference type) { |
| 83 | + this.returnType = type; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + DefaultFunctionBuilder category(SqlFunctionCategory cat) { |
| 88 | + this.category = cat; |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Defines how this function executes by rewriting to existing Calcite operators. Applied only |
| 94 | + * at compilation time (late binding) — the logical plan preserves the original function call. |
| 95 | + * |
| 96 | + * @param impl rewrite function that converts this call into executable RexNodes |
| 97 | + * @return this builder |
| 98 | + */ |
| 99 | + DefaultFunctionBuilder impl(BiFunction<RexBuilder, RexCall, RexNode> impl) { |
| 100 | + this.impl = impl; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + UnifiedFunctionSpec build() { |
| 105 | + Objects.requireNonNull(returnType, "returns() is required"); |
| 106 | + SqlFunction op = |
| 107 | + new SqlFunction( |
| 108 | + name.toUpperCase(), |
| 109 | + SqlKind.OTHER_FUNCTION, |
| 110 | + returnType, |
| 111 | + null, |
| 112 | + OperandTypes.family(operandFamilies), |
| 113 | + category); |
| 114 | + return new UnifiedFunctionSpec(name.toLowerCase(), op, impl); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @RequiredArgsConstructor |
| 119 | + static class DelegateFunctionBuilder { |
| 120 | + private final String name; |
| 121 | + private final SqlOperator operator; |
| 122 | + |
| 123 | + UnifiedFunctionSpec build() { |
| 124 | + return new UnifiedFunctionSpec(name.toLowerCase(), operator, null); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @RequiredArgsConstructor |
| 129 | + static class CatalogFunctionBuilder { |
| 130 | + private final String name; |
| 131 | + private final List<String> paramNames; |
| 132 | + private SqlReturnTypeInference returnType; |
| 133 | + |
| 134 | + CatalogFunctionBuilder returnType(SqlReturnTypeInference type) { |
| 135 | + this.returnType = type; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + UnifiedFunctionSpec build() { |
| 140 | + Objects.requireNonNull(returnType, "returnType is required"); |
| 141 | + return new UnifiedFunctionSpec( |
| 142 | + name, |
| 143 | + new SqlUserDefinedFunction( |
| 144 | + new SqlIdentifier(name, SqlParserPos.ZERO), |
| 145 | + SqlKind.OTHER_FUNCTION, |
| 146 | + returnType, |
| 147 | + InferTypes.ANY_NULLABLE, |
| 148 | + new VariadicOperandMetadata(paramNames), |
| 149 | + List::of), // Pushdown-only: no local implementation |
| 150 | + null); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Custom operand metadata that bypasses Calcite's built-in type checking. Calcite's {@code |
| 156 | + * FamilyOperandTypeChecker} rejects variadic calls (CALCITE-5366), so this implementation accepts |
| 157 | + * any operand types and delegates validation to pushdown. |
| 158 | + */ |
| 159 | + record VariadicOperandMetadata(List<String> paramNames) implements SqlOperandMetadata { |
| 160 | + |
| 161 | + @Override |
| 162 | + public List<String> paramNames() { |
| 163 | + return paramNames; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public List<RelDataType> paramTypes(RelDataTypeFactory tf) { |
| 168 | + return List.of(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean checkOperandTypes(SqlCallBinding binding, boolean throwOnFailure) { |
| 173 | + return true; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public SqlOperandCountRange getOperandCountRange() { |
| 178 | + return SqlOperandCountRanges.from(paramNames.size()); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public String getAllowedSignatures(SqlOperator op, String opName) { |
| 183 | + return opName + "(" + String.join(", ", paramNames) + "[, option=value ...])"; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments