Search in sources :

Example 71 with SqlType

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

the class ApiSqlValueCoercerTest method shouldNotCoerceToStruct.

@Test
public void shouldNotCoerceToStruct() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.STRING).build();
    // When / Then:
    assertThat(coercer.coerce(ImmutableMap.of("foo", "bar"), structType), is(Result.failure()));
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Example 72 with SqlType

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

the class ApiSqlValueCoercerTest method shouldNotCoerceToStructIfAnyFieldFailsToCoerce.

@Test
public void shouldNotCoerceToStructIfAnyFieldFailsToCoerce() {
    // Given:
    final Schema schema = SchemaBuilder.struct().field("foo", Schema.INT64_SCHEMA);
    final Struct struct = new Struct(schema).put("foo", 2L);
    final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.array(SqlTypes.INTEGER)).build();
    // When:
    final Result result = coercer.coerce(struct, structType);
    // Then:
    assertThat(result, is(Result.failure()));
}
Also used : Schema(org.apache.kafka.connect.data.Schema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Struct(org.apache.kafka.connect.data.Struct) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Test(org.junit.Test)

Example 73 with SqlType

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

the class ApiSqlValueCoercerTest method shouldCoerceNestedJsonStructWithCaseInsensitiveFields.

@Test
public void shouldCoerceNestedJsonStructWithCaseInsensitiveFields() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("F1", SqlTypes.struct().field("FOO", SqlTypes.BIGINT).field("foo", SqlTypes.STRING).field("bar", SqlTypes.STRING).build()).build();
    final JsonObject obj = new JsonObject().put("F1", new JsonObject().put("foo", 12).put("`foo`", "v1").put("\"bar\"", "v2"));
    // When:
    final Result result = coercer.coerce(obj, structType);
    // Then:
    assertThat("", !result.failed());
    final Struct coerced = ((Optional<Struct>) result.value()).get();
    final Struct innerStruct = ((Struct) coerced.get("F1"));
    assertThat(innerStruct.get("FOO"), is(12L));
    assertThat(innerStruct.get("foo"), is("v1"));
    assertThat(innerStruct.get("bar"), is("v2"));
}
Also used : Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 74 with SqlType

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

the class ApiSqlValueCoercerTest method shouldNotCoerceToMap.

@Test
public void shouldNotCoerceToMap() {
    final SqlType mapType = SqlTypes.map(SqlTypes.STRING, SqlTypes.DOUBLE);
    assertThat(coercer.coerce(true, mapType), is(Result.failure()));
    assertThat(coercer.coerce(1L, mapType), is(Result.failure()));
    assertThat(coercer.coerce("foo", mapType), is(Result.failure()));
    assertThat(coercer.coerce(new Timestamp(3213), mapType), is(Result.failure()));
    assertThat(coercer.coerce(new Time(3213), mapType), is(Result.failure()));
    assertThat(coercer.coerce(new Date(3213), mapType), is(Result.failure()));
    assertThat(coercer.coerce(ByteBuffer.wrap(new byte[] { 123 }), mapType), is(Result.failure()));
    assertThat(coercer.coerce(ImmutableList.of("foo"), mapType), is(Result.failure()));
    assertThat(coercer.coerce(new JsonArray().add("foo"), mapType), is(Result.failure()));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Test(org.junit.Test)

Example 75 with SqlType

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

the class ApiSqlValueCoercerTest method getType.

private static SqlType getType(final SqlBaseType baseType) {
    final SqlType type = TYPES.get(baseType);
    assertThat("invalid test: need type for base type:" + baseType, type, is(notNullValue()));
    return type;
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

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