Skip to content

Incorrect field reference in distributed query with distributed_product_mode=local #4756

Closed

Description

@hohaftf

Describe the bug
If setting distributed_product_mode=local, distributed query contains IN condition and
a) fields in subquery are referenced with database name, ClickHouse replaces distributed names with local names only in FROM and JOIN sections, but not in SELECT, WHERE, ORDER BY
b) fields in subquery are referenced with table alias, ClickHouse replaces distributed names and aliases with local names. So aliases are lost.

How to reproduce
ClickHouse version 19.4.0.49 cluster (shards)
HTTP interface
setting distributed_product_mode=local

create table bug_shard1 on cluster mycluster
	(id Int32) 
	engine = MergeTree 
	order by cityHash64(id);

create table bug_distr1 on cluster mycluster
    as bug_shard1
    engine Distributed(
        mycluster,
        `default`,
        bug_shard1,
        cityHash64(id)
    );

create table bug_shard2 on cluster mycluster
	(id Int32) 
	engine = MergeTree 
	order by cityHash64(id);

create table bug_distr2 on cluster mycluster
    as bug_shard2
    engine Distributed(
        mycluster,
        `default`,
        bug_shard2,
        cityHash64(id)
    );

Queries to run that lead to unexpected result
a)

select `default`.bug_distr1.id 
	from `default`.bug_distr1
	where `default`.bug_distr1.id in 
	(
  		select `default`.bug_distr1.id 
  			from `default`.bug_distr1
  				inner join `default`.bug_distr2 on `default`.bug_distr1.id = `default`.bug_distr2.id
			where `default`.bug_distr1.id  > 0
			order by `default`.bug_distr1.id  
	);

b)

select d0.id 
	from bug_distr1 d0
	where d0.id in 
	(
  		select d1.id 
  			from bug_distr1 as d1
  				inner join bug_distr2 as d2 on d1.id = d2.id
			where d1.id  > 0
			order by d1.id  
	);

Expected behavior
a) distributed table names has been replaced with local names in all query sections
b) aliases in FROM and JOIN sections has not been removed

Error message and/or stacktrace
a)

SQL Error [47]: ClickHouse exception, code: 47, host: localhost, port: 8125; Code: 47, e.displayText() = DB::Exception: Unknown identifier: default.bug_distr1.id, context: query: 'SELECT default.bug_distr1.id FROM default.bug_shard1 INNER JOIN default.bug_shard2 ON default.bug_distr1.id = default.bug_distr2.id WHERE default.bug_distr1.id > 0 ORDER BY default.bug_distr1.id ASC' required_names: 'default.bug_distr2.id''default.bug_distr1.id' source_tables: 'default.bug_shard1' 'default.bug_shard2' table_aliases: complex_aliases: masked_columns: array_join_columns: source_columns:

b)

SQL Error [47]: ClickHouse exception, code: 47, host: localhost, port: 8125; Code: 47, e.displayText() = DB::Exception: Unknown identifier: d1.id, context: query: 'SELECT d1.id FROM default.bug_shard1 INNER JOIN default.bug_shard2 ON d1.id = d2.id WHERE d1.id > 0 ORDER BY d1.id ASC' required_names: 'd2.id''d1.id' source_tables: 'default.bug_shard1' 'default.bug_shard2' table_aliases: complex_aliases: masked_columns: array_join_columns: source_columns:

Activity

  1. added
    bug
    on Mar 21, 2019
  2. 4ertus2 commented on Apr 11, 2019

    @4ertus2
    Contributor

    Probably related #4879

  3. 4ertus2 commented on Apr 11, 2019

    @4ertus2
    Contributor

    Partial fix #4986

  4. hohaftf commented on Apr 17, 2019

    @hohaftf
    Author

    I checked fix for #4879 on v 19.5.2.6
    Both queries from this issue work in global and allow modes, and they did not before.
    Both queries fail in local mode.

  5. hohaftf commented on Oct 10, 2019

    @hohaftf
    Author

    UPD.
    Clickhouse v 19.15.3.6
    Query with aliases works in local mode
    Query with db names throws exception

    SQL Error [288]: ClickHouse exception, code: 288, host: localhost, port: 8123; Code: 288, e.displayText() = DB::Exception: Distributed table should have an alias when distributed_product_mode set to local. (version 19.15.3.6 (official build))

  6. added
    comp-distributed
    on Mar 25, 2020
  7. hohaftf commented on Apr 16, 2020

    @hohaftf
    Author

    As I understand this must be fixed in v 20.3.6.40. But case A still does not work.
    query

    select `default`.bug_distr1.id 
        from `default`.bug_distr1 
          inner join `default`.bug_distr2  on `default`.bug_distr1.id = `default`.bug_distr2.id
      where `default`.bug_distr1.id  > 0
      order by `default`.bug_distr1.id
    

    is translated to

    SELECT id 
      FROM default.bug_shard1 
        ALL INNER JOIN default.bug_distr2 ON default.bug_distr1.id = bug_distr2.id
        WHERE id > 0 
        ORDER BY id ASC
    

    The table name in left join field is not converted to default.bug_shard1. The table name in right join field is also confusing. And I got exception

    Missing columns: 'default.bug_distr1.id' while processing query: 'SELECT id FROM default.bug_shard1 ALL INNER JOIN default.bug_distr2 ON default.bug_distr1.id = bug_distr2.id WHERE id > 0 ORDER BY id ASC', required columns: 'id' 'default.bug_distr1.id' 'bug_distr2.id', source columns: 'id', joined columns: 'bug_distr2.id' (version 20.3.6.40 (official build))

  8. 4ertus2 commented on Apr 16, 2020

    @4ertus2
    Contributor

    It's not backported to 20.3 cause of code conflicts and backport complexity. It should be fixed in 20.4. Could you check it on muster build?

    If you have version that passes this test https://github.com/ClickHouse/ClickHouse/blob/master/tests/queries/0_stateless/01104_distributed_one_test.sql but does not pass your test, it's a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcomp-distributed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions