Skip to content

CROSS JOIN to INNER JOIN rewrite depends on tables order in query. #9194

@qoega

Description

@qoega

How to reproduce

set enable_debug_queries = 1;

create table o (orderkey UInt32, custkey UInt32) engine = Memory;
create table l (orderkey UInt32) engine = Memory;
create table c (custkey UInt32) engine = Memory;

ANALYZE select * from l, o, c
where c.custkey = o.custkey and l.orderkey = o.orderkey;

ANALYZE select * from l, c, o
where c.custkey = o.custkey and l.orderkey = o.orderkey;

Second query is interpreted as CROSS JOIN

SELECT 
    `--l.orderkey` AS `l.orderkey`, 
    `--o.orderkey` AS `o.orderkey`, 
    `--o.custkey` AS `o.custkey`, 
    custkey AS `c.custkey`
FROM 
(
    SELECT 
        orderkey AS `--l.orderkey`, 
        o.orderkey AS `--o.orderkey`, 
        custkey AS `--o.custkey`
    FROM l
    ALL INNER JOIN 
    (
        SELECT *
        FROM o
    ) AS o ON `--l.orderkey` = `--o.orderkey`
    WHERE `--l.orderkey` = `--o.orderkey`
)
ALL INNER JOIN 
(
    SELECT *
    FROM c
) AS c ON custkey = `--o.custkey`
WHERE (custkey = `--o.custkey`) AND (`--l.orderkey` = `--o.orderkey`)
SELECT 
    `--l.orderkey` AS `l.orderkey`, 
    `--c.custkey` AS `c.custkey`, 
    orderkey AS `o.orderkey`, 
    custkey AS `o.custkey`
FROM 
(
    SELECT 
        orderkey AS `--l.orderkey`, 
        custkey AS `--c.custkey`
    FROM l
    CROSS JOIN c
)
ALL INNER JOIN 
(
    SELECT *
    FROM o
) AS o ON (`--c.custkey` = custkey) AND (`--l.orderkey` = orderkey)
WHERE (`--c.custkey` = custkey) AND (`--l.orderkey` = orderkey)

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions