You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "ahmedriza (via GitHub)" <gi...@apache.org> on 2023/02/16 19:29:15 UTC

[GitHub] [arrow-datafusion] ahmedriza opened a new pull request, #5311: Fix nested list indexing when the index is 0 or larger than the list size

ahmedriza opened a new pull request, #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes https://github.com/apache/arrow-datafusion/issues/5310
   
   # Rationale for this change
   Indexing a nested list should not fail even when the index is out of range
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   Correctly handle the case when an index is out or range, i.e. 0 or larger than the value of the list
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are these changes tested?
   Yes
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   No
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] ahmedriza commented on a diff in pull request #5311: Fix nested list indexing when the index is 0 or larger than the list size

Posted by "ahmedriza (via GitHub)" <gi...@apache.org>.
ahmedriza commented on code in PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#discussion_r1111109682


##########
datafusion/physical-expr/src/expressions/get_indexed_field.rs:
##########
@@ -390,4 +404,78 @@ mod tests {
         )?;
         Ok(())
     }
+
+    #[test]
+    fn get_indexed_field_list_out_of_bounds() {
+        let fields = vec![
+            Field::new("id", DataType::Int64, true),
+            Field::new(
+                "a",
+                DataType::List(Box::new(Field::new("item", DataType::Float64, true))),
+                true,
+            ),
+        ];
+
+        let schema = Schema::new(fields);
+        let mut int_builder = PrimitiveBuilder::<Int64Type>::new();
+        int_builder.append_value(1);
+
+        let mut lb = ListBuilder::new(PrimitiveBuilder::<Float64Type>::new());
+        lb.values().append_value(1.0);
+        lb.values().append_null();
+        lb.values().append_value(3.0);
+        lb.append(true);
+
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(int_builder.finish()), Arc::new(lb.finish())],
+        )
+        .unwrap();
+
+        let col_a = col("a", &schema).unwrap();
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 0, float64_array(None));
+
+        verify_index_evaluation(&batch, col_a.clone(), 1, float64_array(Some(1.0)));
+        verify_index_evaluation(&batch, col_a.clone(), 2, float64_array(None));
+        verify_index_evaluation(&batch, col_a.clone(), 3, float64_array(Some(3.0)));
+
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 100, float64_array(None));
+    }
+
+    fn verify_index_evaluation(
+        batch: &RecordBatch,
+        arg: Arc<dyn PhysicalExpr>,
+        index: i64,
+        expected_result: ArrayRef,
+    ) {
+        let expr = Arc::new(GetIndexedFieldExpr::new(
+            arg,
+            ScalarValue::Int64(Some(index)),
+        ));
+        let result = expr.evaluate(batch).unwrap().into_array(batch.num_rows());
+        assert!(

Review Comment:
   Thanks @alamb.  Didn't know about this macro. Can see it in `datafusion::core::test_util.rs`.  However, can't see `core` being a dependency of `datafusion-physical-expr` where this test is.  Perhaps I've misunderstood what was meant.



-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] ursabot commented on pull request #5311: Fix nested list indexing when the index is 0 or larger than the list size

Posted by "ursabot (via GitHub)" <gi...@apache.org>.
ursabot commented on PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#issuecomment-1436023335

   Benchmark runs are scheduled for baseline = 6ec254442456e33d517c021ce56a0423398ffea7 and contender = db4f9984eb18a495b1ce48765fdf651343e1b10f. db4f9984eb18a495b1ce48765fdf651343e1b10f is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/043f4a44380d46b68bf09abee3800dd9...067e3575d772407b9604c8b490b3a814/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/9a9ca1b9dd6d4521acc8b3649001eb07...9e1c32be43374ded91dc2de1a2d9f283/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/7a16bd5e398845098495e32628365ef4...624f9763a5a44aba8efdc80a329f15f8/)
   [Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/f438e37f1386482999fd51572bee96e1...ed8187ed9e56451c88d68646ed7f7694/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on pull request #5311: Fix nested list indexing when the index is 0 or larger than the list size

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#issuecomment-1436021000

   Thanks again @ahmedriza 


-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] ahmedriza commented on a diff in pull request #5311: Fix nested list indexing when the index is 0 or larger than the list size

Posted by "ahmedriza (via GitHub)" <gi...@apache.org>.
ahmedriza commented on code in PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#discussion_r1111109682


##########
datafusion/physical-expr/src/expressions/get_indexed_field.rs:
##########
@@ -390,4 +404,78 @@ mod tests {
         )?;
         Ok(())
     }
+
+    #[test]
+    fn get_indexed_field_list_out_of_bounds() {
+        let fields = vec![
+            Field::new("id", DataType::Int64, true),
+            Field::new(
+                "a",
+                DataType::List(Box::new(Field::new("item", DataType::Float64, true))),
+                true,
+            ),
+        ];
+
+        let schema = Schema::new(fields);
+        let mut int_builder = PrimitiveBuilder::<Int64Type>::new();
+        int_builder.append_value(1);
+
+        let mut lb = ListBuilder::new(PrimitiveBuilder::<Float64Type>::new());
+        lb.values().append_value(1.0);
+        lb.values().append_null();
+        lb.values().append_value(3.0);
+        lb.append(true);
+
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(int_builder.finish()), Arc::new(lb.finish())],
+        )
+        .unwrap();
+
+        let col_a = col("a", &schema).unwrap();
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 0, float64_array(None));
+
+        verify_index_evaluation(&batch, col_a.clone(), 1, float64_array(Some(1.0)));
+        verify_index_evaluation(&batch, col_a.clone(), 2, float64_array(None));
+        verify_index_evaluation(&batch, col_a.clone(), 3, float64_array(Some(3.0)));
+
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 100, float64_array(None));
+    }
+
+    fn verify_index_evaluation(
+        batch: &RecordBatch,
+        arg: Arc<dyn PhysicalExpr>,
+        index: i64,
+        expected_result: ArrayRef,
+    ) {
+        let expr = Arc::new(GetIndexedFieldExpr::new(
+            arg,
+            ScalarValue::Int64(Some(index)),
+        ));
+        let result = expr.evaluate(batch).unwrap().into_array(batch.num_rows());
+        assert!(

Review Comment:
   Thanks @alamb.  Didn't know about this macro. Can see it in `datafusion::core::test_util.rs`.  However, can't see `core` being a dependency of `datafusion-physical-expr` where this test is (hence, in order to use the macro, will need to add an additional dependency here).  Perhaps I've misunderstood what was meant.



-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5311: Fix nested list indexing when the index is 0 or larger than the list size

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311#discussion_r1111055533


##########
datafusion/physical-expr/src/expressions/get_indexed_field.rs:
##########
@@ -390,4 +404,78 @@ mod tests {
         )?;
         Ok(())
     }
+
+    #[test]
+    fn get_indexed_field_list_out_of_bounds() {
+        let fields = vec![
+            Field::new("id", DataType::Int64, true),
+            Field::new(
+                "a",
+                DataType::List(Box::new(Field::new("item", DataType::Float64, true))),
+                true,
+            ),
+        ];
+
+        let schema = Schema::new(fields);
+        let mut int_builder = PrimitiveBuilder::<Int64Type>::new();
+        int_builder.append_value(1);
+
+        let mut lb = ListBuilder::new(PrimitiveBuilder::<Float64Type>::new());
+        lb.values().append_value(1.0);
+        lb.values().append_null();
+        lb.values().append_value(3.0);
+        lb.append(true);
+
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(int_builder.finish()), Arc::new(lb.finish())],
+        )
+        .unwrap();
+
+        let col_a = col("a", &schema).unwrap();
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 0, float64_array(None));
+
+        verify_index_evaluation(&batch, col_a.clone(), 1, float64_array(Some(1.0)));
+        verify_index_evaluation(&batch, col_a.clone(), 2, float64_array(None));
+        verify_index_evaluation(&batch, col_a.clone(), 3, float64_array(Some(3.0)));
+
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 100, float64_array(None));
+    }
+
+    fn verify_index_evaluation(
+        batch: &RecordBatch,
+        arg: Arc<dyn PhysicalExpr>,
+        index: i64,
+        expected_result: ArrayRef,
+    ) {
+        let expr = Arc::new(GetIndexedFieldExpr::new(
+            arg,
+            ScalarValue::Int64(Some(index)),
+        ));
+        let result = expr.evaluate(batch).unwrap().into_array(batch.num_rows());
+        assert!(

Review Comment:
   the `assert_batches_eq!` macro is often used for such comparisons too



##########
datafusion/physical-expr/src/expressions/get_indexed_field.rs:
##########
@@ -390,4 +404,78 @@ mod tests {
         )?;
         Ok(())
     }
+
+    #[test]
+    fn get_indexed_field_list_out_of_bounds() {
+        let fields = vec![
+            Field::new("id", DataType::Int64, true),
+            Field::new(
+                "a",
+                DataType::List(Box::new(Field::new("item", DataType::Float64, true))),
+                true,
+            ),
+        ];
+
+        let schema = Schema::new(fields);
+        let mut int_builder = PrimitiveBuilder::<Int64Type>::new();
+        int_builder.append_value(1);
+
+        let mut lb = ListBuilder::new(PrimitiveBuilder::<Float64Type>::new());
+        lb.values().append_value(1.0);
+        lb.values().append_null();
+        lb.values().append_value(3.0);
+        lb.append(true);
+
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(int_builder.finish()), Arc::new(lb.finish())],
+        )
+        .unwrap();
+
+        let col_a = col("a", &schema).unwrap();
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 0, float64_array(None));
+
+        verify_index_evaluation(&batch, col_a.clone(), 1, float64_array(Some(1.0)));
+        verify_index_evaluation(&batch, col_a.clone(), 2, float64_array(None));
+        verify_index_evaluation(&batch, col_a.clone(), 3, float64_array(Some(3.0)));
+
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 100, float64_array(None));

