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 all 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
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/developer-guide/syntax-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
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 = "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());
}

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

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

}
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 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:
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);
}
}