0

I'm generating and expression from a string.

Let us suppose to have a data frame with a close column ant I'm trying to compute all in parallel through expressions.

If the function receives a string "rolling_mean" I need to compute close - close.rolling_mean(of some window).

I defined an expression for the close column to be used in match (don't repeat yourself):

let close_column = Expr::Column(Arc::from("close"));

After that I have a match to get the computation on close column:

close_column
            - close_column.rolling_mean(RollingOptions {
                window_size: Duration::new(window as i64),
                min_periods: 1 as usize,
                weights: None,
                center: true,
                by: None,
                closed_window: None,
            }))

The problem is that match statement moved the value of close_column inside the brace of match in the first value, so it is impossible to use for the second term (where I need rolling).

If I clone close_column the problem disappear (of course) but I'm not sure it is the best strategy.

Here my function:

pub fn generate_expr_from_str(function_name: &str, window: usize) -> Expr {
    let close_column = Expr::Column(Arc::from("close"));
    let expression = match function_name {
        "rolling" => (close_column
            - close_column.rolling_mean(RollingOptions {
                window_size: Duration::new(window as i64),
                min_periods: 1 as usize,
                weights: None,
                center: true,
                by: None,
                closed_window: None,
            }))
        .alias(format!("rolling_{window}").as_str()),
        _ => panic!("function not implemented"),
    };
    expression
}

In case I clone even just the first close, the value is moved on the second and there is no problem for the compiler. Suggestions?

Progman
  • 16,827
  • 6
  • 33
  • 48
Sigi
  • 53
  • 8
  • Since I'm not modifying close_column I tried to borrow close_column with & but on references minus operator is not supported. – Sigi Oct 22 '22 at 08:27
  • You may need to `clone()` it if it doesn't support working via references. – tadman Oct 22 '22 at 12:14
  • You may also want to move the `let close_column` initialization inside the `match` branch where it's actually used, or eliminate that entirely, just stub in `Expr::Column(Arc::from("close"))` twice instead. – tadman Oct 22 '22 at 12:15
  • Actually, this is only an example. I'll have many branch. It's outside cause I need to reuse inside match. – Sigi Oct 22 '22 at 12:42
  • 1
    I mean, I can clone of course. I'm only was wondering if I did or just there were another way. – Sigi Oct 22 '22 at 12:55
  • Unless you have control over the implementation, probably not. I'm supposing the cost of creation here is pretty minimal, especially with `Arc` involved, so I wouldn't sweat it. – tadman Oct 22 '22 at 15:59

0 Answers0