mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-25 04:04:09 +00:00
fix: drop physical prefilter for columns whose file type differs
Follow-up to the review feedback on CompatBatch/prune reader/filter handling for widened time index units. Between/InList/IsNull predicates are prefiltered by PhysicalFilterContext, which builds its physical expression against the FILE's schema while the predicate literals are in the expected (post-alter) unit. Evaluating them against an old-unit SST raised a cross-unit comparison error (Timestamp(ms) >= Timestamp(µs)) that failed the whole scan. Physical prefilter predicates are best-effort pruning hints (the query layer re-applies them above the scan), so drop the prefilter when the column's file type differs from the expected type, mirroring the simple-filter strategy. Verified: Between and a non-rewritten (large) InList on old-unit data no longer error and filter exactly end-to-end (sqlness), and no matching rows are lost at the engine level (engine test). Signed-off-by: Ning Sun <sunning@greptime.com>
This commit is contained in:
@@ -2799,3 +2799,102 @@ async fn test_bulk_insert_stale_wal_entry_replay_with_format(flat_format: bool)
|
||||
"flat_format: {flat_format}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Regression test: `Between` / `InList` predicates (physical prefilter
|
||||
/// expressions, not `SimpleFilterEvaluator`s) must not fail the scan when the
|
||||
/// file stores the time index in the old unit - evaluating the new-unit
|
||||
/// literals against the file's column raises a cross-unit comparison error.
|
||||
/// The prefilter is dropped for such files instead; the query layer re-applies
|
||||
/// the predicate, so at the engine level only "no matching row is lost" is
|
||||
/// guaranteed (exactness is covered by the sqlness cases).
|
||||
#[tokio::test]
|
||||
async fn test_alter_time_index_widen_old_sst_between_inlist() {
|
||||
common_telemetry::init_default_ut_logging();
|
||||
|
||||
let mut env = TestEnv::new().await;
|
||||
let engine = env.create_engine(MitoConfig::default()).await;
|
||||
|
||||
let region_id = RegionId::new(1, 1);
|
||||
let request = CreateRequestBuilder::new()
|
||||
.insert_option("max_row_group_row_count", "5")
|
||||
.build();
|
||||
|
||||
env.get_schema_metadata_manager()
|
||||
.register_region_table_info(
|
||||
region_id.table_id(),
|
||||
"test_table",
|
||||
"test_catalog",
|
||||
"test_schema",
|
||||
None,
|
||||
env.get_kv_backend(),
|
||||
)
|
||||
.await;
|
||||
|
||||
let column_schemas = rows_schema(&request);
|
||||
engine
|
||||
.handle_request(region_id, RegionRequest::Create(request))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
put_rows(
|
||||
&engine,
|
||||
region_id,
|
||||
Rows {
|
||||
schema: column_schemas,
|
||||
rows: build_rows(0, 10),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
flush_region(&engine, region_id, None).await;
|
||||
|
||||
engine
|
||||
.handle_request(
|
||||
region_id,
|
||||
RegionRequest::Alter(RegionAlterRequest {
|
||||
kind: AlterKind::ModifyColumnTypes {
|
||||
columns: vec![ModifyColumnType {
|
||||
column_name: "ts".to_string(),
|
||||
target_type: ConcreteDataType::timestamp_microsecond_datatype(),
|
||||
}],
|
||||
},
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let between_expected: Vec<(i64, f64)> = (3..=6).map(|ts| (ts * 1_000_000, ts as f64)).collect();
|
||||
assert_rows_present(
|
||||
&between_expected,
|
||||
scan_ts_and_field(
|
||||
&engine,
|
||||
region_id,
|
||||
vec![col("ts").between(ts_us_lit(3_000_000), ts_us_lit(6_000_000))],
|
||||
)
|
||||
.await,
|
||||
);
|
||||
|
||||
let inlist_expected = vec![(2_000_000, 2.0)];
|
||||
assert_rows_present(
|
||||
&inlist_expected,
|
||||
scan_ts_and_field(
|
||||
&engine,
|
||||
region_id,
|
||||
vec![col("ts").in_list(vec![ts_us_lit(2_000_000), ts_us_lit(5_000_500)], false)],
|
||||
)
|
||||
.await,
|
||||
);
|
||||
|
||||
let not_in_expected: Vec<(i64, f64)> = (0..10)
|
||||
.filter(|ts| *ts != 2)
|
||||
.map(|ts| (ts * 1_000_000, ts as f64))
|
||||
.collect();
|
||||
assert_rows_present(
|
||||
¬_in_expected,
|
||||
scan_ts_and_field(
|
||||
&engine,
|
||||
region_id,
|
||||
vec![col("ts").in_list(vec![ts_us_lit(2_000_000), ts_us_lit(5_000_500)], true)],
|
||||
)
|
||||
.await,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2170,6 +2170,15 @@ impl PhysicalFilterContext {
|
||||
if sst_column.column_schema.name != column_name {
|
||||
return None;
|
||||
}
|
||||
// Schema evolution (an altered field column, or a widened time
|
||||
// index unit) can make the column's file type differ from the
|
||||
// expected type; evaluating the original expr against the
|
||||
// file's column would raise a cross-type comparison error
|
||||
// (e.g. Timestamp(ms) >= Timestamp(µs)). Drop the prefilter;
|
||||
// the query layer re-applies the predicate above the scan.
|
||||
if sst_column.column_schema.data_type != column.column_schema.data_type {
|
||||
return None;
|
||||
}
|
||||
column
|
||||
}
|
||||
None => sst_meta.column_by_name(&column_name)?,
|
||||
|
||||
@@ -250,6 +250,41 @@ SELECT * FROM ts_widen_multi WHERE ts <= '2024-01-01 00:00:05.0005' ORDER BY ts;
|
||||
| f | 2024-01-01T00:00:05 |
|
||||
+------+---------------------+
|
||||
|
||||
-- BETWEEN must not fail or misfilter on old-unit rows
|
||||
SELECT * FROM ts_widen_multi WHERE ts BETWEEN '2024-01-01 00:00:03' AND '2024-01-01 00:00:06.5' ORDER BY ts;
|
||||
|
||||
+------+---------------------+
|
||||
| host | ts |
|
||||
+------+---------------------+
|
||||
| d | 2024-01-01T00:00:03 |
|
||||
| e | 2024-01-01T00:00:04 |
|
||||
| f | 2024-01-01T00:00:05 |
|
||||
| g | 2024-01-01T00:00:06 |
|
||||
+------+---------------------+
|
||||
|
||||
-- a large IN-list is not rewritten to OR; it must still filter exactly
|
||||
SELECT * FROM ts_widen_multi WHERE ts IN (
|
||||
'2024-01-01 00:00:00', '2024-01-01 00:00:01', '2024-01-01 00:00:02',
|
||||
'2024-01-01 00:00:03', '2024-01-01 00:00:04', '2024-01-01 00:00:05',
|
||||
'2024-01-01 00:00:06', '2024-01-01 00:00:07', '2024-01-01 00:00:08',
|
||||
'2024-01-01 00:00:09', '2024-01-01 00:00:02.5'
|
||||
) ORDER BY ts;
|
||||
|
||||
+------+---------------------+
|
||||
| host | ts |
|
||||
+------+---------------------+
|
||||
| a | 2024-01-01T00:00:00 |
|
||||
| b | 2024-01-01T00:00:01 |
|
||||
| c | 2024-01-01T00:00:02 |
|
||||
| d | 2024-01-01T00:00:03 |
|
||||
| e | 2024-01-01T00:00:04 |
|
||||
| f | 2024-01-01T00:00:05 |
|
||||
| g | 2024-01-01T00:00:06 |
|
||||
| h | 2024-01-01T00:00:07 |
|
||||
| i | 2024-01-01T00:00:08 |
|
||||
| j | 2024-01-01T00:00:09 |
|
||||
+------+---------------------+
|
||||
|
||||
-- an IN-list literal that no millisecond row can equal matches nothing
|
||||
SELECT * FROM ts_widen_multi WHERE ts IN ('2024-01-01 00:00:02', '2024-01-01 00:00:05.5', '2024-01-01 00:00:08') ORDER BY ts;
|
||||
|
||||
|
||||
@@ -86,6 +86,17 @@ SELECT * FROM ts_widen_multi WHERE ts > '2024-01-01 00:00:06.5' ORDER BY ts;
|
||||
|
||||
SELECT * FROM ts_widen_multi WHERE ts <= '2024-01-01 00:00:05.0005' ORDER BY ts;
|
||||
|
||||
-- BETWEEN must not fail or misfilter on old-unit rows
|
||||
SELECT * FROM ts_widen_multi WHERE ts BETWEEN '2024-01-01 00:00:03' AND '2024-01-01 00:00:06.5' ORDER BY ts;
|
||||
|
||||
-- a large IN-list is not rewritten to OR; it must still filter exactly
|
||||
SELECT * FROM ts_widen_multi WHERE ts IN (
|
||||
'2024-01-01 00:00:00', '2024-01-01 00:00:01', '2024-01-01 00:00:02',
|
||||
'2024-01-01 00:00:03', '2024-01-01 00:00:04', '2024-01-01 00:00:05',
|
||||
'2024-01-01 00:00:06', '2024-01-01 00:00:07', '2024-01-01 00:00:08',
|
||||
'2024-01-01 00:00:09', '2024-01-01 00:00:02.5'
|
||||
) ORDER BY ts;
|
||||
|
||||
-- an IN-list literal that no millisecond row can equal matches nothing
|
||||
SELECT * FROM ts_widen_multi WHERE ts IN ('2024-01-01 00:00:02', '2024-01-01 00:00:05.5', '2024-01-01 00:00:08') ORDER BY ts;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user