Skip to content
Snippets Groups Projects

Refactor: separate QryFunc name and args formatting

Merged Maaz Ahmed requested to merge 26-refactor-separate-qryfunc-name-and-args-formatting into main
5 files
+ 90
50
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 7
13
//! Aggregate query functions (includes PromQL aggregation operators)
use super::{basic_fn, QryFunc};
use super::{basic_fn, qry_fn, QryFunc};
use crate::{
query::{
ops::{
@@ -133,7 +133,7 @@ pub fn count_values<'a>(
label: &'_ str,
vec_expr: impl Operable + 'static,
) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result + '_> {
QryFunc::new(move |f| write!(f, r#"count_values("{label}", {vec_expr})"#)).into()
qry_fn!(count_values, r#""{label}", {vec_expr}"#).into()
}
/// The topk aggregate operator/function
@@ -142,7 +142,7 @@ pub fn topk<'a>(
k: usize,
vec_expr: impl Operable + 'static,
) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
QryFunc::new(move |f| write!(f, "topk({k}, {vec_expr})")).into()
qry_fn!(topk, "{k}, {vec_expr}").into()
}
/// The bottomk aggregate operator/function
@@ -151,7 +151,7 @@ pub fn bottomk<'a>(
k: usize,
vec_expr: impl Operable + 'static,
) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
QryFunc::new(move |f| write!(f, "bottomk({k}, {vec_expr})")).into()
qry_fn!(bottomk, "{k}, {vec_expr}").into()
}
/// The bottomk aggregate operator/function (phi value should always be >= 0 and <= 1)
@@ -160,7 +160,7 @@ pub fn quantile<'a>(
phi: f32,
vec_expr: impl Operable + 'static,
) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
QryFunc::new(move |f| write!(f, "quantile({phi}, {vec_expr})")).into()
qry_fn!(quantile, "{phi}, {vec_expr}").into()
}
#[cfg(test)]
@@ -171,20 +171,14 @@ mod tests {
#[test]
fn by_mod() {
let agr = AggrFunc {
inner: QryFunc::new(|f| f.write_str("func(x)")),
mod_type: Default::default(),
};
let agr: AggrFunc<_> = qry_fn!(func, "x").into();
let by_str = agr.by(["label", "label2"]).to_string();
assert_eq!(by_str, "func(x) by (label,label2)");
}
#[test]
fn without_mod() {
let agr = AggrFunc {
inner: QryFunc::new(|f| f.write_str("func(x)")),
mod_type: Default::default(),
};
let agr: AggrFunc<_> = qry_fn!(func, "x").into();
let by_str = agr.without(["label", "label2"]).to_string();
assert_eq!(by_str, "func(x) without (label,label2)");
}
Loading