Search in sources :

Example 1 with WindowTimeClause

use of io.confluent.ksql.execution.windows.WindowTimeClause in project ksql by confluentinc.

the class RewrittenAnalysis method getWindowExpression.

@Override
public Optional<WindowExpression> getWindowExpression() {
    final Optional<WindowExpression> windowExpression = original.getWindowExpression();
    final Optional<RefinementInfo> refinementInfo = original.getRefinementInfo();
    /* Return the original window expression, unless there is no grace period provided during a
    suppression, in which case we rewrite the window expression to have a default grace period
    of zero.
    */
    if (!(windowExpression.isPresent() && !windowExpression.get().getKsqlWindowExpression().getGracePeriod().isPresent() && refinementInfo.isPresent() && refinementInfo.get().getOutputRefinement() == OutputRefinement.FINAL)) {
        return original.getWindowExpression();
    }
    final WindowExpression window = original.getWindowExpression().get();
    final KsqlWindowExpression ksqlWindowNew;
    final KsqlWindowExpression ksqlWindowOld = window.getKsqlWindowExpression();
    final Optional<NodeLocation> location = ksqlWindowOld.getLocation();
    final Optional<WindowTimeClause> retention = ksqlWindowOld.getRetention();
    if (ksqlWindowOld instanceof HoppingWindowExpression) {
        ksqlWindowNew = new HoppingWindowExpression(location, ((HoppingWindowExpression) ksqlWindowOld).getSize(), ((HoppingWindowExpression) ksqlWindowOld).getAdvanceBy(), retention, Optional.of(zeroGracePeriod));
    } else if (ksqlWindowOld instanceof TumblingWindowExpression) {
        ksqlWindowNew = new TumblingWindowExpression(location, ((TumblingWindowExpression) ksqlWindowOld).getSize(), retention, Optional.of(zeroGracePeriod));
    } else if (ksqlWindowOld instanceof SessionWindowExpression) {
        ksqlWindowNew = new SessionWindowExpression(location, ((SessionWindowExpression) ksqlWindowOld).getGap(), retention, Optional.of(zeroGracePeriod));
    } else {
        throw new KsqlException("WINDOW type must be HOPPING, TUMBLING, or SESSION");
    }
    return Optional.of(new WindowExpression(original.getWindowExpression().get().getWindowName(), ksqlWindowNew));
}
Also used : HoppingWindowExpression(io.confluent.ksql.execution.windows.HoppingWindowExpression) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) SessionWindowExpression(io.confluent.ksql.execution.windows.SessionWindowExpression) HoppingWindowExpression(io.confluent.ksql.execution.windows.HoppingWindowExpression) KsqlException(io.confluent.ksql.util.KsqlException) SessionWindowExpression(io.confluent.ksql.execution.windows.SessionWindowExpression) NodeLocation(io.confluent.ksql.parser.NodeLocation) RefinementInfo(io.confluent.ksql.serde.RefinementInfo) KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression)

Example 2 with WindowTimeClause

use of io.confluent.ksql.execution.windows.WindowTimeClause in project ksql by confluentinc.

the class ExpressionParserTest method shouldParseWindowExpressionWithRetention.

@Test
public void shouldParseWindowExpressionWithRetention() {
    // When:
    final KsqlWindowExpression parsed = ExpressionParser.parseWindowExpression("TUMBLING (SIZE 1 DAYS, RETENTION 2 DAYS, GRACE PERIOD 2 DAYS)");
    // Then:
    assertThat(parsed, equalTo(new TumblingWindowExpression(parsed.getLocation(), new WindowTimeClause(1, TimeUnit.DAYS), Optional.of(new WindowTimeClause(2, TimeUnit.DAYS)), Optional.of(new WindowTimeClause(2, TimeUnit.DAYS)))));
}
Also used : KsqlWindowExpression(io.confluent.ksql.execution.windows.KsqlWindowExpression) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression) Test(org.junit.Test)

Example 3 with WindowTimeClause

use of io.confluent.ksql.execution.windows.WindowTimeClause in project ksql by confluentinc.

