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

Fixing off by one error in state_agg interpolate #465

Merged
merged 1 commit into from
Jul 27, 2022
Merged
Changes from all commits
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
41 changes: 40 additions & 1 deletion extension/src/state_aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub mod toolkit_experimental {
durations.push(DurationInState {
duration: start_interval,
state_beg: states.len() as u32,
state_end: (states.len() + start_state.len() - 1) as u32,
state_end: (states.len() + start_state.len()) as u32,
});
states += start_state;
}
Expand Down Expand Up @@ -865,4 +865,43 @@ SELECT toolkit_experimental.duration_in('one', toolkit_experimental.state_agg(ts
);
})
}

#[pg_test]
fn interpolate_introduces_state() {
Spi::execute(|client| {
client.select("CREATE TABLE states(time TIMESTAMPTZ, state TEXT, bucket INT)", None, None);
client.select(
r#"INSERT INTO states VALUES
('1-1-2020 10:00', 'starting', 1),
('1-1-2020 10:30', 'running', 1),
('1-2-2020 16:00', 'error', 2),
('1-3-2020 18:30', 'starting', 3),
('1-3-2020 19:30', 'running', 3),
('1-4-2020 12:00', 'stopping', 4)"#,
None, None);

let mut durations = client.select(
r#"SELECT
toolkit_experimental.interpolated_duration_in(
'running',
agg,
'2019-12-31 0:00'::timestamptz + (bucket * '1 day'::interval), '1 day'::interval,
LAG(agg) OVER (ORDER BY bucket),
LEAD(agg) OVER (ORDER BY bucket)
)::TEXT FROM (
SELECT bucket, toolkit_experimental.state_agg(time, state) as agg
FROM states
GROUP BY bucket
) s
ORDER BY bucket"#,
None,
None,
);

assert_eq!(durations.next().unwrap()[1].value(), Some("13:30:00"));
assert_eq!(durations.next().unwrap()[1].value(), Some("16:00:00"));
assert_eq!(durations.next().unwrap()[1].value(), Some("04:30:00"));
assert_eq!(durations.next().unwrap()[1].value(), Some("12:00:00"));
})
}
}