News for hackers. Stuff that matters.
Re:Against SQL by goto11 on Saturday July 10, 2021 @04:59AM (#27792124)
You can already do that by having circle and rectangle relations. Union types dos not give you any additional power compared to relations.

Your proposal might be more convenient though than creating multiple relations, so we should look into making it just as convenient to create the necessary relations to express this. So lets say your syntax proposal creates multiple relations under the hood - then I'm all aboard!

The "everything is a relation" is a really powerful concept in the relational model. Adding composite column types like arrays, unions etc. are still less powerful than just using relations. But people like it because the syntax is more convenient to use.

Re:Against SQL by NavinF on Saturday July 10, 2021 @06:10AM (#27792389)
Agreed, that would solve my aesthetic concerns and kinda solve OP's XY problem with JSON.

But the performance of auto-created tables for every union member would kinda suck if you never take advantage of the relation and still pay the cost. You'd also have to auto-create indexes so that queries that read full objects is as fast (IOPS) as storing BLOBs.

I was imagining a future database that takes this a step further and lets you define named parameterized queries next to your tables and chooses the best implementation of unions based on your queries. That's what I meant by "without thinking about it". We already do this for algorithms (query optimizer), and I'd love to see it work with data structures

Re:Against SQL by goto11 on Saturday July 10, 2021 @10:36AM (#27793603)
The basic premise of the relational model is that the physical storage structure and optimizations (including indexes) are independent of the logical model. A table could be stored row-by-row or column-by-column or any other clever way without it affecting the schema and queries - although it will affect the performance of the queries.
Re:Against SQL by laszlokorte on Saturday July 10, 2021 @07:20AM (#27792627)
But if I have a circle relation and a triangle relation, how do I create a foreign key for the favourite_shape column in my user relation?
Re:Against SQL by laszlokorte on Saturday July 10, 2021 @08:56AM (#27793053)
Thinking a bit more about it I think this would be a very elegant solution for Tagged-Unions in SQL:

- simply allow tagged unions for foreign key constraints: CONSTRAINT my_fk FOREIGN KEY (own_column) REFERENCES EITHER table_a (a_id) OR table_b (b_id) OR table_c (c_id)

- add join syntax to join via defined foreign key: FROM own_table JOIN VIA my_fk

The union FK would store an additional flag determining the target table and the JOIN VIA would switch on that flag and it would be allowed to use any columns from all target tables in the query but only the matching ones would be not null.

Re:Against SQL by goto11 on Saturday July 10, 2021 @10:29AM (#27793553)
If you put the foreign keys on the table_a/b/c tables, then you can express the same without having to introduce tagged unions.
Re:Against SQL by laszlokorte on Saturday July 10, 2021 @10:34AM (#27793585)
As I said in response to a sibling: But can you can not (in a simple way) prevent that both a circle and a triangle point to the same user and you can not enforce a NOT NULL for a user to must have a favourite shape.
Re:Against SQL by goto11 on Saturday July 10, 2021 @10:14AM (#27793463)
You would have the foreign key in the circle/triangle relations pointing to favourite_shape rather than the other way around.
Re:Against SQL by laszlokorte on Saturday July 10, 2021 @10:33AM (#27793582)
But can you can not (in a simple way) prevent that both a circle and a triangle point to the same user and you can not enforce a NOT NULL for a user to must have a favourite shape.
Re:Against SQL by goto11 on Saturday July 10, 2021 @10:41AM (#27793633)
You can do that with a check constraint, although I don't know if you consider it simple. It would be cool with better support for common constraint patterns.
Re:Against SQL by laszlokorte on Saturday July 10, 2021 @01:38PM (#27795071)
Could you give a concrete example for such a check constraint? I imagine each shape-table would need to have it's own check to test the existence of the same id in all other shape tables, which scales qudratically.

Or if you inverse the FK to make the user-table reference the shape tables you would need multiple potential nullable columns and a check constrain that exactly one is non-null. And it would still not solve the query side of things.

Re:Against SQL by goto11 on Tuesday July 13, 2021 @01:38PM (#27823767)
Check constraints are just SQL expressions, so in pesudocode it would be something like:

    (SELECT COUNT(*) shape JOIN square JOIN circle) = 1
How conveniently this can be expressed depend on the database system in question.

But if we are talking about adding features to SQL, it could have built-in support for such a constraint, just as there is built-in support for say foreign key constraints.

Re:Against SQL by Twisell on Monday July 12, 2021 @11:28AM (#27811259)
Are you by any chance generating GPT-3 comments as part of an experiment?

If not please take time to actually dive into modern SQL concepts there is likely a way to achieve your goal but I personally consider that it's pretty rude to ask random strangers a proof for a complex and loosely defined problem in the comments section. Stack Exchange should be a better place for instance.