Skip to content

Commit

Permalink
addressing review feedback and semantic suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Rowe committed Nov 28, 2022
1 parent b6c3c36 commit 77c8a42
Showing 1 changed file with 9 additions and 43 deletions.
52 changes: 9 additions & 43 deletions extension/src/time_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,19 @@ CREATE AGGREGATE rollup(\n\
pub mod toolkit_experimental {
use super::*;

#[pg_extern(immutable, parallel_safe)]
// Only making this available through the arrow operator right now, as the semantics are cleaner that way
pub fn asof_join<'a, 'b>(
from: Timevector_TSTZ_F64<'a>,
into: Timevector_TSTZ_F64<'b>,
) -> TableIterator<
'a,
(
name!(time, crate::raw::TimestampTz),
name!(value1, Option<f64>),
name!(value2, f64),
name!(time, crate::raw::TimestampTz),
),
> {
assert!(from.num_points > 0 && into.num_points > 0, "both timevectors must be populated for an asof join");
let mut from = from
.into_iter()
.map(|points| (points.ts.into(), points.val))
Expand All @@ -395,7 +396,7 @@ pub mod toolkit_experimental {
for (into_time, into_val) in into {
// Handle case where into starts before from
if into_time < from_time {
results.push((crate::raw::TimestampTz::from(into_time), None, into_val));
results.push((None, into_val, crate::raw::TimestampTz::from(into_time)));
continue;
}

Expand All @@ -407,9 +408,9 @@ pub mod toolkit_experimental {
}

results.push((
crate::raw::TimestampTz::from(into_time),
Some(from_val),
into_val,
crate::raw::TimestampTz::from(into_time),
));
}

Expand Down Expand Up @@ -445,9 +446,9 @@ pub fn arrow_timevector_asof<'a>(
) -> TableIterator<
'a,
(
name!(time, crate::raw::TimestampTz),
name!(value1, Option<f64>),
name!(value2, f64),
name!(time, crate::raw::TimestampTz),
),
> {
toolkit_experimental::asof_join(series, accessor.into.clone().into())
Expand Down Expand Up @@ -735,41 +736,6 @@ mod tests {
Spi::execute(|client| {
client.select("SET timezone TO 'UTC'", None, None);

let mut result = client.select(
"WITH s as (
SELECT timevector(time, value) AS v1 FROM
(VALUES
('2022-10-1 1:00 UTC'::TIMESTAMPTZ, 20.0),
('2022-10-1 2:00 UTC'::TIMESTAMPTZ, 30.0),
('2022-10-1 3:00 UTC'::TIMESTAMPTZ, 40.0)
) as v(time, value)),
t as (
SELECT timevector(time, value) AS v2 FROM
(VALUES
('2022-10-1 0:30 UTC'::TIMESTAMPTZ, 15.0),
('2022-10-1 2:00 UTC'::TIMESTAMPTZ, 45.0),
('2022-10-1 3:30 UTC'::TIMESTAMPTZ, 60.0)
) as v(time, value))
SELECT toolkit_experimental.asof_join(v1, v2)::TEXT
FROM s, t;",
None,
None,
);

assert_eq!(
result.next().unwrap()[1].value(),
Some("(\"2022-10-01 00:30:00+00\",,15)")
);
assert_eq!(
result.next().unwrap()[1].value(),
Some("(\"2022-10-01 02:00:00+00\",30,45)")
);
assert_eq!(
result.next().unwrap()[1].value(),
Some("(\"2022-10-01 03:30:00+00\",40,60)")
);
assert!(result.next().is_none());

let mut result = client.select(
"WITH s as (
SELECT timevector(time, value) AS v1 FROM
Expand All @@ -793,15 +759,15 @@ mod tests {

assert_eq!(
result.next().unwrap()[1].value(),
Some("(\"2022-10-01 00:30:00+00\",,15)")
Some("(,15,\"2022-10-01 00:30:00+00\")")
);
assert_eq!(
result.next().unwrap()[1].value(),
Some("(\"2022-10-01 02:00:00+00\",30,45)")
Some("(30,45,\"2022-10-01 02:00:00+00\")")
);
assert_eq!(
result.next().unwrap()[1].value(),
Some("(\"2022-10-01 03:30:00+00\",40,60)")
Some("(40,60,\"2022-10-01 03:30:00+00\")")
);
assert!(result.next().is_none());
})
Expand Down

0 comments on commit 77c8a42

Please sign in to comment.