From e25bd94a8ddc64e1509369cceb68762d869a98f2 Mon Sep 17 00:00:00 2001
From: Maaz Ahmed <maaz.a@subcom.tech>
Date: Thu, 28 Dec 2023 11:43:27 +0530
Subject: [PATCH] fix: remove static bounds on query fn params

This fixes the compiler complaining about the arguments not living long enough or escaping the function.
Now the query functions can take external arguments and compose well together.

Fixes #28
---
 src/query/fns/aggregate.rs | 28 ++++++++++++-------------
 src/query/fns/mod.rs       |  4 ++--
 src/query/fns/rollup.rs    | 20 +++++++++---------
 src/query/fns/transform.rs | 42 ++++++++++----------------------------
 4 files changed, 37 insertions(+), 57 deletions(-)

diff --git a/src/query/fns/aggregate.rs b/src/query/fns/aggregate.rs
index 56b50e1..810bee3 100644
--- a/src/query/fns/aggregate.rs
+++ b/src/query/fns/aggregate.rs
@@ -66,7 +66,7 @@ impl<F: Fn(&mut fmt::Formatter) -> fmt::Result> IntoQuery for AggrFunc<'_, F> {
 /// The sum aggregate operator/function
 #[inline]
 pub fn sum<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("sum", vec_expr).into()
 }
@@ -74,7 +74,7 @@ pub fn sum<'a>(
 /// The min aggregate operator/function
 #[inline]
 pub fn min<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("min", vec_expr).into()
 }
@@ -82,7 +82,7 @@ pub fn min<'a>(
 /// The max aggregate operator/function
 #[inline]
 pub fn max<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("max", vec_expr).into()
 }
@@ -90,7 +90,7 @@ pub fn max<'a>(
 /// The avg aggregate operator/function
 #[inline]
 pub fn avg<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("avg", vec_expr).into()
 }
@@ -98,7 +98,7 @@ pub fn avg<'a>(
 /// The group aggregate operator/function
 #[inline]
 pub fn group<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("group", vec_expr).into()
 }
@@ -106,7 +106,7 @@ pub fn group<'a>(
 /// The stddev aggregate operator/function
 #[inline]
 pub fn stddev<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("stddev", vec_expr).into()
 }
@@ -114,7 +114,7 @@ pub fn stddev<'a>(
 /// The stdvar aggregate operator/function
 #[inline]
 pub fn stdvar<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("stdvar", vec_expr).into()
 }
@@ -122,7 +122,7 @@ pub fn stdvar<'a>(
 /// The count aggregate operator/function
 #[inline]
 pub fn count<'a>(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("count", vec_expr).into()
 }
@@ -130,9 +130,9 @@ pub fn count<'a>(
 /// The count_values aggregate operator/function
 #[inline]
 pub fn count_values<'a>(
-    label: &'_ str,
-    vec_expr: impl Operable + 'static,
-) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result + '_> {
+    label: &'a str,
+    vec_expr: impl Operable + 'a,
+) -> AggrFunc<'_, impl Fn(&mut fmt::Formatter) -> fmt::Result + '_> {
     qry_fn!(count_values, r#""{label}", {vec_expr}"#).into()
 }
 
@@ -140,7 +140,7 @@ pub fn count_values<'a>(
 #[inline]
 pub fn topk<'a>(
     k: usize,
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(topk, "{k}, {vec_expr}").into()
 }
@@ -149,7 +149,7 @@ pub fn topk<'a>(
 #[inline]
 pub fn bottomk<'a>(
     k: usize,
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(bottomk, "{k}, {vec_expr}").into()
 }
@@ -158,7 +158,7 @@ pub fn bottomk<'a>(
 #[inline]
 pub fn quantile<'a>(
     phi: f32,
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
 ) -> AggrFunc<'a, impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(quantile, "{phi}, {vec_expr}").into()
 }
diff --git a/src/query/fns/mod.rs b/src/query/fns/mod.rs
index 0b10770..d2760d1 100644
--- a/src/query/fns/mod.rs
+++ b/src/query/fns/mod.rs
@@ -78,8 +78,8 @@ impl<F: Fn(&mut fmt::Formatter) -> fmt::Result> IntoQuery for QryFunc<F> {
 #[inline]
 fn basic_fn(
     name: &'static str,
-    expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result + '_> {
+    expr: impl Operable,
+) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(var: name, "{expr}")
 }
 
