Skip to content

Commit

Permalink
stabilizing approx percentile array
Browse files Browse the repository at this point in the history
  • Loading branch information
thatzopoulos committed Apr 4, 2023
1 parent 06c1a0b commit 9d65355
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This changelog should be updated as part of a PR if the work is worth noting (mo

#### Stabilized features
- [#741](https://github.com/timescale/timescaledb-toolkit/pull/741): Stabilize `approx_count_distinct`
- [#748](https://github.com/timescale/timescaledb-toolkit/pull/748): Stabilize `approx_percentile_array`

#### Other notable changes
- [#743](https://github.com/timescale/timescaledb-toolkit/pull/743): Remove support for direct upgrades from toolkit versions more than 1 year old. Toolkit versions 1.4.x and 1.5.x will have to upgrade to an intermediate version before upgrading to 1.16.0.
Expand Down
34 changes: 15 additions & 19 deletions extension/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,28 +578,24 @@ pub fn accessor_integral(unit: default!(&str, "'second'")) -> AccessorIntegral<'
}
}
}
#[pg_schema]
pub mod toolkit_experimental {
use super::*;

pg_type! {
#[derive(Debug)]
struct AccessorPercentileArray<'input> {
len: u64,
percentile: [f64; self.len],
}

pg_type! {
#[derive(Debug)]
struct AccessorPercentileArray<'input> {
len: u64,
percentile: [f64; self.len],
}
}

ron_inout_funcs!(AccessorPercentileArray);
ron_inout_funcs!(AccessorPercentileArray);

#[pg_extern(immutable, name = "approx_percentiles")]
pub fn accessor_percentiles(unit: Vec<f64>) -> AccessorPercentileArray<'static> {
unsafe {
flatten! {
AccessorPercentileArray{
len: unit.len().try_into().unwrap(),
percentile: unit.into(),
}
#[pg_extern(immutable, name = "approx_percentiles")]
pub fn accessor_percentiles(unit: Vec<f64>) -> AccessorPercentileArray<'static> {
unsafe {
flatten! {
AccessorPercentileArray{
len: unit.len().try_into().unwrap(),
percentile: unit.into(),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions extension/src/stabilization_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ crate::functions_stabilized_at! {
num_live_ranges(heartbeatagg),
trim_to(heartbeatagg,timestamp with time zone,interval),
trim_to(timestamp with time zone,interval),
accessorpercentilearray_in(cstring),
accessorpercentilearray_out(accessorpercentilearray),
arrow_uddsketch_approx_percentile_array(uddsketch,accessorpercentilearray),
}
"1.15.0" => {
arrow_counter_interpolated_delta(countersummary,counterinterpolateddeltaaccessor),
Expand Down Expand Up @@ -692,6 +695,7 @@ crate::types_stabilized_at! {
accessornumgaps,
accessornumliveranges,
heartbeattrimtoaccessor,
accessorpercentilearray
}
"1.15.0" => {
counterinterpolateddeltaaccessor,
Expand Down Expand Up @@ -821,6 +825,7 @@ crate::operators_stabilized_at! {
"->"(heartbeatagg,accessornumgaps),
"->"(heartbeatagg,accessornumliveranges),
"->"(heartbeatagg,heartbeattrimtoaccessor),
"->"(uddsketch,accessorpercentilearray),
}
"1.15.0" => {
"->"(countersummary,counterinterpolateddeltaaccessor),
Expand Down
7 changes: 7 additions & 0 deletions extension/src/stabilization_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ mod tests {
return None;
}

// Hack to fix the function macro's inability to handle [] in the type double precision[].
if function == "approx_percentile_array(double precision[],uddsketch)"
|| function == "approx_percentiles(double precision[])"
{
return None;
}

return Some(val);
}

Expand Down
20 changes: 8 additions & 12 deletions extension/src/uddsketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use uddsketch::{SketchHashKey, UDDSketch as UddSketchInternal};

use crate::{
accessors::{
toolkit_experimental, AccessorApproxPercentile, AccessorApproxPercentileRank,
AccessorError, AccessorMean, AccessorNumVals,
AccessorApproxPercentile, AccessorApproxPercentileRank, AccessorError, AccessorMean,
AccessorNumVals, AccessorPercentileArray,
},
aggregate_utils::in_aggregate_context,
flatten,
Expand Down Expand Up @@ -585,17 +585,13 @@ pub fn uddsketch_approx_percentile<'a>(percentile: f64, sketch: UddSketch<'a>) -
#[opname(->)]
pub fn arrow_uddsketch_approx_percentile_array<'a>(
sketch: UddSketch<'a>,
percentiles: toolkit_experimental::AccessorPercentileArray<'a>,
percentiles: AccessorPercentileArray<'a>,
) -> Vec<f64> {
approx_percentile_slice(percentiles.percentile.as_slice(), sketch)
}

// Approximate the value at the given approx_percentile (0.0-1.0) for each entry in an array
#[pg_extern(
immutable,
schema = "toolkit_experimental",
name = "approx_percentile_array"
)]
#[pg_extern(immutable, name = "approx_percentile_array")]
pub fn uddsketch_approx_percentile_array<'a>(
percentiles: Vec<f64>,
sketch: UddSketch<'a>,
Expand Down Expand Up @@ -1047,7 +1043,7 @@ mod tests {
let (value, error) = client
.update(
"SELECT \
toolkit_experimental.approx_percentile_array(array[0.9,0.5,0.2], approx), \
approx_percentile_array(array[0.9,0.5,0.2], approx), \
error(approx) \
FROM uddsketch_test",
None,
Expand All @@ -1061,7 +1057,7 @@ mod tests {
let (test_value, test_error) = client
.update(
"SELECT \
toolkit_experimental.approx_percentile_array(array[0.9,0.5,0.2], approx), \
approx_percentile_array(array[0.9,0.5,0.2], approx), \
error(approx) \
FROM percentile_agg",
None,
Expand Down Expand Up @@ -1134,7 +1130,7 @@ mod tests {
let (value, error) = client
.update(
"SELECT \
toolkit_experimental.approx_percentile_array(array[0.9,0.5,0.2], approx), \
approx_percentile_array(array[0.9,0.5,0.2], approx), \
error(approx) \
FROM uddsketch_test",
None,
Expand All @@ -1147,7 +1143,7 @@ mod tests {

let (test_value_arrow, test_error_arrow) = client
.update(
"SELECT approx->toolkit_experimental.approx_percentiles(array[0.9,0.5,0.2]), \
"SELECT approx->approx_percentiles(array[0.9,0.5,0.2]), \
error(approx) \
FROM uddsketch_test",
None,
Expand Down

0 comments on commit 9d65355

Please sign in to comment.