Querying DateTimes in ClickHouse
ClickHouse's datetime functions cover rounding, filtering and duration calculations, demonstrated on about 3 million NYC taxi trips. toStartOfHour and toStartOfFifteenMinutes bucket timestamps, casting to ::Time groups or filters by time of day across all dates, and dateDiff computes trip durations to derive average speed, which falls from about 19 mph at 6am to about 11 mph by 8:30am. countIf with toDayOfWeek splits weekday and weekend trips, and the lag window function computes percentage change between windows. formatDateTime and bar make the output readable.
Related posts
- Working with Time Series Data in ClickHouse
ClickHouse mediumAdept
ClickHouse handles time-series workloads with Date, DateTime and DateTime64 types (up to nanosecond precision, with per-column timezones) plus SQL functions for bucketing by period. toStartOfHour and toStartOfInterval group by fixed or custom intervals, WITH FILL fills empty buckets, dateDiff builds offset rolling windows, and bar() gives quick inline visuals. Gauges can be filled forward with any() plus INTERPOLATE, and histogram() builds adaptive histograms. Examples run against a 1-billion-row Wikipedia pageviews table, where full scans complete in about 0.26 seconds.
- ClickHouse and the MTA Data Challenge
ClickHouse mediumAdept
Loading and cleaning the ~95 million-row NYC MTA subway turnstile dataset (2014–2022) in ClickHouse, using schema inference over S3, a staging table, and an optimized schema with LowCardinality strings, a combined DateTime column, UInt32 counters, sorted line-name arrays and a (station, date_time) ordering key. Because entry/exit counters are cumulative odometer-style readings that reset, count down and overflow, window functions partitioned by turnstile (ca, unit, scp) compute per-period deltas and discard negative or implausible values above a per-hour threshold. The steps are reproducible with clickhouse-local, and the cleaned data is published in the ClickHouse SQL playground.
- What's New in pg_clickhouse - JSONB Support, SQL value functions, Streaming, and more
ClickHouse mediumAdept
pg_clickhouse, the Postgres foreign data wrapper for querying ClickHouse, now pushes down far more operations to ClickHouse. JSONB accessors (->, ->>, jsonb_extract_path) are translated to ClickHouse sub-column syntax. CURRENT_DATE, CURRENT_TIMESTAMP and clock_timestamp() are translated to ClickHouse now()/now64()/nowInBlock64 calls that use the Postgres session time zone and timestamp precision. Array functions now map to their ClickHouse equivalents. Streaming of HTTP driver results in roughly 50MB batches kept container memory under about 100MiB during a full NYC taxi table scan, versus about 600MiB without it.
- 5 ways to parse Dates and DateTimes in ClickHouse
ClickHouse mediumAdept
ClickHouse offers five main routes for turning raw values into Date/DateTime types. `fromUnixTimestamp` and its 64Milli/64Micro variants convert epoch timestamps to `DateTime` or `DateTime64(3/6)`. `YYYYMMDDToDate` and `YYYYMMDDhhmmssToDateTime` handle packed numeric dates, and `parseDateTime` or `parseDateTimeInJodaSyntax` parse known format strings. For mixed or unknown formats, the `parseDateTimeBestEffort` family works, with OrNull and OrZero variants that avoid exceptions on invalid input. Plain `::DateTime` casts can also accept varied formats, including in queries over CSV files, once `cast_string_to_date_time_mode` is set to `best_effort` per query or per session.
- ClickHouse Newsletter August 2022: Airlines are maybe not that bad
ClickHouse mediumAdept
ClickHouse's August 2022 newsletter introduces ClickBench, a benchmark for analytical databases built on a real-world web analytics dataset, and summarizes the 22.7 release: positional arguments on by default, deprecation of the Ordinary engine, new direct and full_sorting_merge join algorithms, a MongoDB table function, additional table and result filters, and HTTP support in clickhouse-go. The query of the month cleans the OnTime flight dataset with a regex on tail numbers, then uses sequenceMatch to count planes whose delays worsen versus recover within a day. Slightly more delayed planes catch up (about 7.26M) than fall further behind (about 6.88M), though average delays have roughly doubled since the 1990s.