Skip to content

Commit 2817069

Browse files
author
ljl
committed
spi-stats:fix ct_agg with.
1 parent b5d4e71 commit 2817069

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

backend/spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bios_basic::spi::{
88
use itertools::Itertools;
99
use tardis::{
1010
basic::{dto::TardisContext, error::TardisError, result::TardisResult},
11+
chrono::format,
1112
db::{
1213
reldb_client::TardisRelDBClient,
1314
sea_orm::{self, FromQueryResult, Value},
@@ -243,7 +244,8 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
243244
rel_external_id: None,
244245
query_limit,
245246
});
246-
247+
// todo 需要更改使用with
248+
let ct_agg = query_req.group.iter().any(|i| i.code == "ct");
247249
let conf_limit = query_limit;
248250
// Dimension configuration, used for group and group_order
249251
// 纬度配置,用于group以及group_order
@@ -449,7 +451,7 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
449451
group.code.clone(),
450452
group.time_window.as_ref().map(|i| i.to_string().to_lowercase()).unwrap_or("".to_string())
451453
);
452-
sql_part_group_infos.push((column_name_with_fun, alias_name, col_conf.show_name.clone()));
454+
sql_part_group_infos.push((column_name_with_fun, alias_name, col_conf.show_name.clone(), col_conf.col_key.clone()));
453455
} else {
454456
return Err(funs.err().not_found(
455457
"metric",
@@ -464,12 +466,12 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
464466
));
465467
}
466468
}
467-
let sql_part_groups = sql_part_group_infos.iter().map(|group| group.1.clone()).collect::<Vec<String>>().join(",");
469+
let mut sql_part_groups = sql_part_group_infos.iter().map(|group| group.1.clone()).collect::<Vec<String>>().join(",");
468470

469471
// Package outer select
470472
// (column name with fun, alias name, show_name, is dimension)
471473
let mut sql_part_outer_select_infos = vec![];
472-
for (column_name_with_fun, alias_name, show_name) in sql_part_group_infos {
474+
for (column_name_with_fun, alias_name, show_name, _) in sql_part_group_infos.clone() {
473475
sql_part_outer_select_infos.push((column_name_with_fun, alias_name, show_name, true));
474476
}
475477
for select in &query_req.select {
@@ -489,7 +491,35 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
489491
"500-spi-stats-internal-error",
490492
)
491493
})?;
492-
let column_name_with_fun = col_data_type.to_pg_select(&format!("_.{}", select.code.clone()), &select.fun);
494+
let column_name_with_fun = if ct_agg {
495+
if select.code != "_count" {
496+
sql_part_groups = format!("{},_.{}", sql_part_groups, select.code.clone());
497+
}
498+
let mut partition_dim = vec![];
499+
let mut order_dim = "".to_string();
500+
for (column_name_with_fun, _, _, col_key) in sql_part_group_infos.clone() {
501+
if col_key == "ct" {
502+
order_dim = column_name_with_fun.clone();
503+
} else {
504+
partition_dim.push(column_name_with_fun);
505+
}
506+
}
507+
format!(
508+
"{} OVER ({})",
509+
col_data_type.to_pg_select(
510+
&format!("_.{}", if select.code == "_count" { "count".to_string() } else { select.code.clone() }),
511+
&select.fun
512+
),
513+
if partition_dim.len() > 0 {
514+
format!("PARTITION BY {} ORDER BY {}", partition_dim.join(","), order_dim)
515+
} else {
516+
format!("ORDER BY {}", order_dim)
517+
}
518+
)
519+
} else {
520+
col_data_type.to_pg_select(&format!("_.{}", select.code.clone()), &select.fun)
521+
};
522+
// let column_name_with_fun = col_data_type.to_pg_select(&format!("_.{}", select.code.clone()), &select.fun);
493523
let alias_name = format!("{}{FUNCTION_SUFFIX_FLAG}{}", select.code.clone(), select.fun.to_string().to_lowercase());
494524
sql_part_outer_select_infos.push((column_name_with_fun, alias_name, col_conf.show_name.clone(), false));
495525
}
@@ -603,6 +633,7 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
603633
} else {
604634
"fact.own_paths LIKE $1".to_string()
605635
};
636+
// todo 提供另一种with语法的实现,用于 ct 统计全表数据
606637
let final_sql = format!(
607638
r#"SELECT {sql_part_outer_selects}{}
608639
FROM (
@@ -640,14 +671,22 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
640671
if query_req.ignore_distinct.unwrap_or(false) {
641672
""
642673
} else if mes_distinct {
643-
"DISTINCT ON (fact.key) fact.key AS _key,"
674+
if ct_agg {
675+
"DISTINCT ON (fact.key,date_part('day',fact.ct)) fact.key AS _key,"
676+
} else {
677+
"DISTINCT ON (fact.key) fact.key AS _key,"
678+
}
644679
} else {
645680
""
646681
},
647682
if query_req.ignore_distinct.unwrap_or(false) {
648683
""
649684
} else if mes_distinct {
650-
"_key,"
685+
if ct_agg {
686+
"_key,date_part('day',fact.ct),"
687+
} else {
688+
"_key,"
689+
}
651690
} else {
652691
""
653692
},

0 commit comments

Comments
 (0)