use of io.confluent.ksql.name.FunctionName in project ksql by confluentinc.
the class AggregateExpressionRewriter method visitFunctionCall.
@Override
public Optional<Expression> visitFunctionCall(final FunctionCall node, final ExpressionTreeRewriter.Context<Void> context) {
final FunctionName functionName = node.getName();
if (functionRegistry.isAggregate(functionName)) {
final ColumnName aggVarName = ColumnNames.aggregateColumn(aggVariableIndex);
aggVariableIndex++;
return Optional.of(new UnqualifiedColumnReferenceExp(node.getLocation(), aggVarName));
} else {
final List<Expression> arguments = new ArrayList<>();
for (final Expression argExpression : node.getArguments()) {
arguments.add(context.process(argExpression));
}
return Optional.of(new FunctionCall(node.getLocation(), node.getName(), arguments));
}
}
use of io.confluent.ksql.name.FunctionName in project ksql by confluentinc.
the class DescribeFunctionExecutor method execute.
public static StatementExecutorResponse execute(final ConfiguredStatement<DescribeFunction> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
final DescribeFunction describeFunction = statement.getStatement();
final FunctionName functionName = FunctionName.of(describeFunction.getFunctionName());
if (executionContext.getMetaStore().isAggregate(functionName)) {
return StatementExecutorResponse.handled(Optional.of(describeAggregateFunction(executionContext, functionName, statement.getStatementText())));
}
if (executionContext.getMetaStore().isTableFunction(functionName)) {
return StatementExecutorResponse.handled(Optional.of(describeTableFunction(executionContext, functionName, statement.getStatementText())));
}
return StatementExecutorResponse.handled(Optional.of(describeNonAggregateFunction(executionContext, functionName, statement.getStatementText())));
}
use of io.confluent.ksql.name.FunctionName in project ksql by confluentinc.
the class UdfLoader method loadUdfFromClass.
@VisibleForTesting
public void loadUdfFromClass(final Class<?> theClass, final String path) {
final UdfDescription udfDescriptionAnnotation = theClass.getAnnotation(UdfDescription.class);
if (udfDescriptionAnnotation == null) {
throw new KsqlException(String.format("Cannot load class %s. Classes containing UDFs must" + "be annotated with @UdfDescription.", theClass.getName()));
}
final String functionName = udfDescriptionAnnotation.name();
final String sensorName = "ksql-udf-" + functionName;
@SuppressWarnings("unchecked") final Class<? extends Kudf> udfClass = metrics.map(m -> (Class) UdfMetricProducer.class).orElse(PluggableUdf.class);
FunctionMetrics.initInvocationSensor(metrics, sensorName, "ksql-udf", functionName + " udf");
final UdfFactory factory = new UdfFactory(udfClass, new UdfMetadata(udfDescriptionAnnotation.name(), udfDescriptionAnnotation.description(), udfDescriptionAnnotation.author(), udfDescriptionAnnotation.version(), udfDescriptionAnnotation.category(), path));
functionRegistry.ensureFunctionFactory(factory);
for (final Method method : theClass.getMethods()) {
final Udf udfAnnotation = method.getAnnotation(Udf.class);
if (udfAnnotation != null) {
final KsqlScalarFunction function;
try {
function = createFunction(theClass, udfDescriptionAnnotation, udfAnnotation, method, path, sensorName, udfClass);
} catch (final KsqlException e) {
if (throwExceptionOnLoadFailure) {
throw e;
} else {
LOGGER.warn("Failed to add UDF to the MetaStore. name={} method={}", udfDescriptionAnnotation.name(), method, e);
continue;
}
}
factory.addFunction(function);
}
}
}
use of io.confluent.ksql.name.FunctionName in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldPostfixFunctionInstancesWithUniqueId.
@Test
public void shouldPostfixFunctionInstancesWithUniqueId() {
// Given:
final UdfFactory ssFactory = mock(UdfFactory.class);
final KsqlScalarFunction ssFunction = mock(KsqlScalarFunction.class);
final UdfFactory catFactory = mock(UdfFactory.class);
final KsqlScalarFunction catFunction = mock(KsqlScalarFunction.class);
givenUdf("SUBSTRING", ssFactory, ssFunction, SqlTypes.STRING);
when(ssFunction.parameters()).thenReturn(ImmutableList.of(ParamTypes.STRING, ParamTypes.INTEGER, ParamTypes.INTEGER));
givenUdf("CONCAT", catFactory, catFunction, SqlTypes.STRING);
when(catFunction.parameters()).thenReturn(ImmutableList.of(ParamTypes.STRING, ParamTypes.STRING));
final FunctionName ssName = FunctionName.of("SUBSTRING");
final FunctionName catName = FunctionName.of("CONCAT");
final FunctionCall substring1 = new FunctionCall(ssName, ImmutableList.of(COL1, new IntegerLiteral(1), new IntegerLiteral(3)));
final FunctionCall substring2 = new FunctionCall(ssName, ImmutableList.of(COL1, new IntegerLiteral(4), new IntegerLiteral(5)));
final FunctionCall concat = new FunctionCall(catName, ImmutableList.of(new StringLiteral("-"), substring2));
final Expression expression = new FunctionCall(catName, ImmutableList.of(substring1, concat));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then:
assertThat(javaExpression, is("((String) CONCAT_0.evaluate(" + "((String) SUBSTRING_1.evaluate(COL1, 1, 3)), " + "((String) CONCAT_2.evaluate(\"-\"," + " ((String) SUBSTRING_3.evaluate(COL1, 4, 5))))))"));
}
use of io.confluent.ksql.name.FunctionName in project ksql by confluentinc.
the class KsqlScalarFunction method createLegacyBuiltIn.
/**
* Create built in / legacy function.
*/
public static KsqlScalarFunction createLegacyBuiltIn(final SqlType returnType, final List<ParamType> arguments, final FunctionName functionName, final Class<? extends Kudf> kudfClass) {
final ParamType javaReturnType = SchemaConverters.sqlToFunctionConverter().toFunctionType(returnType);
final List<ParameterInfo> paramInfos = arguments.stream().map(type -> new ParameterInfo("", type, "", false)).collect(Collectors.toList());
return create((i1, i2) -> returnType, javaReturnType, paramInfos, functionName, kudfClass, // findbugs was complaining about a dead store so I inlined this
ksqlConfig -> {
try {
return kudfClass.newInstance();
} catch (final Exception e) {
throw new KsqlException("Failed to create instance of kudfClass " + kudfClass + " for function " + functionName, e);
}
}, "", INTERNAL_PATH, false);
}
Aggregations