pub struct IntervalSum { /* private fields */ }Expand description
The running state of PostgreSQL’s sum(interval) and avg(interval).
The finite values are summed exactly per field and the infinities are counted, as PostgreSQL counts them. PostgreSQL adds in scan order and raises on a partial sum leaving a field’s range, so the same rows can sum or raise by the order they are read in. Whenever a field’s net total fits, some order of the rows never leaves the range, and its answer is that net total, which is the one reported here.
The counts are signed so that a row leaving is the same type as one arriving, and folding either in is componentwise addition.
use sql_values::postgres::{IntervalSum, PgInterval, parse_interval};
let mut total = IntervalSum::default();
for text in ["1 mon", "2 days", "-01:00:00"] {
total.merge(IntervalSum::of(parse_interval(text).unwrap(), 1));
}
assert_eq!(total.sum().unwrap().unwrap().to_string(), "1 mon 2 days -01:00:00");
assert_eq!(total.avg().unwrap().unwrap().to_string(), "10 days 15:40:00");
total.merge(IntervalSum::of(PgInterval::INFINITY, 1));
assert_eq!(total.avg(), Ok(Some(PgInterval::INFINITY)));Implementations§
Source§impl IntervalSum
impl IntervalSum
Sourcepub fn of(value: PgInterval, weight: i64) -> Self
pub fn of(value: PgInterval, weight: i64) -> Self
One row’s interval contributing with weight, 1 for a row arriving and -1 for one
leaving.
Sourcepub fn seeded(
finite_sum: Option<PgInterval>,
finite: i64,
positive_infinities: i64,
negative_infinities: i64,
) -> Self
pub fn seeded( finite_sum: Option<PgInterval>, finite: i64, positive_infinities: i64, negative_infinities: i64, ) -> Self
The state a database reports for some rows: the sum of the finite ones, how many there are, and how many are each infinity.
use sql_values::postgres::{IntervalSum, parse_interval};
let mut state = IntervalSum::seeded(Some(parse_interval("3 days").unwrap()), 2, 0, 0);
assert_eq!(state.count(), 2);
state.merge(IntervalSum::of(parse_interval("3 days").unwrap(), 1));
assert_eq!(state.avg().unwrap().unwrap().to_string(), "2 days");Sourcepub fn sum(&self) -> Result<Option<PgInterval>, IntervalError>
pub fn sum(&self) -> Result<Option<PgInterval>, IntervalError>
PostgreSQL’s interval_sum, None over no rows.
§Errors
IntervalError::OutOfRange for infinities of both signs and for a field whose net total
leaves its range.
Sourcepub fn avg(&self) -> Result<Option<PgInterval>, IntervalError>
pub fn avg(&self) -> Result<Option<PgInterval>, IntervalError>
PostgreSQL’s interval_avg, the sum divided by the count of finite rows as
interval_div divides, None over no rows.
§Errors
The errors of sum, and IntervalError::OutOfRange for a quotient beyond
the type.