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);
}
}
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));
}
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()));
}
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()));
}
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()));
}
Aggregations