Search in sources :

Example 6 with Cast

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

the class InterpretedExpressionTest method shouldEvaluateCastToTime.

@Test
public void shouldEvaluateCastToTime() {
    // Given:
    final Expression cast1 = new Cast(new TimestampLiteral(Timestamp.from(Instant.ofEpochMilli(1000))), new Type(SqlPrimitiveType.of("TIME")));
    final Expression cast2 = new Cast(new StringLiteral("23:59:58"), new Type(SqlPrimitiveType.of("TIME")));
    final Expression cast3 = new Cast(new TimeLiteral(new Time(1000)), new Type(SqlPrimitiveType.of("TIME")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(new Time(1000L)));
    assertThat(interpreter2.evaluate(ROW), is(new Time(86398000)));
    assertThat(interpreter3.evaluate(ROW), is(new Time(1000L)));
}
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) TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) 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) Time(java.sql.Time) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) Test(org.junit.Test)

Example 7 with Cast

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

the class InterpretedExpressionTest method shouldEvaluateCastToMap.

@Test
public void shouldEvaluateCastToMap() {
    // Given:
    final Expression cast1 = new Cast(new CreateMapExpression(ImmutableMap.of(new StringLiteral("1"), new StringLiteral("2"), new StringLiteral("3"), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.INTEGER, SqlTypes.INTEGER)));
    final Expression cast2 = new Cast(new CreateMapExpression(ImmutableMap.of(new DoubleLiteral(2.5), new StringLiteral("2"), new DoubleLiteral(3.5), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.STRING, SqlTypes.BIGINT)));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableMap.of(1, 2, 3, 4)));
    assertThat(interpreter2.evaluate(ROW), is(ImmutableMap.of("2.5", 2L, "3.5", 4L)));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) 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) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Test(org.junit.Test)

Example 8 with Cast

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

the class ExpressionFormatterTest method shouldFormatCastToStruct.

@Test
public void shouldFormatCastToStruct() {
    // Given:
    final Cast cast = new Cast(new StringLiteral("foo"), new Type(SqlStruct.builder().field("field", SqlTypes.STRING).build()));
    // When:
    final String result = ExpressionFormatter.formatExpression(cast, FormatOptions.none());
    // Then:
    assertThat(result, equalTo("CAST('foo' AS STRUCT<`field` STRING>)"));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) Type(io.confluent.ksql.execution.expression.tree.Type) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Test(org.junit.Test)

Example 9 with Cast

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

the class SqlToJavaVisitorTest method shouldCreateCorrectCastJavaExpression.

@Test
public void shouldCreateCorrectCastJavaExpression() {
    // Given:
    final Expression castBigintInteger = new Cast(COL0, new io.confluent.ksql.execution.expression.tree.Type(SqlPrimitiveType.of("INTEGER")));
    // When:
    final String actual = sqlToJavaVisitor.process(castBigintInteger);
    // Then:
    final String expected = CastEvaluator.generateCode("COL0", SqlTypes.BIGINT, SqlTypes.INTEGER, ksqlConfig);
    assertThat(actual, is(expected));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 10 with Cast

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

the class InterpretedExpressionTest method shouldEvaluateCastToDate.

@Test
public void shouldEvaluateCastToDate() {
    // Given:
    final Expression cast1 = new Cast(new TimestampLiteral(Timestamp.from(Instant.ofEpochMilli(864000500))), new Type(SqlPrimitiveType.of("DATE")));
    final Expression cast2 = new Cast(new StringLiteral("2017-11-13"), new Type(SqlPrimitiveType.of("DATE")));
    final Expression cast3 = new Cast(new DateLiteral(new Date(864000000)), new Type(SqlPrimitiveType.of("DATE")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(new Date(864000000)));
    assertThat(interpreter2.evaluate(ROW), is(new Date(1510531200000L)));
    assertThat(interpreter3.evaluate(ROW), is(new Date(864000000)));
}
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) TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) 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) Date(java.sql.Date) 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