Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create accessors to first/last data points #513

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ accessor! { num_elements() }
accessor! { num_changes() }
accessor! { num_resets() }
accessor! { counter_zero_time() }
accessor! { first_val() }
accessor! { last_val() }
accessor! { first_time() }
accessor! { last_time() }


// The rest are more complex, with String or other challenges. Leaving alone for now.
Expand Down
187 changes: 187 additions & 0 deletions extension/src/counter_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
AccessorExtrapolatedRate, AccessorIdeltaLeft, AccessorIdeltaRight, AccessorIntercept,
AccessorIrateLeft, AccessorIrateRight, AccessorNumChanges, AccessorNumElements,
AccessorNumResets, AccessorRate, AccessorSlope, AccessorTimeDelta, AccessorWithBounds,
AccessorFirstTime, AccessorFirstVal, AccessorLastTime, AccessorLastVal,
rtwalker marked this conversation as resolved.
Show resolved Hide resolved
rtwalker marked this conversation as resolved.
Show resolved Hide resolved
},
aggregate_utils::in_aggregate_context,
flatten,
Expand Down Expand Up @@ -774,6 +775,70 @@ fn counter_agg_counter_zero_time(
Some(((summary.to_internal_counter_summary().stats.x_intercept()? * 1_000_000.0) as i64).into())
}

#[pg_operator(immutable, parallel_safe)]
#[opname(->)]
pub fn arrow_counter_agg_first_val(
sketch: CounterSummary,
_accessor: AccessorFirstVal,
) -> f64 {
counter_agg_first_val(sketch)
}

#[pg_extern(name="first_val", strict, immutable, parallel_safe)]
fn counter_agg_first_val(
summary: CounterSummary,
)-> f64 {
summary.to_internal_counter_summary().first.val
}

#[pg_operator(immutable, parallel_safe)]
#[opname(->)]
pub fn arrow_counter_agg_last_val(
sketch: CounterSummary,
_accessor: AccessorLastVal,
) -> f64 {
counter_agg_last_val(sketch)
}

#[pg_extern(name="last_val", strict, immutable, parallel_safe)]
fn counter_agg_last_val(
summary: CounterSummary,
)-> f64 {
summary.to_internal_counter_summary().last.val
}

#[pg_operator(immutable, parallel_safe)]
#[opname(->)]
pub fn arrow_counter_agg_first_time(
sketch: CounterSummary,
_accessor: AccessorFirstTime,
) -> crate::raw::TimestampTz {
counter_agg_first_time(sketch)
}

#[pg_extern(name="first_time", strict, immutable, parallel_safe)]
fn counter_agg_first_time(
summary: CounterSummary,
)-> crate::raw::TimestampTz {
summary.to_internal_counter_summary().first.ts.into()
}

#[pg_operator(immutable, parallel_safe)]
#[opname(->)]
pub fn arrow_counter_agg_last_time(
sketch: CounterSummary,
_accessor: AccessorLastTime,
) -> crate::raw::TimestampTz {
counter_agg_last_time(sketch)
}

#[pg_extern(name="last_time", strict, immutable, parallel_safe)]
fn counter_agg_last_time(
summary: CounterSummary,
)-> crate::raw::TimestampTz {
summary.to_internal_counter_summary().last.ts.into()
}

#[derive(Clone, Copy)]
pub enum Method {
Prometheus,
Expand Down Expand Up @@ -1353,6 +1418,128 @@ mod tests {
});
}

#[pg_test]
fn first_and_last_val() {
Spi::execute(|client| {
make_test_table(&client, "test");

assert_relative_eq!(
select_one!(
client,
"SELECT \
first_val(counter_agg(ts, val)) \
FROM test",
f64
),
10.0,
);

assert_relative_eq!(
select_one!(
client,
"SELECT \
last_val(counter_agg(ts, val)) \
FROM test",
f64
),
20.0,
);
});
}

#[pg_test]
fn first_and_last_val_arrow_match() {
Spi::execute(|client| {
make_test_table(&client, "test");

assert_relative_eq!(
select_and_check_one!(
client,
"SELECT \
first_val(counter_agg(ts, val)), \
counter_agg(ts, val) -> first_val() \
FROM test",
f64
),
10.0,
);

assert_relative_eq!(
select_and_check_one!(
client,
"SELECT \
last_val(counter_agg(ts, val)), \
counter_agg(ts, val) -> last_val() \
FROM test",
f64
),
20.0,
);
});
}

#[pg_test]
fn first_and_last_time() {
Spi::execute(|client| {
make_test_table(&client, "test");
client.select("SET TIME ZONE 'UTC'", None, None);

assert_eq!(
select_one!(
client,
"SELECT \
first_time(counter_agg(ts, val))::text \
FROM test",
&str
),
"2020-01-01 00:00:00+00",
);

assert_eq!(
select_one!(
client,
"SELECT \
last_time(counter_agg(ts, val))::text \
FROM test",
&str
),
"2020-01-01 00:01:00+00",
);
});
}

#[pg_test]
fn first_and_last_time_arrow_match() {
Spi::execute(|client| {
make_test_table(&client, "test");
client.select("SET TIME ZONE 'UTC'", None, None);

assert_eq!(
select_and_check_one!(
client,
"SELECT \
first_time(counter_agg(ts, val))::text, \
(counter_agg(ts, val) -> first_time())::text \
FROM test",
&str
),
"2020-01-01 00:00:00+00",
);

assert_eq!(
select_and_check_one!(
client,
"SELECT \
last_time(counter_agg(ts, val))::text, \
(counter_agg(ts, val) -> last_time())::text \
FROM test",
&str
),
"2020-01-01 00:01:00+00",
);
});
}

// #[pg_test]
// fn test_combine_aggregate(){
// Spi::execute(|client| {
Expand Down
46 changes: 46 additions & 0 deletions extension/src/stabilization_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@

crate::functions_stabilized_at! {
STABLE_FUNCTIONS
"1.11.0" => {
accessorfirsttime_in(cstring),
accessorfirsttime_out(accessorfirsttime),
accessorfirstval_in(cstring),
accessorfirstval_out(accessorfirstval),
accessorlasttime_in(cstring),
accessorlasttime_out(accessorlasttime),
accessorlastval_in(cstring),
accessorlastval_out(accessorlastval),
arrow_counter_agg_first_time(countersummary,accessorfirsttime),
arrow_counter_agg_first_val(countersummary,accessorfirstval),
arrow_counter_agg_last_time(countersummary,accessorlasttime),
arrow_counter_agg_last_val(countersummary,accessorlastval),
arrow_time_weight_first_time(timeweightsummary,accessorfirsttime),
arrow_time_weight_first_val(timeweightsummary,accessorfirstval),
arrow_time_weight_last_time(timeweightsummary,accessorlasttime),
arrow_time_weight_last_val(timeweightsummary,accessorlastval),
first_time(),
first_time(countersummary),
first_time(timeweightsummary),
first_val(),
first_val(countersummary),
first_val(timeweightsummary),
last_time(),
last_time(countersummary),
last_time(timeweightsummary),
last_val(),
last_val(countersummary),
last_val(timeweightsummary),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It really is interesting to see how much a simple feature costs.

}
"1.9.0" => {
accessorapproxpercentile_in(cstring),
accessorapproxpercentile_out(accessorapproxpercentile),
Expand Down Expand Up @@ -387,6 +417,12 @@ crate::functions_stabilized_at! {

crate::types_stabilized_at! {
STABLE_TYPES
"1.11.0" => {
accessorfirsttime,
accessorfirstval,
accessorlasttime,
accessorlastval,
}
"1.9.0" => {
accessorapproxpercentile,
accessorapproxpercentilerank,
Expand Down Expand Up @@ -459,6 +495,16 @@ crate::types_stabilized_at! {

crate::operators_stabilized_at! {
STABLE_OPERATORS
"1.11.0" => {
"->"(countersummary,accessorfirsttime),
"->"(countersummary,accessorfirstval),
"->"(countersummary,accessorlasttime),
"->"(countersummary,accessorlastval),
"->"(timeweightsummary,accessorfirsttime),
"->"(timeweightsummary,accessorfirstval),
"->"(timeweightsummary,accessorlasttime),
"->"(timeweightsummary,accessorlastval),
}
"1.9.0" => {
"->"(countersummary,accessorcorr),
"->"(countersummary,accessorcounterzerotime),
Expand Down
Loading