Skip to content

Commit e9077d1

Browse files
committed
store: use inline FILTER comment, do not group by timestamp
1 parent 1a02089 commit e9077d1

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

store/postgres/src/relational/rollup.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub(crate) struct Rollup {
238238

239239
/// The SQL query that loads the current, partially filled bucket.
240240
///
241-
/// Contains a `--FILTERS;` comment that can be replaced with additional filters like `and c.block$ <= $1`.
241+
/// Contains a `/*FILTERS*/` comment that can be replaced with additional filters like `and c.block$ <= $1`.
242242
/// The filters are applied to the SQL query that loads the time series entities, not to the aggregated values.
243243
pub(crate) select_current_sql: String,
244244
}
@@ -549,7 +549,7 @@ impl<'a> RollupSql<'a> {
549549

550550
/// Generates the SQL query that loads the current, partially filled bucket.
551551
///
552-
/// The generated SQL query contains a `--FILTERS;` comment that can be replaced
552+
/// The generated SQL query contains a `/*FILTERS*/` comment that can be replaced
553553
/// with additional filters like `and c.block$ <= $1`. The filters are applied to
554554
/// the SQL query that loads the time series entities, not to the aggregated values.
555555
fn select_current(&self, w: &mut dyn fmt::Write) -> fmt::Result {
@@ -658,7 +658,7 @@ impl<'a> RollupSql<'a> {
658658
/// {dimensions},
659659
/// {aggregates}
660660
/// from
661-
/// ({select timeseries entities} where timestamp >= {last rollup timestamp} {--FILTERS;})
661+
/// ({select timeseries entities} where timestamp >= {last rollup timestamp} {/*FILTERS*/})
662662
/// group by
663663
/// {dimensions};
664664
fn select_current_bucket(&self, w: &mut dyn fmt::Write) -> fmt::Result {
@@ -694,9 +694,11 @@ impl<'a> RollupSql<'a> {
694694
src_table = self.src_table,
695695
last_rollup = self.last_rollup(),
696696
)?;
697-
write!(w, " --FILTERS;) c")?;
698-
write!(w, " group by timestamp")?;
699-
write_dims(self.dimensions, w, true)?;
697+
write!(w, " /*FILTERS*/) c")?;
698+
if !self.dimensions.is_empty() {
699+
write!(w, " group by ")?;
700+
write_dims(self.dimensions, w, false)?;
701+
}
700702
Ok(())
701703
}
702704
}
@@ -894,8 +896,8 @@ mod tests {
894896
"token", "amount", "price" \
895897
from "sgd007"."data" as c \
896898
where c.timestamp >= coalesce((select max(timestamp) + '3601 s'::interval as last_rollup \
897-
from "sgd007"."stats_hour"), '-infinity'::timestamptz) --FILTERS;) c \
898-
group by timestamp, "token""#;
899+
from "sgd007"."stats_hour"), '-infinity'::timestamptz) /*FILTERS*/) c \
900+
group by "token""#;
899901

900902
const STATS_DAY_CURRENT_SQL: &str = r#"\
901903
select max(id) as id, max(vid) as vid, max(block$) as block$, max(timestamp) as timestamp, \
@@ -904,8 +906,8 @@ mod tests {
904906
"token", "amount", "price" \
905907
from "sgd007"."data" as c \
906908
where c.timestamp >= coalesce((select max(timestamp) + '86401 s'::interval as last_rollup \
907-
from "sgd007"."stats_day"), '-infinity'::timestamptz) --FILTERS;) c \
908-
group by timestamp, "token""#;
909+
from "sgd007"."stats_day"), '-infinity'::timestamptz) /*FILTERS*/) c \
910+
group by "token""#;
909911

