Skip to content

Commit

Permalink
spi-stats:fix ext date.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed May 23, 2024
1 parent 2d716f6 commit 75bcb8a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
3 changes: 3 additions & 0 deletions backend/spi/spi-stats/src/serv/pg/stats_pg_metric_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ pub async fn query_metrics(query_req: &StatsQueryMetricsReq, funs: &TardisFunsIn
// Not distinguish between dimensions and measures, used for fields in where and having conditions
// 不区分维度和度量,用于where及having条件的字段
let conf_info = conf_info.into_iter().map(|v| (v.col_key.clone().to_string(), v)).collect::<HashMap<String, StatsConfInfo>>();
info!("conf_info: {:?}", conf_info.keys());
info!("dim_conf_info: {:?}", dim_conf_info.keys());
info!("measure_conf_info: {:?}", measure_conf_info.keys());
if query_req.select.iter().any(|i| !measure_conf_info.contains_key(&i.code.to_string()))
// should be equivalent:
// original: || query_req.group.iter().any(|i| !dim_conf_info.contains_key(&i.code) || dim_conf_info.get(&i.code).unwrap().col_kind != StatsFactColKind::Dimension))
Expand Down
51 changes: 28 additions & 23 deletions backend/spi/spi-stats/src/stats_enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ impl StatsDataTypeKind {
.collect::<Vec<_>>()
.join(", ");
Some((format!("{column_name} {} ({})", op.to_sql(), param_sql), value))
} else if self == &StatsDataTypeKind::DateTime || self == &StatsDataTypeKind::Date {
value.pop().map(|value| (format!("coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone {} ${param_idx}", op.to_sql()), vec![value]))
} else {
value.pop().map(|value| (format!("{column_name} {} ${param_idx}", op.to_sql()), vec![value]))
},
Expand Down Expand Up @@ -398,32 +400,33 @@ pub enum StatsQueryTimeWindowKind {
}

impl StatsQueryTimeWindowKind {
// todo 支撑指定时区
pub fn to_sql(&self, column_name: &str, is_date_time: bool) -> String {
if is_date_time {
match self {
StatsQueryTimeWindowKind::Date => format!("date(timezone('UTC', {column_name}))"),
StatsQueryTimeWindowKind::Date => format!("date(timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))"),
// StatsQueryTimeWindowKind::Hour => format!("date_part('hour',timezone('UTC', {column_name}))"),
StatsQueryTimeWindowKind::Hour => format!(
"CONCAT(date_part('year', timezone('UTC', {column_name})), '-',
LPAD(date_part('month', timezone('UTC', {column_name}))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('UTC', {column_name}))::text, 2, '0'), ' ',
LPAD(date_part('hour', timezone('UTC', {column_name}))::text, 2, '0'))"
"CONCAT(date_part('year', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), '-',
LPAD(date_part('month', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'), ' ',
LPAD(date_part('hour', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'))"
),
// StatsQueryTimeWindowKind::Day => format!("date_part('day',timezone('UTC', {column_name}))"),
StatsQueryTimeWindowKind::Day => format!(
"CONCAT(date_part('year', timezone('UTC', {column_name})), '-',
LPAD(date_part('month', timezone('UTC', {column_name}))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('UTC', {column_name}))::text, 2, '0'))"
"CONCAT(date_part('year', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), '-',
LPAD(date_part('month', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'))"
),
StatsQueryTimeWindowKind::Week => format!(
"CONCAT(date_part('year', timezone('UTC', {column_name})), ' ',
date_part('week', timezone('UTC', {column_name})))"
"CONCAT(date_part('year', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), ' ',
date_part('week', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)))"
),
// StatsQueryTimeWindowKind::Month => format!("date_part('month',timezone('UTC', {column_name}))"),
StatsQueryTimeWindowKind::Month => {
format!("CONCAT(date_part('year', timezone('UTC',{column_name})), '-',LPAD(date_part('month', timezone('UTC', {column_name}))::text, 2, '0'))")
format!("CONCAT(date_part('year', timezone('Asia/Shanghai',coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), '-',LPAD(date_part('month', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'))")
}
StatsQueryTimeWindowKind::Year => format!("CONCAT(date_part('year',timezone('UTC', {column_name})),'')"),
StatsQueryTimeWindowKind::Year => format!("CONCAT(date_part('year',timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)),'')"),
}
} else {
match self {
Expand All @@ -433,24 +436,26 @@ impl StatsQueryTimeWindowKind {
// StatsQueryTimeWindowKind::Month => format!("date_part('month', {column_name})"),
// StatsQueryTimeWindowKind::Year => format!("date_part('year', {column_name})"),
StatsQueryTimeWindowKind::Hour => format!(
"CONCAT(date_part('year', timezone('UTC', {column_name})), '-',
LPAD(date_part('month', timezone('UTC', {column_name}))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('UTC', {column_name}))::text, 2, '0'), ' ',
LPAD(date_part('hour', timezone('UTC', {column_name}))::text, 2, '0'))"
"CONCAT(date_part('year', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), '-',
LPAD(date_part('month', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'), ' ',
LPAD(date_part('hour', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'))"
),
StatsQueryTimeWindowKind::Day => format!(
"CONCAT(date_part('year', timezone('UTC', {column_name})), '-',
LPAD(date_part('month', timezone('UTC', {column_name}))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('UTC', {column_name}))::text, 2, '0'))"
"CONCAT(date_part('year', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), '-',
LPAD(date_part('month', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'), '-',
LPAD(date_part('day', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'))"
),
StatsQueryTimeWindowKind::Week => format!(
"CONCAT(date_part('year', timezone('UTC', {column_name})), ' ',
date_part('week', timezone('UTC', {column_name})))"
"CONCAT(date_part('year', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), ' ',
date_part('week', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)))"
),
StatsQueryTimeWindowKind::Month => {
format!("CONCAT(date_part('year', timezone('UTC',{column_name})), '-',LPAD(date_part('month', timezone('UTC', {column_name}))::text, 2, '0'))")
format!(
"CONCAT(date_part('year', timezone('Asia/Shanghai',coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)), '-',LPAD(date_part('month', timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone))::text, 2, '0'))"
)
}
StatsQueryTimeWindowKind::Year => format!("CONCAT(date_part('year',timezone('UTC', {column_name})),'')"),
StatsQueryTimeWindowKind::Year => format!("CONCAT(date_part('year',timezone('Asia/Shanghai', coalesce({column_name},'1970-01-01 00:00:00 +00:00')::timestamp with time zone)),'')"),
}
}
}
Expand Down

0 comments on commit 75bcb8a

Please sign in to comment.