use of io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field 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.Field 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.Field in project ksql by confluentinc.
the class TermCompiler method visitStructExpression.
@Override
public Term visitStructExpression(final CreateStructExpression node, final Context context) {
final ImmutableMap.Builder<String, Term> nameToTerm = ImmutableMap.builder();
for (final Field field : node.getFields()) {
nameToTerm.put(field.getName(), process(field.getValue(), context));
}
final SqlType resultType = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
return new StructTerm(nameToTerm.build(), resultType);
}
use of io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field 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.Field 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))"));
}
Aggregations