Aggregation function lastChangeAt by KirIgor · Pull Request #103458 · ClickHouse/ClickHouse · GitHub
Skip to content

Aggregation function lastChangeAt#103458

Open
KirIgor wants to merge 1 commit intoClickHouse:masterfrom
KirIgor:last-change-at
Open

Aggregation function lastChangeAt#103458
KirIgor wants to merge 1 commit intoClickHouse:masterfrom
KirIgor:last-change-at

Conversation

@KirIgor
Copy link
Copy Markdown

@KirIgor KirIgor commented Apr 23, 2026

Changelog category: New Feature

Changelog entry:

Added aggregate function lastChangeAt(value, calculated_at) that returns the time of the most recent actual change in value across a time-ordered sequence. Supports numeric, Decimal, Date, DateTime, DateTime64, and BitmapData value types. Designed for use with AggregatingMergeTree to maintain a changed_at column without storing full history.

Documentation entry:

Motivation:

In AggregatingMergeTree tables it is sometimes necessary to track when a metric last changed its value. Storing the full history is expensive, and without lastChangeAt there is no way to answer "when did this value last change?" across incremental merges while keeping only a fixed-size state.

Parameters:

  • value — the metric to observe. Supported types: (U)Int*, Float*, Decimal*, Date, DateTime, DateTime64, AggregateFunction(groupBitmap, UInt*). For bitmaps, equality is tested via bitmapXorCardinality.

  • calculated_at — timestamp of each observation (rows must be ordered by this column). Supported types: (U)Int*, Float*, Date, DateTime, DateTime64.

    Example:

    -- Value changes from 0 to 1 at ts=3, then stays constant. Returns 3, not 5.
    SELECT lastChangeAt(value, ts) FROM (SELECT number AS ts, [0, 0, 1, 1, 1][number] AS value FROM numbers(1, 5));

Returns the `calculated_at` timestamp of the most recent actual change
in `value` across a time-ordered sequence.  A change is counted only
when the value genuinely differs between consecutive observations.
If the value never changes, the first `calculated_at` is returned.

Supported value types: `(U)Int*`, `Float*`, `Decimal*`, `Date`,
`DateTime`, `DateTime64`, and `AggregateFunction(groupBitmap, UInt*)`.
Supported timestamp types: `(U)Int*`, `Float*`, `Date`, `DateTime`,
`DateTime64`.

Designed for `AggregatingMergeTree` to maintain a `changed_at` column
without storing full history.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 23, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants