Skip to content

Commit

Permalink
fix: check for null inputs for various timestamp functions (#7180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zara Lim committed Mar 10, 2021
1 parent 55f5403 commit 42496c1
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public Timestamp convertTz(
description = "The toTimeZone in java.util.TimeZone ID format. For example: \"UTC\","
+ " \"America/Los_Angeles\", \"PST\", \"Europe/London\"") final String toTimeZone
) {
if (timestamp == null) {
return null;
}
try {
final long offset = TimeZone.getTimeZone(ZoneId.of(toTimeZone)).getOffset(timestamp.getTime())
- TimeZone.getTimeZone(ZoneId.of(fromTimeZone)).getOffset(timestamp.getTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public String formatTimestamp(
@UdfParameter(
description = " timeZone is a java.util.TimeZone ID format, for example: \"UTC\","
+ " \"America/Los_Angeles\", \"PST\", \"Europe/London\"") final String timeZone) {
if (timestamp == null) {
return null;
}
try {
final DateTimeFormatter formatter = formatters.get(formatPattern);
final ZoneId zoneId = ZoneId.of(timeZone);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public class FromUnixTime {
public Timestamp fromUnixTime(
@UdfParameter(
description = "Milliseconds since"
+ " January 1, 1970, 00:00:00 GMT.") final long epochMilli
+ " January 1, 1970, 00:00:00 GMT.") final Long epochMilli
) {
if (epochMilli == null) {
return null;
}
return new Timestamp(epochMilli);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ public long unixTimestamp() {

@Udf(description = "Returns the number of milliseconds "
+ "since 1970-01-01 00:00:00 UTC/GMT represented by the given timestamp.")
public long unixTimestamp(
public Long unixTimestamp(
@UdfParameter(
description = "the TIMESTAMP value.") final Timestamp timestamp
) {
if (timestamp == null) {
return null;
}
return timestamp.getTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import io.confluent.ksql.function.KsqlFunctionException;
Expand Down Expand Up @@ -61,4 +62,13 @@ public void shouldThrowOnInvalidTimezone() {
// Then:
assertThat(e.getMessage(), containsString("Invalid time zone"));
}

@Test
public void shouldReturnNull() {
// When:
final Object result = udf.convertTz(null, "America/Los_Angeles", "America/New_York");

// Then:
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -127,6 +128,15 @@ public void shouldSupportEmbeddedChars() {
assertThat(result, is(expectedResult));
}

@Test
public void shoudlReturnNull() {
// When:
final Object result = udf.formatTimestamp(null, "yyyy-MM-dd'T'HH:mm:ss.SSS");

// Then:
assertNull(result);
}

@Test
public void shouldThrowIfFormatInvalid() {
// When:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;

import java.sql.Timestamp;
import org.junit.Before;
Expand All @@ -37,4 +38,13 @@ public void shouldConvertToTimestamp() {
// Then:
assertThat(result, is(new Timestamp(100L)));
}

@Test
public void shouldReturnNull() {
// When:
final Object result = udf.fromUnixTime(null);

// Then:
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class UnixTimestampTest {
Expand Down Expand Up @@ -52,4 +53,13 @@ public void shouldReturnMilliseconds() {
// Then:
assertThat(result, is(100L));
}

@Test
public void shouldReturnNull() {
// When:
final Long result = udf.unixTimestamp(null);

// Then:
assertNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
],
"inputs": [
{"topic": "test_topic", "key": "0", "value": "100"},
{"topic": "test_topic", "key": "0", "value": "1589234313000"}
{"topic": "test_topic", "key": "0", "value": "1589234313000"},
{"topic": "test_topic", "key": "0", "value": null}
],
"outputs": [
{"topic": "TS", "key": "0", "value": "10800100"},
{"topic": "TS", "key": "0", "value": "1589245113000"}
{"topic": "TS", "key": "0", "value": "1589245113000"},
{"topic": "TS", "key": "0", "value": null}
]
},
{
Expand All @@ -26,11 +28,13 @@
],
"inputs": [
{"topic": "test_topic", "key": "0", "value": "100"},
{"topic": "test_topic", "key": "0", "value": "1589234313000"}
{"topic": "test_topic", "key": "0", "value": "1589234313000"},
{"topic": "test_topic", "key": "0", "value": null}
],
"outputs": [
{"topic": "TS", "key": "0", "value": "10800100"},
{"topic": "TS", "key": "0", "value": "1589245113000"}
{"topic": "TS", "key": "0", "value": "1589245113000"},
{"topic": "TS", "key": "0", "value": null}
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
],
"inputs": [
{"topic": "test_topic", "key": "0", "value": "100"},
{"topic": "test_topic", "key": "0", "value": "1589234313000"}
{"topic": "test_topic", "key": "0", "value": "1589234313000"},
{"topic": "test_topic", "key": "0", "value": null}
],
"outputs": [
{"topic": "TS", "key": "0", "value": "100"},
{"topic": "TS", "key": "0", "value": "1589234313000"}
{"topic": "TS", "key": "0", "value": "1589234313000"},
{"topic": "TS", "key": "0", "value": null}
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
"CREATE STREAM OUTPUT AS SELECT K, FORMAT_TIMESTAMP(TIME, 'yyyy-MM-dd HH:mm:ss zzz','America/Los_Angeles') FROM input;"
],
"inputs": [
{"topic": "input", "value": {"time": 1526075913000}, "timestamp": 1526075913000}
{"topic": "input", "value": {"time": 1526075913000}, "timestamp": 1526075913000},
{"topic": "input", "value": {"time": null}, "timestamp": 1526075913000}
],
"outputs": [
{"topic": "OUTPUT", "value": {"KSQL_COL_0":"2018-05-11 14:58:33 PDT"}, "timestamp": 1526075913000}
{"topic": "OUTPUT", "value": {"KSQL_COL_0":"2018-05-11 14:58:33 PDT"}, "timestamp": 1526075913000},
{"topic": "OUTPUT", "value": {"KSQL_COL_0":null}, "timestamp": 1526075913000}
]
},
{
Expand All @@ -51,10 +53,12 @@
"CREATE STREAM OUTPUT AS SELECT K, FORMAT_TIMESTAMP(TIME, 'yyyy-MM-dd HH:mm:ss') FROM input;"
],
"inputs": [
{"topic": "input", "value": {"time": 1526075913000}, "timestamp": 1526075913000}
{"topic": "input", "value": {"time": 1526075913000}, "timestamp": 1526075913000},
{"topic": "input", "value": {"time": null}, "timestamp": 1526075913000}
],
"outputs": [
{"topic": "OUTPUT", "value": {"KSQL_COL_0":"2018-05-11 21:58:33"}, "timestamp": 1526075913000}
{"topic": "OUTPUT", "value": {"KSQL_COL_0":"2018-05-11 21:58:33"}, "timestamp": 1526075913000},
{"topic": "OUTPUT", "value": {"KSQL_COL_0":null}, "timestamp": 1526075913000}
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
],
"inputs": [
{"topic": "test_topic", "key": "0", "value": "a"},
{"topic": "test_topic", "key": "0", "value": "b"}
{"topic": "test_topic", "key": "0", "value": "b"},
{"topic": "test_topic", "key": "0", "value": null}
],
"outputs": [
{"topic": "TS", "key": "0", "value": "true"},
{"topic": "TS", "key": "0", "value": "true"}
{"topic": "TS", "key": "0", "value": "true"},
{"topic": "TS", "key": "0", "value": null}
]
},
{
Expand All @@ -26,11 +28,13 @@
],
"inputs": [
{"topic": "test_topic", "key": "0", "value": "100"},
{"topic": "test_topic", "key": "0", "value": "1589234313000"}
{"topic": "test_topic", "key": "0", "value": "1589234313000"},
{"topic": "test_topic", "key": "0", "value": null}
],
"outputs": [
{"topic": "TS", "key": "0", "value": "100"},
{"topic": "TS", "key": "0", "value": "1589234313000"}
{"topic": "TS", "key": "0", "value": "1589234313000"},
{"topic": "TS", "key": "0", "value": null}
]
}
]
Expand Down

0 comments on commit 42496c1

Please sign in to comment.