Skip to content

Commit

Permalink
feat: New UNIX_TIMESTAMP and UNIX_DATE datetime functions (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmolimar authored and vcrfxia committed Jun 27, 2019
1 parent 14b6741 commit 39ce7f4
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
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:
final long result = udf.unixTimestamp();
final long after = System.currentTimeMillis();

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

0 comments on commit 39ce7f4

Please sign in to comment.