-
Notifications
You must be signed in to change notification settings - Fork 167
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
Handle deparse of INTERVAL correctly when used in SET statements #184
Handle deparse of INTERVAL correctly when used in SET statements #184
Conversation
Just giving this one a bump. Maybe @lfittl you could take a look if you're around? |
Giving this another bump. I can also chip in and help once @lfittl has provided some guidance. |
@coderdan Thanks for the contribution! I've just pushed up a review commit (thanks for allowing maintainer edits), that should fix the "SET TIME ZONE" vs "SET timezone" output problem. It seems the Postgres grammar is actually special casing "SET TIME ZONE" (see gram.y), so we need to make sure to generate that on output when an interval is involved (even though the VariableSetStmt internally calls it "timezone"). This should make things work, but I'm still surprised why there is a hard crash when using "SET TIMEZONE" with an interval (it should just error out). Still trying to see if I can find the root cause of that. |
A-ha! just found the problem with the crash - the deparse tests were not handling errors correctly, and if we use "SET timezone" with an interval it will error out like this:
So this should now work as expected. @coderdan @freshtonic Could you look over the changes I made and see if that looks good to you as well? |
Thanks @lfittl! Appreciate the review and tweaks. Your additions look very reasonable to me. Glad I was on the right track :) |
Awesome, thank you @lfittl! LGTM. |
@lfittl is this OK to merge, no? |
@coderdan @freshtonic Yep! Merged now - thanks for your patience. |
I recently found an issue when parsing and then deparsing statements in pg_query which turned out to be a bug (or odd edgecase) in
libpg_query
.Given the following statement:
The parsing and then deparsing results in:
But this is not valid in PostgreSQL 15.1:
Docs are a little thin but it appears that PG has special case interval handling when used with
SET TIME ZONE
.See https://www.postgresql.org/docs/15/sql-set.html
Approach in this PR
I added a variant to the
DeparseContext
enum so that I can handle deparsing ofTypeCast
nodes for interval when used in aSET
statement. Unclear if this is the best approach or not and I welcome feedback/guidance.The tests still don't pass due to a remaining (odd) problem.
The test case I added to test/deparse_tests.c looks as follows:
However, the parse/deparse round-trip converts it back to:
Note the missing space between time and zone. While this is valid SQL the strings no longer match.
Oddly, changing the test to have no space results in a SEGFAULT! Perhaps I'm doing something wrong or I've uncovered another bug. Seeking input!
Many thanks.