Search in sources :

Example 1 with Result

use of io.confluent.ksql.execution.util.CoercionUtil.Result 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 2 with Result

use of io.confluent.ksql.execution.util.CoercionUtil.Result in project ksql by confluentinc.

the class CoercionUtilTest method shouldCoerceStringNumericWithDecimalPointToDecimals.

@Test
public void shouldCoerceStringNumericWithDecimalPointToDecimals() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new IntegerLiteral(10), new StringLiteral("1.0"));
    // When:
    final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
    // Then:
    assertThat(result.commonType(), is(Optional.of(SqlTypes.decimal(11, 1))));
    assertThat(result.expressions(), is(ImmutableList.of(new DecimalLiteral(new BigDecimal("10.0")), new DecimalLiteral(new BigDecimal("1.0")))));
}
Also used : 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) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

Example 3 with Result

use of io.confluent.ksql.execution.util.CoercionUtil.Result in project ksql by confluentinc.

the class CoercionUtilTest method shouldCoerceToDoubles.

@Test
public void shouldCoerceToDoubles() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new IntegerLiteral(10), new LongLiteral(1234567890), new DoubleLiteral(123.456), new StringLiteral("\t -100.010 \t"), BIGINT_EXPRESSION, DECIMAL_EXPRESSION, INT_EXPRESSION, DOUBLE_EXPRESSION);
    // When:
    final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
    // Then:
    assertThat(result.commonType(), is(Optional.of(SqlTypes.DOUBLE)));
    assertThat(result.expressions(), is(ImmutableList.of(new DoubleLiteral(10.0), new DoubleLiteral(1234567890.0), new DoubleLiteral(123.456), new DoubleLiteral(-100.01), cast(BIGINT_EXPRESSION, SqlTypes.DOUBLE), cast(DECIMAL_EXPRESSION, SqlTypes.DOUBLE), cast(INT_EXPRESSION, SqlTypes.DOUBLE), DOUBLE_EXPRESSION)));
}
Also used : 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

Example 4 with Result

use of io.confluent.ksql.execution.util.CoercionUtil.Result in project ksql by confluentinc.

the class CoercionUtilTest method shouldCoerceToBigIntIfStringNumericTooWideForInt.

@Test
public void shouldCoerceToBigIntIfStringNumericTooWideForInt() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new IntegerLiteral(10), new StringLiteral("1234567890000"));
    // When:
    final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
    // Then:
    assertThat(result.commonType(), is(Optional.of(SqlTypes.BIGINT)));
    assertThat(result.expressions(), is(ImmutableList.of(new LongLiteral(10), new LongLiteral(1234567890000L))));
}
Also used : 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

Example 5 with Result

use of io.confluent.ksql.execution.util.CoercionUtil.Result in project ksql by confluentinc.

the class CoercionUtilTest method shouldCoerceToBooleans.

@Test
public void shouldCoerceToBooleans() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new BooleanLiteral(true), new StringLiteral("FaLsE"), BOOL_EXPRESSION);
    // When:
    final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
    // Then:
    assertThat(result.commonType(), is(Optional.of(SqlTypes.BOOLEAN)));
    assertThat(result.expressions(), is(ImmutableList.of(new BooleanLiteral(true), new BooleanLiteral(false), BOOL_EXPRESSION)));
}
Also used : 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) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

Aggregations

CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)17 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)17 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)17 Expression (io.confluent.ksql.execution.expression.tree.Expression)17 Result (io.confluent.ksql.execution.util.CoercionUtil.Result)17 Test (org.junit.Test)17 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)12 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)12 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)5 DecimalLiteral (io.confluent.ksql.execution.expression.tree.DecimalLiteral)4 BigDecimal (java.math.BigDecimal)4 NullLiteral (io.confluent.ksql.execution.expression.tree.NullLiteral)3 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)2 SqlDecimal (io.confluent.ksql.schema.ksql.types.SqlDecimal)2 Field (io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field)1 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)1 LambdaVariable (io.confluent.ksql.execution.expression.tree.LambdaVariable)1 SqlStruct (io.confluent.ksql.schema.ksql.types.SqlStruct)1