jcsherin commented on code in PR #11845:
URL: https://github.com/apache/datafusion/pull/11845#discussion_r1705828522


##########
datafusion/functions-aggregate/src/nth_value.rs:
##########
@@ -87,27 +85,37 @@ impl AggregateUDFImpl for NthValueAgg {
     }
 
     fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn 
Accumulator>> {
-        let n = match acc_args.input_exprs[1] {
-            Expr::Literal(ScalarValue::Int64(Some(value))) => {
-                if acc_args.is_reversed {
-                    Ok(-value)
-                } else {
-                    Ok(value)
+        let n = match acc_args.physical_exprs[1]
+            .as_any()
+            .downcast_ref::<Literal>()
+        {
+            Some(lit) => match lit.value() {
+                ScalarValue::Int64(Some(value)) => {
+                    if acc_args.is_reversed {
+                        -*value
+                    } else {
+                        *value
+                    }
+                }
+                _ => {
+                    return not_impl_err!(
+                        "{} not supported for n: {}",
+                        self.name(),
+                        &acc_args.physical_exprs[1]
+                    );
                 }
+            },
+            None => {
+                return not_impl_err!(
+                    "{} not supported for n: {}",
+                    self.name(),
+                    &acc_args.physical_exprs[1]
+                );

Review Comment:
   The existing code is readable in the current form. 
   
   But I wanted to explore ways for removing the repetition in the error 
handling block. Finally, ended up having to split it into two separate bindings.
   
   I don't have any strong opinions about this. So please feel free to use it 
or not :smiley: 
   
   ```rust
   let scalar = acc_args.physical_exprs[1]
        .as_any()
        .downcast_ref::<Literal>()
        .and_then(|lit| {
                if let ScalarValue::Int64(inner) = lit.value() {
                        Some(*inner)
                } else {
                        None
                }
        })
        .flatten();
   
   let n = if let Some(value) = scalar {
               if acc_args.is_reversed {
                   value
               } else {
                   -value
               }
   } else {
        return not_impl_err!(
                "{} not supported for n: {}",
                self.name(),
                &acc_args.physical_exprs[1]
        );
   };
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to