diff --git a/weed/query/engine/system_columns.go b/weed/query/engine/system_columns.go index 256ad90ff9..5945885114 100644 --- a/weed/query/engine/system_columns.go +++ b/weed/query/engine/system_columns.go @@ -84,12 +84,14 @@ func (e *SQLEngine) getSystemColumnGlobalMin(columnName string, allFileStats map switch lowerName { case SW_COLUMN_NAME_TIMESTAMP: // For timestamps, find the earliest timestamp across all files - // This should match what's in the Extended[mq.ExtendedAttrTimestampMin] metadata var minTimestamp *int64 for _, fileStats := range allFileStats { for _, fileStat := range fileStats { - // Extract timestamp from filename (format: YYYY-MM-DD-HH-MM-SS.parquet) - timestamp := e.extractTimestampFromFilename(fileStat.FileName) + // a name is only the start second of the file's first source log + timestamp := fileStat.MinTimestampNs + if timestamp == 0 { + timestamp = e.extractTimestampFromFilename(fileStat.FileName) + } if timestamp != 0 { if minTimestamp == nil || timestamp < *minTimestamp { minTimestamp = ×tamp @@ -121,12 +123,13 @@ func (e *SQLEngine) getSystemColumnGlobalMax(columnName string, allFileStats map switch lowerName { case SW_COLUMN_NAME_TIMESTAMP: // For timestamps, find the latest timestamp across all files - // This should match what's in the Extended[mq.ExtendedAttrTimestampMax] metadata var maxTimestamp *int64 for _, fileStats := range allFileStats { for _, fileStat := range fileStats { - // Extract timestamp from filename (format: YYYY-MM-DD-HH-MM-SS.parquet) - timestamp := e.extractTimestampFromFilename(fileStat.FileName) + timestamp := fileStat.MaxTimestampNs + if timestamp == 0 { + timestamp = e.extractTimestampFromFilename(fileStat.FileName) + } if timestamp != 0 { if maxTimestamp == nil || timestamp > *maxTimestamp { maxTimestamp = ×tamp diff --git a/weed/query/engine/system_columns_test.go b/weed/query/engine/system_columns_test.go new file mode 100644 index 0000000000..d2e2517ad7 --- /dev/null +++ b/weed/query/engine/system_columns_test.go @@ -0,0 +1,32 @@ +package engine + +import ( + "testing" + "time" + + "github.com/seaweedfs/seaweedfs/weed/mq/topic" +) + +// A file's name is the start second of its first source log; MIN and MAX of _ts +// come from the timestamp range the file records, and from names only without one. +func TestSystemColumnTimestampRangePrefersFileStats(t *testing.T) { + base := time.Date(2026, 11, 30, 10, 0, 0, 0, time.UTC) + stats := map[string][]*ParquetFileStats{ + "p0": { + {FileName: topic.LogFileName(base, 0) + ".parquet", MinTimestampNs: base.Add(500 * time.Millisecond).UnixNano(), MaxTimestampNs: base.Add(9 * time.Second).UnixNano()}, + {FileName: topic.LogFileName(base.Add(5*time.Second), 4) + ".parquet"}, + }, + } + e := &SQLEngine{} + if got, want := e.getSystemColumnGlobalMin(SW_COLUMN_NAME_TIMESTAMP, stats), base.Add(500*time.Millisecond).UnixNano(); got != want { + t.Errorf("min %v, want %v", got, want) + } + if got, want := e.getSystemColumnGlobalMax(SW_COLUMN_NAME_TIMESTAMP, stats), base.Add(9*time.Second).UnixNano(); got != want { + t.Errorf("max %v, want %v", got, want) + } + + stats["p0"] = stats["p0"][1:] + if got, want := e.getSystemColumnGlobalMax(SW_COLUMN_NAME_TIMESTAMP, stats), base.Add(5*time.Second).UnixNano(); got != want { + t.Errorf("max without stats %v, want the name's %v", got, want) + } +}