Skip to content
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

Add postgreSql column default interval #4142

Merged

Conversation

griffio
Copy link
Contributor

@griffio griffio commented May 7, 2023

close #4122

  • Add PostgreSql specific grammar
  • Update fixture test

This brings the PostgreSql grammar to similar level of MySql support for Interval

Provides grammar for:

  some_interval_a INTERVAL DEFAULT INTERVAL '5 days',
  some_interval_b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - INTERVAL '5 days',
  some_interval_c TIMESTAMP NOT NULL DEFAULT NOW() - INTERVAL '5 days',
  some_interval_d INTERVAL DEFAULT INTERVAL '3h' + INTERVAL '20m'

Note:- Does not support
arithmetic e.g INTERVAL ’1 day’ * 7
“::interval” short-hand
“5 days” validation of literals
Modifiers e.g INTERVAL ‘5’ DAY

Other examples of Interval expressions that could be supported
https://github.com/postgres/postgres/blob/master/src/test/regress/sql/interval.sql

Provides grammer for:

  some_interval_a INTERVAL DEFAULT INTERVAL '5 days',
  some_interval_b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - INTERVAL '5 days',
  some_interval_c TIMESTAMP NOT NULL DEFAULT NOW() - INTERVAL '5 days',
  some_interval_d INTERVAL DEFAULT INTERVAL '3h' + INTERVAL '20m'

Note:- Does not support
arithmetic e.g INTERVAL ’1 day’ * 7
“::interval” short-hand
“5 days” validation of literals
Modifiers e.g INTERVAL ‘5’ DAY

Close cashapp#4122
Examples for:

Interval column with default Interval
Interval column with default Interval + interval
Timestamp column with default interval
Timestamp column with default now + interval
Timestamp column with default timestamp + interval
@hfhbd
Copy link
Collaborator

hfhbd commented May 7, 2023

Could you also add an integration test:
SELECT INTERVAL '20m'

Maybe we also need support in the type resolver or bnf to return a PostgreSqlType.INTERVAL.

@griffio
Copy link
Contributor Author

griffio commented May 8, 2023

I was looking into that. 👓

My understanding of expression INTERVAL '20m' is not a function call with parentheses like NOW(), the function_expr won't match it and PostgreSqlTypeResolver won't be used.

It's more like a dynamic column type with an argument.

Use 1: SELECT NOW() - INTERVAL '1 year 3 hours 20 minutes'
Use 2: SELECT * FROM T1 WHERE T1.subscription = INTERVAL '@ 1 month';'

Also - it could be argued that INTERVAL is a new token could be added to sql-psi SqlLexer.flex.
(If that project is still used)

We could merge what is currently working and then a new PR to work out the extended INTERVAL syntax (Sqlite doesn't support INTERVAL).

@hfhbd
Copy link
Collaborator

hfhbd commented May 8, 2023

Yeah, I am fine with the current state.
Of course we still use sql-psi, its the frontend of the compiler.

My understanding of expression INTERVAL '20m' is not a function call with parentheses like NOW(), the function_expr won't match it and PostgreSqlTypeResolver won't be used.

Yes, this is true. We would need to overwrite the literal expressions. But I could also do it myself :)

@hfhbd hfhbd merged commit e573c9f into cashapp:master May 8, 2023
6 checks passed
@griffio griffio deleted the add-postgreSql-column-default-interval branch May 8, 2023 11:01
hfhbd pushed a commit that referenced this pull request May 19, 2023
* Add PostgreSql interval support on column default

Provides grammer for:

  some_interval_a INTERVAL DEFAULT INTERVAL '5 days',
  some_interval_b TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP - INTERVAL '5 days',
  some_interval_c TIMESTAMP NOT NULL DEFAULT NOW() - INTERVAL '5 days',
  some_interval_d INTERVAL DEFAULT INTERVAL '3h' + INTERVAL '20m'

Note:- Does not support
arithmetic e.g INTERVAL ’1 day’ * 7
“::interval” short-hand
“5 days” validation of literals
Modifiers e.g INTERVAL ‘5’ DAY

Close #4122

* Update column fixture test for Interval

Examples for:

Interval column with default Interval
Interval column with default Interval + interval
Timestamp column with default interval
Timestamp column with default now + interval
Timestamp column with default timestamp + interval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

interval support for Postgres
2 participants