910912
const TOTAL_CURRENT_SQL: &str = r#"\
911913
select max(id) as id, max(vid) as vid, max(block$) as block$, max(timestamp) as timestamp, \
@@ -914,8 +916,7 @@ mod tests {
914916
"amount", "price" \
915917
from "sgd007"."data" as c \
916918
where c.timestamp >= coalesce((select max(timestamp) + '86401 s'::interval as last_rollup \
917-
from "sgd007"."total_stats_day"), '-infinity'::timestamptz) --FILTERS;) c \
918-
group by timestamp"#;
919+
from "sgd007"."total_stats_day"), '-infinity'::timestamptz) /*FILTERS*/) c"#;
919920

920921
const OPEN_CLOSE_CURRENT_SQL: &str = r#"\
921922
select max(id) as id, max(vid) as vid, max(block$) as block$, max(timestamp) as timestamp, \
@@ -926,8 +927,7 @@ mod tests {
926927
"amount", "price" \
927928
from "sgd007"."data" as c \
928929
where c.timestamp >= coalesce((select max(timestamp) + '86401 s'::interval as last_rollup \
929-
from "sgd007"."open_close_day"), '-infinity'::timestamptz) --FILTERS;) c \
930-
group by timestamp"#;
930+
from "sgd007"."open_close_day"), '-infinity'::timestamptz) /*FILTERS*/) c"#;
931931

932932
const LIFETIME_CURRENT_SQL: &str = r#"
933933
with bucket as (select max(id) as id, max(vid) as vid, max(block$) as block$,
@@ -940,8 +940,7 @@ mod tests {
940940
from "sgd007"."data" as c
941941
where c.timestamp >= coalesce((select max(timestamp) + '86401 s'::interval
942942
as last_rollup from "sgd007"."lifetime_day"),
943-
'-infinity'::timestamptz) --FILTERS;) c
944-
group by timestamp),
943+
'-infinity'::timestamptz) /*FILTERS*/) c),
945944
prev as (select bucket.id, bucket.vid, bucket.block$, bucket.timestamp,
946945
null::int8 as "count", null::numeric as "sum",
947946
prev."total_count", prev."total_sum"
@@ -969,8 +968,7 @@ mod tests {
969968
from (select id, vid, block$, date_bin('86400s', timestamp, 'epoch'::timestamptz) as timestamp \
970969
from "sgd007"."data" as c \
971970
where c.timestamp >= coalesce((select max(timestamp) + '86401 s'::interval as last_rollup \
972-
from "sgd007"."count_only_day"), '-infinity'::timestamptz) --FILTERS;) c \
973-
group by timestamp"#;
971+
from "sgd007"."count_only_day"), '-infinity'::timestamptz) /*FILTERS*/) c"#;
974972

975973
#[track_caller]
976974
fn rollup_for<'a>(layout: &'a Layout, table_name: &str) -> &'a Rollup {

store/postgres/src/relational_queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,7 +4451,7 @@ impl<'a> FilterQuery<'a> {
44514451
out.push_sql(") union all (select ");
44524452
write_column_names(&wh.column_names, wh.table, Some("c."), out)?;
44534453
out.push_sql(" from (");
4454-
let mut current_sql_split = rollup.select_current_sql.split("--FILTERS;");
4454+
let mut current_sql_split = rollup.select_current_sql.split("/*FILTERS*/");
44554455
out.push_sql(current_sql_split.next().unwrap());
44564456
out.push_sql(" and ");
44574457
wh.at_block.walk_ast(out.reborrow())?;
@@ -4534,7 +4534,7 @@ impl<'a> FilterQuery<'a> {
45344534
out.push_sql(PARENT_ID);
45354535
out.push_sql(" from (");
45364536

4537-
let mut current_sql_split = rollup.select_current_sql.split("--FILTERS;");
4537+
let mut current_sql_split = rollup.select_current_sql.split("/*FILTERS*/");
45384538
out.push_sql(current_sql_split.next().unwrap());
45394539
out.push_sql(" and ");
45404540
window.at_block.walk_ast(out.reborrow())?;

0 commit comments

Comments
 (0)