Skip to content

Honor Nullable types in LEFT JOIN #346

Closed

Description

@mikageymir

I want LEFT JOIN to honor Nullable types. In the current
implementation, a DEFAULT value is returned when there is no
corresponding record, but I want value2 to be NULL in the following
example:

DROP TABLE test1;
DROP TABLE test2;

CREATE TABLE test1 (day Date, key String, value1 Nullable(UInt32)) ENGINE=MergeTree(day, (key), 8192);
CREATE TABLE test2 (day Date, key String, value2 Nullable(UInt32)) ENGINE=MergeTree(day, (key), 8192);
INSERT INTO test1 VALUES ('0000-00-00', '1', NULL);

SELECT day, key, value1, value2 FROM test1 ANY LEFT JOIN test2 USING (key) FORMAT CSVWithNames;

:) SELECT day, key, value1, value2 FROM test1 ANY LEFT JOIN test2 USING (key) FORMAT CSVWithNames;

SELECT
    day,
    key,
    value1,
    value2
FROM test1
ANY LEFT JOIN test2 USING (key)
FORMAT CSVWithNames

"day","key","value1","value2"
"0000-00-00","1",NULL,0

1 rows in set. Elapsed: 0.004 sec.

Activity

  1. ludv1x commented on Jan 18, 2017

    @ludv1x
    Contributor

    Thank you for you note.
    NULL functionality is unstable and not finalized yet (especially for JOIN clauses).
    Finalized functionality will include proper JOIN implementation.

  2. mxzwrnz commented on May 22, 2017

    @mxzwrnz

    Is there any news on that? Thanks a lot!

  3. alexey-milovidov commented on May 22, 2017

    @alexey-milovidov
    Member

    It is implemented.

    Behaviour is controlled by join_use_nulls setting.
    If set to 1, NULLs will be substituted instead of default values.

    You could adjust this setting like any other - per query, per session, per user...

  4. ramazanpolat commented on Nov 27, 2019

    @ramazanpolat
    Contributor

    Behaviour is controlled by join_use_nulls setting.
    If set to 1, NULLs will be substituted instead of default values.

    If the RIGHT table have Array(String) column, it is substituted with [], even if join_use_nulls is set to 1.

    Is this expected behaviour?

    SELECT *
    FROM
    (
        SELECT *
        FROM
        (
            SELECT
                [1, 2, 3, 4, 5] AS TCKN,
                ['ali', 'veli', 'celil', 'delil', 'halil'] AS AD
        )
        ARRAY JOIN
            TCKN,
            AD
    ) AS K
    LEFT JOIN
    (
        SELECT *
        FROM
        (
            SELECT
                *,
                rand() % 3 AS SAYI,
                ['X', 'Y'] AS X
            FROM
            (
                SELECT
                    [2, 3, 4, 5] AS TCKN,
                    ['ali', 'veli', 'celil', 'delil'] AS ADLAR
            )
            ARRAY JOIN
                TCKN,
                ADLAR
        )
    ) AS N ON K.TCKN = N.TCKN
    
    ┌─TCKN─┬─AD────┬─N.TCKN─┬─ADLAR─┬─SAYI─┬─X─────────┐
    │    1 │ ali   │   ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ  │ ᴺᵁᴸᴸ │ []        │
    │    2 │ veli  │      2 │ ali   │    0 │ ['X','Y'] │
    │    3 │ celil │      3 │ veli  │    0 │ ['X','Y'] │
    │    4 │ delil │      4 │ celil │    0 │ ['X','Y'] │
    │    5 │ halil │      5 │ delil │    0 │ ['X','Y'] │
    └──────┴───────┴────────┴───────┴──────┴───────────┘
    
    5 rows in set. Elapsed: 0.015 sec.

    As you can see the column X is [] while column AD is ᴺᵁᴸᴸ.

  5. den-crane commented on Nov 27, 2019

    @den-crane
    Contributor

    @ramazanpolat because for Arrays empty array [] is analog for Null. Arrays cannot be nullable itself but can contain nullable types.

  6. crakjie commented on Apr 17, 2020

    @crakjie
    Contributor

    is join_use_nulls affect a function like emptyArrayToSingle ?
    I'm using this to make left arrayJoin() that could be nice to have both LEFT ARRAY JOIN and arrayJoin linked by the same setting.

  7. 4ertus2 commented on Apr 17, 2020

    @4ertus2
    Contributor

    You may try and make an issue if there's something unexpected.

  8. crakjie commented on Apr 20, 2020

    @crakjie
    Contributor

    @4ertus2 I tried and it seems emptyArrayToSingle is not impacted by join_use_nulls. What is the expected behavior ?

  9. 4ertus2 commented on Apr 20, 2020

    @4ertus2
    Contributor

    @4ertus2 I tried and it seems emptyArrayToSingle is not impacted by join_use_nulls. What is the expected behavior ?

    Let's make an issue. It could be something unknown or related to #9253

  10. added
    comp-joins
    on Apr 21, 2020
  11. added
    comp-datatype-wrapper
    on Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-datatype-wrappercomp-joinsfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions