Skip to content

Commit 083950c

Browse files
committed
chore: add test for composite index trailing columns cover
1 parent 82c4f72 commit 083950c

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

src/optimizer/rule/normalization/pushdown_predicates.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use crate::types::index::{IndexInfo, IndexMetaRef, IndexType};
1313
use crate::types::value::DataValue;
1414
use crate::types::LogicalType;
1515
use itertools::Itertools;
16-
use std::mem;
1716
use std::ops::Bound;
1817
use std::sync::LazyLock;
18+
use std::{mem, slice};
1919

2020
static PUSH_PREDICATE_THROUGH_JOIN: LazyLock<Pattern> = LazyLock::new(|| Pattern {
2121
predicate: |op| matches!(op, Operator::Filter(_)),
@@ -238,16 +238,27 @@ impl NormalizationRule for PushPredicateIntoScan {
238238
}
239239
};
240240
// try index covered
241-
let mut deserializers = Vec::with_capacity(scan_op.columns.len());
242-
for column_id in &meta.column_ids {
241+
let mut deserializers = Vec::with_capacity(meta.column_ids.len());
242+
let mut cover_count = 0;
243+
let index_column_types = match &meta.value_ty {
244+
LogicalType::Tuple(tys) => tys,
245+
ty => slice::from_ref(ty),
246+
};
247+
for (i, column_id) in meta.column_ids.iter().enumerate() {
243248
for column in scan_op.columns.values() {
244-
if column.id().map(|id| &id == column_id).unwrap_or(false) {
245-
deserializers.push(column.datatype().serializable());
246-
}
249+
deserializers.push(
250+
if column.id().map(|id| &id == column_id).unwrap_or(false) {
251+
cover_count += 1;
252+
column.datatype().serializable()
253+
} else {
254+
index_column_types[i].skip_serializable()
255+
},
256+
);
247257
}
248258
}
249-
*covered_deserializers =
250-
(!deserializers.is_empty()).then_some(deserializers);
259+
if cover_count == scan_op.columns.len() {
260+
*covered_deserializers = Some(deserializers)
261+
}
251262
}
252263
}
253264
}

src/storage/table_codec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pub(crate) const BOUND_MAX_TAG: u8 = u8::MAX;
1919
pub(crate) const NULL_TAG: u8 = 0u8;
2020
pub(crate) const NOTNULL_TAG: u8 = 1u8;
2121

22-
static ROOT_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| b"R".to_vec());
23-
static VIEW_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| b"V".to_vec());
24-
static HASH_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| b"H".to_vec());
22+
static ROOT_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| b"Root".to_vec());
23+
static VIEW_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| b"View".to_vec());
24+
static HASH_BYTES: LazyLock<Vec<u8>> = LazyLock::new(|| b"Hash".to_vec());
2525
static EMPTY_REFERENCE_TABLES: LazyLock<ReferenceTables> = LazyLock::new(ReferenceTables::new);
2626

2727
pub type Bytes = Vec<u8>;

tests/slt/where_by_index.slt

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ select * from t1 where c2 > 0 and c2 < 10;
202202
query I rowsort
203203
select c1 from t1 where c1 < 10;
204204
----
205+
1
206+
7
207+
208+
# unique covered with primary key projection
209+
query II rowsort
210+
select c1, id from t1 where c1 < 10;
211+
----
212+
1 0
213+
7 6
205214

206215
statement ok
207216
drop index t1.u_c1_index;
@@ -210,15 +219,49 @@ drop index t1.u_c1_index;
210219
query II rowsort
211220
select c2 from t1 where c2 < 10 and c2 > 0;
212221
----
222+
8
223+
9
213224

214225
statement ok
215226
drop index t1.c2_index;
216227

217228
# composite covered
218229
query II rowsort
219-
select c1 c2 from t1 where c1 < 10 and c1 > 0 and c2 >0 and c2 < 10;
230+
select c1, c2 from t1 where c1 < 10 and c1 > 0 and c2 >0 and c2 < 10;
220231
----
232+
1 9
233+
7 8
234+
235+
# composite covered projection reorder
236+
query II rowsort
237+
select c2, c1 from t1 where c1 < 10 and c1 > 0 and c2 > 0 and c2 < 10;
238+
----
239+
8 7
240+
9 1
241+
242+
243+
statement ok
244+
drop table t1;
221245

246+
statement ok
247+
create table t_cover(id int primary key, c1 int, c2 int, c3 int);
248+
249+
statement ok
250+
insert into t_cover values
251+
(1, 1, 10, 11),
252+
(2, 2, 20, 21),
253+
(3, 2, 22, 23),
254+
(4, 3, 30, 31);
255+
256+
statement ok
257+
create index idx_cover on t_cover (c1, c2, c3);
258+
259+
# composite index trailing columns cover (index columns > output columns)
260+
query II rowsort
261+
select c2, c3 from t_cover where c1 = 2;
262+
----
263+
20 21
264+
22 23
222265

223266
statement ok
224-
drop table t1;
267+
drop table t_cover;

0 commit comments

Comments
 (0)