Skip to content

Lazy column loading does not work with if and CTEs #79771

@shachibista

Description

@shachibista

Company or project name

No response

Describe the unexpected behaviour

We came across an unexpected behaviour on a query that was working on v24.5 that is not working as expected on v25.3. We belive it is due to the lazy column loading behaviour recently introduced.

How to reproduce

ClickHouse server version: 25.3

Original query:

:) create table a (id Int32) Engine=Memory;
:) create table b (id Int32, email Nullable(String)) Engine=Memory;
:) insert into a values (1), (2), (3);
:) insert into b values (2, 'two@example.com');
:) select a.id, if(b.email is null, false, true) as has_email from a left join b on a.id = b.id;

   ┌─id─┬─has_email─┐
1. │  1 │ true      │
2. │  2 │ true      │
3. │  3 │ true      │
   └────┴───────────┘

Works as expected when explicitly projecting the column inside the if function.

:) select a.id, b.email, if(b.email is null, false, true) as has_email from a left join b on a.id = b.id;

   ┌─id─┬─email───────────┬─has_email─┐
1. │  1 │ ᴺᵁᴸᴸ            │ false     │
2. │  2 │ two@example.com │ true      │
3. │  3 │ ᴺᵁᴸᴸ            │ false     │
   └────┴─────────────────┴───────────┘

Selecting inside CTEs fails to load the column as well:

:) with ds as (select a.id, b.email, if(b.email is null, false, true) as has_email from a left join b on a.id = b.id) select id, has_email from ds;

   ┌─id─┬─has_email─┐
1. │  1 │ true      │
2. │  2 │ true      │
3. │  3 │ true      │
   └────┴───────────┘

:)  with ds as (select a.id, b.email, if(b.email is null, false, true) as has_email from a left join b on a.id = b.id) select id, email, has_email from ds;

   ┌─id─┬─email───────────┬─has_email─┐
1. │  1 │ ᴺᵁᴸᴸ            │ false     │
2. │  2 │ two@example.com │ true      │
3. │  3 │ ᴺᵁᴸᴸ            │ false     │
   └────┴─────────────────┴───────────┘

Expected behavior

Columns inside funtion calls should be loaded. CTEs should be evaluated eagerly. Behaviour should at least show a warning.

Error message and/or stacktrace

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    unexpected behaviourResult is unexpected, but not entirely wrong at the same time.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions