Jun 01, 2021

ClickHouse tips #8: Generating time-series on ClickHouse

ClickHouse doesn't have a generate_series function yet, but you can achieve the same with other functions. Learn how here.
Xoel López
Founder at TheirStack

You may be wondering if generate_series exists on ClickHouse like it does on Postgres. The bad news is that it doesn’t (yet); the good news is this post will teach you how to do on ClickHouse everything you can do on Postgres with generate_series, and more.

First, let’s see how the numbers function works on ClickHouse. It generates integers between a minimum value (0 by default) and a maximum value that you pass to it:

A similar result can be obtained with the range function, that returns arrays. If we only provide an argument, it behaves like numbers. And with range we can also specify a start, end and step:

This, combined with arrayJoin lets us do the same as generate_series:

Generating timestamp ranges on Postgres

The generate_series on Postgres can produce results with other types different than integers, while ClickHouse’s range and numbers only output integers. But, with some smart logic, we can achieve the same results. For example, on Postgres you’d generate a time-series with a DateTime value for each hour in a day this way, as in described here:

Generate a DateTime time series on ClickHouse, specifying the start and end DateTimes, and the step

On ClickHouse, you can achieve the same this way:

Generate a DateTime time series specifying the start date and the number of intervals

Another way of doing the same thing:

More functions like addHours are available: dateAdd, timestampAdd and addYears, addMonths, addWeeks, addDays, addHours, addMinutes, addSeconds, addQuarters.

Generate a time series using timeSlots

Using the timeSlots function, we can specify the start (DateTime), duration (seconds) and step (seconds) and it generates an array of DateTime values.

Generate a Date time series specifying the start and end date and the step

If you want to generate Date instead of DateTime objects, you’d change the toDateTime function by toDate from the first snippet. This is how you’d generate all the dates of January:

Do you like this post?

Related posts

ClickHouse tips #5: Adding and subtracting intervals
ClickHouse tips #9: Filling gaps in time-series on ClickHouse
Coming soon on ClickHouse: Window functions
Clickhouse Tips #1: Calculating Aggregations After a Given Date
ClickHouse tips #3: the transform function
Roll up data with Materialized Views
Simple Time Series Prediction Modeling Using Tinybird
The cron job that will speed up your Postgres queries 100x
Want a managed ClickHouse®️? Here are some options
Want a managed ClickHouse®️? Here are some options

Build fast data products, faster.

Try Tinybird and bring your data sources together and enable engineers to build with data in minutes. No credit card required, free to get started.
Need more? Contact sales for Enterprise support.