Clickhouse creating view on cluster on non default database fails

89 Views Asked by At

I am trying to create a view on a clickhouse server that is ran in docker based on image clickhouse/clickhouse-server:22.8.13.20 I create a table in a database named X (not default), the connection url and client are connected to the correct database (Go code) it properly creates a table on the clickhouse server and its replica statement -

CREATE TABLE IF NOT EXISTS statuses ON CLUSTER '{cluster}'
                    id Nullable(UUID),
                    team__id UUID,
                    event_id UUID,
                    domain__id Int64,
                    type String,
                    user_name String,
                    status String,
                    comment String,
                    time_utc DateTime
                ) 
                Engine = ReplicatedMergeTree('/clickhouse/tables/statuses', '{replica}')
                PARTITION BY (toYYYYMM(toDate(time_utc)))
                ORDER BY (status, time_utc)

However if when i try to create a view that references this table i get an error that table default.statuses does not exist (it exists at X.statuses and the clickhouse url in the client contains the database name and creates the table at X database) create view statement -

CREATE OR REPLACE VIEW status_view ON CLUSTER '{cluster}' AS SELECT 
        statuses.status as status_status, 
        statuses.comment as status_comment, 
        statuses.event_id as status_event_id, 
        statuses.time_utc as status_time_utc
    FROM 
        statuses 
        JOIN (
        SELECT 
            statuses.event_id, 
            max(statuses.time_utc) AS time 
        FROM 
            statuses 
        GROUP BY 
            statuses.event_id
        ) AS last_status ON (
        (
            (
            statuses.event_id = last_status.event_id
            ) 
            AND (statuses.time_utc = last_status.time)
        )
        );

What could be the problem that the same client successfully creates a table in the correct database on both the server and its replica, however it cannot create a view

The go Client that executes the statement is a struct -

type Client struct {
    conn     *sql.DB
    replicas []string
    mode     string
}

and both views and tables are created in a function that calls c.conn.Exec(stmt) - where statements to create table and view are provided above

1

There are 1 best solutions below

1
Slach On

Maybe, ON CLUSTER just not pass your current database from your connection to distributed DDL worker which executes separately. For 22.8, it doesn't reproduce on https://fiddle.clickhouse.com/9268aba0-36b8-48e4-8c3e-f306635c8433

Or possible, your golang code is wrong and your CREATE VIEW executed with default as default_database with different connection.

I would like suggest always use database.table instead of table to avoid this kind of errors