Skip to content

Commit

Permalink
fix panic when no clauses exist
Browse files Browse the repository at this point in the history
  • Loading branch information
amitayh committed Feb 13, 2024
1 parent caeda00 commit 465392e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
38 changes: 13 additions & 25 deletions src/query/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl AggregationState {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum AggregationFunction {
Count,
Min(Rc<str>),
Expand Down Expand Up @@ -196,12 +196,10 @@ mod tests {
fn non_empty() {
let variable = Rc::from("foo");
let min = AggregationFunction::Min(Rc::clone(&variable));
let assignment1 = HashMap::from([(Rc::clone(&variable), Value::I64(1))]);
let assignment2 = HashMap::from([(Rc::clone(&variable), Value::I64(2))]);

let mut state = min.empty_state();
state.consume(&assignment1);
state.consume(&assignment2);
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(1))]));
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(2))]));

assert_eq!(Value::I64(1), state.result());
}
Expand All @@ -220,12 +218,10 @@ mod tests {
fn non_empty() {
let variable = Rc::from("foo");
let max = AggregationFunction::Max(Rc::clone(&variable));
let assignment1 = HashMap::from([(Rc::clone(&variable), Value::I64(1))]);
let assignment2 = HashMap::from([(Rc::clone(&variable), Value::I64(2))]);

let mut state = max.empty_state();
state.consume(&assignment1);
state.consume(&assignment2);
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(1))]));
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(2))]));

assert_eq!(Value::I64(2), state.result());
}
Expand All @@ -244,12 +240,10 @@ mod tests {
fn non_empty() {
let variable = Rc::from("foo");
let average = AggregationFunction::Average(Rc::clone(&variable));
let assignment1 = HashMap::from([(Rc::clone(&variable), Value::I64(1))]);
let assignment2 = HashMap::from([(Rc::clone(&variable), Value::I64(2))]);

let mut state = average.empty_state();
state.consume(&assignment1);
state.consume(&assignment2);
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(1))]));
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(2))]));

assert_eq!(
Value::Decimal(Decimal::from_f64(1.5).unwrap()),
Expand All @@ -271,12 +265,10 @@ mod tests {
fn non_empty() {
let variable = Rc::from("foo");
let sum = AggregationFunction::Sum(Rc::clone(&variable));
let assignment1 = HashMap::from([(Rc::clone(&variable), Value::I64(1))]);
let assignment2 = HashMap::from([(Rc::clone(&variable), Value::I64(2))]);

let mut state = sum.empty_state();
state.consume(&assignment1);
state.consume(&assignment2);
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(1))]));
state.consume(&HashMap::from([(Rc::clone(&variable), Value::I64(2))]));

assert_eq!(Value::I64(3), state.result());
}
Expand All @@ -295,12 +287,10 @@ mod tests {
fn equal_values() {
let variable = Rc::from("foo");
let count_distinct = AggregationFunction::CountDistinct(Rc::clone(&variable));
let assignment1 = HashMap::from([(Rc::clone(&variable), Value::U64(1))]);
let assignment2 = HashMap::from([(Rc::clone(&variable), Value::U64(1))]);

let mut state = count_distinct.empty_state();
state.consume(&assignment1);
state.consume(&assignment2);
state.consume(&HashMap::from([(Rc::clone(&variable), Value::U64(1))]));
state.consume(&HashMap::from([(Rc::clone(&variable), Value::U64(1))]));

assert_eq!(Value::U64(1), state.result());
}
Expand All @@ -309,12 +299,10 @@ mod tests {
fn distinct_values() {
let variable = Rc::from("foo");
let count_distinct = AggregationFunction::CountDistinct(Rc::clone(&variable));
let assignment1 = HashMap::from([(Rc::clone(&variable), Value::U64(1))]);
let assignment2 = HashMap::from([(Rc::clone(&variable), Value::U64(2))]);

let mut state = count_distinct.empty_state();
state.consume(&assignment1);
state.consume(&assignment2);
state.consume(&HashMap::from([(Rc::clone(&variable), Value::U64(1))]));
state.consume(&HashMap::from([(Rc::clone(&variable), Value::U64(2))]));

assert_eq!(Value::U64(2), state.result());
}
Expand Down
2 changes: 2 additions & 0 deletions src/query/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl PartialAssignment {
)
}

/// An assignment is considered "complete" when there are no more unassigned variables.
///
/// ```
/// use std::rc::Rc;
/// use std::collections::HashSet;
Expand Down
3 changes: 2 additions & 1 deletion src/query/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ impl<'a, S: ReadStorage<'a>> Resolver<'a, S> {
}

fn iterator(storage: &'a S, frame: &Frame, clauses: &[Clause], basis_tx: u64) -> S::Iter {
let clause = clauses.get(frame.clause_index);
let restricts = Restricts::from(
&clauses[frame.clause_index],
clause.unwrap_or(&Clause::default()),
&frame.assignment.assigned,
basis_tx,
);
Expand Down

0 comments on commit 465392e

Please sign in to comment.