Search in sources :

Example 1 with SqlStruct

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

the class ExpressionFormatterTest method shouldFormatStructWithColumnWithReservedWordName.

@Test
public void shouldFormatStructWithColumnWithReservedWordName() {
    final SqlStruct struct = SqlStruct.builder().field("RESERVED", SqlTypes.INTEGER).build();
    assertThat(ExpressionFormatter.formatExpression(new Type(struct), FormatOptions.none()), equalTo("STRUCT<`RESERVED` INTEGER>"));
}
Also used : Type(io.confluent.ksql.execution.expression.tree.Type) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) Test(org.junit.Test)

Example 2 with SqlStruct

use of io.confluent.ksql.schema.ksql.types.SqlStruct 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 SqlStruct

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

the class ExpressionTypeManagerTest method shouldThrowGoodErrorMessageForSubscriptOnStruct.

@Test
public void shouldThrowGoodErrorMessageForSubscriptOnStruct() {
    // Given:
    final SqlStruct structType = SqlTypes.struct().field("IN0", SqlTypes.INTEGER).build();
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, structType).build();
    expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    final Expression expression = new SubscriptExpression(Optional.empty(), TestExpressions.COL0, new StringLiteral("IN0"));
    // When:
    final UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), is("Subscript expression (COL0['IN0']) do not apply to STRUCT<`IN0` INTEGER>. " + "Use the dereference operator for STRUCTS: COL0->'IN0'"));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) 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) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Test(org.junit.Test)

Example 4 with SqlStruct

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

the class ExpressionTypeManagerTest method shouldEvaluateTypeForArrayReferenceInStruct.

@Test
public void shouldEvaluateTypeForArrayReferenceInStruct() {
    // Given:
    final SqlStruct inner = SqlTypes.struct().field("IN0", SqlTypes.array(SqlTypes.INTEGER)).build();
    final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, inner).build();
    expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    final Expression structRef = new DereferenceExpression(Optional.empty(), new UnqualifiedColumnReferenceExp(COL0), "IN0");
    final Expression expression = new SubscriptExpression(structRef, new IntegerLiteral(1));
    // When:
    final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(result, is(SqlTypes.INTEGER));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) 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) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 5 with SqlStruct

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

the class KsqlTypesSerdeModuleTest method shouldSerDeStructType.

@Test
public void shouldSerDeStructType() throws JsonProcessingException {
    // Given:
    SqlStruct struct = SqlStruct.builder().field("foo", SqlArray.of(SqlTypes.STRING)).build();
    // When:
    final SqlType out = MAPPER.readValue(MAPPER.writeValueAsString(struct), SqlType.class);
    // Then:
    assertThat(out, is(struct));
}
Also used : SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Aggregations

SqlStruct (io.confluent.ksql.schema.ksql.types.SqlStruct)14 Test (org.junit.Test)11 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)5 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)4 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)4 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)4 Expression (io.confluent.ksql.execution.expression.tree.Expression)4 Field (io.confluent.ksql.schema.ksql.types.SqlStruct.Field)4 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)3 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)3 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)3 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)3 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)3 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)3 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)3 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)3 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)3 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)2