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
Remove struct implicit casting when names mismatch and prevent table initialization using ROW #8942
Conversation
To not kill the usability of CREATE TABLE AS, it's also possible to use statement ok
CREATE TABLE t1 AS (
SELECT ARRAY[
(1, 'x'),
(2, 'y'),
(4, 's')
]::STRUCT(a int, b varchar)[] AS list
); So it doesn't have to be a two step process |
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.
LGTM! I only have two minor questions.
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.
Why are these changes included in this PR?
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.
Because spatial tests were failing I had to update the, and therefore add the most recent version of spatial to the CI run, but @Maxxen had also created new functions in spatial so those had to be added too.
@@ -75,6 +75,7 @@ def test_struct_type(self): | |||
type = duckdb.struct_type({'a': BIGINT, 'b': BOOLEAN}) | |||
assert str(type) == 'STRUCT(a BIGINT, b BOOLEAN)' | |||
|
|||
# FIXME: create an unnamed struct when fields are provided as a list |
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 you know if this is still the case? I think that we are using default names for the struct fields, as this is not the ROW
function.
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.
import duckdb
from duckdb.value.constant import StructValue
duckdb.execute("""
create table tbl (a STRUCT(a BIGINT, b VARCHAR, c BOOL))
""")
# Equivalent to ROW(42, 'bla', True)
duckdb.execute("insert into tbl select $col_a", {
'col_a': StructValue((42, 'bla', True), [int, str, bool])
})
res = duckdb.table("tbl").fetchall()
print(res)
Currently this passes, because we don't check for name equality, but after this PR this no longer works because the provided struct value is considered "named"
Internally this calls struct_type(children)
where children here is [int, str, bool]
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.
Thanks for the PR! Looks good - some comments:
@@ -31,4 +31,4 @@ SELECT count(c0 ORDER BY 0) FROM (SELECT 2 EXCEPT SELECT 2) c0; | |||
query I | |||
SELECT mode((c0, 0)) FROM (SELECT 1 c0), (SELECT 2); | |||
---- | |||
{'c0': 1, 'v2': 0} | |||
{'c0': 1, '': 0} |
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 would say that ()
should always create an unnamed struct. That's the behavior in Postgres.
Perhaps we can also change the ToString
of unnamed structs to leave out the name entirely, although that might be better done in a future PR.
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 also thought about the ToString modification, but that would break roundtripping of the type string, which is something to consider
Thanks! LGTM |
This PR is a slightly different implementation of #8854 but does the same thing but more succinctly.
Solves issues #3884, #7303, #7765, and #8301.
Structs can no longer be inserted unless the names match.
The following will now throw an error:
The
ROW
function is still supported and unnamed structs are properly auto-casted to any struct.Additionally tests can no longer be initialized using the
ROW
function, the following will now throw an exception:And should be rewritten as the following: