-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bf3191c
Added new currenttimestamp and currentdate functions
mmolimar 671bb3c
Updating syntax reference
mmolimar 131802b
Changing descriptions in docs
mmolimar 91a83ee
Merge branch 'master' of https://github.com/confluentinc/ksql into fe…
mmolimar b13d9df
Renamed currenttimestamp function to unix_timestamp
mmolimar c9005d6
Merge branch 'master' of https://github.com/confluentinc/ksql into fe…
mmolimar 51b53a9
Updating documentation
mmolimar 2afc64f
Merge branch 'master' of https://github.com/confluentinc/ksql into fe…
mmolimar 85c56df
Merge branch 'master' of https://github.com/confluentinc/ksql into fe…
mmolimar 6a1ff15
Renaming currentdate function to unix_date
mmolimar 60ddd3b
Update docs to show return value of UNIX_DATE can vary
big-andy-coates ad31836
Remove duplicate cate.
big-andy-coates 9c79f10
docs: update changelog
vcrfxia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixDate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
ksql-engine/src/main/java/io/confluent/ksql/function/udf/datetime/UnixTimestamp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixDateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
ksql-engine/src/test/java/io/confluent/ksql/function/udf/datetime/UnixTimestampTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)