the class StreamAggregateBuilderTest method givenSessionWindowedAggregate.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void givenSessionWindowedAggregate() {
    when(materializedFactory.<GenericKey, SessionStore<Bytes, byte[]>>create(any(), any(), any(), any())).thenReturn(sessionWindowMaterialized);
    when(groupedStream.windowedBy(any(SessionWindows.class))).thenReturn(sessionWindowedStream);
    when(sessionWindowedStream.aggregate(any(), any(), any(), any(Materialized.class))).thenReturn(windowed);
    when(windowed.transformValues(any(), any(Named.class))).thenReturn((KTable) windowedWithResults);
    when(windowedWithResults.transformValues(any(), any(Named.class))).thenReturn((KTable) windowedWithWindowBounds);
    windowedAggregate = new StreamWindowedAggregate(new ExecutionStepPropertiesV1(CTX), sourceStep, Formats.of(KEY_FORMAT, VALUE_FORMAT, SerdeFeatures.of(), SerdeFeatures.of()), NON_AGG_COLUMNS, FUNCTIONS, new SessionWindowExpression(Optional.empty(), new WindowTimeClause(WINDOW.getSeconds(), TimeUnit.SECONDS), Optional.of(retentionClause), Optional.of(gracePeriodClause)));
}
Also used : SessionStore(org.apache.kafka.streams.state.SessionStore) Named(org.apache.kafka.streams.kstream.Named) SessionWindows(org.apache.kafka.streams.kstream.SessionWindows) ExecutionStepPropertiesV1(io.confluent.ksql.execution.plan.ExecutionStepPropertiesV1) StreamWindowedAggregate(io.confluent.ksql.execution.plan.StreamWindowedAggregate) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) GenericKey(io.confluent.ksql.GenericKey) Materialized(org.apache.kafka.streams.kstream.Materialized) SessionWindowExpression(io.confluent.ksql.execution.windows.SessionWindowExpression)

Example 4 with WindowTimeClause

use of io.confluent.ksql.execution.windows.WindowTimeClause 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 5 with WindowTimeClause

use of io.confluent.ksql.execution.windows.WindowTimeClause in project ksql by confluentinc.

the class ScalablePushUtilTest method isScalablePushQuery_false_hasWindow.

@Test
public void isScalablePushQuery_false_hasWindow() {
    try (MockedStatic<ColumnExtractor> columnExtractor = mockStatic(ColumnExtractor.class)) {
        // When:
        expectIsSPQ(ColumnName.of("foo"), columnExtractor);
        when(query.getWindow()).thenReturn(Optional.of(new WindowExpression("foo", new TumblingWindowExpression(new WindowTimeClause(1, TimeUnit.MILLISECONDS)))));
        // Then:
        assertThat(ScalablePushUtil.isScalablePushQuery(query, ksqlEngine, ksqlConfig, ImmutableMap.of(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest")), equalTo(false));
    }
}
Also used : ColumnExtractor(io.confluent.ksql.execution.util.ColumnExtractor) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) WindowTimeClause(io.confluent.ksql.execution.windows.WindowTimeClause) TumblingWindowExpression(io.confluent.ksql.execution.windows.TumblingWindowExpression) Test(org.junit.Test)

Aggregations

WindowTimeClause (io.confluent.ksql.execution.windows.WindowTimeClause)14 Test (org.junit.Test)10 TumblingWindowExpression (io.confluent.ksql.execution.windows.TumblingWindowExpression)7 StreamWindowedAggregate (io.confluent.ksql.execution.plan.StreamWindowedAggregate)4 ExecutionStepPropertiesV1 (io.confluent.ksql.execution.plan.ExecutionStepPropertiesV1)3 KsqlWindowExpression (io.confluent.ksql.execution.windows.KsqlWindowExpression)3 WithinExpression (io.confluent.ksql.parser.tree.WithinExpression)3 HoppingWindowExpression (io.confluent.ksql.execution.windows.HoppingWindowExpression)2 SessionWindowExpression (io.confluent.ksql.execution.windows.SessionWindowExpression)2 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)2 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)2 Join (io.confluent.ksql.parser.tree.Join)2 JoinedSource (io.confluent.ksql.parser.tree.JoinedSource)2 Query (io.confluent.ksql.parser.tree.Query)2 Statement (io.confluent.ksql.parser.tree.Statement)2 TerminateQuery (io.confluent.ksql.parser.tree.TerminateQuery)2 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 GenericKey (io.confluent.ksql.GenericKey)1 JoinType (io.confluent.ksql.execution.plan.JoinType)1