Function: bucketExpr()
function bucketExpr(
columnSql: Sql,
bucket: "year" | "month" | "day" | "week" | "quarter",
timezone: string,
fieldType: "date" | "datetime"
): Sql;Defined in: server/insights/compiler/bucket-sql.ts:39
Truncate a temporal column to a bucket, producing a timestamp (never a
timestamptz) so the result is independent of the session TimeZone. Both
the bucket keyword and the timezone are bind params; the column is a
pre-quoted registry identifier fragment.
The conversion DEPENDS ON THE FIELD'S SEMANTIC TYPE, because the two temporal types need opposite treatment:
datetime(a PGtimestamptz) —<col> AT TIME ZONE <tz>converts the instant to the report timezone's wall clock and yields atimestamp, sodate_trunctruncates in the report timezone. Correct.date(a PGdate) — a date has NO instant and no zone; it is already a wall-clock value.<date> AT TIME ZONE <tz>widens the date to midnighttimestampand then INTERPRETS that midnight intz, producing atimestamptz.date_trunc(<bucket>, <timestamptz>)then truncates in the SESSIONTimeZone(not the report's) and returns atimestamptz, so the bucket label silently depends on server session state and can land on the wrong day/week/month. Adatetherefore gets a plain widening cast and no timezone conversion at all.
Parameters
| Parameter | Type |
|---|---|
columnSql | Sql |
bucket | "year" | "month" | "day" | "week" | "quarter" |
timezone | string |
fieldType | "date" | "datetime" |
Returns
Sql