Search in sources :

Example 46 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project flink by splunk.

the class SqlValidatorImpl method deriveConstructorType.

public RelDataType deriveConstructorType(SqlValidatorScope scope, SqlCall call, SqlFunction unresolvedConstructor, SqlFunction resolvedConstructor, List<RelDataType> argTypes) {
    SqlIdentifier sqlIdentifier = unresolvedConstructor.getSqlIdentifier();
    assert sqlIdentifier != null;
    RelDataType type = catalogReader.getNamedType(sqlIdentifier);
    if (type == null) {
        // TODO jvs 12-Feb-2005:  proper type name formatting
        throw newValidationError(sqlIdentifier, RESOURCE.unknownDatatypeName(sqlIdentifier.toString()));
    }
    if (resolvedConstructor == null) {
        if (call.operandCount() > 0) {
            // no user-defined constructor could be found
            throw handleUnresolvedFunction(call, unresolvedConstructor, argTypes, null);
        }
    } else {
        SqlCall testCall = resolvedConstructor.createCall(call.getParserPosition(), call.getOperandList());
        RelDataType returnType = resolvedConstructor.validateOperands(this, scope, testCall);
        assert type == returnType;
    }
    if (config.identifierExpansion()) {
        if (resolvedConstructor != null) {
            ((SqlBasicCall) call).setOperator(resolvedConstructor);
        } else {
            // fake a fully-qualified call to the default constructor
            ((SqlBasicCall) call).setOperator(new SqlFunction(type.getSqlIdentifier(), ReturnTypes.explicit(type), null, null, null, SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR));
        }
    }
    return type;
}
Also used : SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlCall(org.apache.calcite.sql.SqlCall) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 47 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project flink by splunk.

the class SqlValidatorImpl method validateCall.

public void validateCall(SqlCall call, SqlValidatorScope scope) {
    final SqlOperator operator = call.getOperator();
    if ((call.operandCount() == 0) && (operator.getSyntax() == SqlSyntax.FUNCTION_ID) && !call.isExpanded() && !this.config.sqlConformance().allowNiladicParentheses()) {
        // SqlIdentifier.)
        throw handleUnresolvedFunction(call, (SqlFunction) operator, ImmutableList.of(), null);
    }
    SqlValidatorScope operandScope = scope.getOperandScope(call);
    if (operator instanceof SqlFunction && ((SqlFunction) operator).getFunctionType() == SqlFunctionCategory.MATCH_RECOGNIZE && !(operandScope instanceof MatchRecognizeScope)) {
        throw newValidationError(call, Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
    }
    // Delegate validation to the operator.
    operator.validateCall(call, this, scope, operandScope);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 48 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project flink by splunk.

the class LegacyScalarFunctionConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    FunctionDefinition def = call.getFunctionDefinition();
    if (def instanceof ScalarFunctionDefinition) {
        ScalarFunction scalaFunc = ((ScalarFunctionDefinition) def).getScalarFunction();
        FunctionIdentifier identifier = call.getFunctionIdentifier().orElse(FunctionIdentifier.of(generateInlineFunctionName(scalaFunc)));
        SqlFunction sqlFunction = UserDefinedFunctionUtils.createScalarSqlFunction(identifier, scalaFunc.toString(), scalaFunc, context.getTypeFactory());
        return Optional.of(context.getRelBuilder().call(sqlFunction, toRexNodes(context, call.getChildren())));
    }
    return Optional.empty();
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) ScalarFunction(org.apache.flink.table.functions.ScalarFunction) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 49 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project flink-mirror by flink-ci.

the class FunctionCatalogOperatorTable method convertToBridgingSqlFunction.

private Optional<SqlFunction> convertToBridgingSqlFunction(@Nullable SqlFunctionCategory category, ContextResolvedFunction resolvedFunction) {
    final FunctionDefinition definition = resolvedFunction.getDefinition();
    if (!verifyFunctionKind(category, resolvedFunction)) {
        return Optional.empty();
    }
    final TypeInference typeInference;
    try {
        typeInference = definition.getTypeInference(dataTypeFactory);
    } catch (Throwable t) {
        throw new ValidationException(String.format("An error occurred in the type inference logic of function '%s'.", resolvedFunction), t);
    }
    if (typeInference.getOutputTypeStrategy() == TypeStrategies.MISSING) {
        return Optional.empty();
    }
    final SqlFunction function;
    if (definition.getKind() == FunctionKind.AGGREGATE || definition.getKind() == FunctionKind.TABLE_AGGREGATE) {
        function = BridgingSqlAggFunction.of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
    } else {
        function = BridgingSqlFunction.of(dataTypeFactory, typeFactory, SqlKind.OTHER_FUNCTION, resolvedFunction, typeInference);
    }
    return Optional.of(function);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) ValidationException(org.apache.flink.table.api.ValidationException) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) HiveAggSqlFunction(org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction) SqlFunction(org.apache.calcite.sql.SqlFunction) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) HiveTableSqlFunction(org.apache.flink.table.planner.functions.utils.HiveTableSqlFunction)

Example 50 with SqlFunction

use of org.apache.calcite.sql.SqlFunction in project flink-mirror by flink-ci.

the class LegacyScalarFunctionConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    FunctionDefinition def = call.getFunctionDefinition();
    if (def instanceof ScalarFunctionDefinition) {
        ScalarFunction scalaFunc = ((ScalarFunctionDefinition) def).getScalarFunction();
        FunctionIdentifier identifier = call.getFunctionIdentifier().orElse(FunctionIdentifier.of(generateInlineFunctionName(scalaFunc)));
        SqlFunction sqlFunction = UserDefinedFunctionUtils.createScalarSqlFunction(identifier, scalaFunc.toString(), scalaFunc, context.getTypeFactory());
        return Optional.of(context.getRelBuilder().call(sqlFunction, toRexNodes(context, call.getChildren())));
    }
    return Optional.empty();
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) ScalarFunction(org.apache.flink.table.functions.ScalarFunction) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) SqlFunction(org.apache.calcite.sql.SqlFunction)

Aggregations

SqlFunction (org.apache.calcite.sql.SqlFunction)57 SqlOperator (org.apache.calcite.sql.SqlOperator)26 ArrayList (java.util.ArrayList)13 RoundOperatorConversion (org.apache.druid.sql.calcite.expression.builtin.RoundOperatorConversion)12 Test (org.junit.Test)12 RexNode (org.apache.calcite.rex.RexNode)8 SqlCall (org.apache.calcite.sql.SqlCall)8 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)8 AssignableOperandTypeChecker (org.apache.calcite.sql.type.AssignableOperandTypeChecker)6 BitString (org.apache.calcite.util.BitString)6 ImmutableList (com.google.common.collect.ImmutableList)5 RelDataType (org.apache.calcite.rel.type.RelDataType)5 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)5 RexCall (org.apache.calcite.rex.RexCall)4 RexLiteral (org.apache.calcite.rex.RexLiteral)4 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)4 ScalarFunctionDefinition (org.apache.flink.table.functions.ScalarFunctionDefinition)4 BigDecimal (java.math.BigDecimal)3 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)3 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)3