Search in sources :

Example 6 with WithinExpression

use of io.confluent.ksql.parser.tree.WithinExpression in project ksql by confluentinc.

the class KsqlParserTest method shouldSetWithinExpressionWithSingleWithinAndGracePeriod.

@Test
public void shouldSetWithinExpressionWithSingleWithinAndGracePeriod() {
    final String statementString = "CREATE STREAM foobar as SELECT * from TEST1 JOIN ORDERS WITHIN " + "10 SECONDS GRACE PERIOD 5 SECONDS ON TEST1.col1 = ORDERS.ORDERID ;";
    final Statement statement = KsqlParserTestUtil.buildSingleAst(statementString, metaStore).getStatement();
    assertThat(statement, instanceOf(CreateStreamAsSelect.class));
    final CreateStreamAsSelect createStreamAsSelect = (CreateStreamAsSelect) statement;
    final Query query = createStreamAsSelect.getQuery();
    assertThat(query.getFrom(), instanceOf(Join.class));
    final JoinedSource join = Iterables.getOnlyElement(((Join) query.getFrom()).getRights());
    assertTrue(join.getWithinExpression().isPresent());
    final WithinExpression withinExpression = join.getWithinExpression().get();
    assertThat(withinExpression.getBefore(), is(10L));
    assertThat(withinExpression.getAfter(), is(10L));
    assertThat(withinExpression.getBeforeTimeUnit(), is(TimeUnit.SECONDS));
    assertThat(withinExpression.getGrace(), is(Optional.of(new WindowTimeClause(5, TimeUnit.SECONDS))));
    assertThat(join.getType(), is(JoinedSource.Type.INNER));
}
Also used : JoinedSource(io.confluent.ksql.parser.tree.JoinedSource) Query(io.confluent.ksql.parser.tree.Query) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Join(io.confluent.ksql.parser.tree.Join) WithinExpression(io.confluent.ksql.parser.tree.WithinExpression) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) Test(org.junit.Test)

Example 7 with WithinExpression

use of io.confluent.ksql.parser.tree.WithinExpression in project ksql by confluentinc.

the class StatementRewriterTest method shouldRewriteJoinWithWindowExpression.

@Test
public void shouldRewriteJoinWithWindowExpression() {
    // Given:
    final WithinExpression withinExpression = mock(WithinExpression.class);
    final WithinExpression rewrittenWithinExpression = mock(WithinExpression.class);
    final Join join = givenJoin(Optional.of(withinExpression));
    when(mockRewriter.apply(withinExpression, context)).thenReturn(rewrittenWithinExpression);
    // When:
    final AstNode rewritten = rewriter.rewrite(join, context);
    // Then:
    assertThat(rewritten, equalTo(new Join(location, rewrittenRelation, ImmutableList.of(new JoinedSource(Optional.empty(), rewrittenRightRelation, Type.LEFT, joinCriteria, Optional.of(rewrittenWithinExpression))))));
}
Also used : JoinedSource(io.confluent.ksql.parser.tree.JoinedSource) WithinExpression(io.confluent.ksql.parser.tree.WithinExpression) Join(io.confluent.ksql.parser.tree.Join) AstNode(io.confluent.ksql.parser.tree.AstNode) Test(org.junit.Test)

Example 8 with WithinExpression

use of io.confluent.ksql.parser.tree.WithinExpression in project ksql by confluentinc.

the class SchemaKStreamTest method shouldBuildStepForStreamStreamJoin.

