Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

METRON-1816: Date format Stellar function #1233

Closed
wants to merge 3 commits into from

Conversation

merrimanr
Copy link
Contributor

Contributor Comments

This PR adds a Stellar function that wraps the Java SimpleDateFormat class. The first argument is a date format string and is required. The second argument is epoch time and is optional (defaults to current time). The third argument is a timezone and is also option (default time zone of the environment is used if missing).

Testing

This has been tested in full dev:

  1. Spin up full dev and launch the Stellar REPL. The DATE_FORMAT function should be available:
Stellar, Go!
Functions are loading lazily in the background and will be unavailable until loaded fully.
{}
[Stellar]>>> %functions DATE
DATE_FORMAT, IS_DATE
  1. Create a variable and set it to an epoch time:
[Stellar]>>> epoch := 1472131630748L
1472131630748
  1. Use the DATE_FORMAT function to format the value in the epoch field created in the previous step. The result should be a correctly formatted date that matches the format string, epoch time and timezone:
[Stellar]>>> DATE_FORMAT('EEE MMM dd yyyy hh:mm:ss zzz', epoch, 'EST')
Thu Aug 25 2016 08:27:10 EST
  1. Format the date using the default timezone. The result should match the default timezone of your environment:
[Stellar]>>> DATE_FORMAT('EEE MMM dd yyyy hh:mm:ss zzzz', epoch)
Thu Aug 25 2016 01:27:10 Coordinated Universal Time
  1. Format the date using the current time:
[Stellar]>>> DATE_FORMAT('EEE MMM dd yyyy hh:mm:ss zzz', 'GMT')
Tue Oct 09 2018 10:13:24 GMT
  1. Format the date using the current time and default time zone:
[Stellar]>>> DATE_FORMAT('EEE MMM dd yyyy hh:mm:ss zzzz')
Tue Oct 09 2018 10:12:54 Coordinated Universal Time

Pull Request Checklist

Thank you for submitting a contribution to Apache Metron.
Please refer to our Development Guidelines for the complete guide to follow for contributions.
Please refer also to our Build Verification Guidelines for complete smoke testing guides.

In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:

For all changes:

  • Is there a JIRA ticket associated with this PR? If not one needs to be created at Metron Jira.
  • Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
  • Has your PR been rebased against the latest commit within the target branch (typically master)?

For code changes:

  • Have you included steps to reproduce the behavior or problem that is being changed or addressed?

  • Have you included steps or a guide to how the change may be verified and tested manually?

  • Have you ensured that the full suite of tests and checks have been executed in the root metron folder via:

    mvn -q clean integration-test install && dev-utilities/build-utils/verify_licenses.sh 
    
  • Have you written or updated unit tests and or integration tests to verify your changes?

  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?

  • Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via site-book/target/site/index.html:

    cd site-book
    mvn site
    

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
It is also recommended that travis-ci is set up for your personal repository such that your branches are built there before submitting a pull request.

@simonellistonball
Copy link
Contributor

Looks like a very useful gap filler to me. +1 (non-binding) by inspection from me for sure.

@ottobackwards
Copy link
Contributor

we should support the ISO date format constants so that users don't have to copy those formats.

Copy link
Contributor

@ottobackwards ottobackwards left a comment

Choose a reason for hiding this comment

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

Just a couple of questions

public static String getDateFormat(String format, Optional<Long> epochTime, Optional<String> timezone) {
Long time = epochTime.orElseGet(System::currentTimeMillis);
TimezonedFormat fmt = timezone.map(s -> new TimezonedFormat(format, s)).orElseGet(() -> new TimezonedFormat(format));
SimpleDateFormat sdf = formatCache.get(fmt).get();
Copy link
Contributor

Choose a reason for hiding this comment

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

are there any errors we need to wrap here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no exceptions thrown by any calls in this method. What are you thinking we should do?

@Test(expected = ParseException.class)
public void testDateFormatNull() {
Object result = run("DATE_FORMAT('EEE MMM dd yyyy hh:mm:ss zzz', nada, 'EST')");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have a test for an invalid format

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

@simonellistonball
Copy link
Contributor

@ottobackwards wouldn't ISO constant support require us to do a check on the string before passing to the DateFormat instance, which on something that is likely to be run A LOT (i.e. multiple times for every message) could actually be a significant for performance, which I would say outweighs the convenience of not having to remember what the format looks like.

@ottobackwards
Copy link
Contributor

I'm not sure what you mean? Can you elaborate?
Also, I'm actually thinking of using DateFormatter and the new java time api's:

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

@simonellistonball
Copy link
Contributor

I would worry about having to parse the strong argument to determine whether to use a constant or custom formatter, but I guess my concern is limited because we’re caching the formatter anyway so maybe I’m just over thinking it. Just very conscious we should keep performance at front of mind for this.

@ottobackwards
Copy link
Contributor

This comes to mind for me, because using Nifi processors that take date formats and in other places I'm sick of cutting and pasting complex ISO formats into configurations when there are named formatters in java. This is just a suggestion, not a blocker.
We could create a new set of Stellar functions or something using an enum of formats like we do with other things in the future.

@simonellistonball
Copy link
Contributor

Right, I hear that! I also really like the idea of a follow on function that returns the default formats as a great solution to this problem. Should we do that?

@merrimanr
Copy link
Contributor Author

The existing Stellar date functions use SimpleDateFormat and already have a caching layer built in so I decided to reuse what was already there. Happy to switch to DateTimeFormatter here or in a follow on.

@ottobackwards
Copy link
Contributor

+1

@asfgit asfgit closed this in bf32fef Oct 18, 2018
justinleet pushed a commit to justinleet/metron that referenced this pull request Oct 25, 2018
justinleet pushed a commit to justinleet/metron that referenced this pull request Oct 25, 2018
justinleet pushed a commit to justinleet/metron that referenced this pull request Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants