Skip to content

Unexpected behavior with materialized columns filtering in joins #13274

Closed

Description

@artbeglaryan

Is there a way to make filtering by right table materialized columns on join?

How to reproduce
Latest lts version

ClickHouse client version 20.3.15.133 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 20.3.15 revision 54433.

Queries for reproduce

CREATE TABLE test1 (time DateTime, foo String, dimension_1 String, dt Date MATERIALIZED toDate(time)) ENGINE = MergeTree() PARTITION BY toYYYYMM(dt) ORDER BY (dt, foo);
CREATE TABLE test2 (time DateTime, bar String, dimension_2 String, dt Date MATERIALIZED toDate(time)) ENGINE = MergeTree() PARTITION BY toYYYYMM(dt) ORDER BY (dt, bar);
INSERT INTO test1 VALUES ('2020-01-01 12:00:00', 'fact1', 'test1_val1'), ('2020-01-01 13:00:00', 'fact2', 'test1_val2');
INSERT INTO test2 VALUES ('2020-01-01 12:00:00', 'fact1', 'test2_val2'), ('2019-01-01 12:00:00', 'fact3', 'test2_val2');

Following works

SELECT *
FROM test1
FULL OUTER JOIN test2 ON test1.foo = test2.bar

┌────────────────time─┬─foo───┬─dimension_1─┬──────────test2.time─┬─bar───┬─dimension_2─┐
│ 2020-01-01 12:00:00 │ fact1 │ test1_val1  │ 2020-01-01 12:00:00 │ fact1 │ test2_val2  │
│ 2020-01-01 13:00:00 │ fact2 │ test1_val2  │ 0000-00-00 00:00:00 │       │             │
└─────────────────────┴───────┴─────────────┴─────────────────────┴───────┴─────────────┘
┌────────────────time─┬─foo─┬─dimension_1─┬──────────test2.time─┬─bar───┬─dimension_2─┐
│ 0000-00-00 00:00:00 │     │             │ 2019-01-01 12:00:00 │ fact3 │ test2_val2  │
└─────────────────────┴─────┴─────────────┴─────────────────────┴───────┴─────────────┘

But when I try to filter on test2.dt server throws an exception

SELECT *
FROM test1
FULL OUTER JOIN test2 ON test1.foo = test2.bar
WHERE test2.dt >= '2020-01-01'


Received exception from server (version 20.3.15):
Code: 47. DB::Exception: Received from localhost:9000. DB::Exception: Missing columns: 'test2.dt' while processing query: 'SELECT time, foo, dimension_1, test2.time, bar, dimension_2 FROM test1 ALL FULL OUTER JOIN (SELECT * FROM test2) AS test2 ON foo = bar WHERE test2.dt >= '2020-01-01'', required columns: 'time' 'foo' 'test2.dt' 'test2.time' 'dimension_1' 'bar' 'dimension_2', source columns: 'dt' 'dimension_1' 'foo' 'time', joined columns: 'test2.time' 'bar' 'dimension_2'. 

0 rows in set. Elapsed: 0.003 sec. 

I understand that joins works other way than in traditional SQL and I have option to move where clause and do separate select like following.

SELECT *
FROM test1
FULL OUTER JOIN 
(
    SELECT *
    FROM test2
    WHERE dt >= '2020-01-01'
) AS test2 ON test1.foo = test2.bar

┌────────────────time─┬─foo───┬─dimension_1─┬──────────test2.time─┬─bar───┬─dimension_2─┐
│ 2020-01-01 12:00:00 │ fact1 │ test1_val1  │ 2020-01-01 12:00:00 │ fact1 │ test2_val2  │
│ 2020-01-01 13:00:00 │ fact2 │ test1_val2  │ 0000-00-00 00:00:00 │       │             │
└─────────────────────┴───────┴─────────────┴─────────────────────┴───────┴─────────────┘

But what if main query contains some complicated where clause with condition_from_left OR condition_from_right_materialized, then I have no way to reduce right table to store less in memory and need to do something like

SELECT *
FROM test1
FULL OUTER JOIN 
(
    SELECT *, dt
    FROM test2
) AS test2 ON test1.foo = test2.bar
WHERE test2.dt >= '2020-01-01'

which may load bunch of unnecessary data into RAM

Activity

  1. added
    bug
    on Aug 3, 2020
  2. added
    comp-joins
    on Aug 3, 2020
  3. self-assigned this
    on Aug 11, 2020
  4. removed their assignment
    on Oct 16, 2020
  5. TheGreatAbyss commented on Nov 5, 2020

    @TheGreatAbyss

    Hello

    I've encountered what I think is a related issue to the above. I have two tables table_a and table_b that have materialized columns column_a and column_b respectively. When I try to write a query that joins the tables and uses those columns:

    SELECT column_a, column_b
    FROM table_a a 
    ALL LEFT OUTER JOIN table_b on a.id = b.id
    
    

    I get an error that looks like this:

    DB::Exception: Missing columns: 'b.column_b' while processing query: 'SELECT...

    If I select from table_b and join on table_a I get the same error for column_a

    The work around is to do the following:

    SELECT * 
    FROM 
    	(SELECT column_a FROM table_a ) a
    	ALL LEFT OUTER JOIN
    	(SELECT *, column_b FROM table_b ) b
    	ON a.id = b.id 
    

    Thanks

  6. jirislav commented on Mar 18, 2024

    @jirislav
    Contributor

    Is this issue actually resolved? Here's an example, when the materialized column is reported to be missing from the table even though it is there:

    CREATE DATABASE asof_bug;
    USE asof_bug;
    
    CREATE TABLE quotes
    (
        symbol String,
        timestamp_nanos UInt64,
        time DateTime64(9, 'UTC') MATERIALIZED fromUnixTimestamp64Nano(timestamp_nanos),
        price Decimal(9, 3),
    )
    ENGINE = MergeTree
    ORDER BY (symbol, time);
    
    
    CREATE TABLE trades
    (	
        symbol String,
        timestamp_nanos UInt64,
        time DateTime64(9, 'UTC') MATERIALIZED fromUnixTimestamp64Nano(timestamp_nanos),
        volume UInt32,
    )
    ENGINE = MergeTree
    ORDER BY (symbol, time);
    
    
    -- Now, this yields an error
    SELECT
        t.symbol,
        t.volume,
        t.time AS trade_time,
        q.time AS closest_quote_time,
        q.price AS quote_price,
        t.volume * q.price AS final_price
    FROM trades AS t
    ASOF LEFT JOIN quotes AS q ON (t.symbol = q.symbol) AND (t.time >= q.time);

    It results in DB::Exception: Unknown column: time, there are only columns symbol, price. error.

  7. den-crane commented on Mar 18, 2024

    @den-crane
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcomp-joins

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions