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);
}
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);
}
}
Aggregations