Search in sources :

Example 11 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToStruct.

@Test
public void shouldCoerceJsonObjectToStruct() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("FOO", SqlTypes.BIGINT).field("NULL", SqlTypes.BIGINT).build();
    // When:
    final Result result = coercer.coerce(new JsonObject().put("FOO", 12), structType);
    // Then:
    assertThat("", !result.failed());
    final Struct coerced = ((Optional<Struct>) result.value()).get();
    assertThat(coerced.get("FOO"), is(12L));
    assertThat(coerced.get("NULL"), is(nullValue()));
}
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 12 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToNestedStruct.

@Test
public void shouldCoerceJsonObjectToNestedStruct() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("F1", SqlTypes.struct().field("G1", SqlTypes.BIGINT).field("NULL", SqlTypes.BIGINT).build()).field("F2", SqlTypes.array(SqlTypes.STRING)).field("F3", SqlTypes.map(SqlTypes.STRING, SqlTypes.STRING)).build();
    final JsonObject obj = new JsonObject().put("F1", new JsonObject().put("G1", 12)).put("F2", new JsonArray().add("v1").add("v2")).put("F3", new JsonObject().put("k1", "v1"));
    // 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("G1"), is(12L));
    assertThat(innerStruct.get("NULL"), is(nullValue()));
    final List<?> innerList = (List<?>) coerced.get("F2");
    assertThat(innerList, is(ImmutableList.of("v1", "v2")));
    final Map<?, ?> innerMap = (Map<?, ?>) coerced.get("F3");
    assertThat(innerMap, is(ImmutableMap.of("k1", "v1")));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 13 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldCoerceJsonStructToNestedDecimal.

@Test
public void shouldCoerceJsonStructToNestedDecimal() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("INT", SqlTypes.decimal(2, 1)).field("LONG", SqlTypes.decimal(2, 1)).field("STRING", SqlTypes.decimal(2, 1)).field("DOUBLE", SqlTypes.decimal(2, 1)).build();
    final JsonObject obj = new JsonObject().put("INT", 1).put("LONG", 1L);
    obj.put("STRING", "1.1").put("DOUBLE", 1.2);
    // When:
    final Result result = coercer.coerce(obj, structType);
    // Then:
    assertThat("", !result.failed());
    final Struct coerced = ((Optional<Struct>) result.value()).get();
    assertThat(coerced.get("INT"), is(new BigDecimal("1.0")));
    assertThat(coerced.get("LONG"), is(new BigDecimal("1.0")));
    assertThat(coerced.get("STRING"), is(new BigDecimal("1.1")));
    assertThat(coerced.get("DOUBLE"), is(new BigDecimal("1.2")));
}
Also used : Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) BigDecimal(java.math.BigDecimal) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 14 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldCoerceToStruct.

@Test
public void shouldCoerceToStruct() {
    // Given:
    final JsonObject struct = new JsonObject().put("foo", 2L);
    final SqlType structType = SqlTypes.struct().field("FOO", SqlTypes.decimal(2, 1)).build();
    // When:
    final Result result = coercer.coerce(struct, structType);
    // Then:
    assertThat("", !result.failed());
    final Optional<Struct> coerced = (Optional<Struct>) result.value();
    assertThat(coerced.get().get("FOO"), is(new BigDecimal("2.0")));
}
Also used : Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) BigDecimal(java.math.BigDecimal) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Aggregations

Result (io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)14 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)14 Test (org.junit.Test)12 JsonObject (io.vertx.core.json.JsonObject)11 Optional (java.util.Optional)11 Struct (org.apache.kafka.connect.data.Struct)10 JsonArray (io.vertx.core.json.JsonArray)2 BigDecimal (java.math.BigDecimal)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 List (java.util.List)1 Map (java.util.Map)1 Schema (org.apache.kafka.connect.data.Schema)1