Skip to content

Commit

Permalink
Stabilize LTTB
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Rowe committed Aug 12, 2022
1 parent 9a6109a commit ef9f926
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
10 changes: 5 additions & 5 deletions docs/lttb.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ to 34 points
```SQL
SELECT time, value::numeric(10,2)
FROM unnest((
SELECT toolkit_experimental.lttb(time, val, 34)
SELECT lttb(time, val, 34)
FROM sample_data))
```

Expand Down Expand Up @@ -102,7 +102,7 @@ resulting data looks less and less like the original
```SQL
SELECT time, value::numeric(10,2)
FROM unnest((
SELECT toolkit_experimental.lttb(time, val, 17)
SELECT lttb(time, val, 17)
FROM sample_data))
```
```output
Expand Down Expand Up @@ -133,7 +133,7 @@ FROM unnest((
```SQL
SELECT time, value::numeric(10,2)
FROM unnest((
SELECT toolkit_experimental.lttb(time, val, 8)
SELECT lttb(time, val, 8)
FROM sample_data))
```
```output
Expand All @@ -157,7 +157,7 @@ FROM unnest((
---
## **lttb** <a id="lttb"></a>
```SQL,ignore
toolkit_experimental.lttb(
lttb(
time TIMESTAMPTZ,
value DOUBLE PRECISION,
resolution INTEGER
Expand All @@ -181,7 +181,7 @@ extract the `(time, value)` pairs from this series
```SQL
SELECT time, value
FROM unnest((
SELECT toolkit_experimental.lttb(time, val, 4)
SELECT lttb(time, val, 4)
FROM sample_data))
```
```output
Expand Down
44 changes: 36 additions & 8 deletions extension/src/lttb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct LttbTrans {
resolution: usize,
}

#[pg_extern(schema = "toolkit_experimental", immutable, parallel_safe)]
#[pg_extern(immutable, parallel_safe)]
pub fn lttb_trans(
state: Internal,
time: crate::raw::TimestampTz,
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn lttb_trans_inner(
}
}

#[pg_extern(schema = "toolkit_experimental", immutable, parallel_safe)]
#[pg_extern(immutable, parallel_safe)]
pub fn lttb_final(
state: Internal,
fcinfo: pg_sys::FunctionCallInfo,
Expand Down Expand Up @@ -96,10 +96,10 @@ pub fn lttb_final_inner(
}

extension_sql!("\n\
CREATE AGGREGATE toolkit_experimental.lttb(ts TIMESTAMPTZ, value DOUBLE PRECISION, resolution integer) (\n\
sfunc = toolkit_experimental.lttb_trans,\n\
CREATE AGGREGATE lttb(ts TIMESTAMPTZ, value DOUBLE PRECISION, resolution integer) (\n\
sfunc = lttb_trans,\n\
stype = internal,\n\
finalfunc = toolkit_experimental.lttb_final\n\
finalfunc = lttb_final\n\
);\n\
",
name = "lttb_agg",
Expand Down Expand Up @@ -183,7 +183,6 @@ pub fn lttb(data: &[TSPoint], threshold: usize) -> Cow<'_, [TSPoint]> {

#[pg_extern(
name = "lttb",
schema = "toolkit_experimental",
immutable,
parallel_safe
)]
Expand Down Expand Up @@ -315,7 +314,7 @@ mod tests {
"INSERT INTO results1
SELECT time, value
FROM unnest(
(SELECT toolkit_experimental.lttb(time, value, 100) FROM test)
(SELECT lttb(time, value, 100) FROM test)
);",
None,
None,
Expand All @@ -330,7 +329,7 @@ mod tests {
"INSERT INTO results2
SELECT time, value
FROM unnest(
(SELECT toolkit_experimental.lttb(
(SELECT lttb(
(SELECT timevector(time, value) FROM test), 100)
)
);",
Expand All @@ -345,4 +344,33 @@ mod tests {
assert_eq!(delta.unwrap(), 0);
})
}

#[pg_test]
fn test_lttb_result() {
Spi::execute(|client| {
client.select("SET timezone TO 'UTC'", None, None);
let mut result = client.select(
r#"SELECT unnest(lttb(ts, val, 5))::TEXT
FROM (VALUES
('2020-1-1'::timestamptz, 10),
('2020-1-2'::timestamptz, 21),
('2020-1-3'::timestamptz, 19),
('2020-1-4'::timestamptz, 32),
('2020-1-5'::timestamptz, 12),
('2020-1-6'::timestamptz, 14),
('2020-1-7'::timestamptz, 18),
('2020-1-8'::timestamptz, 29),
('2020-1-9'::timestamptz, 23),
('2020-1-10'::timestamptz, 27),
('2020-1-11'::timestamptz, 14)
) AS v(ts, val)"#, None, None);

assert_eq!(result.next().unwrap()[1].value(), Some("(\"2020-01-01 00:00:00+00\",10)"));
assert_eq!(result.next().unwrap()[1].value(), Some("(\"2020-01-04 00:00:00+00\",32)"));
assert_eq!(result.next().unwrap()[1].value(), Some("(\"2020-01-05 00:00:00+00\",12)"));
assert_eq!(result.next().unwrap()[1].value(), Some("(\"2020-01-08 00:00:00+00\",29)"));
assert_eq!(result.next().unwrap()[1].value(), Some("(\"2020-01-11 00:00:00+00\",14)"));
assert!(result.next().is_none());
})
}
}
4 changes: 4 additions & 0 deletions extension/src/stabilization_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ crate::functions_stabilized_at! {
timevector_tstz_f64_trans(internal,timestamp with time zone,double precision),
unnest(timevector_tstz_f64),
unnest(),
lttb(timestamp with time zone,double precision,integer),
lttb(timevector_tstz_f64,integer),
lttb_final(internal),
lttb_trans(internal,timestamp with time zone,double precision,integer),
}
"1.8.0" => {
}
Expand Down

0 comments on commit ef9f926

Please sign in to comment.