Skip to content

Cannot rewrite JOINs when alias in right on is the same as select alias #23104

Closed

Description

@Zhile

Reproduced in 20.12, 21.3
How to reproduce?

drop database if exists test_01600;
create database test_01600;

CREATE TABLE test_01600.base
(
`id` UInt64,
`id2` UInt64,
`d` UInt64,
`value` UInt64
)
ENGINE=MergeTree()
PARTITION BY d
ORDER BY (id,id2,d);

CREATE TABLE test_01600.derived1
(
    `id1` UInt64,
    `d1` UInt64,
    `value1` UInt64
)
ENGINE = MergeTree()
PARTITION BY d1
ORDER BY (id1, d1)
;

CREATE TABLE test_01600.derived2
(
    `id2` UInt64,
    `d2` UInt64,
    `value2` UInt64
)
ENGINE = MergeTree()
PARTITION BY d2
ORDER BY (id2, d2)
;

SELECT
    base.id AS `base.id`,
    derived2.id2 AS `derived2.id2`,
    derived2.value2 AS `derived2.value2`,
    derived1.value1 AS `derived1.value1`
FROM test_01600.base AS base
LEFT JOIN test_01600.derived2 AS derived2 ON base.id2 = derived2.id2
LEFT JOIN test_01600.derived1 AS derived1 ON base.id = derived1.id1

Error message:

Code: 48. DB::Exception: Received from localhost:9000. DB::Exception: Cannot rewrite JOINs. Alias 'derived2.id2' used in ON section depends on another alias 'derived2.id2': While processing SELECT base.id AS `base.id`, derived2.id2 AS `derived2.id2`, derived2.value2 AS `derived2.value2`, derived1.value1 AS `derived1.value1` FROM test_01600.base AS base LEFT JOIN test_01600.derived2 AS derived2 ON base.id2 = derived2.id2 LEFT JOIN test_01600.derived1 AS derived1 ON base.id = derived1.id1.

Activity

  1. added
    bug
    on Apr 15, 2021
  2. added
    comp-joins
    on Apr 15, 2021
  3. filimonov commented on Apr 15, 2021

    @filimonov
    Contributor

    20.6 and earlier - works. 20.7 and newer - fails.

  4. dimovnike commented on Nov 24, 2022

    @dimovnike

    one join works, two fail.
    here is another example:

    select
      t1.col1 AS `t1.col1`,  
      t1.col2 AS `t1.col2`,  
      t2.col1 AS `t2.col1`,  
      t2.col2 AS `t2.col2`
    FROM
      (SELECT 1 AS col1, 2 AS col2) AS t1
    FULL OUTER JOIN
      (SELECT 1 AS col1, 3 AS col2) AS t2
    ON t1.col1 == t2.col1;

    works

    ┌─t1.col1─┬─t1.col2─┬─t2.col1─┬─t2.col2─┐
    │       1 │       2 │       1 │       3 │
    └─────────┴─────────┴─────────┴─────────┘
    
    
    SELECT
        t1.col1 AS `t1.col1`,
        t1.col2 AS `t1.col2`,
        t2.col1 AS `t2.col1`,
        t2.col2 AS `t2.col2`,
        t3.col1 AS `t3.col1`,
        t3.col2 AS `t3.col2`
    FROM
        (SELECT 1 AS col1, 2 AS col2) AS t1
    FULL OUTER JOIN
        (SELECT 1 AS col1, 3 AS col2) AS t2 
    ON t1.col1 = t2.col1
    FULL OUTER JOIN
        (SELECT 1 AS col1, 4 AS col2) AS t3 
    ON t2.col1 = t3.col1;

    fails:

    Code: 48. DB::Exception: Received from localhost:9000. DB::Exception: Cannot rewrite JOINs. Alias 't2.col1' used in ON section depends on another alias 't2.col1': While processing SELECT t1.col1 AS `t1.col1`, t1.col2 AS `t1.col2`, t2.col1 AS `t2.col1`, t2.col2 AS `t2.col2`, t3.col1 AS `t3.col1`, t3.col2 AS `t3.col2` FROM (SELECT 1 AS col1, 2 AS col2) AS t1 FULL OUTER JOIN (SELECT 1 AS col1, 3 AS col2) AS t2 ON t1.col1 = t2.col1 FULL OUTER JOIN (SELECT 1 AS col1, 4 AS col2) AS t3 ON t2.col1 = t3.col1. (NOT_IMPLEMENTED)
    
    ┌─version()───┐
    │ 22.6.1.1985 │
    └─────────────┘
    
    
  5. IssaMisherky commented on Jul 2, 2023

    @IssaMisherky

    Remove the aliasing from the main select, then it should work. Or at least like this:

    SELECT
        t1.col1,
        t1.col2 AS `t1.col2`,
        t2.col1,
        t2.col2 AS `t2.col2`,
        t3.col1,
        t3.col2 AS `t3.col2`
    FROM
        (SELECT 1 AS col1, 2 AS col2) AS t1
    FULL OUTER JOIN
        (SELECT 1 AS col1, 3 AS col2) AS t2 
    ON t1.col1 = t2.col1
    FULL OUTER JOIN
        (SELECT 1 AS col1, 4 AS col2) AS t3 
    ON t2.col1 = t3.col1;
    
  6. added a commit that references this issue on Apr 3, 2024
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

    bugcomp-joinsminor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions