Skip to content

Distributed Join over tables in different databases with distributed_product_mode=local #8547

Closed

Description

@mrtarsa

I experience troubles in joining data locally at every shard.

Setup

I have cluster with 3 shards

<ha1>
    <shard>
        <weight>1</weight>
        <internal_replication>true</internal_replication>
        <replica>
            <host>localhost</host>
            <port>9000</port>
            <default_database>db0</default_database>
        </replica>
    </shard>
    <shard>
        <weight>1</weight>
        <internal_replication>true</internal_replication>
        <replica>
            <host>localhost</host>
            <port>9001</port>
            <default_database>db0</default_database>
        </replica>
    </shard>
    <shard>
        <weight>1</weight>
        <internal_replication>true</internal_replication>
        <replica>
            <host>localhost</host>
            <port>9002</port>
            <default_database>db0</default_database>
        </replica>
    </shard>
</ha1>

and next tables

CREATE DATABASE db0 ENGINE = Ordinary

CREATE TABLE db0.a1_replicated (
    day Date,
    id UInt32
)
ENGINE = MergeTree() PARTITION BY day ORDER BY id

CREATE TABLE a1 (
    day Date,
    id UInt32
)
ENGINE = Distributed('ha1', 'db0', a1_replicated, id)

CREATE TABLE db0.b1_replicated (
    day Date,
    id UInt32
)
ENGINE = MergeTree() PARTITION BY day ORDER BY id

CREATE TABLE b1 (
    day Date,
    id UInt32
)
ENGINE = Distributed('ha1', 'db0', b1_replicated, id)

filled with data

INSERT INTO a1 (day, id) VALUES
    ('2019-01-01', 9),
    ('2019-01-01', 10), ('2019-01-01', 13),
    ('2019-01-01', 11), ('2019-01-01', 14), ('2019-01-01', 17)

INSERT INTO b1 (day, id) VALUES
    ('2019-01-01', 9),
    ('2019-01-01', 10), ('2019-01-01', 13),
    ('2019-01-01', 11), ('2019-01-01', 14), ('2019-01-01', 17)

Expected behavior

Because of id being sharding key for both tables, I expect the query

SELECT id, count()
FROM a1 AS a1
LEFT JOIN b1 b1 ON a1.id = b1.id
GROUP BY id

to be

  • sent from initiator to all of the shards
  • computed at every shard
  • results from all shards combined at initiator

Actual behavior

I tried different approaches.

Query 1

SELECT id, count()
FROM a1 AS a1
LEFT JOIN b1 AS b1 ON a1.id = b1.id
GROUP BY id

The error is DB::Exception: Table db0.b1 doesn't exist..

Query 2

SELECT id, count()
FROM a1 a1
LEFT JOIN (SELECT id FROM b1 b1) b1 ON a1.id = b1.id
GROUP BY id

The error is DB::Exception: Table default.b1_replicated doesn't exist

Query 3

SELECT id, count()
FROM (SELECT id FROM a1) a1
LEFT JOIN (SELECT id FROM b1) b1 ON a1.id = b1.id
GROUP BY id

Initial query is not sent to all of the shards. Instead, both subqueries are sent to all of the shards and the results of subqueries are then joined at initiator (that may not be efficient).

Questions

  • How to explain different behaviors of Query 1, Query 2 and Query 3?
  • How to JOIN data locally at every shard using this topology and schema?

Reproduce

The version of Clickhouse is latest 19.17.6.

The only non-default setting was <distributed_product_mode>local</distributed_product_mode>

Activity

  1. added
    bug
    on Jan 6, 2020
  2. added
    question
    and removed
    bug
    on Jan 6, 2020
  3. den-crane commented on Jan 6, 2020

    @den-crane
    Contributor

    Your expectations are wrong.
    CH does not create tables at other nodes automatically. All CH nodes are independent and are not aware about cluster. You need to execute all 4 create table at all 3 nodes manually. Or use on cluster helper https://clickhouse.yandex/docs/en/query_language/create/#create-table-query but it requires Zookeeper.

    CH does not trust to a Distributed table sharding key. Because you can insert data to b1_replicated locally or you can change sharding key in Distributed table.

    So you should do a proper join manually.

    options:

      SELECT id, count()
        FROM a1 AS a1
        LEFT JOIN b1_replicated b1 ON a1.id = b1.id
        GROUP BY id
    
    or 
    
    set distributed_product_mode='local';
    
    SELECT id, count()
    FROM a1 AS a1
    LEFT JOIN (select * from b1) b1 ON a1.id = b1.id
    GROUP BY id
    
  4. mrtarsa commented on Jan 6, 2020

    @mrtarsa
    Author

    Yes, I executed CREATE TABLE statements at all 3 nodes. Sorry, forgot to mention that.

    And distributed_product_mode was set to local on config level at every node.

  5. den-crane commented on Jan 6, 2020

    @den-crane
    Contributor

    probably you've missed a database qualifier db0 and created Distributed tables in a default database.

    check desc db0.a1 vs desc default.a1

    CREATE TABLE db0.a1 (
        day Date,
        id UInt32
    )
    ENGINE = Distributed('ha1', 'db0', a1_replicated, id);
    
    CREATE TABLE db0.b1 (
        day Date,
        id UInt32
    )
    ENGINE = Distributed('ha1', 'db0', b1_replicated, id)
    
    
    SELECT
        a1.id,
        count()
    FROM db0.a1 AS a1
    LEFT JOIN db0.b1_replicated AS b1 ON a1.id = b1.id
    GROUP BY a1.id
    
    ┌─id─┬─count()─┐
    │  9 │       1 │
    │ 14 │       1 │
    │ 11 │       1 │
    │ 10 │       1 │
    │ 13 │       1 │
    │ 17 │       1 │
    └────┴─────────┘
    
    set distributed_product_mode='local';
    
    SELECT
        a1.id,
        count()
    FROM db0.a1 AS a1
    LEFT JOIN db0.b1 ON a1.id = b1.id
    GROUP BY a1.id
    
    ┌─id─┬─count()─┐
    │  9 │       1 │
    │ 14 │       1 │
    │ 11 │       1 │
    │ 10 │       1 │
    │ 13 │       1 │
    │ 17 │       1 │
    └────┴─────────┘
    
  6. mrtarsa commented on Jan 7, 2020

    @mrtarsa
    Author

    In my case tables a1 and b1 are Distributed in default database. That was intended and I cannot move the tables into another database.

    I simplified it and just for example explained the problem with single replica for every shard. In such case topology is like

    node0   node1    node2
    s0r0    s1r0     s2r0    <-- db0
    

    where there is only one replica for every shard in database db0.

    In real case topology is cross replicated and may be like

    node0   node1    node2
    s0r0    s1r0     s2r0    <-- db0
    s2r1    s0r1     s1r1    <-- db1
    

    where s2r1 is second replica of third shard.

    Here all replicas r0 are located in database db0. All replicas r1 are located in database db1.

    Consider query SELECT * FROM a1.

    For every shard from the cluster h1, where Distributed table a1 is defined, the replica will be selected. Imagine that selected replicas are s0r0, s1r0 and s2r1, that are located in databases db0, db0 and db1. Since databases may be different

    • I cannot use concrete database in my query
    • database in Distributed definition is empty string

    I expect that default_database from config will be used for every selected replica.

    I tried next queries based on your suggestions

    SELECT id, count()
    FROM a1 AS a1
    LEFT JOIN b1_replicated b1 ON a1.id = b1.id
    GROUP BY id
    

    The error is DB::Exception: Table default.b1_replicated doesn't exist..

    SELECT id, count()
    FROM a1 AS a1
    LEFT JOIN (SELECT * FROM b1 AS b1) AS b1 ON a1.id = b1.id
    GROUP BY id
    

    The error is DB::Exception: Table default.b1_replicated doesn't exist..

    In my case the only way to execute the query with JOIN without error is

    SELECT id, count()
    FROM (SELECT * FROM a1) AS a1
    LEFT JOIN (SELECT * FROM b1) AS b1 ON a1.id = b1.id
    GROUP BY id
    

    But with such approach, instead of sending the whole query to every shard, initiator sends only the subqueries SELECT * FROM a1 and SELECT * FROM b1. Results are then collected from every shard, joined and grouped by at initiator node. This is not efficient, since GROUP BY may reduce the result set significantly. That's why I find a way to perform the whole query at every shard and then just combine the results at initiator. I wonder whether Distributed engine is capable of that, or I should take the cluster topology into account on application level and execute multiple queries in parallel from there.

  7. added
    comp-distributed
    on May 13, 2020
  8. self-assigned this
    on May 13, 2020
  9. 4ertus2 commented on May 13, 2020

    @4ertus2
    Contributor

    Probably we need rethink Distributed logic with different databases.
    Related #10471 #6571

  10. 4ertus2 commented on May 13, 2020

    @4ertus2
    Contributor
    Since databases may be different
    I cannot use concrete database in my query
    

    As workaround you may try to make views in same db that look at original tables and make Distributed table over them. It's not well defined zone when you combine tables from different databases in one query. Something could go wrong.

  11. changed the title [-]Join distributed tables with same sharding key[/-] [+]Distributed Join over tables in different databases with distributed_product_mode=local[/+] on May 13, 2020
  12. 4ertus2 commented on May 13, 2020

    @4ertus2
    Contributor

    Your main problem is most probably cause of #6571

    The error is DB::Exception: Table default.b1_replicated doesn't exist..
    
  13. added
    comp-joins
    on May 13, 2020
  14. deleted a comment from blinkov on Aug 14, 2020
  15. removed their assignment
    on Oct 16, 2020
  16. added a commit that references this issue on Apr 11, 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

    comp-distributedcomp-joinsfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions