You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/06/01 18:13:54 UTC

[GitHub] [arrow-datafusion] alamb commented on a change in pull request #452: Optimize `nth_value`, remove `first_value`, `last_value` structs and use idiomatic rust style

alamb commented on a change in pull request #452:
URL: https://github.com/apache/arrow-datafusion/pull/452#discussion_r643375138



##########
File path: datafusion/src/physical_plan/expressions/nth_value.rs
##########
@@ -205,19 +142,98 @@ impl NthValueAccumulator {
 
 impl WindowAccumulator for NthValueAccumulator {
     fn scan(&mut self, values: &[ScalarValue]) -> Result<Option<ScalarValue>> {
-        if self.n == SPECIAL_SIZE_VALUE_FOR_LAST {
-            // for last_value function
-            self.value = values[0].clone();
-        } else if self.offset < self.n {
-            self.offset += 1;
-            if self.offset == self.n {
+        self.offset += 1;
+        match self.kind {
+            NthValueKind::Last => {
+                self.value = values[0].clone();
+            }
+            NthValueKind::First if self.offset == 1 => {
+                self.value = values[0].clone();
+            }
+            NthValueKind::Nth(n) if self.offset == n => {
                 self.value = values[0].clone();
             }
+            _ => {}
         }
+
         Ok(None)
     }
 
     fn evaluate(&self) -> Result<Option<ScalarValue>> {
         Ok(Some(self.value.clone()))
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::error::Result;
+    use crate::physical_plan::expressions::col;
+    use arrow::record_batch::RecordBatch;
+    use arrow::{array::*, datatypes::*};
+
+    fn test_i32_result(expr: Arc<NthValue>, expected: i32) -> Result<()> {
+        let arr: ArrayRef = Arc::new(Int32Array::from(vec![1, -2, 3, -4, 5, -6, 7, 8]));
+        let schema = Schema::new(vec![Field::new("arr", DataType::Int32, false)]);
+        let batch = RecordBatch::try_new(Arc::new(schema), vec![arr])?;

Review comment:
       FWIW for tests like this you can also use [`RecordBatch::try_from_iter`](https://docs.rs/arrow/4.2.0/arrow/record_batch/struct.RecordBatch.html#method.try_from_iter) to avoid having to construct the `Schema` directly.
   
   This way is great too, I just figured I would point it out for the future




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org