@Test
// can be fixed after GRACE clause is made mandatory
@SuppressWarnings({ "rawtypes", "deprecation" })
public void shouldBuildStepForStreamStreamJoin() {
    // Given:
    final SchemaKStream initialSchemaKStream = buildSchemaKStreamForJoin(ksqlStream);
    final List<Pair<JoinType, StreamStreamJoin>> cases = ImmutableList.of(Pair.of(JoinType.OUTER, initialSchemaKStream::outerJoin), Pair.of(JoinType.LEFT, initialSchemaKStream::leftJoin), Pair.of(JoinType.INNER, initialSchemaKStream::innerJoin));
    final JoinWindows joinWindows = JoinWindows.of(Duration.ofSeconds(1));
    final WindowTimeClause grace = new WindowTimeClause(5, TimeUnit.SECONDS);
    final WithinExpression withinExpression = new WithinExpression(1, TimeUnit.SECONDS, grace);
    for (final Pair<JoinType, StreamStreamJoin> testcase : cases) {
        final SchemaKStream joinedKStream = testcase.right.join(schemaKStream, KEY, withinExpression, valueFormat.getFormatInfo(), valueFormat.getFormatInfo(), childContextStacker);
        // Then:
        assertThat(joinedKStream.getSourceStep(), equalTo(ExecutionStepFactory.streamStreamJoin(childContextStacker, testcase.left, KEY, InternalFormats.of(keyFormat, valueFormat.getFormatInfo()), InternalFormats.of(keyFormat, valueFormat.getFormatInfo()), initialSchemaKStream.getSourceStep(), schemaKStream.getSourceStep(), joinWindows, Optional.of(grace))));
    }
}
Also used : JoinWindows(org.apache.kafka.streams.kstream.JoinWindows) WithinExpression(io.confluent.ksql.parser.tree.WithinExpression) JoinType(io.confluent.ksql.execution.plan.JoinType) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) Pair(io.confluent.ksql.util.Pair) Test(org.junit.Test)

Example 9 with WithinExpression

use of io.confluent.ksql.parser.tree.WithinExpression in project ksql by confluentinc.

the class SqlFormatterTest method shouldFormatMultipleJoinsWithDifferentTypes.

@Test
public void shouldFormatMultipleJoinsWithDifferentTypes() {
    final List<JoinedSource> rights = ImmutableList.of(new JoinedSource(Optional.empty(), rightAlias, JoinedSource.Type.LEFT, criteria, Optional.of(new WithinExpression(10, TimeUnit.SECONDS))), new JoinedSource(Optional.empty(), right2Alias, JoinedSource.Type.INNER, criteria2, Optional.of(new WithinExpression(10, TimeUnit.SECONDS))));
    final Join join = new Join(leftAlias, rights);
    final String expected = "`left` L" + "\nLEFT OUTER JOIN `right` R WITHIN 10 SECONDS ON (('left.col0' = 'right.col0'))" + "\nINNER JOIN `right2` R2 WITHIN 10 SECONDS ON (('left.col0' = 'right2.col0'))";
    assertEquals(expected, SqlFormatter.formatSql(join));
}
Also used : JoinedSource(io.confluent.ksql.parser.tree.JoinedSource) WithinExpression(io.confluent.ksql.parser.tree.WithinExpression) Join(io.confluent.ksql.parser.tree.Join) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Example 10 with WithinExpression

use of io.confluent.ksql.parser.tree.WithinExpression in project ksql by confluentinc.

the class SqlFormatterTest method shouldFormatOuterJoin.

@Test
public void shouldFormatOuterJoin() {
    final Join join = new Join(leftAlias, ImmutableList.of(new JoinedSource(Optional.empty(), rightAlias, JoinedSource.Type.OUTER, criteria, Optional.of(new WithinExpression(10, TimeUnit.SECONDS)))));
    final String expected = "`left` L\nFULL OUTER JOIN `right` R WITHIN 10 SECONDS ON" + " (('left.col0' = 'right.col0'))";
    assertEquals(expected, SqlFormatter.formatSql(join));
}
Also used : JoinedSource(io.confluent.ksql.parser.tree.JoinedSource) Join(io.confluent.ksql.parser.tree.Join) WithinExpression(io.confluent.ksql.parser.tree.WithinExpression) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Aggregations

WithinExpression (io.confluent.ksql.parser.tree.WithinExpression)13 Test (org.junit.Test)13 Join (io.confluent.ksql.parser.tree.Join)10 JoinedSource (io.confluent.ksql.parser.tree.JoinedSource)10 StringContains.containsString (org.hamcrest.core.StringContains.containsString)5 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)4 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)4 Query (io.confluent.ksql.parser.tree.Query)4 Statement (io.confluent.ksql.parser.tree.Statement)4 TerminateQuery (io.confluent.ksql.parser.tree.TerminateQuery)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 WindowTimeClause (io.confluent.ksql.execution.windows.WindowTimeClause)3 KsqlException (io.confluent.ksql.util.KsqlException)2 JoinType (io.confluent.ksql.execution.plan.JoinType)1 AstNode (io.confluent.ksql.parser.tree.AstNode)1 Pair (io.confluent.ksql.util.Pair)1 JoinWindows (org.apache.kafka.streams.kstream.JoinWindows)1