fix limit/offset logic

main
Ziyang Hu 2 years ago
parent 3d8da482b8
commit dcf78b2f8a

@ -23,9 +23,6 @@ pub(crate) struct QueryLimiter {
} }
impl QueryLimiter { impl QueryLimiter {
pub(crate) fn is_used(&self) -> bool {
self.total.is_some() || self.skip.is_some()
}
pub(crate) fn incr_and_should_stop(&mut self) -> bool { pub(crate) fn incr_and_should_stop(&mut self) -> bool {
if let Some(limit) = self.total { if let Some(limit) = self.total {
self.counter += 1; self.counter += 1;
@ -86,6 +83,8 @@ impl SessionTx {
counter: 0, counter: 0,
}; };
let mut used_limiter = false;
for epoch in 0u32.. { for epoch in 0u32.. {
debug!("epoch {}", epoch); debug!("epoch {}", epoch);
if epoch == 0 { if epoch == 0 {
@ -93,7 +92,7 @@ impl SessionTx {
match compiled_ruleset { match compiled_ruleset {
CompiledRuleSet::Rules(ruleset) => { CompiledRuleSet::Rules(ruleset) => {
let aggr_kind = compiled_ruleset.aggr_kind(); let aggr_kind = compiled_ruleset.aggr_kind();
if self.initial_rule_eval( used_limiter = self.initial_rule_eval(
k, k,
ruleset, ruleset,
aggr_kind, aggr_kind,
@ -101,9 +100,7 @@ impl SessionTx {
&mut changed, &mut changed,
&mut limiter, &mut limiter,
poison.clone(), poison.clone(),
)? { )? || used_limiter;
return Ok(true);
};
} }
CompiledRuleSet::Algo(algo_apply) => { CompiledRuleSet::Algo(algo_apply) => {
self.algo_application_eval(k, algo_apply, stores, poison.clone())?; self.algo_application_eval(k, algo_apply, stores, poison.clone())?;
@ -124,7 +121,7 @@ impl SessionTx {
AggrKind::Normal => false, AggrKind::Normal => false,
AggrKind::Meet => true, AggrKind::Meet => true,
}; };
if self.incremental_rule_eval( used_limiter = self.incremental_rule_eval(
k, k,
ruleset, ruleset,
epoch, epoch,
@ -134,9 +131,7 @@ impl SessionTx {
&mut changed, &mut changed,
&mut limiter, &mut limiter,
poison.clone(), poison.clone(),
)? { )? || used_limiter;
return Ok(true);
};
} }
CompiledRuleSet::Algo(_) => unreachable!(), CompiledRuleSet::Algo(_) => unreachable!(),
@ -147,7 +142,7 @@ impl SessionTx {
break; break;
} }
} }
Ok(limiter.is_used()) Ok(used_limiter)
} }
fn algo_application_eval( fn algo_application_eval(
&self, &self,
@ -172,7 +167,8 @@ impl SessionTx {
) -> Result<bool> { ) -> Result<bool> {
let store = stores.get(rule_symb).unwrap(); let store = stores.get(rule_symb).unwrap();
let use_delta = BTreeSet::default(); let use_delta = BTreeSet::default();
let should_check_limit = limiter.total.is_some() && rule_symb.is_prog_entry(); let should_check_limit =
limiter.total.is_some() && rule_symb.is_prog_entry() && aggr_kind != AggrKind::Meet;
match aggr_kind { match aggr_kind {
AggrKind::None | AggrKind::Meet => { AggrKind::None | AggrKind::Meet => {
let is_meet = aggr_kind == AggrKind::Meet; let is_meet = aggr_kind == AggrKind::Meet;
@ -234,7 +230,7 @@ impl SessionTx {
} }
} }
} }
Ok(false) Ok(should_check_limit)
} }
fn incremental_rule_eval( fn incremental_rule_eval(
&self, &self,
@ -249,7 +245,8 @@ impl SessionTx {
poison: Poison, poison: Poison,
) -> Result<bool> { ) -> Result<bool> {
let store = stores.get(rule_symb).unwrap(); let store = stores.get(rule_symb).unwrap();
let should_check_limit = limiter.total.is_some() && rule_symb.is_prog_entry(); let should_check_limit =
limiter.total.is_some() && rule_symb.is_prog_entry() && !is_meet_aggr;
for (rule_n, rule) in ruleset.iter().enumerate() { for (rule_n, rule) in ruleset.iter().enumerate() {
let mut should_do_calculation = false; let mut should_do_calculation = false;
for d_rule in &rule.contained_rules { for d_rule in &rule.contained_rules {
@ -314,6 +311,6 @@ impl SessionTx {
} }
} }
} }
Ok(false) Ok(should_check_limit)
} }
} }

@ -619,8 +619,7 @@ impl Db {
} }
} else { } else {
let scan = if early_return { let scan = if early_return {
let limit = input_program.out_opts.limit.unwrap_or(usize::MAX); Right(Left(result.scan_early_returned()))
Right(Left(result.scan_early_returned().take(limit)))
} else if input_program.out_opts.limit.is_some() } else if input_program.out_opts.limit.is_some()
|| input_program.out_opts.offset.is_some() || input_program.out_opts.offset.is_some()
{ {

Loading…
Cancel
Save