Review Comment:
   👍 



##########
datafusion/physical-expr/src/expressions/get_indexed_field.rs:
##########
@@ -390,4 +404,78 @@ mod tests {
         )?;
         Ok(())
     }
+
+    #[test]
+    fn get_indexed_field_list_out_of_bounds() {
+        let fields = vec![
+            Field::new("id", DataType::Int64, true),
+            Field::new(
+                "a",
+                DataType::List(Box::new(Field::new("item", DataType::Float64, true))),
+                true,
+            ),
+        ];
+
+        let schema = Schema::new(fields);
+        let mut int_builder = PrimitiveBuilder::<Int64Type>::new();
+        int_builder.append_value(1);
+
+        let mut lb = ListBuilder::new(PrimitiveBuilder::<Float64Type>::new());
+        lb.values().append_value(1.0);
+        lb.values().append_null();
+        lb.values().append_value(3.0);
+        lb.append(true);
+
+        let batch = RecordBatch::try_new(
+            Arc::new(schema.clone()),
+            vec![Arc::new(int_builder.finish()), Arc::new(lb.finish())],
+        )
+        .unwrap();
+
+        let col_a = col("a", &schema).unwrap();
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 0, float64_array(None));
+
+        verify_index_evaluation(&batch, col_a.clone(), 1, float64_array(Some(1.0)));
+        verify_index_evaluation(&batch, col_a.clone(), 2, float64_array(None));
+        verify_index_evaluation(&batch, col_a.clone(), 3, float64_array(Some(3.0)));
+
+        // out of bounds index
+        verify_index_evaluation(&batch, col_a.clone(), 100, float64_array(None));
+    }
+
+    fn verify_index_evaluation(
+        batch: &RecordBatch,
+        arg: Arc<dyn PhysicalExpr>,
+        index: i64,
+        expected_result: ArrayRef,
+    ) {
+        let expr = Arc::new(GetIndexedFieldExpr::new(
+            arg,
+            ScalarValue::Int64(Some(index)),
+        ));
+        let result = expr.evaluate(batch).unwrap().into_array(batch.num_rows());
+        assert!(
+            result == expected_result.clone(),
+            "result: {:?} != expected result: {:?}",
+            result,
+            expected_result
+        );
+        assert_eq!(result.data_type(), &DataType::Float64);
+    }
+
+    fn float64_array(value: Option<f64>) -> ArrayRef {
+        match value {
+            Some(v) => {
+                let mut b = PrimitiveBuilder::<Float64Type>::new();
+                b.append_value(v);
+                Arc::new(b.finish())
+            }
+            None => {
+                let mut b = PrimitiveBuilder::<Float64Type>::new();
+                b.append_null();
+                Arc::new(b.finish())
+            }
+        }

Review Comment:
   I think you can make a Float64Array directly from the value if you wanted:
   
   
   ```rust
   Arc::new(Float64Array::from_value(value, 1))
   ```
   
   https://docs.rs/arrow/33.0.0/arrow/array/struct.PrimitiveArray.html#method.from_value
   
   



-- 
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-datafusion] alamb merged pull request #5311: Fix nested list indexing when the index is 0 or larger than the list size

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #5311:
URL: https://github.com/apache/arrow-datafusion/pull/5311


-- 
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: github-unsubscribe@arrow.apache.org

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