BigQuery: convert epoch to TIMESTAMP

RoyalTS picture RoyalTS · Jun 16, 2016 · Viewed 29k times · Source

I'm trying to range-join two tables, like so

SELECT *
FROM main_table h
INNER JOIN
    test.delay_pairs d
ON
    d.interval_start_time_utc < h.visitStartTime
    AND h.visitStartTime < d.interval_end_time_utc

where h.visitStartTime is an INT64 epoch and d.interval_start_time_utc and d.interval_end_time_utc are proper TIMESTAMPs.

The above fails with

No matching signature for operator < for argument types: TIMESTAMP, INT64. Supported signature: ANY < ANY

Neither wrapping h.visitStartTime in TIMESTAMP() nor CAST(d.interval_start_time_utc AS INT64) work. How do I make the two comparable in BigQuery's Standard SQL dialect?

Answer

Mikhail Berlyant picture Mikhail Berlyant · Jun 16, 2016

You can use timestamp conversion functions like TIMESTAMP_SECONDS, TIMESTAMP_MILLIS, TIMESTAMP_MICROS

for example, assuming your h.visitStartTime is microseconds since the unix epoch

SELECT *
FROM main_table h
INNER JOIN test.delay_pairs d
ON d.interval_start_time_utc < TIMESTAMP_MICROS(h.visitStartTime)
AND TIMESTAMP_MICROS(h.visitStartTime) < d.interval_end_time_utc