SQLite User Forum

USING Clause on Full Outer Join Gives Nulls?
Login

USING Clause on Full Outer Join Gives Nulls?

(1) By SeverKetor on 2022-04-14 03:05:47 [link] [source]

I just updated to the latest trunk check-in to try out full outer join on a query I had that used the workaround with two left join selects union'd together, and got properly bamboozled by the results I was getting for a bit.

If you use USING (Column) then Column seems to be whatever the left-hand side table's value of Column is, even if that's null and the right-hand side's value isn't. This is easily worked around by just using t1.Column and t2.Column, but it feels like the sort of thing that's unintentional. I tested Postgres and that did not exhibit this behaviour.

I suppose my question is: is this on purpose or just a result of full joins being extremely new and still in progress?

Example:

    CREATE TABLE t1 (ID INTEGER);
    CREATE TABLE t2 (ID INTEGER);
    INSERT INTO t1 VALUES (1);
    INSERT INTO t2 VALUES (2);
    SELECT * FROM t1 FULL JOIN t2 USING (ID);

    SQLite:
    ┌──────┐
    │  ID  │
    ├──────┤
    │ 1    │
    │ NULL │
    └──────┘

    Postgres:
     id
    ----
      1
      2

(2) By Harald Hanche-Olsen (hanche) on 2022-04-14 06:00:53 in reply to 1 [link] [source]

Looks to me like they haven't got around to dealing properly with the shared column in full joins yet. It's a bit easier to see what is going on with an extra column to mark the data:

CREATE TABLE t1 (ID INTEGER,t1);
    CREATE TABLE t2 (ID INTEGER,t2);
    INSERT INTO t1 VALUES (1,'x');
    INSERT INTO t2 VALUES (2,'x');
    SELECT * FROM t1 FULL JOIN t2 USING (ID);

with the result

┌────┬────┬────┐
│ ID │ t1 │ t2 │
├────┼────┼────┤
│ 1  │ x  │    │
│    │    │ x  │
└────┴────┴────┘

Most likely, that's the old code for left joins still in use for the shared column, since left joins used to be the only outer join available.

(3) By ddevienne on 2022-04-14 07:22:24 in reply to 2 [link] [source]

FULL is even not documented as valid JOIN syntax in fact.

Yet you didn't get an error, so is that a doc oversight?

Or someone FULL is silently ignored somehow? But then why?
FULL is documented as being a keyword though. Maybe that's why...

(4) By Larry Brasfield (larrybr) on 2022-04-14 09:20:43 in reply to 3 [source]

The published documentation reflects the latest release, not features in-work for the next release. So this discrepancy, between the published docs and the behavior of trunk tip prior to a release tag, is not an oversight.

The FULL JOIN feature should be considered a work-in-progress. Reports such as SK's are welcome. That's why the recent snapshot was dropped.

(5) By ddevienne on 2022-04-14 10:43:14 in reply to 4 [link] [source]

Sure. I had missed the fact this was on unreleased trunk, so my bad.
Then consider it just a reminder to update the railroad diagrams when this lands.

(6) By Richard Hipp (drh) on 2022-04-14 12:41:16 in reply to 1 [link] [source]

Thank you for the bug report, and for trying out the new feature prior to release!

I have clearly messed up the handling of USING (and by extension NATURAL) for RIGHT and LEFT JOIN. I'll work on the problem and post a follow-up if and when I have a solution.

(7) By Richard Hipp (drh) on 2022-04-14 19:57:26 in reply to 6 [link] [source]

I have independently found other problems with RIGHT/FULL JOIN over and above the USING problem described here. For that reason, the enhancement has been moved back onto its branch. Check-in fa9d206f904280e3 which was originally a merge from the "right-join" feature branch into trunk, has been converted into a merge of trunk into the right-join feature branch (a change which one can accomplish in Fossil simply by adding a tag to the check-in in question - no refactoring or history rewriting required).

I will continue to work the problems and report back if and when the new RIGHT/FULL JOIN feature seems to be working better.

(8) By Keith Medcalf (kmedcalf) on 2022-04-15 00:13:46 in reply to 7 [link] [source]

While it is nice that it is easy for you to do that, how does one make a "clone" repository consistent again?

Do I have to delete my private branch and start all over again?

Delete the entire clone repository and start al over again from scratch?

How do I get your "rolled back" changes to "roll back" out of private branches that they have been merged into?

(9) By Warren Young (wyoung) on 2022-04-15 00:34:23 in reply to 8 [link] [source]

How do I get your "rolled back" changes to "roll back" out of private branches that they have been merged into?

$ fossil merge --backout ABCD1234

where ABCD1234 is the commit you want backed out. You can give this multiple times to back out multiple commits.

(10) By Richard Hipp (drh) on 2022-04-15 01:00:20 in reply to 8 [link] [source]

Just "fossil sync" and all will be well. What problem did you observe?

(11) By Keith Medcalf (kmedcalf) on 2022-04-15 01:12:22 in reply to 10 [link] [source]

THe only way I managed to get the changes out of my private branch was to:
1 - get rid of the checkout and wipe the subdirectories completely.
2 - SHUN all the merges and commits since your change
3 - rebuild the entire repository
4 - unshun the artifacts
5 - checkout my private branch (which appears to be OK)
6 - re-merge from trunk

I am now inspecting that all the changes have actually managed to make it. If not, I will simply start all over again with a new repository clone.

And no, sync did nothing. My private branch then simply contained a merge from right-join that would not go away.

(12) By Richard Hipp (drh) on 2022-04-21 19:44:55 in reply to 1 [link] [source]

RIGHT/FULL JOIN has relanded on trunk. A new snapshot tarball is available at https://sqlite.org/download.html.

Please try once again to break it. Report any problems you find on this thread, or on a new thread, or directly to me at drh at sqlite dot org.

(13) By SeverKetor on 2022-04-21 21:20:06 in reply to 12 [link] [source]

Well, seems to be working just fine for me now. Thanks for the fix (and the new feature)!