You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
current: counts api returns the date binned result for each distinct value
for the field(s) present in group by
if the field used is high cardinal, the response will be bloated
Prism uses this response to build a chart that might crash in render
change: add optional topk param to group by
default to 10
api returns topk results for the group by field(s)
let f = get_filter_string(conditions).map_err(QueryError::CustomError)?;
676
-
format!(
677
-
"SELECT {date_bin}{group_clause}, COUNT(*) as count FROM \"{table_name}\" WHERE {} GROUP BY {end_time_col_name},{start_time_col_name}{group_clause} ORDER BY {end_time_col_name}{group_clause}",
678
-
f
679
-
)
686
+
format!(" WHERE {f}")
680
687
}else{
681
-
format!(
682
-
"SELECT {date_bin}{group_clause}, COUNT(*) as count FROM \"{table_name}\" GROUP BY {end_time_col_name},{start_time_col_name}{group_clause} ORDER BY {end_time_col_name}{group_clause}",
683
-
)
688
+
String::default()
684
689
};
690
+
691
+
let query = format!(
692
+
"SELECT {date_bin}{group_clause}, COUNT(*) as count FROM {table_ref}{where_clause} GROUP BY {end_time_col_name},{start_time_col_name}{group_clause} ORDER BY {end_time_col_name}{group_clause}",
693
+
);
694
+
695
+
if group_by_cols.is_empty(){
696
+
returnOk(query);
697
+
}
698
+
699
+
let top_k = count_conditions.top_k.unwrap_or(DEFAULT_COUNTS_TOP_K);
700
+
701
+
if top_k == 0{
702
+
returnErr(QueryError::CustomError(
703
+
"topK must be greater than 0".to_string(),
704
+
));
705
+
}
706
+
707
+
let top_group_cols = group_by_cols.join(", ");
708
+
let top_group_join = group_by_cols
709
+
.iter()
710
+
.map(|col| format!("(gc.{col} = tg.{col} OR (gc.{col} IS NULL AND tg.{col} IS NULL))"))
711
+
.join(" AND ");
712
+
let top_group_select = group_by_cols
713
+
.iter()
714
+
.map(|col| format!(", gc.{col} AS {col}"))
715
+
.join("");
716
+
let top_group_order = group_by_cols
717
+
.iter()
718
+
.map(|col| format!(", gc.{col}"))
719
+
.join("");
720
+
721
+
let query = format!(
722
+
"WITH grouped_counts AS (SELECT {date_bin}{group_clause}, COUNT(*) as count FROM {table_ref}{where_clause} GROUP BY {end_time_col_name},{start_time_col_name}{group_clause}), top_groups AS (SELECT {top_group_cols} FROM grouped_counts GROUP BY {top_group_cols} ORDER BY SUM(\"count\") DESC LIMIT {top_k}) SELECT gc.{start_time_col_name} AS {start_time_col_name}, gc.{end_time_col_name} AS {end_time_col_name}{top_group_select}, gc.\"count\" AS count FROM grouped_counts gc INNER JOIN top_groups tg ON {top_group_join} ORDER BY gc.{end_time_col_name}{top_group_order}",
0 commit comments