Search in sources :

Example 1 with FunctionName

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));
    }
}
Also used : FunctionName(io.confluent.ksql.name.FunctionName) ColumnName(io.confluent.ksql.name.ColumnName) Expression(io.confluent.ksql.execution.expression.tree.Expression) ArrayList(java.util.ArrayList) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)

Example 2 with FunctionName

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())));
}
Also used : FunctionName(io.confluent.ksql.name.FunctionName) DescribeFunction(io.confluent.ksql.parser.tree.DescribeFunction)

Example 3 with FunctionName

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);
        }
    }
}
Also used : UdfDescription(io.confluent.ksql.function.udf.UdfDescription) Logger(org.slf4j.Logger) Kudf(io.confluent.ksql.function.udf.Kudf) Udf(io.confluent.ksql.function.udf.Udf) Configurable(org.apache.kafka.common.Configurable) Time(org.apache.kafka.common.utils.Time) FunctionName(io.confluent.ksql.name.FunctionName) ParamType(io.confluent.ksql.function.types.ParamType) LoggerFactory(org.slf4j.LoggerFactory) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Objects(java.util.Objects) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) List(java.util.List) PluggableUdf(io.confluent.ksql.function.udf.PluggableUdf) Metrics(org.apache.kafka.common.metrics.Metrics) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Method(java.lang.reflect.Method) Udf(io.confluent.ksql.function.udf.Udf) PluggableUdf(io.confluent.ksql.function.udf.PluggableUdf) Method(java.lang.reflect.Method) UdfDescription(io.confluent.ksql.function.udf.UdfDescription) KsqlException(io.confluent.ksql.util.KsqlException) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with FunctionName

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))))))"));
}
Also used : FunctionName(io.confluent.ksql.name.FunctionName) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 5 with FunctionName

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);
}
Also used : Kudf(io.confluent.ksql.function.udf.Kudf) FunctionName(io.confluent.ksql.name.FunctionName) ParamType(io.confluent.ksql.function.types.ParamType) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Immutable(com.google.errorprone.annotations.Immutable) KsqlException(io.confluent.ksql.util.KsqlException) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) EffectivelyImmutable(io.confluent.ksql.testing.EffectivelyImmutable) KsqlException(io.confluent.ksql.util.KsqlException) ParamType(io.confluent.ksql.function.types.ParamType) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

FunctionName (io.confluent.ksql.name.FunctionName)6 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)3 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 KsqlException (io.confluent.ksql.util.KsqlException)3 List (java.util.List)3 Objects (java.util.Objects)3 Expression (io.confluent.ksql.execution.expression.tree.Expression)2 ParamType (io.confluent.ksql.function.types.ParamType)2 Kudf (io.confluent.ksql.function.udf.Kudf)2 ColumnName (io.confluent.ksql.name.ColumnName)2 Function (java.util.function.Function)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Immutable (com.google.errorprone.annotations.Immutable)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 GenericKey (io.confluent.ksql.GenericKey)1 Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)1 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)1 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)1 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)1