diff --git a/src/mito2/src/read/last_row.rs b/src/mito2/src/read/last_row.rs index 0a4ff7a1fc..69bd02ae60 100644 --- a/src/mito2/src/read/last_row.rs +++ b/src/mito2/src/read/last_row.rs @@ -515,6 +515,13 @@ impl FlatRowGroupLastRowReader { if self.yielded_batches.is_empty() { return; } + + // Filtered flat last-row scans only contain the subset of series that matched the + // encoded primary-key prefilter, so they cannot be published under the shared + // selector cache key. + if self.primary_key_filter.is_some() { + return; + } let batches = std::mem::take(&mut self.yielded_batches); let value = Arc::new(SelectorResultValue::new_flat( batches, diff --git a/tests/cases/standalone/common/aggregate/last_row_selector_cache_filter.result b/tests/cases/standalone/common/aggregate/last_row_selector_cache_filter.result new file mode 100644 index 0000000000..b932891f4a --- /dev/null +++ b/tests/cases/standalone/common/aggregate/last_row_selector_cache_filter.result @@ -0,0 +1,53 @@ +CREATE TABLE last_row_selector_cache_filter ( + host STRING, + cpu DOUBLE, + ts TIMESTAMP TIME INDEX, + PRIMARY KEY (host) +) WITH ('sst_format' = 'flat'); + +Affected Rows: 0 + +INSERT INTO last_row_selector_cache_filter VALUES + ('a', 1.0, 1000), + ('a', 2.0, 2000), + ('b', 3.0, 1000), + ('b', 4.0, 2000); + +Affected Rows: 4 + +ADMIN FLUSH_TABLE('last_row_selector_cache_filter'); + ++-----------------------------------------------------+ +| ADMIN FLUSH_TABLE('last_row_selector_cache_filter') | ++-----------------------------------------------------+ +| 0 | ++-----------------------------------------------------+ + +SELECT host, last_value(cpu ORDER BY ts) AS last_cpu +FROM last_row_selector_cache_filter +WHERE host = 'a' +GROUP BY host +ORDER BY host; + ++------+----------+ +| host | last_cpu | ++------+----------+ +| a | 2.0 | ++------+----------+ + +SELECT host, last_value(cpu ORDER BY ts) AS last_cpu +FROM last_row_selector_cache_filter +GROUP BY host +ORDER BY host; + ++------+----------+ +| host | last_cpu | ++------+----------+ +| a | 2.0 | +| b | 4.0 | ++------+----------+ + +DROP TABLE last_row_selector_cache_filter; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/aggregate/last_row_selector_cache_filter.sql b/tests/cases/standalone/common/aggregate/last_row_selector_cache_filter.sql new file mode 100644 index 0000000000..2e591d77ff --- /dev/null +++ b/tests/cases/standalone/common/aggregate/last_row_selector_cache_filter.sql @@ -0,0 +1,27 @@ +CREATE TABLE last_row_selector_cache_filter ( + host STRING, + cpu DOUBLE, + ts TIMESTAMP TIME INDEX, + PRIMARY KEY (host) +) WITH ('sst_format' = 'flat'); + +INSERT INTO last_row_selector_cache_filter VALUES + ('a', 1.0, 1000), + ('a', 2.0, 2000), + ('b', 3.0, 1000), + ('b', 4.0, 2000); + +ADMIN FLUSH_TABLE('last_row_selector_cache_filter'); + +SELECT host, last_value(cpu ORDER BY ts) AS last_cpu +FROM last_row_selector_cache_filter +WHERE host = 'a' +GROUP BY host +ORDER BY host; + +SELECT host, last_value(cpu ORDER BY ts) AS last_cpu +FROM last_row_selector_cache_filter +GROUP BY host +ORDER BY host; + +DROP TABLE last_row_selector_cache_filter;