Skip to content

Commit 2e1af2d

Browse files
committed
docs(opencode): say what the time_created sample actually proves
Follow-up to a CodeRabbit review of 7ee95ee. The comments claimed more than the code establishes: sampling eight values cannot show that the whole column is millisecond-scaled, and matching scales say nothing about matching values. Rename `time_created_is_millis` to `time_created_looks_like_millis` and state the limits in place of the guarantee, including that the margin absorbs drift only up to a day. No behavior change.
1 parent 7ee95ee commit 2e1af2d

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

  • rust/crates/ccusage/src/adapter/opencode

‎rust/crates/ccusage/src/adapter/opencode/loader.rs‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ fn load_entries_from_database(
201201
);
202202
return Vec::new();
203203
};
204-
// Only push the window into SQL once the column is known to hold the same
205-
// millisecond scale as the payload; the payload check in the loop stays
206-
// authoritative either way.
207-
let pushdown = if window.is_unbounded() || time_created_is_millis(&connection) {
204+
// Push the window into SQL only while a sample of `time_created` still looks
205+
// millisecond-scaled. The sample cannot prove the whole column is, which is
206+
// why the payload check in the loop stays authoritative either way.
207+
let pushdown = if window.is_unbounded() || time_created_looks_like_millis(&connection) {
208208
window.widened_for_pushdown()
209209
} else {
210210
debug_log(
@@ -372,9 +372,11 @@ impl DateWindow {
372372
/// SQL filters on `message.time_created` while the report filters on the
373373
/// payload's `time.created`. They hold the same value on every OpenCode
374374
/// build checked here, but the column is only a proxy for the payload, so
375-
/// the pushed-down window is kept loose: a column that drifts from its
376-
/// payload then costs a few extra rows to scan instead of excluding a row
377-
/// the report wanted. The exact window is still applied per row below.
375+
/// the pushed-down window is kept loose: a column drifting from its payload
376+
/// by up to a day costs a few extra rows to scan instead of excluding a row
377+
/// the report wanted. Drift past a day is still excluded — ruling that out
378+
/// would take the full-column scan this push-down exists to avoid. The exact
379+
/// window is applied per row either way.
378380
fn widened_for_pushdown(self) -> Self {
379381
Self {
380382
start: self.start.map(|start| start - crate::MILLIS_PER_DAY),
@@ -428,14 +430,16 @@ fn prepare_message_query(
428430
/// needs at least 12 digits, while second-scale values stay far below it.
429431
const MIN_MILLIS_SCALE: i64 = 100_000_000_000;
430432

431-
/// Reports whether `message.time_created` holds Unix milliseconds like the
432-
/// payload's `time.created` does.
433+
/// Reports whether a sample of `message.time_created` looks like Unix
434+
/// milliseconds, the scale the payload's `time.created` uses.
433435
///
434-
/// Sampling a few rows stays cheap on databases tens of gigabytes in size. A
435-
/// build that stored seconds, or left the column at zero, would otherwise have
436-
/// every row excluded by millisecond bounds, so an unrecognized scale disables
437-
/// the push-down and leaves the payload check to filter.
438-
fn time_created_is_millis(connection: &sqlite::Connection) -> bool {
436+
/// Sampling keeps this cheap on databases tens of gigabytes in size, and proves
437+
/// nothing about the rows it did not read: a column with mixed scales can still
438+
/// slip through, and matching scales say nothing about matching values. What it
439+
/// does catch is a build that stored seconds, or left the column at zero, where
440+
/// millisecond bounds would otherwise exclude every row — those disable the
441+
/// push-down and leave the payload check to filter.
442+
fn time_created_looks_like_millis(connection: &sqlite::Connection) -> bool {
439443
let Ok(mut statement) = connection
440444
.prepare("SELECT max(time_created) FROM (SELECT time_created FROM message LIMIT 8)")
441445
else {

0 commit comments

Comments
 (0)