Search in sources :

Example 21 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class GenericRecordFactory method resolveValues.

private static Map<ColumnName, Object> resolveValues(final List<ColumnName> columns, final List<Expression> expressions, final LogicalSchema schema, final FunctionRegistry functionRegistry, final KsqlConfig config) {
    final Map<ColumnName, Object> values = new HashMap<>();
    for (int i = 0; i < columns.size(); i++) {
        final ColumnName column = columns.get(i);
        final SqlType columnType = columnType(column, schema);
        final Expression valueExp = expressions.get(i);
        final Object value = new GenericExpressionResolver(columnType, column, functionRegistry, config, "insert value", false).resolve(valueExp);
        values.put(column, value);
    }
    return values;
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) HashMap(java.util.HashMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 22 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class FunctionLoaderUtils method handleUdfSchemaProviderAnnotation.

private static Function<List<SqlArgument>, SqlType> handleUdfSchemaProviderAnnotation(final String schemaProviderName, final Class theClass, final String functionName) {
    // throws exception if it cannot find the method
    final Method m = findSchemaProvider(theClass, schemaProviderName);
    final Object instance = FunctionLoaderUtils.instantiateFunctionInstance(theClass, functionName);
    return parameterSchemas -> invokeSchemaProviderMethod(instance, m, parameterSchemas, functionName);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) UdfParameter(io.confluent.ksql.function.udf.UdfParameter) ParamType(io.confluent.ksql.function.types.ParamType) HashMap(java.util.HashMap) Function(java.util.function.Function) UdfUtil(io.confluent.ksql.execution.function.UdfUtil) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Method(java.lang.reflect.Method) Udf(io.confluent.ksql.function.udf.Udf) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) ParamTypes(io.confluent.ksql.function.types.ParamTypes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) LambdaType(io.confluent.ksql.function.types.LambdaType) List(java.util.List) Type(java.lang.reflect.Type) UdfSchemaProvider(io.confluent.ksql.function.udf.UdfSchemaProvider) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) GenericType(io.confluent.ksql.function.types.GenericType) Method(java.lang.reflect.Method)

Example 23 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class GenericExpressionResolverTest method shouldThrowIfCannotCoerce.

@Test
public void shouldThrowIfCannotCoerce() {
    // Given:
    final SqlType type = SqlTypes.array(SqlTypes.INTEGER);
    final Expression exp = new IntegerLiteral(1);
    // When:
    final KsqlException e = assertThrows(KsqlException.class, () -> new GenericExpressionResolver(type, FIELD_NAME, registry, config, "insert value", false).resolve(exp));
    // Then:
    assertThat(e.getMessage(), containsString("Expected type ARRAY<INTEGER> for field `FOO` but got INTEGER(1)"));
}
Also used : CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 24 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class GenericExpressionResolverTest method shouldParseDate.

@Test
public void shouldParseDate() {
    // Given:
    final SqlType type = SqlTypes.DATE;
    final Expression exp = new StringLiteral("2021-01-09");
    // When:
    Object o = new GenericExpressionResolver(type, FIELD_NAME, registry, config, "insert value", false).resolve(exp);
    // Then:
    assertTrue(o instanceof Date);
    assertThat(((Date) o).getTime(), is(1610150400000L));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Date(java.sql.Date) Test(org.junit.Test)

Example 25 with SqlType

use of io.confluent.ksql.schema.ksql.types.SqlType in project ksql by confluentinc.

the class GenericExpressionResolverTest method shouldParseTimestamp.

@Test
public void shouldParseTimestamp() {
    // Given:
    final SqlType type = SqlTypes.TIMESTAMP;
    final Expression exp = new StringLiteral("2021-01-09T04:40:02");
    // When:
    Object o = new GenericExpressionResolver(type, FIELD_NAME, registry, config, "insert value", false).resolve(exp);
    // Then:
    assertTrue(o instanceof Timestamp);
    assertThat(((Timestamp) o).getTime(), is(1610167202000L));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Aggregations

SqlType (io.confluent.ksql.schema.ksql.types.SqlType)140 Test (org.junit.Test)80 Expression (io.confluent.ksql.execution.expression.tree.Expression)47 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)38 KsqlException (io.confluent.ksql.util.KsqlException)33 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)30 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)29 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)29 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)29 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)29 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)29 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)29 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)29 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)29 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)29 Optional (java.util.Optional)20 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)15 Result (io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)14 Struct (org.apache.kafka.connect.data.Struct)14 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)13