Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New UNIX_TIMESTAMP and UNIX_DATE datetime functions #2459

Merged
merged 13 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions docs/developer-guide/syntax-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,18 @@ Scalar functions
+------------------------+---------------------------------------------------------------------------+---------------------------------------------------+
| CONCAT | ``CONCAT(col1, '_hello')`` | Concatenate two strings. |
+------------------------+---------------------------------------------------------------------------+---------------------------------------------------+
| CURRENTDATE | ``CURRENTDATE()`` | Gets an integer representing days since epoch. |
big-andy-coates marked this conversation as resolved.
Show resolved Hide resolved
+------------------------+---------------------------------------------------------------------------+---------------------------------------------------+
| UNIX_TIMESTAMP | ``UNIX_TIMESTAMP([TIMEZONE])`` | Gets the Unix timestamp in milliseconds, |
big-andy-coates marked this conversation as resolved.
Show resolved Hide resolved
| | | 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 |
Copy link
Member

@JimGalasyn JimGalasyn Apr 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest: "The returned timestamp may differ depending on the local time of different KSQL Server instances."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

| | | 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 |
| | | the given format. Single quotes in the |
Expand Down Expand Up @@ -1350,8 +1362,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 |
Expand Down Expand Up @@ -1383,8 +1396,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. |
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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 = "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();
}

@Udf(description = "Gets a BIGINT millisecond from the Unix timestamp"
+ " in the given time zone.")
public long unixTimestamp(
big-andy-coates marked this conversation as resolved.
Show resolved Hide resolved
@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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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 CurrentDateTest {

private CurrentDate udf;

@Before
public void setUp() {
udf = new CurrentDate();
}

@Test
public void shouldGetTheCurrentDate() {
// Given:
final int now = ((int) LocalDate.now().toEpochDay());

// When:
final int result = udf.currentDate();

// Then:
assertEquals(now, result);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if you refactor the code so that you could mock the timestamp generation and make the tests more deterministic.

Copy link
Contributor

@big-andy-coates big-andy-coates Jun 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I'd agree, but in this case if we change the test to inject the time we're not really testing the class anymore, given its sole purpose is to provide the time!

(Note, this is in response to @hjafarpour's comment above)

final long result = udf.unixTimestamp();
final long after = System.currentTimeMillis();

// Then:
assertTrue(before <= result && result <= after);
}

@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 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");
}

}