-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add TIMEADD and TIMESUB functions (#7727)
* feat: add TIMEADD and TIMESUB functions * generate historic plans
- Loading branch information
Zara Lim
authored
Jun 30, 2021
1 parent
a15b108
commit 75806a0
Showing
25 changed files
with
2,233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/datetime/TimeAdd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.datetime; | ||
|
||
import io.confluent.ksql.function.FunctionCategory; | ||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.sql.Time; | ||
import java.time.LocalTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@UdfDescription( | ||
name = "timeadd", | ||
category = FunctionCategory.DATE_TIME, | ||
author = KsqlConstants.CONFLUENT_AUTHOR, | ||
description = "Adds a duration to a TIME value." | ||
) | ||
public class TimeAdd { | ||
|
||
@Udf(description = "Adds a duration to a time") | ||
public Time timeAdd( | ||
@UdfParameter(description = "A unit of time, for example SECOND or HOUR") final TimeUnit unit, | ||
@UdfParameter(description = "An integer number of intervals to add") final Integer interval, | ||
@UdfParameter(description = "A TIME value.") final Time time | ||
) { | ||
if (unit == null || interval == null || time == null) { | ||
return null; | ||
} | ||
final long nanoResult = LocalTime.ofNanoOfDay(time.getTime() * 1000_000) | ||
.plus(unit.toNanos(interval), ChronoUnit.NANOS) | ||
.toNanoOfDay(); | ||
return new Time(TimeUnit.NANOSECONDS.toMillis(nanoResult)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
ksqldb-engine/src/main/java/io/confluent/ksql/function/udf/datetime/TimeSub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.datetime; | ||
|
||
import io.confluent.ksql.function.FunctionCategory; | ||
import io.confluent.ksql.function.udf.Udf; | ||
import io.confluent.ksql.function.udf.UdfDescription; | ||
import io.confluent.ksql.function.udf.UdfParameter; | ||
import io.confluent.ksql.util.KsqlConstants; | ||
import java.sql.Time; | ||
import java.time.LocalTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@UdfDescription( | ||
name = "timesub", | ||
category = FunctionCategory.DATE_TIME, | ||
author = KsqlConstants.CONFLUENT_AUTHOR, | ||
description = "Subtracts a duration from a TIME value." | ||
) | ||
public class TimeSub { | ||
|
||
@Udf(description = "Subtracts a duration from a time") | ||
public Time timeSub( | ||
@UdfParameter(description = "A unit of time, for example SECOND or HOUR") final TimeUnit unit, | ||
@UdfParameter( | ||
description = "An integer number of intervals to subtract") final Integer interval, | ||
@UdfParameter(description = "A TIME value.") final Time time | ||
) { | ||
if (unit == null || interval == null || time == null) { | ||
return null; | ||
} | ||
final long nanoResult = LocalTime.ofNanoOfDay(time.getTime() * 1000_000) | ||
.minus(unit.toNanos(interval), ChronoUnit.NANOS) | ||
.toNanoOfDay(); | ||
return new Time(TimeUnit.NANOSECONDS.toMillis(nanoResult)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
ksqldb-engine/src/test/java/io/confluent/ksql/function/udf/datetime/TimeAddTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.datetime; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.sql.Time; | ||
import java.sql.Timestamp; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class TimeAddTest { | ||
private TimeAdd udf; | ||
|
||
@Before | ||
public void setUp() { | ||
udf = new TimeAdd(); | ||
} | ||
|
||
@Test | ||
public void shouldAddToTime() { | ||
// When: | ||
assertThat(udf.timeAdd(TimeUnit.MILLISECONDS, 50, new Time(1000)).getTime(), is(1050L)); | ||
assertThat(udf.timeAdd(TimeUnit.DAYS, 2, new Time(1000)).getTime(), is(1000L)); | ||
assertThat(udf.timeAdd(TimeUnit.DAYS, -2, new Time(1000)).getTime(), is(1000L)); | ||
assertThat(udf.timeAdd(TimeUnit.MINUTES, -1, new Time(60000)).getTime(), is(0L)); | ||
} | ||
|
||
@Test | ||
public void handleNullTime() { | ||
assertNull(udf.timeAdd(TimeUnit.MILLISECONDS, -300, null)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
ksqldb-engine/src/test/java/io/confluent/ksql/function/udf/datetime/TimeSubTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.function.udf.datetime; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.sql.Time; | ||
import java.sql.Timestamp; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class TimeSubTest { | ||
private TimeSub udf; | ||
|
||
@Before | ||
public void setUp() { | ||
udf = new TimeSub(); | ||
} | ||
|
||
@Test | ||
public void shouldAddToTime() { | ||
// When: | ||
assertThat(udf.timeSub(TimeUnit.MILLISECONDS, 50, new Time(1000)).getTime(), is(950L)); | ||
assertThat(udf.timeSub(TimeUnit.DAYS, 2, new Time(1000)).getTime(), is(1000L)); | ||
assertThat(udf.timeSub(TimeUnit.DAYS, -2, new Time(1000)).getTime(), is(1000L)); | ||
assertThat(udf.timeSub(TimeUnit.MINUTES, -1, new Time(60000)).getTime(), is(120000L)); | ||
} | ||
|
||
@Test | ||
public void handleNullTime() { | ||
assertNull(udf.timeSub(TimeUnit.MILLISECONDS, -300, null)); | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
...al-tests/src/test/resources/historical_plans/timeadd_-_adds/7.0.0_1624587222167/plan.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
{ | ||
"plan" : [ { | ||
"@type" : "ksqlPlanV1", | ||
"statementText" : "CREATE STREAM TEST (ID STRING KEY, TIME TIME) WITH (KAFKA_TOPIC='test', KEY_FORMAT='KAFKA', VALUE_FORMAT='DELIMITED');", | ||
"ddlCommand" : { | ||
"@type" : "createStreamV1", | ||
"sourceName" : "TEST", | ||
"schema" : "`ID` STRING KEY, `TIME` TIME", | ||
"topicName" : "test", | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "DELIMITED" | ||
} | ||
}, | ||
"orReplace" : false | ||
} | ||
}, { | ||
"@type" : "ksqlPlanV1", | ||
"statementText" : "CREATE STREAM TEST2 AS SELECT\n TEST.ID ID,\n TIMEADD(MILLISECONDS, 10, TEST.TIME) KSQL_COL_0\nFROM TEST TEST\nEMIT CHANGES", | ||
"ddlCommand" : { | ||
"@type" : "createStreamV1", | ||
"sourceName" : "TEST2", | ||
"schema" : "`ID` STRING KEY, `KSQL_COL_0` TIME", | ||
"topicName" : "TEST2", | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "DELIMITED" | ||
} | ||
}, | ||
"orReplace" : false | ||
}, | ||
"queryPlan" : { | ||
"sources" : [ "TEST" ], | ||
"sink" : "TEST2", | ||
"physicalPlan" : { | ||
"@type" : "streamSinkV1", | ||
"properties" : { | ||
"queryContext" : "TEST2" | ||
}, | ||
"source" : { | ||
"@type" : "streamSelectV1", | ||
"properties" : { | ||
"queryContext" : "Project" | ||
}, | ||
"source" : { | ||
"@type" : "streamSourceV1", | ||
"properties" : { | ||
"queryContext" : "KsqlTopic/Source" | ||
}, | ||
"topicName" : "test", | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "DELIMITED" | ||
} | ||
}, | ||
"sourceSchema" : "`ID` STRING KEY, `TIME` TIME" | ||
}, | ||
"keyColumnNames" : [ "ID" ], | ||
"selectExpressions" : [ "TIMEADD(MILLISECONDS, 10, TIME) AS KSQL_COL_0" ] | ||
}, | ||
"formats" : { | ||
"keyFormat" : { | ||
"format" : "KAFKA" | ||
}, | ||
"valueFormat" : { | ||
"format" : "DELIMITED" | ||
} | ||
}, | ||
"topicName" : "TEST2" | ||
}, | ||
"queryId" : "CSAS_TEST2_0" | ||
} | ||
} ], | ||
"configs" : { | ||
"ksql.extension.dir" : "ext", | ||
"ksql.streams.cache.max.bytes.buffering" : "0", | ||
"ksql.security.extension.class" : null, | ||
"metric.reporters" : "", | ||
"ksql.transient.prefix" : "transient_", | ||
"ksql.query.status.running.threshold.seconds" : "300", | ||
"ksql.streams.default.deserialization.exception.handler" : "io.confluent.ksql.errors.LogMetricAndContinueExceptionHandler", | ||
"ksql.output.topic.name.prefix" : "", | ||
"ksql.query.pull.enable.standby.reads" : "false", | ||
"ksql.persistence.default.format.key" : "KAFKA", | ||
"ksql.query.persistent.max.bytes.buffering.total" : "-1", | ||
"ksql.queryanonymizer.logs_enabled" : "true", | ||
"ksql.query.error.max.queue.size" : "10", | ||
"ksql.variable.substitution.enable" : "true", | ||
"ksql.internal.topic.min.insync.replicas" : "1", | ||
"ksql.streams.shutdown.timeout.ms" : "300000", | ||
"ksql.internal.topic.replicas" : "1", | ||
"ksql.insert.into.values.enabled" : "true", | ||
"ksql.query.pull.max.allowed.offset.lag" : "9223372036854775807", | ||
"ksql.query.pull.max.qps" : "2147483647", | ||
"ksql.access.validator.enable" : "auto", | ||
"ksql.streams.bootstrap.servers" : "localhost:0", | ||
"ksql.queryanonymizer.cluster_namespace" : null, | ||
"ksql.query.pull.metrics.enabled" : "true", | ||
"ksql.create.or.replace.enabled" : "true", | ||
"ksql.metrics.extension" : null, | ||
"ksql.hidden.topics" : "_confluent.*,__confluent.*,_schemas,__consumer_offsets,__transaction_state,connect-configs,connect-offsets,connect-status,connect-statuses", | ||
"ksql.cast.strings.preserve.nulls" : "true", | ||
"ksql.authorization.cache.max.entries" : "10000", | ||
"ksql.pull.queries.enable" : "true", | ||
"ksql.lambdas.enabled" : "true", | ||
"ksql.suppress.enabled" : "false", | ||
"ksql.query.push.scalable.enabled" : "false", | ||
"ksql.query.push.scalable.interpreter.enabled" : "true", | ||
"ksql.sink.window.change.log.additional.retention" : "1000000", | ||
"ksql.readonly.topics" : "_confluent.*,__confluent.*,_schemas,__consumer_offsets,__transaction_state,connect-configs,connect-offsets,connect-status,connect-statuses", | ||
"ksql.query.persistent.active.limit" : "2147483647", | ||
"ksql.persistence.wrap.single.values" : null, | ||
"ksql.authorization.cache.expiry.time.secs" : "30", | ||
"ksql.query.retry.backoff.initial.ms" : "15000", | ||
"ksql.query.transient.max.bytes.buffering.total" : "-1", | ||
"ksql.schema.registry.url" : "", | ||
"ksql.properties.overrides.denylist" : "", | ||
"ksql.query.pull.max.concurrent.requests" : "2147483647", | ||
"ksql.streams.auto.offset.reset" : "earliest", | ||
"ksql.connect.url" : "http://localhost:8083", | ||
"ksql.service.id" : "some.ksql.service.id", | ||
"ksql.streams.default.production.exception.handler" : "io.confluent.ksql.errors.ProductionExceptionHandlerUtil$LogAndFailProductionExceptionHandler", | ||
"ksql.query.pull.interpreter.enabled" : "true", | ||
"ksql.streams.commit.interval.ms" : "2000", | ||
"ksql.query.pull.table.scan.enabled" : "false", | ||
"ksql.streams.auto.commit.interval.ms" : "0", | ||
"ksql.streams.topology.optimization" : "all", | ||
"ksql.query.retry.backoff.max.ms" : "900000", | ||
"ksql.streams.num.stream.threads" : "4", | ||
"ksql.timestamp.throw.on.invalid" : "false", | ||
"ksql.metrics.tags.custom" : "", | ||
"ksql.persistence.default.format.value" : null, | ||
"ksql.udfs.enabled" : "true", | ||
"ksql.udf.enable.security.manager" : "true", | ||
"ksql.connect.worker.config" : "", | ||
"ksql.nested.error.set.null" : "true", | ||
"ksql.udf.collect.metrics" : "false", | ||
"ksql.query.pull.thread.pool.size" : "100", | ||
"ksql.persistent.prefix" : "query_", | ||
"ksql.metastore.backup.location" : "", | ||
"ksql.error.classifier.regex" : "", | ||
"ksql.suppress.buffer.size.bytes" : "-1" | ||
} | ||
} |
Oops, something went wrong.