edgedb icon indicating copy to clipboard operation
edgedb copied to clipboard

Sets with zero or one elements are assumed to be singular

Open jollygreenlaser opened this issue 1 year ago • 5 comments

This is more an issue with the client libraries than edgedb itself, but still

Discord thread

My example in Rust is:

with users := (select User filter ...)
select users { name, email, username }

With an output type of Vec<User>

This will work fine if it finds more than one user, but die if it got zero or one users.

It's really atrocious in Rust because it just gives a blank DescriptorMismatch as the error. If it's in a larger query it's a pretty subtle bug.

jollygreenlaser avatar Feb 23 '24 05:02 jollygreenlaser

Per the thread, the workaround is to do

with users := (select User filter ...)
select (users union {}) { name, email, username }

to force a set

jollygreenlaser avatar Feb 23 '24 05:02 jollygreenlaser

Maybe a new function assert_many() to force the cardinality. Though it wouldn't be an assertion. Does

with multi users :=

do the trick? Oh that's not valid syntax. Maybe that could be the solution?

CarsonF avatar Mar 06 '24 22:03 CarsonF

Sorry, forgot to come back and update this - multi is actually the better solution than the union thing. It goes on the final shape rather than the with variable.

I still think the db should be able to put an empty vec for no items, a single item vec for one item, rather than dying, but multi is semantically good as a syntax solution.

jollygreenlaser avatar Mar 06 '24 22:03 jollygreenlaser

select multi users? That isn't valid syntax either. I think that only works inside of a shape.

This works:

select { multi u := users }

not very pretty though, and it nests the output.

CarsonF avatar Mar 06 '24 22:03 CarsonF

Yes, that

jollygreenlaser avatar Mar 06 '24 22:03 jollygreenlaser