Search in sources :

Example 1 with FunctionDefinition

use of org.apache.flink.table.functions.FunctionDefinition in project flink by apache.

the class FunctionCatalogOperatorTable method verifyFunctionKind.

/**
 * Verifies which kinds of functions are allowed to be returned from the catalog given the
 * context information.
 */
private boolean verifyFunctionKind(@Nullable SqlFunctionCategory category, ContextResolvedFunction resolvedFunction) {
    final FunctionDefinition definition = resolvedFunction.getDefinition();
    // built-in functions without implementation are handled separately
    if (definition instanceof BuiltInFunctionDefinition) {
        final BuiltInFunctionDefinition builtInFunction = (BuiltInFunctionDefinition) definition;
        if (!builtInFunction.hasRuntimeImplementation()) {
            return false;
        }
    }
    final FunctionKind kind = definition.getKind();
    if (kind == FunctionKind.TABLE) {
        return true;
    } else if (kind == FunctionKind.SCALAR || kind == FunctionKind.AGGREGATE || kind == FunctionKind.TABLE_AGGREGATE) {
        if (category != null && category.isTableFunction()) {
            throw new ValidationException(String.format("Function '%s' cannot be used as a table function.", resolvedFunction));
        }
        return true;
    }
    return false;
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) 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) FunctionKind(org.apache.flink.table.functions.FunctionKind)

Example 2 with FunctionDefinition

use of org.apache.flink.table.functions.FunctionDefinition in project flink by apache.

the class SqlAggFunctionVisitor method createSqlAggFunction.

private SqlAggFunction createSqlAggFunction(CallExpression call) {
    final FunctionDefinition definition = call.getFunctionDefinition();
    // legacy
    if (definition instanceof AggregateFunctionDefinition) {
        return createLegacySqlAggregateFunction(call.getFunctionIdentifier().orElse(null), (AggregateFunctionDefinition) definition);
    } else if (definition instanceof TableAggregateFunctionDefinition) {
        return createLegacySqlTableAggregateFunction(call.getFunctionIdentifier().orElse(null), (TableAggregateFunctionDefinition) definition);
    }
    // new stack
    final DataTypeFactory dataTypeFactory = ShortcutUtils.unwrapContext(relBuilder).getCatalogManager().getDataTypeFactory();
    final TypeInference typeInference = definition.getTypeInference(dataTypeFactory);
    return BridgingSqlAggFunction.of(dataTypeFactory, ShortcutUtils.unwrapTypeFactory(relBuilder), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Example 3 with FunctionDefinition

use of org.apache.flink.table.functions.FunctionDefinition in project flink by apache.

the class FunctionDefinitionConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    final FunctionDefinition definition = call.getFunctionDefinition();
    // built-in functions without implementation are handled separately
    if (definition instanceof BuiltInFunctionDefinition) {
        final BuiltInFunctionDefinition builtInFunction = (BuiltInFunctionDefinition) definition;
        if (!builtInFunction.hasRuntimeImplementation()) {
            return Optional.empty();
        }
    }
    final TypeInference typeInference = definition.getTypeInference(context.getDataTypeFactory());
    if (typeInference.getOutputTypeStrategy() == TypeStrategies.MISSING) {
        return Optional.empty();
    }
    switch(definition.getKind()) {
        case SCALAR:
        case TABLE:
            final List<RexNode> args = call.getChildren().stream().map(context::toRexNode).collect(Collectors.toList());
            final BridgingSqlFunction sqlFunction = BridgingSqlFunction.of(context.getDataTypeFactory(), context.getTypeFactory(), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
            return Optional.of(context.getRelBuilder().call(sqlFunction, args));
        default:
            return Optional.empty();
    }
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) BridgingSqlFunction(org.apache.flink.table.planner.functions.bridging.BridgingSqlFunction) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with FunctionDefinition

use of org.apache.flink.table.functions.FunctionDefinition in project flink by apache.

the class OverConvertRule method createBound.

private RexWindowBound createBound(ConvertContext context, Expression bound, SqlKind sqlKind) {
    if (bound instanceof CallExpression) {
        CallExpression callExpr = (CallExpression) bound;
        FunctionDefinition func = callExpr.getFunctionDefinition();
        if (BuiltInFunctionDefinitions.UNBOUNDED_ROW.equals(func) || BuiltInFunctionDefinitions.UNBOUNDED_RANGE.equals(func)) {
            SqlNode unbounded = sqlKind.equals(SqlKind.PRECEDING) ? SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO) : SqlWindow.createUnboundedFollowing(SqlParserPos.ZERO);
            return RexWindowBound.create(unbounded, null);
        } else if (BuiltInFunctionDefinitions.CURRENT_ROW.equals(func) || BuiltInFunctionDefinitions.CURRENT_RANGE.equals(func)) {
            SqlNode currentRow = SqlWindow.createCurrentRow(SqlParserPos.ZERO);
            return RexWindowBound.create(currentRow, null);
        } else {
            throw new IllegalArgumentException("Unexpected expression: " + bound);
        }
    } else if (bound instanceof ValueLiteralExpression) {
        RelDataType returnType = context.getTypeFactory().createFieldTypeFromLogicalType(new DecimalType(true, 19, 0));
        SqlOperator sqlOperator = new SqlPostfixOperator(sqlKind.name(), sqlKind, 2, new OrdinalReturnTypeInference(0), null, null);
        SqlNode[] operands = new SqlNode[] { SqlLiteral.createExactNumeric("1", SqlParserPos.ZERO) };
        SqlNode node = new SqlBasicCall(sqlOperator, operands, SqlParserPos.ZERO);
        ValueLiteralExpression literalExpr = (ValueLiteralExpression) bound;
        RexNode literalRexNode = literalExpr.getValueAs(BigDecimal.class).map(v -> context.getRelBuilder().literal(v)).orElse(context.getRelBuilder().literal(extractValue(literalExpr, Object.class)));
        List<RexNode> expressions = new ArrayList<>();
        expressions.add(literalRexNode);
        RexNode rexNode = context.getRelBuilder().getRexBuilder().makeCall(returnType, sqlOperator, expressions);
        return RexWindowBound.create(node, rexNode);
    } else {
        throw new TableException("Unexpected expression: " + bound);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) BigDecimal(java.math.BigDecimal) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) SqlPostfixOperator(org.apache.calcite.sql.SqlPostfixOperator) OrdinalReturnTypeInference(org.apache.calcite.sql.type.OrdinalReturnTypeInference) DecimalType(org.apache.flink.table.types.logical.DecimalType) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) CallExpression(org.apache.flink.table.expressions.CallExpression) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 5 with FunctionDefinition

use of org.apache.flink.table.functions.FunctionDefinition in project flink by apache.

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

FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)18 AggregateFunctionDefinition (org.apache.flink.table.functions.AggregateFunctionDefinition)7 ScalarFunctionDefinition (org.apache.flink.table.functions.ScalarFunctionDefinition)7 CallExpression (org.apache.flink.table.expressions.CallExpression)6 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)6 TableFunctionDefinition (org.apache.flink.table.functions.TableFunctionDefinition)6 BuiltInFunctionDefinition (org.apache.flink.table.functions.BuiltInFunctionDefinition)5 TableException (org.apache.flink.table.api.TableException)4 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)4 TableAggregateFunctionDefinition (org.apache.flink.table.functions.TableAggregateFunctionDefinition)4 RexNode (org.apache.calcite.rex.RexNode)3 ValidationException (org.apache.flink.table.api.ValidationException)3 Expression (org.apache.flink.table.expressions.Expression)3 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)3 ImmutableList (com.google.common.collect.ImmutableList)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)2