Skip to content

JOIN unexpected results #3145

Closed
Closed

Description

@den-crane
create table t(a Int64, b Int64) engine = TinyLog;
insert into t values(1,1);
insert into t values(2,2);
create table s(a Int64, b Int64) engine = TinyLog;
insert into s values(1,1);

Right(expected) result:
select a, b, s_a, s_b from t all left join (select a,b,a s_a, b s_b from s) using (a,b)

a b s_a s_b
1 1 1 1
2 2 0 0

select a,b,s_a,s_b from t all left join (select a, b, a s_a, b s_b from s) s on (s.a = t.a and s.b = t.b)

a b s_a s_b
1 1 1 1
2 2 0 0

Wrong (unexpected) results

select * from t all left join s using (a,b)

a b a b
1 1 1 1
2 2 2 2

select t.*, s.* from t all left join s on (s.a = t.a and s.b = t.b)

a b a b a b a b
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2

select * from t all left join (select a s_a, b s_b from s) on (s_a = t.a and s_b = t.b)

a b s_a s_b
1 1 1 1
2 2 2 2

select a,b,s_a,s_b from t all left join (select a,b, a s_a, b s_b from s) on (s_a = t.a and s_b = t.b)

a b s_a s_b
1 1 1 1
2 2 2 2

Activity

  1. changed the title [-]Left outer join requires to rename columns to get right result.[/-] [+]Left outer join requires to rename columns to get a correct result.[/+] on Sep 16, 2018
  2. changed the title [-]Left outer join requires to rename columns to get a correct result.[/-] [+]Left outer join requires to rename columns to get the correct result.[/+] on Sep 16, 2018
  3. den-crane commented on Sep 16, 2018

    @den-crane
    ContributorAuthor
    create table t(a Int64, b Int64) engine = TinyLog;
    insert into t values(1,1);
    insert into t values(2,2);
    create table z(c Int64, d Int64, e Int64) engine = TinyLog;
    insert into z values(1,1,1);
    

    Wrong result
    select * from t all left join z on (z.c = t.a and z.d = t.b)

    a b c d e
    1 1 1 1 1
    2 2 2 2 0
  4. zhang2014 commented on Sep 17, 2018

    @zhang2014
    Contributor

    Everything except select t.*, s.* from t all left join s on (s.a = t.a and s.b = t.b) is expected.
    You can refer to Backward incompatible changes of 18.12.13 CHANGELOG

  5. den-crane commented on Sep 17, 2018

    @den-crane
    ContributorAuthor

    I did not mean that something wrong with the last version.
    I mean it works and always worked incompatible with other SQL databases.

    psql
    create table t(a int, b int); insert into t values (1,1); insert into t values (2,2);
    create table s(a int, b int);
    insert into s values (1,1);

    
    select * from t left outer join s using (a,b);
     a | b
    ---+---
     1 | 1
     2 | 2
    
    select t.*, s.* from t left outer join s using (a,b);
     a | b | a | b
    ---+---+---+---
     1 | 1 | 1 | 1
     2 | 2 |   |
    
    select * from t left outer join s on t.a=s.a and t.b=s.b;
     a | b | a | b
    ---+---+---+---
     1 | 1 | 1 | 1
     2 | 2 |   |
    

    mysql
    create table t(a int, b int); insert into t values (1,1); insert into t values (2,2);
    create table s(a int, b int); insert into s values (1,1);

    select * from t left outer join s using (a,b);
    +------+------+
    | a    | b    |
    +------+------+
    |    1 |    1 |
    |    2 |    2 |
    +------+------+
    
    select t.*, s.* from t left outer join s using (a,b);
    +------+------+------+------+
    | a    | b    | a    | b    |
    +------+------+------+------+
    |    1 |    1 |    1 |    1 |
    |    2 |    2 | NULL | NULL |
    +------+------+------+------+
    
    select * from t left outer join s on (t.a = s.a and t.b = s.b);
    +------+------+------+------+
    | a    | b    | a    | b    |
    +------+------+------+------+
    |    1 |    1 |    1 |    1 |
    |    2 |    2 | NULL | NULL |
    +------+------+------+------+
    
  6. zhang2014 commented on Sep 17, 2018

    @zhang2014
    Contributor

    OK,Two questions, and I'll try to fix that : )

    • Unexpected : SELECT t.*, s.* FROM t ALL LEFT JOIN s ON (s.a = t.a and s.b = t.b) .
    • Remove duplicate join key when use USING.
  7. den-crane commented on Sep 17, 2018

    @den-crane
    ContributorAuthor

    One more test.
    There is a very useful use-case find all rows in T which are "not-exists" in S using semi-join.

    mysql

    create table t(a int, b int, c varchar(100)); insert into t values (1,1,'a'); insert into t values (2,2,'b');
    create table s(a int, b int, c varchar(100)); insert into s values (1,1,'a');
    
    select t.* from t left outer join s on (t.a = s.a and t.b = s.b) where s.a is null and s.b is null;
    +------+------+------+
    | a    | b    | c    |
    +------+------+------+
    |    2 |    2 | b    |
    +------+------+------+
    
    select t.* from t left outer join s using (a,b) where s.a is null and s.b is null;
    +------+------+------+
    | a    | b    | c    |
    +------+------+------+
    |    2 |    2 | b    |
    +------+------+------+
    

    psql

    select t.* from t left outer join s on (t.a = s.a and t.b = s.b) where s.a is null and s.b is null;
     a | b | c
    ---+---+---
     2 | 2 | b
    
    select t.* from t left outer join s using (a,b) where s.a is null and s.b is null;
     a | b | c
    ---+---+---
     2 | 2 | b
    

    CH:

    create table t(a Int64, b Int64, c String) engine = TinyLog;
    insert into t values(1,1,'a'),(2,2,'b');
    create table s(a Int64, b Int64, c String) engine = TinyLog;
    insert into s values(1,1,'a');
    
    
    select t.* from t all left join s on (s.a = t.a and s.b = t.b) where s.a = 0 and s.b = 0;
    
    Received exception from server (version 18.10.3):
    Code: 47. DB::Exception: Received from localhost:9000, ::1. DB::Exception: Unknown identifier: default.s.b.
    
    
    create table z(d Int64, e Int64, f String) engine = TinyLog; insert into z values(1,1,'a');
    select a,b,c from (select a,b,c,d,e,f from t all left join z on (z.d = t.a and z.e = t.b)) where d=0 and e=0;
    0 rows in set. Elapsed: 0.003 sec.
    
  8. 3 remaining items

  9. 4ertus2 commented on Feb 1, 2019

    @4ertus2
    Contributor

    Am I right that only unsolved issue here is incorrect result with 'join_use_nulls = 1'?

    use test;
    
    drop table if exists t;
    drop table if exists s;
    
    create table t(a Int64, b Int64) engine = TinyLog;
    create table s(a Int64, b Int64) engine = TinyLog;
    
    insert into t values(1,1), (2,2);
    insert into s values(1,1);
    
    set join_use_nulls = 0;
    select * from t all left join s using (a,b) format PrettyCompact;
    select t.*, s.* from t all left join s using (a,b) format PrettyCompact;
    select * from t all left join (select a,b,a s_a, b s_b from s) using (a,b) format PrettyCompact;
    
    set join_use_nulls = 1;
    select * from t all left join s using (a,b) format PrettyCompact;
    select t.*, s.* from t all left join s using (a,b) format PrettyCompact; -- NULLs expected
    select * from t all left join (select a,b,a s_a, b s_b from s) using (a,b) format PrettyCompact;
    
    drop table t;
    drop table s;
    
    ┌─a─┬─b─┐
    │ 1 │ 1 │
    │ 2 │ 2 │
    └───┴───┘
    ┌─a─┬─b─┬─s.a─┬─s.b─┐
    │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │   0 │   0 │
    └───┴───┴─────┴─────┘
    ┌─a─┬─b─┬─s_a─┬─s_b─┐
    │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │   0 │   0 │
    └───┴───┴─────┴─────┘
    ┌─a─┬─b─┐
    │ 1 │ 1 │
    │ 2 │ 2 │
    └───┴───┘
    ┌─a─┬─b─┬─s.a─┬─s.b─┐
    │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │   0 │   0 │
    └───┴───┴─────┴─────┘
    ┌─a─┬─b─┬──s_a─┬──s_b─┐
    │ 1 │ 1 │    1 │    1 │
    │ 2 │ 2 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
    └───┴───┴──────┴──────┘
    

    5th query result is wrong. It should be the same as 6th one.

  10. den-crane commented on Feb 1, 2019

    @den-crane
    ContributorAuthor

    5, 3, 6 have wrong results.

    set join_use_nulls = 1;
    select t.*, s.* from t all left join s using (a,b) format PrettyCompact; -- NULLs expected
    

    current result

    ┌─a─┬─b─┬─s.a─┬─s.b─┐
    │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │   0 │   0 │
    └───┴───┴─────┴─────┘
    

    expected result

    ┌─a─┬─b─┬─s.a─┬─s.b─┐
    │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ│
    └───┴───┴─────┴─────┘
    
  11. den-crane commented on Feb 1, 2019

    @den-crane
    ContributorAuthor

    19.1.6. still wrong with table qualifiers

    select x, z.y, z.x from (select 666 x) t any left join (select 555 x, x as y) z  using(x)
    ┌───x─┬─y─┬─z.x─┐
    │ 666 │ 0 │ 666 │
    └─────┴───┴─────┘
    expected
    ┌───x─┬─y─┬───x─┐
    │ 666 │ 0 │ 0      │
    └─────┴───┴─────┘
    

    select x, z.y, z.x from (select 666 x) t any left join (select 555 x, x as y) z on (t.x = z.x)
    ┌───x─┬─y─┬─z.x─┐
    │ 666 │ 0 │ 666 │
    └─────┴───┴─────┘
    expected
    ┌───x─┬─y─┬─z.x─┐
    │ 666 │ 0 │ 0    │
    └─────┴───┴─────┘
    
  12. den-crane commented on Feb 1, 2019

    @den-crane
    ContributorAuthor

    I think 3d and 6 also wrong and they have 2 issues.

    select * from t all left join (select a,b,a s_a, b s_b from s) using (a,b)
    ┌─a─┬─b─┬─a─┬─b─┬─s_a─┬─s_b─┐
    │ 1 │ 1 │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │ 2 │ 2 │   0 │   0 │
    └───┴───┴───┴───┴─────┴─────┘
    expected
    ┌─a─┬─b─┬─a─┬─b─┬─s_a─┬─s_b─┐
    │ 1 │ 1 │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │ 0 │ 0 │   0 │   0 │
    └───┴───┴───┴───┴─────┴─────┘
    
    
    select * from t all left join (select a,b,a s_a, b s_b from s) using (a,b)
    ┌─a─┬─b─┬─a─┬─b─┬──s_a─┬──s_b─┐
    │ 1 │ 1 │ 1 │ 1 │    1 │    1 │
    │ 2 │ 2 │ 2 │ 2 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
    └───┴───┴───┴───┴──────┴──────┘
    expected
    ┌─a─┬─b─┬─a─┬─b─┬──s_a─┬──s_b─┐
    │ 1 │ 1 │ 1 │ 1 │    1 │    1 │
    │ 2 │ 2 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
    └───┴───┴───┴───┴──────┴──────┘
    

    Actually the problem that * with using should not return columns from right table at all.

    select * from t all left join (select a,b,a s_a, b s_b from s) using (a,b)
    ┌─a─┬─b─┬─a─┬─b─┬─s_a─┬─s_b─┐
    │ 1 │ 1 │ 1 │ 1 │   1 │   1 │
    │ 2 │ 2 │ 2 │ 2 │   0 │   0 │
    └───┴───┴───┴───┴─────┴─────┘
    expected
    ┌─a─┬─b─┐
    │ 1 │ 1  │
    │ 2 │ 2 │
    └───┴──┘
    
  13. den-crane commented on Feb 1, 2019

    @den-crane
    ContributorAuthor

    And let's remove a setting asterisk_left_columns_only
    It's broken in master anyway and has no sense.

  14. den-crane commented on Feb 1, 2019

    @den-crane
    ContributorAuthor

    I did typo and got
    select * from t left outer join s on (t.a=s.a and t.b = s.a);
    DB::Exception: Not found column dw.s.a in block. There are only columns: dw.s.b.

  15. 4ertus2 commented on Feb 5, 2019

    @4ertus2
    Contributor

    Actually the problem that * with using should not return columns from right table at all.
    Current behaviour * with using is return all columns from left and right table except duplicates. It's almost like MySQL workd with using(). So this result (master) is correct: all the columns except duplicates.

    create table t(a Nullable(Int64), b Nullable(Int64), c Nullable(String)) engine = Memory;
    create table s(a Nullable(Int64), b Nullable(Int64), c Nullable(String)) engine = Memory;
    
    insert into t values(1,1,'a'), (2,2,'b');
    insert into s values(1,1,'a');
    
    select * from t all left join (select a,b,a s_a, b s_b from s) using (a,b)
    ┌─a─┬─b─┬─c─┬──s_a─┬──s_b─┐
    │ 1 │ 1 │ a │    1 │    1 │
    │ 2 │ 2 │ b │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
    └───┴───┴───┴──────┴──────┘
    

    Then if I replace * with t., s. I've got expected NULLs in right table (if a and b are nullables)

    select t.*, s.* from t all left join (select a,b,a s_a, b s_b from s) s using (a,b) format PrettyCompact;
    ┌─a─┬─b─┬─c─┬──s.a─┬──s.b─┬──s_a─┬──s_b─┐
    │ 1 │ 1 │ a │    1 │    1 │    1 │    1 │
    │ 2 │ 2 │ b │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
    └───┴───┴───┴──────┴──────┴──────┴──────┘
    

    The same with JOIN ON.

    select t.*, s.* from t all left join (select a,b,a s_a, b s_b from s) s on t.a = s.a and t.b = s.b format PrettyCompact;
    ┌─a─┬─b─┬─c─┬──s.a─┬──s.b─┬──s_a─┬──s_b─┐
    │ 1 │ 1 │ a │    1 │    1 │    1 │    1 │
    │ 2 │ 2 │ b │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
    └───┴───┴───┴──────┴──────┴──────┴──────┘
    

    I did typo and got ... DB::Exception

    It's totally another issue. So the issues are:

    1. If join_use_nulls = 1 we have default values instead of NULLs sometimes. (this issue)
    2. If we duplicate columns in on section they would be lost in block. Not found column for duplicate columns in JOIN ON section #4271

    Are there any other issues here? If so lets make own tickets for them.

  16. changed the title [-]Left outer join requires to rename columns to get the correct result.[/-] [+]JOIN return default values when NULLs are expected (join_use_nulls = 1)[/+] on Feb 5, 2019
  17. changed the title [-]JOIN return default values when NULLs are expected (join_use_nulls = 1)[/-] [+]JOIN unexpected results[/+] on Feb 6, 2019
  18. added
    comp-joins
    on May 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp-joins

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions