use of io.confluent.ksql.execution.expression.tree.CreateStructExpression in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateStruct.
@Test
public void shouldEvaluateStruct() {
// Given:
final Expression expression1 = new CreateStructExpression(ImmutableList.of(new Field("A", new IntegerLiteral(10)), new Field("B", new StringLiteral("abc"))));
// When:
InterpretedExpression interpreter1 = interpreter(expression1);
// Then:
assertThat(interpreter1.evaluate(ROW), is(new Struct(SchemaBuilder.struct().optional().field("A", SchemaBuilder.int32().optional().build()).field("B", SchemaBuilder.string().optional().build()).build()).put("A", 10).put("B", "abc")));
}
use of io.confluent.ksql.execution.expression.tree.CreateStructExpression in project ksql by confluentinc.
the class CoercionUtilTest method shouldCoerceStructOfCompatibleLiterals.
@Test
public void shouldCoerceStructOfCompatibleLiterals() {
// Given:
final ImmutableList<Expression> expressions = ImmutableList.of(new CreateStructExpression(ImmutableList.of(new Field("a", new IntegerLiteral(10)))), new CreateStructExpression(ImmutableList.of(new Field("a", new StringLiteral("123456789000")))));
// When:
final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
// Then:
final SqlStruct sqlStruct = SqlTypes.struct().field("a", SqlTypes.BIGINT).build();
assertThat(result.commonType(), is(Optional.of(sqlStruct)));
assertThat(result.expressions(), is(ImmutableList.of(cast(new CreateStructExpression(ImmutableList.of(new Field("a", new IntegerLiteral(10)))), sqlStruct), cast(new CreateStructExpression(ImmutableList.of(new Field("a", new StringLiteral("123456789000")))), sqlStruct))));
}
use of io.confluent.ksql.execution.expression.tree.CreateStructExpression in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldProcessStructExpressionCorrectly.
@Test
public void shouldProcessStructExpressionCorrectly() {
// Given:
final Expression expression = new CreateStructExpression(ImmutableList.of(new Field("col1", new StringLiteral("foo")), new Field("col2", new SubscriptExpression(MAPCOL, new StringLiteral("key1")))));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then:
assertThat(javaExpression, equalTo("((Struct)new Struct(schema0)" + ".put(\"col1\", (new Supplier<Object>() {@Override public Object get() { try { return \"foo\"; } catch (Exception e) { " + onException("struct field") + " }}}).get())" + ".put(\"col2\", (new Supplier<Object>() {@Override public Object get() { try { return ((Double) ((java.util.Map)COL5).get(\"key1\")); } catch (Exception e) { " + onException("struct field") + " }}}).get()))"));
}
use of io.confluent.ksql.execution.expression.tree.CreateStructExpression in project ksql by confluentinc.
the class CoercionUtilTest method shouldNotCoerceStructOfIncompatibleLiterals.
@Test
public void shouldNotCoerceStructOfIncompatibleLiterals() {
// Given:
final ImmutableList<Expression> expressions = ImmutableList.of(new CreateStructExpression(ImmutableList.of(new Field("a", new IntegerLiteral(10)))), new CreateStructExpression(ImmutableList.of(new Field("a", new BooleanLiteral(false)))));
// When:
final Exception e = assertThrows(KsqlException.class, () -> CoercionUtil.coerceUserList(expressions, typeManager));
// Then:
assertThat(e.getMessage(), startsWith("operator does not exist: STRUCT<`a` INTEGER> = STRUCT<`a` BOOLEAN> (STRUCT(a:=false))"));
}
use of io.confluent.ksql.execution.expression.tree.CreateStructExpression in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldEvaluateTypeForStructExpression.
@Test
public void shouldEvaluateTypeForStructExpression() {
// Given:
final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, SqlTypes.array(SqlTypes.INTEGER)).build();
expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
final Expression exp = new CreateStructExpression(ImmutableList.of(new Field("field1", new StringLiteral("foo")), new Field("field2", new UnqualifiedColumnReferenceExp(COL0)), new Field("field3", new CreateStructExpression(ImmutableList.of()))));
// When:
final SqlType sqlType = expressionTypeManager.getExpressionSqlType(exp);
// Then:
assertThat(sqlType, is(SqlTypes.struct().field("field1", SqlTypes.STRING).field("field2", SqlTypes.array(SqlTypes.INTEGER)).field("field3", SqlTypes.struct().build()).build()));
}
Aggregations