fix(body): preserve timestamp range type

Merged
AS
4mo ago
+180-6

Summary

  • filter body-measurement ranges while recorded_at is still a native
    ClickHouse DateTime64
  • serialize timestamps only after the inclusive local-date predicate runs
  • add a real ClickHouse regression covering measurements on opposite sides of
    a Los Angeles midnight boundary

Root cause

BodyRepository.listRange() projected toString(recorded_at) AS recorded_at
in the same query scope where WHERE passed recorded_at to toTimeZone.
ClickHouse rebound that identifier to the projected String alias, producing
Illegal type String of argument of function toTimezone.

Validation

  • pnpm vitest run --project unit packages/server/src/repositories/body-repository.test.ts
    (16 passed)
  • root, server, and web pnpm tsc --noEmit passed
  • focused Biome and git diff --check passed
  • full lint passed through every repository policy check; SQLFluff compiled the
    dbt project but then stalled under the same host I/O pressure
  • local real-engine execution was blocked in isolated ClickHouse bootstrap
    (beforeAll timed out at 120 seconds); hosted integration is the required
    real-engine red/green gate before this PR leaves draft

Fixes #2294

Summary by Sourcery

Fix body metrics range queries to filter on native ClickHouse timestamps before string serialization, preventing timezone alias type errors and ensuring correct local-date ranges.

Bug Fixes:

  • Prevent ClickHouse timezone conversion errors in body measurement range queries by avoiding reuse of a stringified timestamp alias in filtering predicates.

Enhancements:

  • Align the body range query structure with existing patterns by introducing an inner subquery that filters raw timestamps and an outer projection that handles serialization.

Documentation:

  • Add a TDD plan document describing the timezone-filter behavior, test strategy, and implementation steps for body metrics range queries.

Tests:

  • Extend unit tests to assert the updated body range query shape and add an integration test that verifies exact local-date range filtering against a real ClickHouse database.

Summary by cubic

Fix body metrics range filtering by applying the date filter to native ClickHouse DateTime64 values and only stringifying after, preventing timezone alias type errors and returning correct inclusive local-date results. Fixes #2294.

  • Bug Fixes

    • Reworked BodyRepository.listRange() to filter in an inner subquery on native recorded_at, then serialize in the outer projection; orders by the inner recorded_at to avoid alias rebound errors.
  • Tests

    • Added a real ClickHouse regression around a Los Angeles midnight boundary; rebuilds analytics.v_body_measurement before asserting exact local-date filtering.
    • Updated the unit test to assert the new query shape in packages/server/src/repositories/body-repository.test.ts.
    • Added a TDD plan in docs/superpowers/plans/2026-07-29-body-metrics-timezone-filter.md.