diff --git a/src/mito2/src/engine/alter_test.rs b/src/mito2/src/engine/alter_test.rs index 374cfff6f3f..86af5803261 100644 --- a/src/mito2/src/engine/alter_test.rs +++ b/src/mito2/src/engine/alter_test.rs @@ -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, + ); +} diff --git a/src/mito2/src/sst/parquet/reader.rs b/src/mito2/src/sst/parquet/reader.rs index 7dc2299a073..901f3e74c61 100644 --- a/src/mito2/src/sst/parquet/reader.rs +++ b/src/mito2/src/sst/parquet/reader.rs @@ -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)?, diff --git a/tests/cases/standalone/common/alter/change_col_type.result b/tests/cases/standalone/common/alter/change_col_type.result index 35580edae80..0cb2f24180e 100644 --- a/tests/cases/standalone/common/alter/change_col_type.result +++ b/tests/cases/standalone/common/alter/change_col_type.result @@ -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; diff --git a/tests/cases/standalone/common/alter/change_col_type.sql b/tests/cases/standalone/common/alter/change_col_type.sql index 34570148462..4fcc92e20ab 100644 --- a/tests/cases/standalone/common/alter/change_col_type.sql +++ b/tests/cases/standalone/common/alter/change_col_type.sql @@ -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;