Search in sources :

Example 1 with Cast

use of io.confluent.ksql.execution.expression.tree.Cast in project ksql by confluentinc.

the class InterpretedExpressionTest method shouldEvaluateCastToString.

@Test
public void shouldEvaluateCastToString() {
    // Given:
    final Expression cast1 = new Cast(new IntegerLiteral(10), new Type(SqlPrimitiveType.of("STRING")));
    final Expression cast2 = new Cast(new LongLiteral(1234L), new Type(SqlPrimitiveType.of("STRING")));
    final Expression cast3 = new Cast(new DoubleLiteral(12.5), new Type(SqlPrimitiveType.of("STRING")));
    final Expression cast4 = new Cast(new DecimalLiteral(new BigDecimal("4567.5")), new Type(SqlPrimitiveType.of("STRING")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    InterpretedExpression interpreter4 = interpreter(cast4);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is("10"));
    assertThat(interpreter2.evaluate(ROW), is("1234"));
    assertThat(interpreter3.evaluate(ROW), is("12.5"));
    assertThat(interpreter4.evaluate(ROW), is("4567.5"));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with Cast

use of io.confluent.ksql.execution.expression.tree.Cast in project ksql by confluentinc.

the class InterpretedExpressionTest method shouldEvaluateCastToDouble.

@Test
public void shouldEvaluateCastToDouble() {
    // Given:
    final Expression cast1 = new Cast(new LongLiteral(10L), new Type(SqlPrimitiveType.of("DOUBLE")));
    final Expression cast2 = new Cast(new StringLiteral("1234.5"), new Type(SqlPrimitiveType.of("DOUBLE")));
    final Expression cast3 = new Cast(new IntegerLiteral(12), new Type(SqlPrimitiveType.of("DOUBLE")));
    final Expression cast4 = new Cast(new DecimalLiteral(new BigDecimal("4567.5")), new Type(SqlPrimitiveType.of("DOUBLE")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    InterpretedExpression interpreter4 = interpreter(cast4);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(10d));
    assertThat(interpreter2.evaluate(ROW), is(1234.5d));
    assertThat(interpreter3.evaluate(ROW), is(12d));
    assertThat(interpreter4.evaluate(ROW), is(4567.5d));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with Cast

use of io.confluent.ksql.execution.expression.tree.Cast in project ksql by confluentinc.

the class InterpretedExpressionTest method shouldEvaluateCastToArray.

@Test
public void shouldEvaluateCastToArray() {
    // Given:
    final Expression cast1 = new Cast(new CreateArrayExpression(ImmutableList.of(new StringLiteral("1"), new StringLiteral("2"))), new Type(SqlTypes.array(SqlTypes.INTEGER)));
    final Expression cast2 = new Cast(new CreateArrayExpression(ImmutableList.of(new DoubleLiteral(2.5), new DoubleLiteral(3.6))), new Type(SqlTypes.array(SqlTypes.INTEGER)));
    final Expression cast3 = new Cast(new CreateArrayExpression(ImmutableList.of(new DoubleLiteral(2.5), new DoubleLiteral(3.6))), new Type(SqlTypes.array(SqlTypes.STRING)));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableList.of(1, 2)));
    assertThat(interpreter2.evaluate(ROW), is(ImmutableList.of(2, 3)));
    assertThat(interpreter3.evaluate(ROW), is(ImmutableList.of("2.5", "3.6")));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) 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) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Test(org.junit.Test)

Example 4 with Cast

use of io.confluent.ksql.execution.expression.tree.Cast in project ksql by confluentinc.

the class InterpretedExpressionTest method shouldEvaluateCastToDecimal.

@Test
public void shouldEvaluateCastToDecimal() {
    // Given:
    final Expression cast1 = new Cast(new LongLiteral(10L), new Type(SqlTypes.decimal(10, 1)));
    final Expression cast2 = new Cast(new StringLiteral("1234.5"), new Type(SqlTypes.decimal(10, 1)));
    final Expression cast3 = new Cast(new IntegerLiteral(12), new Type(SqlTypes.decimal(10, 1)));
    final Expression cast4 = new Cast(new DoubleLiteral(4567.5), new Type(SqlTypes.decimal(10, 1)));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    InterpretedExpression interpreter4 = interpreter(cast4);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(BigDecimal.valueOf(10L).setScale(1)));
    assertThat(interpreter2.evaluate(ROW), is(BigDecimal.valueOf(1234.5d).setScale(1)));
    assertThat(interpreter3.evaluate(ROW), is(BigDecimal.valueOf(12).setScale(1)));
    assertThat(interpreter4.evaluate(ROW), is(BigDecimal.valueOf(4567.5d).setScale(1)));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 5 with Cast

use of io.confluent.ksql.execution.expression.tree.Cast in project ksql by confluentinc.

the class InterpretedExpressionTest method shouldEvaluateCastToBytes.

@Test
public void shouldEvaluateCastToBytes() {
    // Given:
    final Expression cast1 = new Cast(new BytesLiteral(ByteBuffer.wrap(new byte[] { 123 })), new Type(SqlPrimitiveType.of("BYTES")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ByteBuffer.wrap(new byte[] { 123 })));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) BytesLiteral(io.confluent.ksql.execution.expression.tree.BytesLiteral) Test(org.junit.Test)

Aggregations

Cast (io.confluent.ksql.execution.expression.tree.Cast)15 Test (org.junit.Test)15 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)13 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)13 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)13 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)13 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)13 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)13 Expression (io.confluent.ksql.execution.expression.tree.Expression)13 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)13 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)13 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)13 Type (io.confluent.ksql.execution.expression.tree.Type)13 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)12 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)12 ArrayType (io.confluent.ksql.function.types.ArrayType)11 GenericType (io.confluent.ksql.function.types.GenericType)11 IntegerType (io.confluent.ksql.function.types.IntegerType)11 IntervalUnitType (io.confluent.ksql.function.types.IntervalUnitType)11 LambdaType (io.confluent.ksql.function.types.LambdaType)11