-
Notifications
You must be signed in to change notification settings - Fork 71
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
INSERT ... SELECT or multi-row VALUES errors when using default values #448
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ec136db
When inserting multiple rows of data
Leo-XM-Zeng eba3a6b
Merge branch 'main' of github.com:Leo-XM-Zeng/pg_duckdb into default_…
Leo-XM-Zeng 546fe1e
Narrow the processing of "INSERT multi-VALUES"
Leo-XM-Zeng c3cf2cc
Merge branch 'main' of github.com:Leo-XM-Zeng/pg_duckdb into default_…
Leo-XM-Zeng 4e7b19e
Merge branch 'main' of github.com:Leo-XM-Zeng/pg_duckdb into default_…
Leo-XM-Zeng 7a217d9
More details are needed to complete the functionality
Leo-XM-Zeng e1e09b5
Merge branch 'main' of github.com:Leo-XM-Zeng/pg_duckdb into default_…
Leo-XM-Zeng b76aa3f
Enhanced INSERT... SELECT
Leo-XM-Zeng 0bd3b20
JelteF is right that we must search recursively for Const nodes
Leo-XM-Zeng e2e3642
Comment changes
Leo-XM-Zeng 9b90e87
Merge branch 'main' of github.com:Leo-XM-Zeng/pg_duckdb into default_…
Leo-XM-Zeng 9a8f3a2
Add test cases and tweak code
Leo-XM-Zeng 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
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
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
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.
This issue is not specific to
VALUES
, anINSERT ... SELECT
has the same problem.I think this is a better way of ignoring the filled in Const values.
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.
oh, I didn't notice the
INSERT... SELECT
also has problems.😒Simply ignoring
Const
here is not enough;FuncExpr
also needs to be ignored when the column type is varchar and has a default value.At present, this PR only deals with multi-VALUES. I would like to ask if I need to add a new PR to deal with
INSERT... SELECT
, or put them together?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.
Const
andFuncExpr
are not enough. Column default can be arbitrary expression, e.g.a int DEFAULT 1 + 2
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.
That's fair. I looked into the behavior a bit more and I think the original Var code was probably best. But it should include
select_rte
.And we should have a few more tests for different types of defaults and using INSERT ... SELECT. Like:
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.
I think you meant
if ((select_rte || values_rte) && !(IsA(tle->expr, Var)))
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.
Okay, I looked into this a bit more. I think there are at least three cases that we still need to handle:
I took a look at the querytree using the
pprint
C function included in Postgres.For case 1, the query tree also contains a
Const
node. Luckily we can differentiate it from theConst
nodes created by DEFAULT clauses by taking a look at itslocation
field. If that's-1
, then it's from the DEFAULT clause, otherwise it's coming from the query.For case 2, the Var node gets wrapped in a RelabelType node. That node type can also occur in DEFAULT clauses, so we need to do a bit more. Similarly for case 3, where the Var is wrapped in a FuncExpr. To solve these, I think we want to walk the expression tree using an
expression_tree_walker
to see if there are anyVar
nodes in the expression. If there are, then it's not a DEFAULT expression.I think this logic starts becoming complex enough that we don't want to copy paste it into every vendored in ruleutils file, but instead want to create a small helper function in
pgduckdb_ruleutils.cpp
that we can call from those vendored files. Just like we callpgduckdb_function_name
.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.
Do we eventually plan to fix the bug upstream in Postgres rather than just in pg_duckdb?
I believe the invariant is that calling
ToSQL()
on any querytree should yield correct SQL. However,rewriteTargetListIU
seems to break this invariant in Postgres sinceToSQL()
produces incorrect SQL on its output querytreeThere 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.
Wow, I'm stuck with these two CONST's! The method you give is really a good idea. I will follow your method to try to implement.
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.
I have tried to run it on PG and there seems to be no problem. Can you give me the abnormal test SQL and let me run it.
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.
I think once we have something that works it's worth trying to upstream it. But I think there's a good chance that it would not be accepted. In postgres
pg_get_querydef
is only used on Query structures that have not been passed through the rewriter, so this problem isn't exposed there. But they might very well accept it, as there is an argument that it's useful for extensions.