mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-26 12:44:10 +00:00
fix(query): correct filtering and ordering for file engine scans (#10547)
* fix(query): retain row filters for file engine scans Signed-off-by: fys <fengys1996@gmail.com> * fix(query): skip time-series scan hints for file regions Signed-off-by: fys <fengys1996@gmail.com> --------- Signed-off-by: fys <fengys1996@gmail.com>
This commit is contained in:
@@ -23,6 +23,7 @@ use api::v1::SemanticType;
|
||||
use async_trait::async_trait;
|
||||
use catalog::error::Result as CatalogResult;
|
||||
use catalog::{CatalogManager, CatalogManagerRef};
|
||||
use common_catalog::consts::FILE_ENGINE;
|
||||
use common_recordbatch::OrderOption;
|
||||
use common_recordbatch::filter::SimpleFilterEvaluator;
|
||||
use datafusion::catalog::{CatalogProvider, CatalogProviderList, SchemaProvider, Session};
|
||||
@@ -206,6 +207,11 @@ impl TableProvider for DummyTableProvider {
|
||||
&self,
|
||||
filters: &[&Expr],
|
||||
) -> datafusion::error::Result<Vec<TableProviderFilterPushDown>> {
|
||||
// File readers may use predicates for pruning without filtering individual rows.
|
||||
if self.is_file_engine() {
|
||||
return Ok(vec![TableProviderFilterPushDown::Inexact; filters.len()]);
|
||||
}
|
||||
|
||||
let supported = filters
|
||||
.iter()
|
||||
.map(|e| {
|
||||
@@ -260,6 +266,11 @@ impl DummyTableProvider {
|
||||
self.metadata.clone()
|
||||
}
|
||||
|
||||
/// Returns whether this provider scans File Engine regions.
|
||||
pub(crate) fn is_file_engine(&self) -> bool {
|
||||
self.engine.name() == FILE_ENGINE
|
||||
}
|
||||
|
||||
/// Sets the ordering hint of the query to the provider.
|
||||
pub fn with_ordering_hint(&self, order_opts: &[OrderOption]) {
|
||||
self.scan_request.lock().unwrap().output_ordering = Some(order_opts.to_vec());
|
||||
|
||||
@@ -86,6 +86,11 @@ impl ScanHintRule {
|
||||
return Ok(Transformed::no(LogicalPlan::TableScan(table_scan)));
|
||||
};
|
||||
|
||||
// File scans follow file order and leave time-series row selection to the query.
|
||||
if original.is_file_engine() {
|
||||
return Ok(Transformed::no(LogicalPlan::TableScan(table_scan)));
|
||||
}
|
||||
|
||||
// Attached scan filters are checked below; residual Filter nodes are
|
||||
// rejected by the single-evaluation path allowlist.
|
||||
let filters_preserve_last_row = if rewriter.inside_single_evaluation {
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
COPY (
|
||||
SELECT * FROM (VALUES
|
||||
(1, 'a', '2024-01-01T00:00:00.000'),
|
||||
(2, 'b', '2024-01-02T00:00:00.000'),
|
||||
(3, 'c', '2024-01-03T00:00:00.000'),
|
||||
(4, 'a', '2023-12-31T00:00:00.000')
|
||||
) AS rows(id, host, ts) ORDER BY id
|
||||
) TO 'external_table_filter/rows.csv' WITH (format = 'csv');
|
||||
|
||||
Affected Rows: 4
|
||||
|
||||
CREATE EXTERNAL TABLE csv_filter_declared (
|
||||
"id" BIGINT,
|
||||
"host" STRING PRIMARY KEY,
|
||||
"ts" TIMESTAMP(3) TIME INDEX
|
||||
) WITH (location = 'external_table_filter/rows.csv', format = 'csv');
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
SELECT id FROM csv_filter_declared WHERE host = 'b';
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 2 |
|
||||
+----+
|
||||
|
||||
SELECT id FROM csv_filter_declared WHERE ts >= TIMESTAMP '2024-01-03 00:00:00';
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 3 |
|
||||
+----+
|
||||
|
||||
SELECT id FROM csv_filter_declared WHERE host = 'b' LIMIT 1;
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 2 |
|
||||
+----+
|
||||
|
||||
-- The earliest row for host 'a' is last in the file.
|
||||
SELECT id FROM csv_filter_declared WHERE host = 'a' ORDER BY host, ts LIMIT 1;
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 4 |
|
||||
+----+
|
||||
|
||||
-- File order can differ from primary-key and time-index order without a filter.
|
||||
SELECT id FROM csv_filter_declared ORDER BY host ASC NULLS FIRST, ts ASC NULLS FIRST LIMIT 1;
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 4 |
|
||||
+----+
|
||||
|
||||
CREATE EXTERNAL TABLE csv_filter_inferred
|
||||
WITH (location = 'external_table_filter/rows.csv', format = 'csv');
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
SELECT COUNT(*) FROM csv_filter_inferred
|
||||
WHERE greptime_timestamp > TIMESTAMP '2000-01-01 00:00:00';
|
||||
|
||||
+----------+
|
||||
| count(*) |
|
||||
+----------+
|
||||
| 0 |
|
||||
+----------+
|
||||
|
||||
DROP TABLE csv_filter_inferred;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
COPY (SELECT * FROM csv_filter_declared ORDER BY id)
|
||||
TO 'external_table_filter/rows.parquet' WITH (format = 'parquet');
|
||||
|
||||
Affected Rows: 4
|
||||
|
||||
CREATE EXTERNAL TABLE parquet_filter_declared (
|
||||
"id" BIGINT,
|
||||
"host" STRING PRIMARY KEY,
|
||||
"ts" TIMESTAMP(3) TIME INDEX
|
||||
) WITH (location = 'external_table_filter/rows.parquet', format = 'parquet');
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
-- The matching row shares a row group with nonmatching rows.
|
||||
SELECT id FROM parquet_filter_declared WHERE host = 'b';
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 2 |
|
||||
+----+
|
||||
|
||||
SELECT id FROM parquet_filter_declared WHERE host = 'a' ORDER BY host, ts LIMIT 1;
|
||||
|
||||
+----+
|
||||
| id |
|
||||
+----+
|
||||
| 4 |
|
||||
+----+
|
||||
|
||||
DROP TABLE parquet_filter_declared;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
DROP TABLE csv_filter_declared;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
COPY (
|
||||
SELECT * FROM (VALUES
|
||||
(1, 'a', '2024-01-01T00:00:00.000'),
|
||||
(2, 'b', '2024-01-02T00:00:00.000'),
|
||||
(3, 'c', '2024-01-03T00:00:00.000'),
|
||||
(4, 'a', '2023-12-31T00:00:00.000')
|
||||
) AS rows(id, host, ts) ORDER BY id
|
||||
) TO 'external_table_filter/rows.csv' WITH (format = 'csv');
|
||||
|
||||
CREATE EXTERNAL TABLE csv_filter_declared (
|
||||
"id" BIGINT,
|
||||
"host" STRING PRIMARY KEY,
|
||||
"ts" TIMESTAMP(3) TIME INDEX
|
||||
) WITH (location = 'external_table_filter/rows.csv', format = 'csv');
|
||||
|
||||
SELECT id FROM csv_filter_declared WHERE host = 'b';
|
||||
|
||||
SELECT id FROM csv_filter_declared WHERE ts >= TIMESTAMP '2024-01-03 00:00:00';
|
||||
|
||||
SELECT id FROM csv_filter_declared WHERE host = 'b' LIMIT 1;
|
||||
|
||||
-- The earliest row for host 'a' is last in the file.
|
||||
SELECT id FROM csv_filter_declared WHERE host = 'a' ORDER BY host, ts LIMIT 1;
|
||||
|
||||
-- File order can differ from primary-key and time-index order without a filter.
|
||||
SELECT id FROM csv_filter_declared ORDER BY host ASC NULLS FIRST, ts ASC NULLS FIRST LIMIT 1;
|
||||
|
||||
CREATE EXTERNAL TABLE csv_filter_inferred
|
||||
WITH (location = 'external_table_filter/rows.csv', format = 'csv');
|
||||
|
||||
SELECT COUNT(*) FROM csv_filter_inferred
|
||||
WHERE greptime_timestamp > TIMESTAMP '2000-01-01 00:00:00';
|
||||
|
||||
DROP TABLE csv_filter_inferred;
|
||||
|
||||
COPY (SELECT * FROM csv_filter_declared ORDER BY id)
|
||||
TO 'external_table_filter/rows.parquet' WITH (format = 'parquet');
|
||||
|
||||
CREATE EXTERNAL TABLE parquet_filter_declared (
|
||||
"id" BIGINT,
|
||||
"host" STRING PRIMARY KEY,
|
||||
"ts" TIMESTAMP(3) TIME INDEX
|
||||
) WITH (location = 'external_table_filter/rows.parquet', format = 'parquet');
|
||||
|
||||
-- The matching row shares a row group with nonmatching rows.
|
||||
SELECT id FROM parquet_filter_declared WHERE host = 'b';
|
||||
|
||||
SELECT id FROM parquet_filter_declared WHERE host = 'a' ORDER BY host, ts LIMIT 1;
|
||||
|
||||
DROP TABLE parquet_filter_declared;
|
||||
|
||||
DROP TABLE csv_filter_declared;
|
||||
Reference in New Issue
Block a user