Skip to content

Commit

Permalink
fix: unconditional statistics (GreptimeTeam#4694)
Browse files Browse the repository at this point in the history
* fix: unconditional statistics

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* add more sqlness case

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

---------

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
  • Loading branch information
waynexia authored and CookiePieWw committed Sep 17, 2024
1 parent 225b409 commit c860601
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/mito2/src/read/scan_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ impl ScanInput {
rows_in_files + rows_in_memtables
}

pub(crate) fn predicate(&self) -> Option<Predicate> {
self.predicate.clone()
}

/// Retrieves [`PartitionRange`] from memtable and files
pub(crate) fn partition_ranges(&self) -> Vec<PartitionRange> {
let mut id = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/mito2/src/read/seq_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ impl RegionScanner for SeqScan {
self.properties.partitions = ranges;
Ok(())
}

fn has_predicate(&self) -> bool {
let predicate = self.stream_ctx.input.predicate();
predicate.map(|p| !p.exprs().is_empty()).unwrap_or(false)
}
}

impl DisplayAs for SeqScan {
Expand Down
5 changes: 5 additions & 0 deletions src/mito2/src/read/unordered_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ impl RegionScanner for UnorderedScan {

Ok(stream)
}

fn has_predicate(&self) -> bool {
let predicate = self.stream_ctx.input.predicate();
predicate.map(|p| !p.exprs().is_empty()).unwrap_or(false)
}
}

impl DisplayAs for UnorderedScan {
Expand Down
7 changes: 7 additions & 0 deletions src/store-api/src/region_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ pub trait RegionScanner: Debug + DisplayAs + Send {
/// # Panics
/// Panics if the `partition` is out of bound.
fn scan_partition(&self, partition: usize) -> Result<SendableRecordBatchStream, BoxedError>;

/// Check if there is any predicate that may be executed in this scanner.
fn has_predicate(&self) -> bool;
}

pub type RegionScannerRef = Box<dyn RegionScanner>;
Expand Down Expand Up @@ -367,6 +370,10 @@ impl RegionScanner for SinglePartitionScanner {
))
})
}

fn has_predicate(&self) -> bool {
false
}
}

impl DisplayAs for SinglePartitionScanner {
Expand Down
2 changes: 1 addition & 1 deletion src/table/src/table/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl ExecutionPlan for RegionScanExec {
}

fn statistics(&self) -> DfResult<Statistics> {
let statistics = if self.append_mode {
let statistics = if self.append_mode && !self.scanner.lock().unwrap().has_predicate() {
let column_statistics = self
.arrow_schema
.fields
Expand Down
47 changes: 47 additions & 0 deletions tests/cases/standalone/common/aggregate/count.result
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,50 @@ drop table test;

Affected Rows: 0

-- Append table
create table count_where_bug (
tag String,
ts TimestampMillisecond time index,
num Int64,
primary key (tag),
) engine=mito with('append_mode'='true');

Affected Rows: 0

insert into count_where_bug (tag, ts, num)
values ('a', '2024-09-06T06:00:01Z', 1),
('a', '2024-09-06T06:00:02Z', 2),
('a', '2024-09-06T06:00:03Z', 3),
('b', '2024-09-06T06:00:04Z', 4),
('b', '2024-09-06T06:00:05Z', 5);

Affected Rows: 5

select count(1) from count_where_bug where tag = 'b';

+-----------------+
| COUNT(Int64(1)) |
+-----------------+
| 2 |
+-----------------+

select count(1) from count_where_bug where ts > '2024-09-06T06:00:04Z';

+-----------------+
| COUNT(Int64(1)) |
+-----------------+
| 1 |
+-----------------+

select count(1) from count_where_bug where num != 3;

+-----------------+
| COUNT(Int64(1)) |
+-----------------+
| 4 |
+-----------------+

drop table count_where_bug;

Affected Rows: 0

24 changes: 24 additions & 0 deletions tests/cases/standalone/common/aggregate/count.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,27 @@ select count(*) from (select * from test cross join "HelloWorld");
drop table "HelloWorld";

drop table test;

-- Append table

create table count_where_bug (
tag String,
ts TimestampMillisecond time index,
num Int64,
primary key (tag),
) engine=mito with('append_mode'='true');

insert into count_where_bug (tag, ts, num)
values ('a', '2024-09-06T06:00:01Z', 1),
('a', '2024-09-06T06:00:02Z', 2),
('a', '2024-09-06T06:00:03Z', 3),
('b', '2024-09-06T06:00:04Z', 4),
('b', '2024-09-06T06:00:05Z', 5);

select count(1) from count_where_bug where tag = 'b';

select count(1) from count_where_bug where ts > '2024-09-06T06:00:04Z';

select count(1) from count_where_bug where num != 3;

drop table count_where_bug;

0 comments on commit c860601

Please sign in to comment.