diff --git a/src/query/fns/rollup.rs b/src/query/fns/rollup.rs
index 25683ca..29b0f26 100644
--- a/src/query/fns/rollup.rs
+++ b/src/query/fns/rollup.rs
@@ -7,7 +7,7 @@ use super::{basic_fn, qry_fn, QryFunc};
 /// The avg_over_time rollup query function
 #[inline]
 pub fn avg_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("avg_over_time", range_vec)
 }
@@ -15,7 +15,7 @@ pub fn avg_over_time(
 /// The min_over_time rollup query function
 #[inline]
 pub fn min_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("min_over_time", range_vec)
 }
@@ -23,7 +23,7 @@ pub fn min_over_time(
 /// The max_over_time rollup query function
 #[inline]
 pub fn max_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("max_over_time", range_vec)
 }
@@ -31,7 +31,7 @@ pub fn max_over_time(
 /// The sum_over_time rollup query function
 #[inline]
 pub fn sum_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("sum_over_time", range_vec)
 }
@@ -39,7 +39,7 @@ pub fn sum_over_time(
 /// The count_over_time rollup query function
 #[inline]
 pub fn count_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("count_over_time", range_vec)
 }
@@ -48,7 +48,7 @@ pub fn count_over_time(
 #[inline]
 pub fn quantile_over_time(
     phi: f32,
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(quantile_over_time, "{phi},{range_vec}")
 }
@@ -56,7 +56,7 @@ pub fn quantile_over_time(
 /// The stddev_over_time rollup query function
 #[inline]
 pub fn stddev_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("stddev_over_time", range_vec)
 }
@@ -64,7 +64,7 @@ pub fn stddev_over_time(
 /// The stdvar_over_time rollup query function
 #[inline]
 pub fn stdvar_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("stdvar_over_time", range_vec)
 }
@@ -72,7 +72,7 @@ pub fn stdvar_over_time(
 /// The present_over_time rollup query function
 #[inline]
 pub fn present_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("present_over_time", range_vec)
 }
@@ -80,7 +80,7 @@ pub fn present_over_time(
 /// The last_over_time rollup query function
 #[inline]
 pub fn last_over_time(
-    range_vec: impl Operable + 'static,
+    range_vec: impl Operable,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     basic_fn("last_over_time", range_vec)
 }
diff --git a/src/query/fns/transform.rs b/src/query/fns/transform.rs
index 9e2f3a8..966a8aa 100644
--- a/src/query/fns/transform.rs
+++ b/src/query/fns/transform.rs
@@ -32,7 +32,7 @@ pub fn mql_union(
 
 /// The `round` query transform function
 pub fn round(
-    vec_expr: impl Operable + 'static,
+    vec_expr: impl Operable,
     nearest: Option<f64>,
 ) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(round, {
@@ -46,70 +46,50 @@ pub fn round(
 }
 
 /// The `abs` query transform function
-pub fn abs(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn abs(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(abs, "{vec_expr}")
 }
 
 /// The `absent` query transform function
-pub fn absent(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn absent(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(absent, "{vec_expr}")
 }
 
 /// The `ceil` query transform function
-pub fn ceil(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn ceil(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(ceil, "{vec_expr}")
 }
 
 /// The `changes` query transform function
-pub fn changes(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn changes(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(changes, "{vec_expr}")
 }
 
 /// The `delta` query transform function
-pub fn delta(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn delta(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(delta, "{vec_expr}")
 }
 /// The `exp` query transform function
-pub fn exp(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn exp(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(exp, "{vec_expr}")
 }
 /// The `floor` query transform function
-pub fn floor(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn floor(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(floor, "{vec_expr}")
 }
 
 /// The `sort` query transform function
-pub fn sort(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn sort(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(sort, "{vec_expr}")
 }
 
 /// The `sort_desc` query transform function
-pub fn sort_desc(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn sort_desc(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(sort_desc, "{vec_expr}")
 }
 
 /// The `sqrt` query transform function
-pub fn sqrt(
-    vec_expr: impl Operable + 'static,
-) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
+pub fn sqrt(vec_expr: impl Operable) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result> {
     qry_fn!(sqrt, "{vec_expr}")
 }
 
-- 
GitLab