datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

Rewrite UDAF reversed expression name

Open jayzhan211 opened this issue 1 year ago • 4 comments

          I think it would be good to eventually move this to a method in `https://github.com/apache/datafusion/pull/11611` though I agree this is good for now. Maybe we can file a ticket to track

Originally posted by @alamb in https://github.com/apache/datafusion/pull/11611#discussion_r1688699754

I think we could extend to rewrite the whole expression name, and it could be more straightforward what the name is rewritten

                let mut name = self.name().to_string();
                // If the function is changed, we need to reverse order_by clause as well
                // i.e. First(a order by b asc null first) -> Last(a order by b desc null last)
                if self.fun().name() == reverse_udf.name() {
                } else {
                    replace_order_by_clause(&mut name);
                }
                replace_fn_name_clause(&mut name, self.fun.name(), reverse_udf.name());

Something like

let mut name = self.name().to_string();
name = self.fun.reverse_name(name)

Add reverse_name in AggregateUDFImpl

trait AggregateUDFImpl {
    fn reverse_name(&self) -> String {
        ...
    }
}

Specificially we need to rewrite name for First/Last. For array_agg, the name should be the same.

jayzhan211 avatar Jul 24 '24 00:07 jayzhan211

take

dharanad avatar Jul 26 '24 15:07 dharanad

I plan to close this by 10th Aug. I hope that okay ? Feel free to re assign if this is a priority

dharanad avatar Jul 26 '24 15:07 dharanad

Apologies for the delay here. Somehow i missed this. @jayzhan211 I went through the conversation and somewhat understood the issue. But for better clarity can you share more info here. Once again sorry, for the delay here.

dharanad avatar Aug 27 '24 17:08 dharanad

What you need to do is, add a new method reverse_name

trait AggregateUDFImpl {
    fn reverse_name(&self) -> String {
        ...
    }
}

Replace with the code here

                let mut name = self.name().to_string();
                // If the function is changed, we need to reverse order_by clause as well
                // i.e. First(a order by b asc null first) -> Last(a order by b desc null last)
                if self.fun().name() == reverse_udf.name() {
                } else {
                    replace_order_by_clause(&mut name);
                }
                replace_fn_name_clause(&mut name, self.fun.name(), reverse_udf.name());

if the function name is changed (i.e. First -> Last), order by clause required to rewrite too

jayzhan211 avatar Aug 27 '24 23:08 jayzhan211