Skip to content

Commit

Permalink
rove_connector: handle interval in fetch_one as a query param
Browse files Browse the repository at this point in the history
  • Loading branch information
intarga committed Dec 6, 2024
1 parent afe1e72 commit 01c6fbe
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions rove_connector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,30 @@ impl Connector {
let (start_time, end_time) =
extract_time_spec(time_spec, num_leading_points, num_trailing_points)?;

// TODO: should this contain an ORDER BY? Actually I think it's not necessary since the
// order is dictated by the generated sequence
// TODO: should we drop ts_rule.timestamp from the SELECT? we don't seem to use it
// TODO: should we make this like the fetch_all query and regularize outside the query?
// I think this query might perform badly because the join against the generated series
// doesn't use the index optimally. Doing this would also save us the "interval" mess
let query_string = format!("SELECT data.obsvalue, ts_rule.timestamp \
FROM (SELECT data.obsvalue, data.obstime FROM data WHERE data.timeseries = $1) as data
RIGHT JOIN generate_series($2::timestamptz, $3::timestamptz, interval '{}') AS ts_rule(timestamp) \
ON data.obstime = ts_rule.timestamp", interval);

let conn = self
.pool
.get()
.await
.map_err(|e| data_switch::Error::Other(Box::new(e)))?;

// TODO: should this contain an ORDER BY? Actually I think it's not necessary since the
// order is dictated by the generated sequence
// TODO: should we drop ts_rule.timestamp from the SELECT? we don't seem to use it
// TODO: should we make this like the fetch_all query and regularize outside the query?
// I think this query might perform badly because the join against the generated series
// doesn't use the index optimally. Doing this would also save us the "interval" mess
let data_results = conn
.query(query_string.as_str(), &[&ts_id, &start_time, &end_time])
.query(
"
SELECT data.obsvalue, ts_rule.timestamp \
FROM ( \
SELECT data.obsvalue, data.obstime \
FROM data \
WHERE data.timeseries = $1 \
) as data \
RIGHT JOIN generate_series($2::timestamptz, $3::timestamptz, ($4::text)::interval) AS ts_rule(timestamp) \
ON data.obstime = ts_rule.timestamp
", &[&ts_id, &start_time, &end_time, &interval])
.await
.map_err(|e| data_switch::Error::Other(Box::new(e)))?;

Expand Down

0 comments on commit 01c6fbe

Please sign in to comment.