Search in sources :

Example 1 with CreateStructExpression

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")));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 2 with CreateStructExpression

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))));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

Example 3 with CreateStructExpression

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()))"));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with CreateStructExpression

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))"));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 5 with CreateStructExpression

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()));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Aggregations

CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)9 Field (io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field)9 Expression (io.confluent.ksql.execution.expression.tree.Expression)9 Test (org.junit.Test)9 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)8 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)8 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)7 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)6 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)5 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)5 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)5 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)5 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)5 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)4 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)4 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)3 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)3 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)2 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)2 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)2