Skip to content

PREWHERE column not included in read plan when used inside parameterized view with JOIN #102074

@Raquel10-RevenueCat

Description

@Raquel10-RevenueCat

Company or project name

No response

Describe the unexpected behaviour

Example

-- 1. Create a ReplacingMergeTree table with event_created_at as version column
CREATE OR REPLACE TABLE prewhere_bug_events ON CLUSTER '{cluster}'
(
    id UInt64,
    project_id UInt64,
    event_created_at DateTime64(3),
    value String,
    is_deleted UInt8 DEFAULT 0
) ENGINE = ReplacingMergeTree(event_created_at, is_deleted)
ORDER BY (project_id, id);

-- 2. Create a simple dimension table for the JOIN
CREATE OR REPLACE TABLE prewhere_bug_dim ON CLUSTER '{cluster}'
(
    project_id UInt64,
    name String
) ENGINE = MergeTree()
ORDER BY project_id;

-- 3. Insert test data
INSERT INTO prewhere_bug_events VALUES (1, 100, now(), 'hello', 0);
INSERT INTO prewhere_bug_dim VALUES (100, 'project_a');

-- 4. Direct query: PREWHERE on version column with FINAL works fine
SELECT count()
FROM prewhere_bug_events FINAL
PREWHERE event_created_at <= now();
-- ✅ Works

-- 5. Parameterized view with a JOIN and PREWHERE on version column
CREATE OR REPLACE VIEW prewhere_bug_view ON CLUSTER '{cluster}' AS
SELECT t.*, d.name
FROM (
    SELECT *
    FROM prewhere_bug_events FINAL
    PREWHERE project_id IN ({project_ids:Array(UInt64)})
        AND event_created_at <= toDateTime64({cutoff:String}, 3)
) AS t
ANY LEFT JOIN prewhere_bug_dim AS d
    ON t.project_id = d.project_id;

-- 6. Query the view referencing the PREWHERE column in the SELECT: WORKS
SELECT count(event_created_at)
FROM prewhere_bug_view(project_ids=[100], cutoff='2099-01-01 00:00:00');
-- ✅ Works

-- 7. Query the view WITHOUT referencing the PREWHERE column: FAILS
SELECT count()
FROM prewhere_bug_view(project_ids=[100], cutoff='2099-01-01 00:00:00');
-- ❌ DB::Exception: Not found column event_created_at in block.

Expected Behavior

Both step 6 and step 7 should succeed. PREWHERE column dependencies should always be included in the column read plan, regardless of whether the calling query references them.

Actual Behavior

Step 7 fails with DB::Exception: Not found column event_created_at in block (NOT_FOUND_COLUMN_IN_BLOCK). The column is only included when the calling query explicitly references it (step 6).

Which ClickHouse versions are affected?

25.8.14.17 (official build)

How to reproduce

Explained in the description

Expected behavior

No response

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions