diff --git a/internal/engine/file_snapshot.go b/internal/engine/file_snapshot.go index c985e0207..59c24a597 100644 --- a/internal/engine/file_snapshot.go +++ b/internal/engine/file_snapshot.go @@ -29,6 +29,28 @@ func overlapping(b telemetry.BatchMetadata, signal string, w queryrows.Window) b return count > 0 && (!bounds.Known || bounds.MaxNanos >= w.Start.UnixNano() && bounds.MinNanos < w.End.UnixNano()) } +// signalWindowBounded reports whether a windowed column from a trusted SQL +// panel bounds this signal's own event-time clock, so that pruning files to the +// read window is sound. A bare column must equal the clock; a qualified column +// must name the signal's own relation (alias qualifiers we cannot resolve stay +// unbounded, which binds every file — correct, if slower). Columns come only +// from the validated macro expansion, never from unvalidated statement text. +func signalWindowBounded(columns []string, s *telemetry.Signal) bool { + clock := s.Clock() + for _, column := range columns { + if dot := strings.LastIndexByte(column, '.'); dot >= 0 { + if column[dot+1:] == clock && strings.EqualFold(column[:dot], s.Name) { + return true + } + continue + } + if column == clock { + return true + } + } + return false +} + func (d *Duck) physicalSource(signal string, batches []telemetry.BatchMetadata) string { s, _ := telemetry.LookupSignal(signal) sort.SliceStable(batches, func(i, j int) bool { @@ -148,6 +170,7 @@ func (d *Duck) bindSnapshot(ctx context.Context, db snapshotSQL, query string, w } queryrows.RecordCoverage(ctx, fresh) } + windowColumns, scopedColumns := queryrows.ReadWindowColumns(ctx) for _, s := range telemetry.Signals { signal := s.Name if _, raw := compiled.names["telemetry."+signal]; !raw { @@ -155,15 +178,25 @@ func (d *Duck) bindSnapshot(ctx context.Context, db snapshotSQL, query string, w continue } } + // A raw signal is event-time pruned only when the statement actually + // bounds its own clock with the window. When a trusted SQL panel windows + // on another column (or joins this signal unwindowed), pruning by the + // clock would drop rows the statement keeps, so bind every captured file. + selectWindow := w + if scopedColumns && !signalWindowBounded(windowColumns, s) { + selectWindow = allTime + } selected := []telemetry.BatchMetadata{} for _, b := range batches { - if overlapping(b, signal, w) { + if overlapping(b, signal, selectWindow) { selected = append(selected, b) } } // Only an explicit internal window may prune. Public arbitrary SQL - // uses allTime and always binds every captured file. - if signal == "metrics" && len(compiled.metricNames) > 0 && w != allTime { + // uses allTime and always binds every captured file. Metric-name + // pruning likewise requires the metrics signal to be genuinely + // window-bounded. + if signal == "metrics" && len(compiled.metricNames) > 0 && selectWindow != allTime { var err error selected, err = pruneMetricNames(ctx, db, selected, compiled.metricNames) if err != nil { diff --git a/internal/engine/file_snapshot_test.go b/internal/engine/file_snapshot_test.go index d8399c587..515dceedf 100644 --- a/internal/engine/file_snapshot_test.go +++ b/internal/engine/file_snapshot_test.go @@ -94,6 +94,69 @@ func TestSnapshotUsesActualEventBoundsAndPreservesParameters(t *testing.T) { } } + +// A trusted SQL panel may window on a column other than a signal's +// footer-sorted event-time clock. Pruning raw files by the clock then drops +// rows the panel's own predicate keeps. When the windowed column is not the +// signal's clock, every captured file for that signal must bind. +func TestSQLPanelBindsAllFilesWhenWindowColumnIsNotTheFooterClock(t *testing.T) { + d, repo := newBatchCacheTest(t) + at := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC) + // Spans are footer-sorted on start_time, but this panel windows on end_time. + commit := func(id string, start, end time.Time) { + t.Helper() + if err := repo.Commit(t.Context(), telemetrystore.Batch{ID: id, Spans: []telemetry.Span{{ + TraceID: "trace", SpanID: id, StartUnixNanos: start.UnixNano(), EndUnixNanos: end.UnixNano(), IngestedAt: at.UnixNano(), + }}}); err != nil { + t.Fatal(err) + } + } + commit("before", at.Add(-time.Hour), at.Add(30*time.Second)) // starts before the window, ends inside it + commit("inside", at, at.Add(time.Second)) + commit("after", at.Add(time.Hour), at.Add(time.Hour+time.Second)) + + w := queryrows.Window{Start: at, End: at.Add(time.Minute)} + ctx := queryrows.WithWindowColumns(queryrows.WithWindow(t.Context(), w), []string{"end_time"}) + var count int + q := `SELECT count(*) FROM spans WHERE end_time>=?::TIMESTAMP_NS::TIMESTAMPTZ_NS AND end_time= 128 { e.sqlPanels = map[string]sqlPanelPlan{} diff --git a/internal/queryrows/window.go b/internal/queryrows/window.go index 3fd1fa4c7..b29c94147 100644 --- a/internal/queryrows/window.go +++ b/internal/queryrows/window.go @@ -20,6 +20,23 @@ type Window struct { type windowKey struct{} type inventoryWindowKey struct{} +type windowColumnsKey struct{} + +// WithWindowColumns records the exact columns a trusted SQL panel bounds with +// the read window, captured at macro expansion where each column is +// unambiguous. A raw signal is event-time pruned by the window only when one of +// these columns is that signal's own event-time clock; a window on any other +// column (or on another relation joined to it) binds every captured file for +// the signal instead of silently dropping rows. Absent entirely, every raw +// signal keeps its default clock-based pruning. +func WithWindowColumns(ctx context.Context, columns []string) context.Context { + return context.WithValue(ctx, windowColumnsKey{}, columns) +} + +func ReadWindowColumns(ctx context.Context) ([]string, bool) { + columns, ok := ctx.Value(windowColumnsKey{}).([]string) + return columns, ok +} // WithInventoryWindow records the logical panel interval before file lookback // expansion. It is independent of the raw-file read window and read kind.