Skip to content

Commit

Permalink
Allow approx_count accessor to take NULL inputs
Browse files Browse the repository at this point in the history
Fixes #567

The `approx_count` function previously unwrapped it's aggregate
argument without handling the NULL case. With this change, it will
instead return NULL when given a NULL for the CountMinSketch.
  • Loading branch information
rtwalker committed Oct 7, 2022
1 parent 5faf4e3 commit 3fbc005
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions extension/src/countminsketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ impl toolkit_experimental::count_min_sketch {
}

#[pg_extern(immutable, parallel_safe, schema = "toolkit_experimental")]
pub fn approx_count(item: String, aggregate: Option<CountMinSketch>) -> i64 {
let sketch = aggregate.unwrap();
CountMinSketch::to_internal_countminsketch(&sketch).estimate(item)
pub fn approx_count(item: String, aggregate: Option<CountMinSketch>) -> Option<i64> {
aggregate.map(|sketch| CountMinSketch::to_internal_countminsketch(&sketch).estimate(item))
}

#[cfg(any(test, feature = "pg_test"))]
Expand Down Expand Up @@ -266,4 +265,19 @@ mod tests {
assert_eq!(output, None)
})
}

#[pg_test]
fn test_approx_count_null_input_yields_null_output() {
Spi::execute(|client| {
let output = client
.select(
"SELECT toolkit_experimental.approx_count('1'::text, NULL::toolkit_experimental.countminsketch)",
None,
None,
)
.first()
.get_one::<String>();
assert_eq!(output, None)
})
}
}

0 comments on commit 3fbc005

Please sign in to comment.