From 43c2ea3f6281871778d58e2548ded7c8a72981b6 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sun, 15 Nov 2026 07:51:50 -0800 Subject: [PATCH] fix(engine): bind all files when a SQL panel windows off the footer clock SQL panels file-pruned every telemetry relation to the panel's event-time window using each signal's own footer bounds, regardless of which column the statement actually windowed on. A statement that windowed on a non-clock column (e.g. $__window(end_time) or $__window(observed_time)), or that joined a windowed rollup to an unwindowed raw relation, silently dropped rows that fell outside the default prune window, violating the read-window contract. A raw signal (spans/logs/metrics) is now event-time pruned only when the statement bounds that signal's own event-time clock. The windowed columns are captured at macro expansion, where each column is unambiguous, threaded through the panel plan, and carried to bindSnapshot via a separate context value (WithWindowColumns) so Window stays a comparable struct and allTime detection is unchanged. When no windowed column resolves to a signal's clock -- bare clock match, or a qualified reference to the signal's own relation -- that signal binds every captured file instead of dropping rows; alias qualifiers that cannot be confidently resolved stay unbounded, correct if slower. Metric-name pruning now applies only when the metrics signal is genuinely window-bounded. Non-SQL-panel reads set no windowed columns and keep their default clock-based pruning, so the common dashboard panels (spans on start_time, logs on time, metrics on time, rollups on bucket) prune exactly as before. Closes #478 --- internal/engine/file_snapshot.go | 39 +++++++++++++++-- internal/engine/file_snapshot_test.go | 63 +++++++++++++++++++++++++++ internal/query/check.go | 2 +- internal/query/exec.go | 3 ++ internal/query/log_redaction_test.go | 2 +- internal/query/params_test.go | 9 ++-- internal/query/sqlpanel.go | 13 ++++-- internal/query/sqlpanel_plan.go | 8 +++- internal/queryrows/window.go | 17 ++++++++ 9 files changed, 142 insertions(+), 14 deletions(-) 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.