ClickhouseGil RaphaelliTutorial
ClickHouse forecasting and anomaly detection with time series foundation models
Summary
ClickHouse stores time-series data such as logs, infrastructure metrics, and sales, but training a separate classical model like ARIMA for every series becomes difficult as the number of series grows. Time-series foundation models (TSFMs) promise zero-shot forecasts from a new series’ history without per-series training, and the post connects TimesFM 3.0 to ClickHouse through executable user-defined functions (UDFs) that send numerical arrays to a model service over HTTP. SQL handles aggregation, time bucketing, and filtering to construct each series before passing it to the UDF, and forecasts return arrays of named tuples with median, q10, and q90 bounds. Anomaly detection scores observations against the model’s expected value, and the same SQL-to-model approach could connect ClickHouse to tabular foundation models for predictions on structured rows. The demo uses the M5 retail sales dataset to forecast daily demand and identify unusual historical observations.
Context
ClickHouse stores time-series data such as logs, infrastructure metrics, sales, and customer activity. Forecasting and anomaly detection are common tasks, but training a separate classical model like ARIMA for every series becomes difficult as the number of series grows. Time-series foundation models (TSFMs) promise zero-shot forecasts from a new series’ history without per-series training, making them a natural fit for ClickHouse’s real-time analytics and data warehousing workloads.
Approach / What changed
The post connects TimesFM 3.0 to ClickHouse through executable user-defined functions (UDFs). A Python UDF receives a numerical array from SQL, sends it to a model service over HTTP, and converts the response into ClickHouse arrays and tuples. The service exposes two endpoints: /v1/timeseries/forecast and /v1/timeseries/anomalies. SQL handles aggregation, time bucketing, and filtering to construct each series before passing it to the UDF.
Takeaways
Time-series foundation models (TSFMs) can produce zero-shot forecasts from a new series’ history without per-series training, making them suitable for ClickHouse’s real-time analytics workloads.
The aiForecast function returns an array of named tuples with median, q10, and q90 bounds; aiForecastQuantile and aiForecastInterval select values from the returned tuple without invoking the model again.
The model service is an external dependency on the query path; context length, series count, inference capacity, and request timeouts need to be bounded for production use.
FAQ
How do the SQL functions receive time series data?
The functions operate on complete series as Array(Float64). Inputs can be SQL expressions built from CTEs, subqueries, joins, or aggregated query results. For example, a query can filter and aggregate events into daily totals, collect those totals into a time-ordered array, and pass it directly to aiForecast.
What does the forecast output contain?
Forecasts return an array of named tuples (expected, lower_bound, upper_bound, quantiles), one per future step. expected is the median; lower and upper bounds are q10 and q90, forming a nominal 80% prediction interval. quantiles contains all nine native TimesFM quantiles, q10 through q90, as (level, value) pairs.
How does the model know the time interval of the input data?
The model receives ordered numbers, not timestamps or table names. SQL determines the aggregation and sampling interval, handles missing periods, and orders each series. A horizon of 28 means 28 days for daily input, or 28 hours for hourly input. Neither the UDF nor the model infers the time grid from the source table.
Source and freshness
- Source
- clickhouse.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.