Skip to content

Add from_date_string as example Trino routine#19968

Merged
mosabua merged 1 commit intotrinodb:masterfrom
kevinjqliu:patch-1
Dec 5, 2023
Merged

Add from_date_string as example Trino routine#19968
mosabua merged 1 commit intotrinodb:masterfrom
kevinjqliu:patch-1

Conversation

@kevinjqliu
Copy link
Contributor

Description

This PR adds a new example to the Trino Routines Examples page.

The new example routine is named from_date_string. It takes a date string represented by VARCHAR and tries to best effort parse it into TIMESTAMP.
The parsing logic assumes the date string is in one of the YYYYmmdd, YYYYmmddHH, or ISO 8601 formats.

Use case: Hive table partitions are typically represented in one of these formats. It's cumbersome to figure out which date string format is used on a specific table. This routine takes care of the date string parsing regardless of the format.

Additional context and related issues

Saw https://trino.io/blog/2023/11/09/routines and wanted to give the new routines a try

Release notes

(x) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

# Section
* Fix some things. ({issue}`issuenumber`)

@cla-bot cla-bot bot added the cla-signed label Nov 30, 2023
@kevinjqliu kevinjqliu requested a review from mosabua November 30, 2023 06:30
@github-actions github-actions bot added the docs label Nov 30, 2023
@kevinjqliu
Copy link
Contributor Author

As a side note, I couldn't get nested If's to work.
Something like

    IF date_string like '%-%' THEN -- iso8601
      RETURN from_iso8601_timestamp(date_string);
    ELSE
      IF length(date_string) = 8 THEN -- YYYYmmdd
        RETURN date_parse(date_string, '%Y%m%d');
      ELSEIF length(date_string) = 10 THEN -- YYYYmmddHH
        RETURN date_parse(date_string, '%Y%m%d%H');
      END IF;
    END IF;

@mosabua
Copy link
Member

mosabua commented Nov 30, 2023

Excellent work @kevinjqliu .. I will review and test later today

@mosabua
Copy link
Member

mosabua commented Nov 30, 2023

As a side note, I couldn't get nested If's to work. Something like

    IF date_string like '%-%' THEN -- iso8601
      RETURN from_iso8601_timestamp(date_string);
    ELSE
      IF length(date_string) = 8 THEN -- YYYYmmdd
        RETURN date_parse(date_string, '%Y%m%d');
      ELSEIF length(date_string) = 10 THEN -- YYYYmmddHH
        RETURN date_parse(date_string, '%Y%m%d%H');
      END IF;
    END IF;

Maybe it works if you put the nested if into a separate begin/end block ..

Copy link
Member

@mosabua mosabua left a comment

Choose a reason for hiding this comment

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

Very nice example... just a few more tweaks and gotchas to add

@kevinjqliu kevinjqliu force-pushed the patch-1 branch 2 times, most recently from 4117517 to b7223fc Compare November 30, 2023 22:05
@kevinjqliu
Copy link
Contributor Author

kevinjqliu commented Nov 30, 2023

Markdown rendered using VSCode (updated to reflect the newest change)

Screenshot 2023-11-30 at 2 19 43 PM

@kevinjqliu kevinjqliu requested a review from mosabua November 30, 2023 22:10
@kevinjqliu kevinjqliu force-pushed the patch-1 branch 2 times, most recently from 1ab3335 to 0b4b5fa Compare November 30, 2023 22:19
@kevinjqliu
Copy link
Contributor Author

BTW @mosabua, Trino SQL routines with multiple ; do not play nice in Supserset
See apache/superset#26162

@mosabua
Copy link
Member

mosabua commented Dec 1, 2023

Yeah .. DBeaver has the same problem .. we had to update the CLI to support that as well

@kevinjqliu
Copy link
Contributor Author

Markdown rendered using VSCode

Screenshot 2023-12-04 at 6 29 27 PM

@kevinjqliu
Copy link
Contributor Author

kevinjqliu commented Dec 5, 2023

@mosabua is TIMESTAMP WITH TIME ZONEW. a typo? Should it be TIMESTAMP WITH TIME ZONE. instead?
https://trino.io/docs/current/language/types.html#timestamp-with-time-zone

@mosabua
Copy link
Member

mosabua commented Dec 5, 2023

@mosabua is TIMESTAMP WITH TIME ZONEW. a typo? Should it be TIMESTAMP WITH TIME ZONE. instead? https://trino.io/docs/current/language/types.html#timestamp-with-time-zone

Ooops .. typo from my end

@kevinjqliu
Copy link
Contributor Author

@mosabua fixed

Screenshot 2023-12-04 at 10 47 08 PM

@kevinjqliu kevinjqliu requested a review from mosabua December 5, 2023 06:47
Copy link
Member

@mosabua mosabua left a comment

Choose a reason for hiding this comment

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

Looks great now!

@mosabua mosabua merged commit a8d7bf7 into trinodb:master Dec 5, 2023
@github-actions github-actions bot added this to the 435 milestone Dec 5, 2023
@kevinjqliu kevinjqliu deleted the patch-1 branch December 5, 2023 16:55
@mosabua
Copy link
Member

mosabua commented Dec 6, 2023

Also dbeaver/dbeaver#21808

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants