From bf3191ce6218588bacf9b357f3ccb1225576aa47 Mon Sep 17 00:00:00 2001 From: Mario Molina Date: Tue, 19 Feb 2019 23:57:17 -0600 Subject: [PATCH 1/9] Added new currenttimestamp and currentdate functions --- .../function/udf/datetime/CurrentDate.java | 30 ++++++++ .../udf/datetime/CurrentTimestamp.java | 50 +++++++++++++ .../udf/datetime/CurrentDateTest.java | 40 +++++++++++ .../udf/datetime/CurrentTimestampTest.java | 71 +++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.java create mode 100644 ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java create mode 100644 ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java create mode 100644 ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.java new file mode 100644 index 000000000000..bbfebb03144e --- /dev/null +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.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 = "currentdate", + description = "Gets an integer representing days since epoch.") +public class CurrentDate { + + @Udf(description = "Gets an integer representing days since epoch.") + public int currentDate() { + return ((int) LocalDate.now().toEpochDay()); + } + +} diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java new file mode 100644 index 000000000000..aabf4c5193a7 --- /dev/null +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.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 io.confluent.ksql.function.KsqlFunctionException; +import io.confluent.ksql.function.udf.Udf; +import io.confluent.ksql.function.udf.UdfDescription; +import io.confluent.ksql.function.udf.UdfParameter; +import java.sql.Timestamp; +import java.time.ZoneId; + +@UdfDescription(name = "currenttimestamp", + description = "Gets the current timestamp represented in millisecond as BIGINT.") +public class CurrentTimestamp { + + @Udf(description = "Gets a BIGINT millisecond from the current timestamp.") + public long currentTimestamp() { + return new Timestamp(System.currentTimeMillis()).getTime(); + } + + @Udf(description = "Gets a BIGINT millisecond from the current timestamp" + + " in the given time zone.") + public long currentTimestamp( + @UdfParameter(value = "timeZone", + description = "timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," + + " \"America/Los_Angeles\", \"PDT\", \"Europe/London\"") final String timeZone) { + try { + final Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + final ZoneId zoneId = ZoneId.of(timeZone); + timestamp.toLocalDateTime().atZone(zoneId); + return timestamp.getTime(); + } catch (final RuntimeException e) { + throw new KsqlFunctionException("Failed to get the current timestamp': " + + "' at timezone '" + timeZone + "': " + e.getMessage(), e); + } + } + +} diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java new file mode 100644 index 000000000000..e7b8fb17c893 --- /dev/null +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java @@ -0,0 +1,40 @@ +/* + * 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 CurrentDateTest { + + private CurrentDate udf; + + @Before + public void setUp() { + udf = new CurrentDate(); + } + + @Test + public void shouldGetTheCurrentDate() { + final int now = ((int) LocalDate.now().toEpochDay()); + final int result = udf.currentDate(); + + assertEquals(now, result); + } + +} diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java new file mode 100644 index 000000000000..0b0f12db6dc6 --- /dev/null +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java @@ -0,0 +1,71 @@ +/* + * 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.KsqlFunctionException; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.sql.Timestamp; +import java.time.ZoneId; + +import static org.junit.Assert.assertTrue; + +public class CurrentTimestampTest { + + private CurrentTimestamp udf; + + @Rule + public final ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() { + udf = new CurrentTimestamp(); + } + + @Test + public void shouldGetTheCurrentTimestamp() { + final long before = new Timestamp(System.currentTimeMillis()).getTime(); + final long result = udf.currentTimestamp(); + final long after = new Timestamp(System.currentTimeMillis()).getTime(); + + assertTrue(before <= result && result <= after); + } + + @Test + public void shouldGetTheCurrentTimestampWithTimeZone() { + final String timeZone = "UTC"; + + final Timestamp before = new Timestamp(System.currentTimeMillis()); + before.toLocalDateTime().atZone(ZoneId.of(timeZone)); + + final long result = udf.currentTimestamp("UTC"); + + final Timestamp after = new Timestamp(System.currentTimeMillis()); + after.toLocalDateTime().atZone(ZoneId.of(timeZone)); + + assertTrue(before.getTime() <= result && result <= after.getTime()); + } + + @Test + public void shouldThrowIfInvalidTimeZone() { + expectedException.expect(KsqlFunctionException.class); + expectedException.expectMessage("Unknown time-zone ID: PST"); + udf.currentTimestamp("PST"); + } + +} From 671bb3cb0717f727b0cae1fd2a8c698a0e7ad6b6 Mon Sep 17 00:00:00 2001 From: Mario Molina Date: Wed, 20 Feb 2019 00:08:51 -0600 Subject: [PATCH 2/9] Updating syntax reference --- docs/developer-guide/syntax-reference.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 0e0df1295a6c..9a13b8915b5a 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1214,6 +1214,14 @@ Scalar functions +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | CONCAT | ``CONCAT(col1, '_hello')`` | Concatenate two strings. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ +| CURRENTDATE | ``CURRENTDATE()`` | Gets an integer representing days since epoch. | ++------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ +| CURRENTTIMESTAMP | ``CURRENTTIMESTAMP([TIMEZONE])`` | Gets the current timestamp represented in | +| | | millisecond as BIGINT. | +| | | TIMEZONE is an optional parameter and it is a | +| | | java.util.TimeZone ID format, for example: "UTC", | +| | | "America/Los_Angeles", "PDT", "Europe/London" | ++------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | 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 | From 131802baf1adba4cc7ec1f7bb2364a4ee420536e Mon Sep 17 00:00:00 2001 From: Mario Molina Date: Wed, 20 Feb 2019 19:04:35 -0600 Subject: [PATCH 3/9] Changing descriptions in docs --- docs/developer-guide/syntax-reference.rst | 19 +++++++++++-------- .../udf/datetime/CurrentTimestamp.java | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 9a13b8915b5a..e329efdac9bf 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1216,11 +1216,12 @@ Scalar functions +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | CURRENTDATE | ``CURRENTDATE()`` | Gets an integer representing days since epoch. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| CURRENTTIMESTAMP | ``CURRENTTIMESTAMP([TIMEZONE])`` | Gets the current timestamp represented in | -| | | millisecond as BIGINT. | +| CURRENTTIMESTAMP | ``CURRENTTIMESTAMP([TIMEZONE])`` | Gets the current timestamp in milliseconds, | +| | | represented as a BIGINT. | | | | TIMEZONE is an optional parameter and it is a | -| | | java.util.TimeZone ID format, for example: "UTC", | -| | | "America/Los_Angeles", "PDT", "Europe/London" | +| | | ``java.util.TimeZone`` ID format, for example: | +| | | "UTC", "America/Los_Angeles", "PDT", | +| | | "Europe/London". | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | DATETOSTRING | ``DATETOSTRING(START_DATE, 'yyyy-MM-dd')`` | Converts an integer representation of a date into | | | | a string representing the date in | @@ -1331,8 +1332,9 @@ Scalar functions | | | two successive single quotes, ``''``, for | | | | example: ``'yyyy-MM-dd''T''HH:mm:ssX'``. | | | | TIMEZONE is an optional parameter and it is a | -| | | java.util.TimeZone ID format, for example: "UTC", | -| | | "America/Los_Angeles", "PDT", "Europe/London" | +| | | ``java.util.TimeZone`` ID format, for example: | +| | | "UTC", "America/Los_Angeles", "PDT", | +| | | "Europe/London". | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | SUBSTRING | ``SUBSTRING(col1, 2, 5)`` | ``SUBSTRING(str, pos, [len]``. | | | | Returns a substring of ``str`` that starts at | @@ -1364,8 +1366,9 @@ Scalar functions | | | successive single quotes, ``''``, for example: | | | | ``'yyyy-MM-dd''T''HH:mm:ssX'``. | | | | TIMEZONE is an optional parameter and it is a | -| | | java.util.TimeZone ID format, for example: "UTC", | -| | | "America/Los_Angeles", "PDT", "Europe/London" | +| | | ``java.util.TimeZone`` ID format, for example: | +| | | "UTC", "America/Los_Angeles", "PDT", | +| | | "Europe/London". | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | TRIM | ``TRIM(col1)`` | Trim the spaces from the beginning and end of | | | | a string. | diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java index aabf4c5193a7..369f165af205 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java @@ -22,7 +22,7 @@ import java.time.ZoneId; @UdfDescription(name = "currenttimestamp", - description = "Gets the current timestamp represented in millisecond as BIGINT.") + description = "Gets the current timestamp in milliseconds, represented as a BIGINT.") public class CurrentTimestamp { @Udf(description = "Gets a BIGINT millisecond from the current timestamp.") From b13d9dfd5aabbf705a695c33d4189e93ceb07b1d Mon Sep 17 00:00:00 2001 From: Mario Molina Date: Thu, 14 Mar 2019 23:22:39 -0600 Subject: [PATCH 4/9] Renamed currenttimestamp function to unix_timestamp --- docs/developer-guide/syntax-reference.rst | 5 ++- ...rrentTimestamp.java => UnixTimestamp.java} | 18 +++++------ .../udf/datetime/CurrentDateTest.java | 4 +++ ...estampTest.java => UnixTimestampTest.java} | 32 ++++++++++++------- 4 files changed, 37 insertions(+), 22 deletions(-) rename ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/{CurrentTimestamp.java => UnixTimestamp.java} (73%) rename ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/{CurrentTimestampTest.java => UnixTimestampTest.java} (75%) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 5fe7a0a3875c..b928777ef293 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1243,12 +1243,15 @@ Scalar functions +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | CURRENTDATE | ``CURRENTDATE()`` | Gets an integer representing days since epoch. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| CURRENTTIMESTAMP | ``CURRENTTIMESTAMP([TIMEZONE])`` | Gets the current timestamp in milliseconds, | +| UNIX_TIMESTAMP | ``UNIX_TIMESTAMP([TIMEZONE])`` | Gets the Unix timestamp in milliseconds, | | | | represented as a BIGINT. | | | | TIMEZONE is an optional parameter and it is a | | | | ``java.util.TimeZone`` ID format, for example: | | | | "UTC", "America/Los_Angeles", "PDT", | | | | "Europe/London". | +| | | Notice that the returned timestamp might be | +| | | different in case of the local time in KSQL | +| | | servers are not synchronized between them. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | DATETOSTRING | ``DATETOSTRING(START_DATE, 'yyyy-MM-dd')`` | Converts an integer representation of a date into | | | | a string representing the date in | diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java similarity index 73% rename from ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java rename to ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java index 369f165af205..6e2ca727ee4e 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentTimestamp.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java @@ -21,18 +21,18 @@ import java.sql.Timestamp; import java.time.ZoneId; -@UdfDescription(name = "currenttimestamp", - description = "Gets the current timestamp in milliseconds, represented as a BIGINT.") -public class CurrentTimestamp { +@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 current timestamp.") - public long currentTimestamp() { - return new Timestamp(System.currentTimeMillis()).getTime(); + @Udf(description = "Gets a BIGINT millisecond from the Unix timestamp.") + public long unixTimestamp() { + return System.currentTimeMillis(); } - @Udf(description = "Gets a BIGINT millisecond from the current timestamp" + @Udf(description = "Gets a BIGINT millisecond from the Unix timestamp" + " in the given time zone.") - public long currentTimestamp( + public long unixTimestamp( @UdfParameter(value = "timeZone", description = "timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," + " \"America/Los_Angeles\", \"PDT\", \"Europe/London\"") final String timeZone) { @@ -42,7 +42,7 @@ public long currentTimestamp( timestamp.toLocalDateTime().atZone(zoneId); return timestamp.getTime(); } catch (final RuntimeException e) { - throw new KsqlFunctionException("Failed to get the current timestamp': " + throw new KsqlFunctionException("Failed to get the Unix timestamp': " + "' at timezone '" + timeZone + "': " + e.getMessage(), e); } } diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java index e7b8fb17c893..24de39cf1dd5 100644 --- a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java @@ -31,9 +31,13 @@ public void setUp() { @Test public void shouldGetTheCurrentDate() { + // Given: final int now = ((int) LocalDate.now().toEpochDay()); + + // When: final int result = udf.currentDate(); + // Then: assertEquals(now, result); } diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java similarity index 75% rename from ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java rename to ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java index 0b0f12db6dc6..6fd2f7de32b1 100644 --- a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentTimestampTest.java +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java @@ -25,47 +25,55 @@ import static org.junit.Assert.assertTrue; -public class CurrentTimestampTest { +public class UnixTimestampTest { - private CurrentTimestamp udf; + private UnixTimestamp udf; @Rule public final ExpectedException expectedException = ExpectedException.none(); @Before public void setUp() { - udf = new CurrentTimestamp(); + udf = new UnixTimestamp(); } @Test - public void shouldGetTheCurrentTimestamp() { - final long before = new Timestamp(System.currentTimeMillis()).getTime(); - final long result = udf.currentTimestamp(); - final long after = new Timestamp(System.currentTimeMillis()).getTime(); + 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); } @Test - public void shouldGetTheCurrentTimestampWithTimeZone() { + public void shouldGetTheUnixTimestampWithTimeZone() { + // Given: final String timeZone = "UTC"; - final Timestamp before = new Timestamp(System.currentTimeMillis()); before.toLocalDateTime().atZone(ZoneId.of(timeZone)); - final long result = udf.currentTimestamp("UTC"); - + // When: + final long result = udf.unixTimestamp(timeZone); final Timestamp after = new Timestamp(System.currentTimeMillis()); after.toLocalDateTime().atZone(ZoneId.of(timeZone)); + // Then: assertTrue(before.getTime() <= result && result <= after.getTime()); } @Test public void shouldThrowIfInvalidTimeZone() { + // Given: expectedException.expect(KsqlFunctionException.class); expectedException.expectMessage("Unknown time-zone ID: PST"); - udf.currentTimestamp("PST"); + + // When: + udf.unixTimestamp("PST"); } } From 51b53a9b6c46610b1ac582a1250fb88752cc4a3e Mon Sep 17 00:00:00 2001 From: Mario Molina Date: Thu, 25 Apr 2019 17:08:41 +0200 Subject: [PATCH 5/9] Updating documentation --- docs/developer-guide/syntax-reference.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 7a9f377938df..4cce7945d9f1 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1338,9 +1338,8 @@ Scalar functions | | | ``java.util.TimeZone`` ID format, for example: | | | | "UTC", "America/Los_Angeles", "PDT", | | | | "Europe/London". | -| | | Notice that the returned timestamp might be | -| | | different in case of the local time in KSQL | -| | | servers are not synchronized between them. | +| | | 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 | From 6a1ff153f0fdcc8348c6a3e973e1a5fa7255f778 Mon Sep 17 00:00:00 2001 From: Mario Molina Date: Tue, 25 Jun 2019 23:53:14 -0500 Subject: [PATCH 6/9] Renaming currentdate function to unix_date --- docs/developer-guide/syntax-reference.rst | 8 ++----- .../{CurrentDate.java => UnixDate.java} | 6 +++--- .../function/udf/datetime/UnixTimestamp.java | 21 ------------------- ...CurrentDateTest.java => UnixDateTest.java} | 10 ++++----- .../udf/datetime/UnixTimestampTest.java | 17 +-------------- 5 files changed, 11 insertions(+), 51 deletions(-) rename ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/{CurrentDate.java => UnixDate.java} (90%) rename ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/{CurrentDateTest.java => UnixDateTest.java} (84%) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 7cfb0e9b82da..702949b8cf2f 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1516,14 +1516,10 @@ Scalar functions +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ | CONCAT | ``CONCAT(col1, '_hello')`` | Concatenate two strings. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| CURRENTDATE | ``CURRENTDATE()`` | Gets an integer representing days since epoch. | +| UNIX_DATE | ``UNIX_DATE()`` | Gets an integer representing days since epoch. | +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ -| UNIX_TIMESTAMP | ``UNIX_TIMESTAMP([TIMEZONE])`` | Gets the Unix timestamp in milliseconds, | +| UNIX_TIMESTAMP | ``UNIX_TIMESTAMP()`` | Gets the Unix timestamp in milliseconds, | | | | represented as a BIGINT. | -| | | TIMEZONE is an optional parameter and it is a | -| | | ``java.util.TimeZone`` ID format, for example: | -| | | "UTC", "America/Los_Angeles", "PDT", | -| | | "Europe/London". | | | | The returned timestamp may differ depending on | | | | the local time of different KSQL Server instances.| +------------------------+---------------------------------------------------------------------------+---------------------------------------------------+ diff --git a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.java b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java similarity index 90% rename from ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.java rename to ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java index bbfebb03144e..bef95bef053a 100644 --- a/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/CurrentDate.java +++ b/ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java @@ -18,12 +18,12 @@ import io.confluent.ksql.function.udf.UdfDescription; import java.time.LocalDate; -@UdfDescription(name = "currentdate", +@UdfDescription(name = "unix_date", description = "Gets an integer representing days since epoch.") -public class CurrentDate { +public class UnixDate { @Udf(description = "Gets an integer representing days since epoch.") - public int currentDate() { + 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 index 6e2ca727ee4e..ef09e4e2bbf7 100644 --- 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 @@ -14,12 +14,8 @@ package io.confluent.ksql.function.udf.datetime; -import io.confluent.ksql.function.KsqlFunctionException; import io.confluent.ksql.function.udf.Udf; import io.confluent.ksql.function.udf.UdfDescription; -import io.confluent.ksql.function.udf.UdfParameter; -import java.sql.Timestamp; -import java.time.ZoneId; @UdfDescription(name = "unix_timestamp", description = "Gets the Unix timestamp in milliseconds, represented as a BIGINT.") @@ -30,21 +26,4 @@ public long unixTimestamp() { return System.currentTimeMillis(); } - @Udf(description = "Gets a BIGINT millisecond from the Unix timestamp" - + " in the given time zone.") - public long unixTimestamp( - @UdfParameter(value = "timeZone", - description = "timeZone is a java.util.TimeZone ID format, for example: \"UTC\"," - + " \"America/Los_Angeles\", \"PDT\", \"Europe/London\"") final String timeZone) { - try { - final Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - final ZoneId zoneId = ZoneId.of(timeZone); - timestamp.toLocalDateTime().atZone(zoneId); - return timestamp.getTime(); - } catch (final RuntimeException e) { - throw new KsqlFunctionException("Failed to get the Unix timestamp': " - + "' at timezone '" + timeZone + "': " + e.getMessage(), e); - } - } - } diff --git a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java similarity index 84% rename from ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java rename to ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java index 24de39cf1dd5..b1e5a1956683 100644 --- a/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/CurrentDateTest.java +++ b/ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java @@ -20,22 +20,22 @@ import static org.junit.Assert.assertEquals; -public class CurrentDateTest { +public class UnixDateTest { - private CurrentDate udf; + private UnixDate udf; @Before public void setUp() { - udf = new CurrentDate(); + udf = new UnixDate(); } @Test - public void shouldGetTheCurrentDate() { + public void shouldGetTheUnixDate() { // Given: final int now = ((int) LocalDate.now().toEpochDay()); // When: - final int result = udf.currentDate(); + 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 index 6fd2f7de32b1..de32f845aada 100644 --- 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 @@ -14,14 +14,12 @@ package io.confluent.ksql.function.udf.datetime; -import io.confluent.ksql.function.KsqlFunctionException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import java.sql.Timestamp; -import java.time.ZoneId; import static org.junit.Assert.assertTrue; @@ -53,27 +51,14 @@ public void shouldGetTheUnixTimestamp() { @Test public void shouldGetTheUnixTimestampWithTimeZone() { // Given: - final String timeZone = "UTC"; final Timestamp before = new Timestamp(System.currentTimeMillis()); - before.toLocalDateTime().atZone(ZoneId.of(timeZone)); // When: - final long result = udf.unixTimestamp(timeZone); + final long result = udf.unixTimestamp(); final Timestamp after = new Timestamp(System.currentTimeMillis()); - after.toLocalDateTime().atZone(ZoneId.of(timeZone)); // Then: assertTrue(before.getTime() <= result && result <= after.getTime()); } - @Test - public void shouldThrowIfInvalidTimeZone() { - // Given: - expectedException.expect(KsqlFunctionException.class); - expectedException.expectMessage("Unknown time-zone ID: PST"); - - // When: - udf.unixTimestamp("PST"); - } - } From 60ddd3b61980a5771aa1e866898016ae641ce575 Mon Sep 17 00:00:00 2001 From: Andy Coates <8012398+big-andy-coates@users.noreply.github.com> Date: Thu, 27 Jun 2019 08:42:31 +0100 Subject: [PATCH 7/9] Update docs to show return value of UNIX_DATE can vary --- docs/developer-guide/syntax-reference.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/developer-guide/syntax-reference.rst b/docs/developer-guide/syntax-reference.rst index 702949b8cf2f..901082243fa9 100644 --- a/docs/developer-guide/syntax-reference.rst +++ b/docs/developer-guide/syntax-reference.rst @@ -1517,6 +1517,8 @@ 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. | From ad318365eed0256faca0c5757468dde245105f30 Mon Sep 17 00:00:00 2001 From: Andy Coates <8012398+big-andy-coates@users.noreply.github.com> Date: Thu, 27 Jun 2019 08:43:32 +0100 Subject: [PATCH 8/9] Remove duplicate cate. --- .../function/udf/datetime/UnixTimestampTest.java | 14 -------------- 1 file changed, 14 deletions(-) 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 index de32f845aada..1e1c6d0bde0a 100644 --- 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 @@ -47,18 +47,4 @@ public void shouldGetTheUnixTimestamp() { // Then: assertTrue(before <= result && result <= after); } - - @Test - public void shouldGetTheUnixTimestampWithTimeZone() { - // Given: - final Timestamp before = new Timestamp(System.currentTimeMillis()); - - // When: - final long result = udf.unixTimestamp(); - final Timestamp after = new Timestamp(System.currentTimeMillis()); - - // Then: - assertTrue(before.getTime() <= result && result <= after.getTime()); - } - } From 9c79f103bf5dcd4ba8f049461ae82a3b249f201d Mon Sep 17 00:00:00 2001 From: Victoria Xia Date: Thu, 27 Jun 2019 11:15:57 -0700 Subject: [PATCH 9/9] docs: update changelog --- docs/changelog.rst | 2 ++ 1 file changed, 2 insertions(+) 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