Search in sources :

Example 16 with FunctionDefinition

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

the class FilterUtils method binaryFilterApplies.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static boolean binaryFilterApplies(CallExpression binExpr, Function<String, Comparable<?>> getter) {
    List<Expression> children = binExpr.getChildren();
    Preconditions.checkArgument(children.size() == 2);
    Comparable lhsValue = getValue(children.get(0), getter);
    Comparable rhsValue = getValue(children.get(1), getter);
    FunctionDefinition functionDefinition = binExpr.getFunctionDefinition();
    if (BuiltInFunctionDefinitions.GREATER_THAN.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) > 0;
    } else if (BuiltInFunctionDefinitions.LESS_THAN.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) < 0;
    } else if (BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) >= 0;
    } else if (BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) <= 0;
    } else if (BuiltInFunctionDefinitions.EQUALS.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) == 0;
    } else if (BuiltInFunctionDefinitions.NOT_EQUALS.equals(functionDefinition)) {
        return lhsValue.compareTo(rhsValue) != 0;
    } else {
        throw new UnsupportedOperationException("Unsupported operator: " + functionDefinition);
    }
}
Also used : CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition)

Example 17 with FunctionDefinition

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

the class FunctionCatalogOperatorTable method convertToSqlFunction.

private Optional<SqlFunction> convertToSqlFunction(@Nullable SqlFunctionCategory category, ContextResolvedFunction resolvedFunction) {
    final FunctionDefinition definition = resolvedFunction.getDefinition();
    final FunctionIdentifier identifier = resolvedFunction.getIdentifier().orElse(null);
    // legacy
    if (definition instanceof AggregateFunctionDefinition) {
        AggregateFunctionDefinition def = (AggregateFunctionDefinition) definition;
        if (isHiveFunc(def.getAggregateFunction())) {
            return Optional.of(new HiveAggSqlFunction(identifier, def.getAggregateFunction(), typeFactory));
        } else {
            return convertAggregateFunction(identifier, (AggregateFunctionDefinition) definition);
        }
    } else if (definition instanceof ScalarFunctionDefinition) {
        ScalarFunctionDefinition def = (ScalarFunctionDefinition) definition;
        return convertScalarFunction(identifier, def);
    } else if (definition instanceof TableFunctionDefinition && category != null && category.isTableFunction()) {
        TableFunctionDefinition def = (TableFunctionDefinition) definition;
        if (isHiveFunc(def.getTableFunction())) {
            DataType returnType = fromLegacyInfoToDataType(new GenericTypeInfo<>(Row.class));
            return Optional.of(new HiveTableSqlFunction(identifier, def.getTableFunction(), returnType, typeFactory, new DeferredTypeFlinkTableFunction(def.getTableFunction(), returnType), HiveTableSqlFunction.operandTypeChecker(identifier.toString(), def.getTableFunction())));
        } else {
            return convertTableFunction(identifier, (TableFunctionDefinition) definition);
        }
    }
    // new stack
    return convertToBridgingSqlFunction(category, resolvedFunction);
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) DeferredTypeFlinkTableFunction(org.apache.flink.table.planner.plan.schema.DeferredTypeFlinkTableFunction) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) DataType(org.apache.flink.table.types.DataType) TypeConversions.fromLegacyInfoToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType) 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) Row(org.apache.flink.types.Row) HiveAggSqlFunction(org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction) HiveTableSqlFunction(org.apache.flink.table.planner.functions.utils.HiveTableSqlFunction)

Example 18 with FunctionDefinition

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

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)

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