diff --git a/docs/changelog.rst b/docs/changelog.rst index 57c7d92bfb13..485eacbc1b3b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -21,6 +21,8 @@ KSQL 5.4.0 includes new features, including: * A new config ``ksql.metrics.tags.custom`` for adding custom tags to emitted JMX metrics. See :ref:`ksql-metrics-tags-custom` for usage. +* New ``UNIX_TIMESTAMP()`` and ``UNIX_DATE()`` functions. + KSQL 5.4.0 includes the following misc. changes: * Require either the value for a ``@UdfParameter`` or for the UDF JAR to be compiled with diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 32c380f0f394..901082243fa9 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1516,6 +1516,15 @@ Scalar functions +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | CONCAT | ``CONCAT(col1, '_hello')`` | Concatenate two strings. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ +| UNIX_DATE | ``UNIX_DATE()`` | Gets an integer representing days since epoch. | +| | | The returned timestamp may differ depending on | +| | | the local time of different KSQL Server instances.| ++------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ +| UNIX_TIMESTAMP | ``UNIX_TIMESTAMP()`` | Gets the Unix timestamp in milliseconds, | +| | | represented as a BIGINT. | +| | | The returned timestamp may differ depending on | +| | | the local time of different KSQL Server instances.| ++------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | DATETOSTRING | ``DATETOSTRING(START_DATE, 'yyyy-MM-dd')`` | Converts an integer representation of a date into | | | | a string representing the date in | | | | the given format. Single quotes in the | diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java new file mode 100644 index 000000000000..bef95bef053a --- /dev/null +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java @@ -0,0 +1,30 @@ +/* + * Copyright 2018 Confluent Inc. + * + * Licensed under the Confluent Community 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.udf.Udf; +import io.confluent.ksql.function.udf.UdfDescription; +import java.time.LocalDate; + +@UdfDescription(name = "unix_date", + description = "Gets an integer representing days since epoch.") +public class UnixDate { + + @Udf(description = "Gets an integer representing days since epoch.") + public int unixDate() { + return ((int) LocalDate.now().toEpochDay()); + } + +} diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java new file mode 100644 index 000000000000..ef09e4e2bbf7 --- /dev/null +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java @@ -0,0 +1,29 @@ +/* + * Copyright 2018 Confluent Inc. + * + * Licensed under the Confluent Community 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.udf.Udf; +import io.confluent.ksql.function.udf.UdfDescription; + +@UdfDescription(name = "unix_timestamp", + description = "Gets the Unix timestamp in milliseconds, represented as a BIGINT.") +public class UnixTimestamp { + + @Udf(description = "Gets a BIGINT millisecond from the Unix timestamp.") + public long unixTimestamp() { + return System.currentTimeMillis(); + } + +} diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java new file mode 100644 index 000000000000..b1e5a1956683 --- /dev/null +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java @@ -0,0 +1,44 @@ +/* + * Copyright 2018 Confluent Inc. + * + * Licensed under the Confluent Community 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 org.junit.Before; +import org.junit.Test; +import java.time.LocalDate; + +import static org.junit.Assert.assertEquals; + +public class UnixDateTest { + + private UnixDate udf; + + @Before + public void setUp() { + udf = new UnixDate(); + } + + @Test + public void shouldGetTheUnixDate() { + // Given: + final int now = ((int) LocalDate.now().toEpochDay()); + + // When: + final int result = udf.unixDate(); + + // Then: + assertEquals(now, result); + } + +} diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java new file mode 100644 index 000000000000..1e1c6d0bde0a --- /dev/null +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2018 Confluent Inc. + * + * Licensed under the Confluent Community 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 org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.sql.Timestamp; + +import static org.junit.Assert.assertTrue; + +public class UnixTimestampTest { + + private UnixTimestamp udf; + + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + udf = new UnixTimestamp(); + } + + @Test + public void shouldGetTheUnixTimestamp() { + // Given: + final long before = System.currentTimeMillis(); + + // When: + final long result = udf.unixTimestamp(); + final long after = System.currentTimeMillis(); + + // Then: + assertTrue(before <= result && result <= after); + } +}