Skip to content

Commit bacb318

Browse files
committed
refactor: simplify parameterized index scan runtime probe flow
1 parent 54068c8 commit bacb318

File tree

21 files changed

+369
-495
lines changed

21 files changed

+369
-495
lines changed

src/db.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,12 @@ impl<S: Storage> State<S> {
463463
&self.view_cache
464464
}
465465

466-
fn build_plan_and_runtime_params<A: AsRef<[(&'static str, DataValue)]>>(
466+
fn build_plan<A: AsRef<[(&'static str, DataValue)]>>(
467467
&self,
468468
stmt: &Statement,
469469
params: A,
470470
transaction: &<S as Storage>::TransactionType<'_>,
471-
) -> Result<(LogicalPlan, usize), DatabaseError> {
471+
) -> Result<LogicalPlan, DatabaseError> {
472472
let mut binder = Binder::new(
473473
BinderContext::new(
474474
self.table_cache(),
@@ -491,7 +491,6 @@ impl<S: Storage> State<S> {
491491
let source_plan = binder.bind(stmt)?;
492492
let mut optimizer = self.optimizer_pipeline.instantiate(source_plan);
493493
optimizer.optimize(Some(&transaction.meta_loader(self.meta_cache())))?;
494-
let runtime_param_count = optimizer.runtime_param_count();
495494
let mut best_plan = optimizer.into_plan();
496495

497496
if let Operator::Analyze(op) = &mut best_plan.operator {
@@ -500,7 +499,7 @@ impl<S: Storage> State<S> {
500499
}
501500
}
502501

503-
Ok((best_plan, runtime_param_count))
502+
Ok(best_plan)
504503
}
505504

506505
fn execute<'a, 'txn, A: AsRef<[(&'static str, DataValue)]>>(
@@ -512,11 +511,9 @@ impl<S: Storage> State<S> {
512511
where
513512
S: 'txn,
514513
{
515-
let (mut plan, runtime_param_count) =
516-
self.build_plan_and_runtime_params(stmt, params, transaction)?;
514+
let mut plan = self.build_plan(stmt, params, transaction)?;
517515
let schema = plan.output_schema().clone();
518516
let mut arena = ExecArena::default();
519-
arena.init_runtime_params(runtime_param_count);
520517
let root = build_write(
521518
&mut arena,
522519
plan,
@@ -1165,10 +1162,7 @@ pub(crate) mod test {
11651162
None,
11661163
);
11671164
let source_plan = binder.bind(&stmt)?;
1168-
let (best_plan, _) =
1169-
kite_sql
1170-
.state
1171-
.build_plan_and_runtime_params(&stmt, [], &transaction)?;
1165+
let best_plan = kite_sql.state.build_plan(&stmt, [], &transaction)?;
11721166

11731167
let join_plan = match source_plan.operator {
11741168
Operator::Project(_) => source_plan.childrens.pop_only(),
@@ -1252,10 +1246,7 @@ pub(crate) mod test {
12521246
None,
12531247
);
12541248
let source_plan = binder.bind(&stmt)?;
1255-
let (best_plan, _) =
1256-
kite_sql
1257-
.state
1258-
.build_plan_and_runtime_params(&stmt, [], &transaction)?;
1249+
let best_plan = kite_sql.state.build_plan(&stmt, [], &transaction)?;
12591250

12601251
let join_plan = match source_plan.operator {
12611252
Operator::Project(_) => source_plan.childrens.pop_only(),
@@ -1353,10 +1344,7 @@ pub(crate) mod test {
13531344
"SELECT o.x, t.y FROM onecolumn o INNER JOIN twocolumn t ON (o.x=t.x AND t.y=53)",
13541345
)?;
13551346
let transaction = kite_sql.storage.transaction()?;
1356-
let (best_plan, _) =
1357-
kite_sql
1358-
.state
1359-
.build_plan_and_runtime_params(&stmt, [], &transaction)?;
1347+
let best_plan = kite_sql.state.build_plan(&stmt, [], &transaction)?;
13601348
let join_plan = match best_plan.operator {
13611349
Operator::Project(_) => best_plan.childrens.pop_only(),
13621350
Operator::Join(_) => best_plan,
@@ -1556,7 +1544,7 @@ pub(crate) mod test {
15561544
"unexpected explain plan: {explain_plan}"
15571545
);
15581546
assert!(
1559-
explain_plan.contains(&format!("IndexScan By {index_name} => Probe $0")),
1547+
explain_plan.contains(&format!("IndexScan By {index_name} => Probe")),
15601548
"unexpected explain plan: {explain_plan}"
15611549
);
15621550
Ok(())
@@ -1727,7 +1715,7 @@ pub(crate) mod test {
17271715
"unexpected explain plan: {explain_plan}"
17281716
);
17291717
assert!(
1730-
explain_plan.contains("IndexScan By exists_inner_v_index => Probe $0"),
1718+
explain_plan.contains("IndexScan By exists_inner_v_index => Probe"),
17311719
"unexpected explain plan: {explain_plan}"
17321720
);
17331721
Ok(())

src/execution/dql/index_scan.rs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,17 @@ use crate::errors::DatabaseError;
1616
use crate::execution::{ExecArena, ExecId, ExecNode, ExecutionCaches, ExecutorNode, ReadExecutor};
1717
use crate::expression::range_detacher::Range;
1818
use crate::planner::operator::table_scan::TableScanOperator;
19-
use crate::storage::{IndexIter, Iter, Transaction};
19+
use crate::storage::{IndexIter, IndexRanges, Iter, Transaction};
2020
use crate::types::index::{IndexLookup, IndexMetaRef, RuntimeIndexProbe};
2121
use crate::types::serialize::TupleValueSerializableImpl;
22-
use std::array;
23-
use std::vec;
24-
25-
enum IndexLookupRanges {
26-
One(array::IntoIter<Range, 1>),
27-
Many(vec::IntoIter<Range>),
28-
}
29-
30-
impl Iterator for IndexLookupRanges {
31-
type Item = Range;
32-
33-
fn next(&mut self) -> Option<Self::Item> {
34-
match self {
35-
IndexLookupRanges::One(iter) => iter.next(),
36-
IndexLookupRanges::Many(iter) => iter.next(),
37-
}
38-
}
39-
}
4022

4123
pub(crate) struct IndexScan<'a, T: Transaction + 'a> {
4224
op: Option<TableScanOperator>,
4325
index_by: IndexMetaRef,
4426
lookup: Option<IndexLookup>,
4527
covered_deserializers: Option<Vec<TupleValueSerializableImpl>>,
4628
cover_mapping: Option<Vec<usize>>,
47-
iter: Option<IndexIter<'a, T, IndexLookupRanges>>,
29+
iter: Option<IndexIter<'a, T>>,
4830
}
4931

5032
impl<'a, T: Transaction + 'a>
@@ -111,23 +93,13 @@ impl<'a, T: Transaction + 'a> ExecutorNode<'a, T> for IndexScan<'a, T> {
11193
}
11294

11395
impl<'a, T: Transaction + 'a> IndexScan<'a, T> {
114-
fn ranges_from_lookup(lookup: IndexLookup, arena: &ExecArena<'a, T>) -> IndexLookupRanges {
96+
fn ranges_from_lookup(lookup: IndexLookup, arena: &mut ExecArena<'a, T>) -> IndexRanges {
11597
match lookup {
116-
IndexLookup::Static(Range::SortedRanges(ranges)) => {
117-
IndexLookupRanges::Many(ranges.into_iter())
118-
}
119-
IndexLookup::Static(range) => IndexLookupRanges::One([range].into_iter()),
120-
IndexLookup::Probe(param) => match arena.runtime_param(param) {
121-
RuntimeIndexProbe::Eq(value) => {
122-
IndexLookupRanges::One([Range::Eq(value.clone())].into_iter())
123-
}
124-
RuntimeIndexProbe::Scope { min, max } => IndexLookupRanges::One(
125-
[Range::Scope {
126-
min: min.clone(),
127-
max: max.clone(),
128-
}]
129-
.into_iter(),
130-
),
98+
IndexLookup::Static(Range::SortedRanges(ranges)) => ranges.into(),
99+
IndexLookup::Static(range) => range.into(),
100+
IndexLookup::Probe => match arena.pop_runtime_probe() {
101+
RuntimeIndexProbe::Eq(value) => Range::Eq(value).into(),
102+
RuntimeIndexProbe::Scope { min, max } => Range::Scope { min, max }.into(),
131103
},
132104
}
133105
}

0 commit comments

Comments
 (0)