Search in sources :

Example 1 with AggregateFunctionInitArguments

use of io.confluent.ksql.function.AggregateFunctionInitArguments in project ksql by confluentinc.

the class UdafUtil method createAggregateFunctionInitArgs.

public static AggregateFunctionInitArguments createAggregateFunctionInitArgs(final int udafIndex, final FunctionCall functionCall, final KsqlConfig config) {
    final List<Expression> args = functionCall.getArguments();
    final List<Object> initArgs = new ArrayList<>(Math.max(0, args.size() - 1));
    // args for index > 0 must be literals:
    for (int idx = 1; idx < args.size(); idx++) {
        final Expression param = args.get(idx);
        if (!(param instanceof Literal)) {
            throw new KsqlException("Parameter " + (idx + 1) + " passed to function " + functionCall.getName().text() + " must be a literal constant, but was expression: '" + param + "'");
        }
        initArgs.add(((Literal) param).getValue());
    }
    final Map<String, Object> functionConfig = config.getKsqlFunctionsConfigProps(functionCall.getName().text());
    return new AggregateFunctionInitArguments(udafIndex, functionConfig, initArgs);
}
Also used : AggregateFunctionInitArguments(io.confluent.ksql.function.AggregateFunctionInitArguments) Expression(io.confluent.ksql.execution.expression.tree.Expression) Literal(io.confluent.ksql.execution.expression.tree.Literal) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException)

Example 2 with AggregateFunctionInitArguments

use of io.confluent.ksql.function.AggregateFunctionInitArguments in project ksql by confluentinc.

the class UdafUtil method resolveAggregateFunction.

public static KsqlAggregateFunction<?, ?, ?> resolveAggregateFunction(final FunctionRegistry functionRegistry, final FunctionCall functionCall, final LogicalSchema schema, final KsqlConfig config) {
    try {
        final ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
        final SqlType argumentType = expressionTypeManager.getExpressionSqlType(functionCall.getArguments().get(0));
        // UDAFs only support one non-constant argument, and that argument must be a column reference
        final Expression arg = functionCall.getArguments().get(0);
        final Optional<Column> possibleValueColumn = arg instanceof UnqualifiedColumnReferenceExp ? schema.findValueColumn(((UnqualifiedColumnReferenceExp) arg).getColumnName()) : // assume that it is a column reference with no alias
        schema.findValueColumn(ColumnName.of(arg.toString()));
        final Column valueColumn = possibleValueColumn.orElseThrow(() -> new KsqlException("Could not find column for expression: " + arg));
        final AggregateFunctionInitArguments aggregateFunctionInitArguments = createAggregateFunctionInitArgs(valueColumn.index(), functionCall, config);
        return functionRegistry.getAggregateFunction(functionCall.getName(), argumentType, aggregateFunctionInitArguments);
    } catch (final Exception e) {
        throw new KsqlException("Failed to create aggregate function: " + functionCall, e);
    }
}
Also used : ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) AggregateFunctionInitArguments(io.confluent.ksql.function.AggregateFunctionInitArguments) Expression(io.confluent.ksql.execution.expression.tree.Expression) Column(io.confluent.ksql.schema.ksql.Column) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

Expression (io.confluent.ksql.execution.expression.tree.Expression)2 AggregateFunctionInitArguments (io.confluent.ksql.function.AggregateFunctionInitArguments)2 KsqlException (io.confluent.ksql.util.KsqlException)2 Literal (io.confluent.ksql.execution.expression.tree.Literal)1 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)1 ExpressionTypeManager (io.confluent.ksql.execution.util.ExpressionTypeManager)1 Column (io.confluent.ksql.schema.ksql.Column)1 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)1 ArrayList (java.util.ArrayList)1