Search in sources :

Example 6 with CreateStructExpression

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

the class CoercionUtilTest method shouldNotCoerceStructWithDifferentExpression.

@Test
public void shouldNotCoerceStructWithDifferentExpression() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new CreateStructExpression(ImmutableList.of(new Field("a", new IntegerLiteral(10)))), new CreateStructExpression(ImmutableList.of(new Field("a", STRING_EXPRESSION))));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> CoercionUtil.coerceUserList(expressions, typeManager));
    // Then:
    assertThat(e.getMessage(), startsWith("operator does not exist: STRUCT<`a` INTEGER> = STRUCT<`a` STRING> (STRUCT(a:=STR))"));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) 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) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 7 with CreateStructExpression

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

the class ExpressionTreeRewriterTest method shouldRewriteStructExpression.

@Test
public void shouldRewriteStructExpression() {
    // Given:
    final CreateStructExpression parsed = parseExpression("STRUCT(FOO := 'foo', BAR := col4[1])");
    final Expression fooVal = parsed.getFields().stream().filter(f -> f.getName().equals("FOO")).findFirst().get().getValue();
    final Expression barVal = parsed.getFields().stream().filter(f -> f.getName().equals("BAR")).findFirst().get().getValue();
    when(processor.apply(fooVal, context)).thenReturn(expr1);
    when(processor.apply(barVal, context)).thenReturn(expr2);
    // When:
    final Expression rewritten = expressionRewriter.rewrite(parsed, context);
    // Then:
    assertThat(rewritten, equalTo(new CreateStructExpression(ImmutableList.of(new Field("FOO", expr1), new Field("BAR", expr2)))));
}
Also used : Query(io.confluent.ksql.parser.tree.Query) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) Time(java.sql.Time) ColumnName(io.confluent.ksql.name.ColumnName) SourceName(io.confluent.ksql.name.SourceName) BiFunction(java.util.function.BiFunction) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) SelectItem(io.confluent.ksql.parser.tree.SelectItem) BigDecimal(java.math.BigDecimal) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) Timestamp(java.sql.Timestamp) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) IsNotNullPredicate(io.confluent.ksql.execution.expression.tree.IsNotNullPredicate) MetaStoreFixture(io.confluent.ksql.util.MetaStoreFixture) List(java.util.List) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IsNullPredicate(io.confluent.ksql.execution.expression.tree.IsNullPredicate) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) Mock(org.mockito.Mock) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) RunWith(org.junit.runner.RunWith) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) KsqlParserTestUtil(io.confluent.ksql.util.KsqlParserTestUtil) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) BetweenPredicate(io.confluent.ksql.execution.expression.tree.BetweenPredicate) Cast(io.confluent.ksql.execution.expression.tree.Cast) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) MetaStore(io.confluent.ksql.metastore.MetaStore) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Before(org.junit.Before) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) Type(io.confluent.ksql.execution.expression.tree.Type) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) 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) Context(io.confluent.ksql.engine.rewrite.ExpressionTreeRewriter.Context) Mockito.reset(org.mockito.Mockito.reset) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) 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) 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) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Test(org.junit.Test)

Example 8 with CreateStructExpression

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

the class GenericExpressionResolverTest method shouldResolveArbitraryExpressions.

@Test
public void shouldResolveArbitraryExpressions() {
    // Given:
    final SqlType type = SqlTypes.struct().field("FOO", SqlTypes.STRING).build();
    final Expression exp = new CreateStructExpression(ImmutableList.of(new Field("FOO", new FunctionCall(FunctionName.of("CONCAT"), ImmutableList.of(new StringLiteral("bar"), new StringLiteral("baz"))))));
    // When:
    final Object o = new GenericExpressionResolver(type, FIELD_NAME, registry, config, "insert value", false).resolve(exp);
    // Then:
    assertThat(o, is(new Struct(SchemaBuilder.struct().field("FOO", Schema.OPTIONAL_STRING_SCHEMA).optional().build()).put("FOO", "barbaz")));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 9 with CreateStructExpression

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

the class InterpretedExpressionTest method shouldEvaluateStructDereference.

@Test
public void shouldEvaluateStructDereference() {
    // Given:
    final Expression expression1 = new DereferenceExpression(Optional.empty(), new CreateStructExpression(ImmutableList.of(new Field("A", new IntegerLiteral(10)), new Field("B", new StringLiteral("abc")))), "A");
    // When:
    InterpretedExpression interpreter1 = interpreter(expression1);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(10));
}
Also used : Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) 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) Test(org.junit.Test)

Aggregations

CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)9 Field (io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field)9 Expression (io.confluent.ksql.execution.expression.tree.Expression)9 Test (org.junit.Test)9 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)8 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)8 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)7 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)6 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)5 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)5 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)5 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)5 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)5 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)4 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)4 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)3 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)3 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)2 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)2 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)2