Search in sources :

Example 6 with SqlBaseType

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

the class ApiSqlValueCoercerTest method shouldCoerceAlmostUsingSameRulesAsBaseTypeUpCastRules.

@Test
public void shouldCoerceAlmostUsingSameRulesAsBaseTypeUpCastRules() {
    for (final SqlBaseType fromBaseType : supportedTypes()) {
        // Given:
        final Map<Boolean, List<SqlBaseType>> partitioned = supportedTypes().stream().collect(Collectors.partitioningBy(toBaseType -> coercionShouldBeSupported(fromBaseType, toBaseType)));
        final List<SqlBaseType> shouldUpCast = partitioned.getOrDefault(true, ImmutableList.of());
        final List<SqlBaseType> shouldNotUpCast = partitioned.getOrDefault(false, ImmutableList.of());
        // Then:
        shouldUpCast.forEach(toBaseType -> assertThat("should coerce " + fromBaseType + " to " + toBaseType, coercer.coerce(getInstance(fromBaseType, toBaseType), getType(toBaseType)), is(not(Result.failure()))));
        shouldNotUpCast.forEach(toBaseType -> assertThat("should not coerce " + fromBaseType + " to " + toBaseType, coercer.coerce(getInstance(fromBaseType, toBaseType), getType(toBaseType)), is(Result.failure())));
    }
}
Also used : Time(java.sql.Time) SqlValueCoercer(io.confluent.ksql.schema.ksql.SqlValueCoercer) Matchers.not(org.hamcrest.Matchers.not) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ByteBuffer(java.nio.ByteBuffer) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Schema(org.apache.kafka.connect.data.Schema) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Before(org.junit.Before) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ImmutableMap(com.google.common.collect.ImmutableMap) Timestamp(java.sql.Timestamp) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Date(java.sql.Date) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Struct(org.apache.kafka.connect.data.Struct) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) Collections(java.util.Collections) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 7 with SqlBaseType

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

the class OperatorTest method shouldThrowExceptionWhenNullType.

@Test
public void shouldThrowExceptionWhenNullType() {
    allOperations().forEach(op -> {
        for (final SqlBaseType leftBaseType : SqlBaseType.values()) {
            // When:
            final Throwable exception = assertThrows(KsqlException.class, () -> op.resultType(getType(leftBaseType), null));
            // Then:
            assertEquals(String.format("Arithmetic on types %s and null are not supported.", getType(leftBaseType)), exception.getMessage());
        }
    });
    allOperations().forEach(op -> {
        for (final SqlBaseType rightBaseType : SqlBaseType.values()) {
            // When:
            final Throwable exception = assertThrows(KsqlException.class, () -> op.resultType(null, getType(rightBaseType)));
            // Then:
            assertEquals(String.format("Arithmetic on types null and %s are not supported.", getType(rightBaseType)), exception.getMessage());
        }
    });
}
Also used : SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) Test(org.junit.Test)

Example 8 with SqlBaseType

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

the class SchemaConvertersTest method shouldGetJavaTypesForAllSqlTypes.

@Test
public void shouldGetJavaTypesForAllSqlTypes() {
    for (final Entry<SqlBaseType, Class<?>> entry : SQL_TO_JAVA.entrySet()) {
        final SqlBaseType sqlType = entry.getKey();
        final Class<?> javaType = entry.getValue();
        final Class<?> result = SchemaConverters.sqlToJavaConverter().toJavaType(sqlType);
        assertThat(result, equalTo(javaType));
    }
}
Also used : SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) Test(org.junit.Test)

Example 9 with SqlBaseType

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

the class SqlTypeWalkerTest method shouldThrowOnUnknownType.

@Test
public void shouldThrowOnUnknownType() {
    // Given:
    final SqlBaseType unknownType = mock(SqlBaseType.class, "bob");
    final SqlType type = mock(SqlType.class);
    when(type.baseType()).thenReturn(unknownType);
    // When:
    final UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, () -> SqlTypeWalker.visit(type, visitor));
    // Then:
    assertThat(e.getMessage(), containsString("Unsupported schema type: bob"));
}
Also used : SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Example 10 with SqlBaseType

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

the class UdafAggregateFunctionFactory method buildAllParams.

private List<SqlArgument> buildAllParams(final List<SqlArgument> argTypeList, final AggregateFunctionInitArguments initArgs) {
    if (initArgs.args().isEmpty()) {
        return argTypeList;
    }
    final List<SqlArgument> allParams = new ArrayList<>(argTypeList.size() + initArgs.args().size());
    allParams.addAll(argTypeList);
    for (final Object arg : initArgs.args()) {
        if (arg == null) {
            allParams.add(null);
            continue;
        }
        final SqlBaseType baseType = SchemaConverters.javaToSqlConverter().toSqlType(arg.getClass());
        try {
            // Only primitive types currently supported:
            final SqlPrimitiveType primitiveType = SqlPrimitiveType.of(baseType);
            allParams.add(SqlArgument.of(primitiveType));
        } catch (final Exception e) {
            throw new KsqlFunctionException("Only primitive init arguments are supported by UDAF " + getName() + ", but got " + arg, e);
        }
    }
    return allParams;
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

SqlBaseType (io.confluent.ksql.schema.ksql.types.SqlBaseType)11 KsqlException (io.confluent.ksql.util.KsqlException)6 Test (org.junit.Test)6 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)4 SqlTypes (io.confluent.ksql.schema.ksql.types.SqlTypes)3 BigDecimal (java.math.BigDecimal)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 CastInterpreter.castToDoubleFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction)2 CastInterpreter.castToIntegerFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction)2 CastInterpreter.castToLongFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction)2 ComparableCastFunction (io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction)2 Term (io.confluent.ksql.execution.interpreter.terms.Term)2 ByteBuffer (java.nio.ByteBuffer)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.is (org.hamcrest.Matchers.is)2