Search in sources :

Example 11 with StringLiteral

use of io.confluent.ksql.execution.expression.tree.StringLiteral 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 12 with StringLiteral

use of io.confluent.ksql.execution.expression.tree.StringLiteral 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 13 with StringLiteral

use of io.confluent.ksql.execution.expression.tree.StringLiteral 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 14 with StringLiteral

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForLikePatternWithEscape.

@Test
public void shouldGenerateCorrectCodeForLikePatternWithEscape() {
    // Given:
    final Expression expression = new LikePredicate(COL1, new StringLiteral("%foo"), Optional.of('!'));
    // When:
    final String javaExpression = sqlToJavaVisitor.process(expression);
    // Then:
    assertThat(javaExpression, equalTo("LikeEvaluator.matches(COL1, \"%foo\", '!')"));
}
Also used : 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) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 15 with StringLiteral

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

the class ExpressionFormatterTest method shouldFormatLogicalBinaryExpression.

@Test
public void shouldFormatLogicalBinaryExpression() {
    final LogicalBinaryExpression expression = new LogicalBinaryExpression(LogicalBinaryExpression.Type.AND, new StringLiteral("a"), new StringLiteral("b"));
    assertThat(ExpressionFormatter.formatExpression(expression), equalTo("('a' AND 'b')"));
}
Also used : LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Test(org.junit.Test)

Aggregations

StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)204 Test (org.junit.Test)199 Expression (io.confluent.ksql.execution.expression.tree.Expression)95 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)70 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)66 KsqlException (io.confluent.ksql.util.KsqlException)64 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)63 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)63 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)63 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)61 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)53 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)51 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)49 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)43 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)36 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)35 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)34 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)31 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)28 Matchers.containsString (org.hamcrest.Matchers.containsString)27