Search code examples
sqlaggregate-functionsclickhousematerialized-views

Can't use Date/DateTime as arg in argMinMerge/argMaxMerge?


In one of Afinity webinars they give an example of using argMin/argMax aggregate functions to find the first/last value in some table. They are using the following table:

CREATE TABLE cpu_last_point_idle_agg (
    created_date AggregateFunction(argMax, Date, DateTime),
...
)
Engine = AggregatingMergeTree

Then they create the materialized view:

CREATE MATERIALIZED VIEW cpu_last_point_idle_mw
TO cpu_last_point_idle_agg
AS SELECT
    argMaxState(created_date, created_at) AS created_date,
...

And finally the view:

CREATE VIEW cpu_last_point_idle AS
SELECT
    argMaxMerge(created_date) AS created_date,
...

However, when I try to replicate this approach, I am getting an error.

My table:

CREATE TABLE candles.ESM20_mthly_data (
    ts DateTime Codec(Delta, LZ4),
    open AggregateFunction(argMin, DateTime, Float64),
...
)
Engine = AggregatingMergeTree
PARTITION BY toYYYYMM(ts)
ORDER BY ts
PRIMARY KEY(ts);

My Materialized View:

CREATE MATERIALIZED VIEW candles.ESM20_mthly_mw
TO candles.ESM20_mthly_data
AS SELECT
    ts,
    argMinState(ts, src.price) AS open,
...
FROM source_table as src
GROUP BY toStartOfInterval(src.ts, INTERVAL 1 month) as ts;

My view:

CREATE VIEW candles.ESM20_mthly 
AS SELECT
    ts,
    argMinMerge(ts) as open,
...
FROM candles.ESM20_mthly_mw
GROUP BY ts;

I get an error:

Code: 43. DB::Exception: Received from localhost:9000. DB::Exception: Illegal type DateTime('UTC') of argument for aggregate function with Merge suffix must be AggregateFunction(...). 

I tried using Date and DateTime, with the same result. If I flip the arg and value, it works but of course doesn't give me what I want. Are dates no longer supported by these aggregating functions? How do I make it work?

I am using Connected to ClickHouse server version 20.12.3 revision 54442.


Solution

  • First of all argMin(a, b) -- take a when b is min.

    --AggregateFunction(argMin, DateTime, Float64),
    ++AggregateFunction(argMin, Float64, DateTime),
    
    --argMinState(ts, src.price) AS open,
    ++argMinState(src.price,ts) AS open,
    

    The second issue is

    --argMinMerge(ts) as open,
    ++argMinMerge(open) as final_open,