News for hackers. Stuff that matters.
Re:Handling Null Values in Postgres by iaabtpbtpnn on Thursday March 05, 2020 @01:34PM (#22496506)
SQL needs to have NULL, because otherwise how does an outer join work? And once NULL is there, you need rules for how it interacts with other values. What is the alternative?
Re:Handling Null Values in Postgres by jeffdavis on Thursday March 05, 2020 @02:02PM (#22496827)
A left outer join duplicates the tuple on the LHS for each tuple on the RHS. It's almost exclusively used for grouping operations, so then you need to group the LHS tuples back into a single tuple and aggregate the RHS.

But that's just weird -- in a normal programming language you would never duplicate the tuples and make this mess; you would keep a single tuple on the LHS and group the RHS into a set (or array/list). Then you'd aggregate the set on the RHS. That model is simple because an empty set is a value with known semantics (sum of an empty set is zero), whereas a half-NULL row is just a hack.

Before someone asks, this has nothing to do with declarative/imperative, and nothing to do with some purity of the relational model. You can still have a declarative relational model with values that happen to be sets (or even relations).

Re:Handling Null Values in Postgres by marcosdumay on Thursday March 05, 2020 @04:15PM (#22498353)
An outer join is also used to query optional values and alternatives. There are probably many other use cases I'm not remembering now.

Some programming languages have a rich set of containers to replace what SQL does with outer joins, others simply lack them, and pile hacks over hacks that are always much worse than the SQL one.

Re:Handling Null Values in Postgres by int_19h on Thursday March 05, 2020 @04:24PM (#22498453)
NULL is actually not a good way to represent an outer join, because an outer join should not produce any "unknown" values - only "missing" values, but NULL's semantics is closer to "unknown".
Re:Handling Null Values in Postgres by derefr on Thursday March 05, 2020 @09:08PM (#22500435)
It depends on your data ingestion semantics.

In a data-warehouse built up from event-sourced data, "missing" and "unknown" are the same thing: "I don't have this in here yet, so once I get it, it might turn out to be anything."

For a column with a value you know is "nothing", I'd suggest you keep a separate column to denote the type of the column, just as you would in a union data structure. After all, the explicit choice to have or not have a value is, itself, data, that you can either be in possession of, or not. You can't use one sentinel value to indicate both "absence of value" and "absence of knowledge of absence-state of value"; you need two! (Of course, if your knowledge of the absence-state is absent, the data is probably absent too, so you'd probably have a CHECK constraint to assert that if the tag is NULL, the data must also be NULL.)

Re:Handling Null Values in Postgres by mjw_byrne on Thursday March 05, 2020 @01:50PM (#22496698)
There are in fact alternatives to NULL for outer joins. For example, the SAS data analysis system has an operation akin to a join which gives you an explicit variable saying whether a corresponding row in the joined table was found. In the result of the join, rows where no row was found get zero data values.

Whether you consider that "good" or not is a matter of taste - personally I quite like it because it is very explicit and non-magical and I think NULL semantics cause more problems than they solve.

Re:Handling Null Values in Postgres by ainar-g on Thursday March 05, 2020 @01:51PM (#22496705)
Sum types?
Re:Handling Null Values in Postgres by pezezin on Friday March 06, 2020 @03:40AM (#22501951)
Sum types is something that I really miss when working with SQL, they would make the data model much more powerful and consistent.
Re:Handling Null Values in Postgres by patrec on Thursday March 05, 2020 @03:09PM (#22497602)
Why would you need outer joins any more than nulls?
Re:Handling Null Values in Postgres by pavel_lishin on Thursday March 05, 2020 @04:56PM (#22498778)
Show me all schools in the system, and their associated teachers, including schools which have no teachers.
Re:Handling Null Values in Postgres by patrec on Thursday March 05, 2020 @06:22PM (#22499482)
Easy:

   select school, (select array_agg(teacher) from teachers where teachers.school = schools.school) from schools;
If you don't like nulls any more than outer joins, you'd probably want:

   select school, (select coalesce(array_agg(teacher), '{}'::text[]) from teachers where teachers.school = schools.school) from schools;
Re:Handling Null Values in Postgres by derefr on Thursday March 05, 2020 @09:11PM (#22500449)
You realize that joins are a part of the formal definition of relational algebra, while subqueries aren't, right? SQL has had outer joins since the beginning, but subqueries came only gradually, and more limited SQL engines tend still to not support them.
Re:Handling Null Values in Postgres by patrec on Friday March 06, 2020 @03:06AM (#22501799)
Why would SQL (fundementaly) need outer joins? "Because it has them", is not really a compelling answer in this context.

A "pure" relational model (like the one C.J. Date has been going on about for decades) has no outer join.

Re:Handling Null Values in Postgres by patrec on Thursday March 05, 2020 @06:37PM (#22499604)
Since I'm getting downvoted, I find nulls way more useful than outer joins, if you disagree I'd be interested in examples.
Re:Handling Null Values in Postgres by Tostino on Friday March 06, 2020 @12:23AM (#22501183)
There are plenty of cases where the data model requires left joins (or some equivalent syntax using subqueries) to answer certain queries, and in any complex long lived application you will have queries you need you didn't anticipate when you originally modeled your data.
Re:Handling Null Values in Postgres by patrec on Friday March 06, 2020 @08:20AM (#22503301)
Examples to base this discussion on would help.

I'm not a hardcore DB person, but I've designed and modified schemas and queries for quite a few DBs over the years and not once can I recall feeling the need to do an outer join for production code, nor do I recall seeing a hardcore DB person using them (but that might just reflect limited exposure on my part).

On the other hand, I do see LEFT OUTER JOINs a lot in code written by inexperienced people.

Basically, the main use case I have for "dynamically creating" NULLs is quick inspection of data by shoe-horning something with more dimensions into the rows with equal numbers of columns model (CUBE and also OUTER JOIN) – but this is essentially just for display purposes.

From a data modelling perspective outer joins seem quite dodgy to me and an aggregating subquery (such as in the teachers example) is not really conceptually equivalent, even if you can implement them with left joins, and in some DBs such as MySQL they have been traditionally faster. An aggregating subquery is a perfectly conceptually "clean" operation, no ternary logic involved.