Search in sources :

Example 66 with SqlType

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

the class CodeGenRunner method buildCodeGenFromParseTree.

public CompiledExpression buildCodeGenFromParseTree(final Expression expression, final String type) {
    try {
        final CodeGenSpec spec = getCodeGenSpec(expression);
        final String javaCode = SqlToJavaVisitor.of(schema, functionRegistry, spec, ksqlConfig).process(expression);
        final SqlType returnType = expressionTypeManager.getExpressionSqlType(expression, new HashMap<>());
        if (returnType == null) {
            // expressionType can be null if expression is NULL.
            throw new KsqlException("NULL expression not supported");
        }
        final Class<?> expressionType = SQL_TO_JAVA_TYPE_CONVERTER.toJavaType(returnType);
        final IExpressionEvaluator ee = cook(javaCode, expressionType, spec.argumentNames(), spec.argumentTypes());
        return new CompiledExpression(ee, spec, returnType, expression);
    } catch (KsqlException | CompileException e) {
        throw new KsqlException("Invalid " + type + ": " + e.getMessage() + ". expression: " + expression + ", schema:" + schema, e);
    } catch (final Exception e) {
        throw new RuntimeException("Unexpected error generating code for " + type + ". expression: " + expression, e);
    }
}
Also used : CompileException(org.codehaus.commons.compiler.CompileException) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) IExpressionEvaluator(org.codehaus.commons.compiler.IExpressionEvaluator) CompileException(org.codehaus.commons.compiler.CompileException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 67 with SqlType

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

the class MetaStoreImplTest method shouldRegisterType.

@Test
public void shouldRegisterType() {
    // Given:
    final SqlType type = SqlPrimitiveType.of(SqlBaseType.STRING);
    metaStore.registerType("foo", type);
    // When:
    final Optional<SqlType> resolved = metaStore.resolveType("foo");
    // Then:
    assertThat("expected to find type", resolved.isPresent());
    assertThat(resolved.get(), is(type));
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Example 68 with SqlType

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

the class ApiSqlValueCoercerTest method shouldNotCoerceToDecimal.

@Test
public void shouldNotCoerceToDecimal() {
    final SqlType decimalType = SqlTypes.decimal(2, 1);
    assertThat(coercer.coerce(true, decimalType), is(Result.failure()));
    assertThat(coercer.coerce(1234L, decimalType), is(Result.failure()));
    assertThat(coercer.coerce(new Timestamp(3213), decimalType), is(Result.failure()));
    assertThat(coercer.coerce(new Time(3213), decimalType), is(Result.failure()));
    assertThat(coercer.coerce(new Date(3213), decimalType), is(Result.failure()));
    assertThat(coercer.coerce(ByteBuffer.wrap(new byte[] { 123 }), decimalType), is(Result.failure()));
}
Also used : 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 69 with SqlType

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

the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToNestedMap.

@Test
public void shouldCoerceJsonObjectToNestedMap() {
    // Given:
    final SqlType mapType = SqlTypes.map(SqlTypes.STRING, SqlTypes.struct().field("F1", SqlTypes.BIGINT).field("F2", SqlTypes.STRING).build());
    final JsonObject obj = new JsonObject().put("k1", new JsonObject().put("F1", 1).put("F2", "foo")).put("k2", new JsonObject().put("F1", 2)).put("k3", new JsonObject()).putNull("k4");
    // When:
    final Result result = coercer.coerce(obj, mapType);
    // Then:
    assertThat("", !result.failed());
    final Map<?, ?> coerced = ((Optional<Map<?, ?>>) result.value()).get();
    assertThat(((Struct) coerced.get("k1")).get("F1"), is(1L));
    assertThat(((Struct) coerced.get("k1")).get("F2"), is("foo"));
    assertThat(((Struct) coerced.get("k2")).get("F1"), is(2L));
    assertThat(((Struct) coerced.get("k2")).get("F2"), is(nullValue()));
    assertThat(((Struct) coerced.get("k3")).get("F1"), is(nullValue()));
    assertThat(((Struct) coerced.get("k3")).get("F2"), is(nullValue()));
    assertThat(coerced.get("k4"), 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) Test(org.junit.Test)

Example 70 with SqlType

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

the class ApiSqlValueCoercerTest method shouldCoerceToJsonObjectWithNullValues.

@Test
public void shouldCoerceToJsonObjectWithNullValues() {
    // Given:
    final JsonObject jsonObject = new JsonObject().put("foo", (Long) null);
    final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.decimal(2, 1)).build();
    // When:
    final Result result = coercer.coerce(jsonObject, structType);
    // Then:
    assertThat("", !result.failed());
    final Optional<Struct> coerced = (Optional<Struct>) result.value();
    assertThat(coerced.get().get("foo"), 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)

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