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

[GitHub] [arrow-rs] viirya opened a new pull request, #3715: Add `into_primitive_dict_builder` to `DictionaryArray`

viirya opened a new pull request, #3715:
URL: https://github.com/apache/arrow-rs/pull/3715

   # 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.
   -->
   
   Part of #3710.
   
   # Rationale for this change
    
   <!--
   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.
   -->
   
   Similar to `into_builder` of `PrimitiveArray`, `into_primitive_dict_builder` is for cow support for `DictionaryArray`. 
   
   # What changes are included in this PR?
   
   <!--
   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.
   -->
   
   Added `into_primitive_dict_builder` API to `DictionaryArray` for mutating its keys and values.
   
   # Are there any user-facing changes?
   
   
   <!--
   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 `breaking 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-rs] viirya merged pull request #3715: Add `into_primitive_dict_builder` to `DictionaryArray`

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


-- 
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-rs] ursabot commented on pull request #3715: Add `into_primitive_dict_builder` to `DictionaryArray`

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

   Benchmark runs are scheduled for baseline = e52574c8b02410128f35d65aa92d4876b1404d6c and contender = 72474a674270685d6ea2d631760da4cd19dfeeea. 72474a674270685d6ea2d631760da4cd19dfeeea 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-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/db52cec92c4e48b68d55d10168e8d02b...928bc406e42c41bd81d233efba1df35c/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/82b44bb39a1a4a548a093cdd05f8453c...2889b00990ce4e548e219c827d280192/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/bdfc35c19b934b59b0e3fb9d7b0ddcdd...38ce8e76cb81416bb393f73e2609b99a/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/74b833a8be6a4f6dad420889d97e6e55...6be464ad85ff4fa1ba548bf2e933b7de/)
   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-rs] alamb commented on a diff in pull request #3715: Add `into_primitive_dict_builder` to `DictionaryArray`

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


##########
arrow-array/src/array/dictionary_array.rs:
##########
@@ -941,4 +982,62 @@ mod tests {
         let a = DictionaryArray::<Int32Type>::from_iter(["32"]);
         let _ = DictionaryArray::<Int64Type>::from(a.into_data());
     }
+
+    #[test]
+    fn test_into_primitive_dict_builder() {
+        let values = Int32Array::from_iter_values([10_i32, 12, 15]);
+        let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]);
+
+        let dict_array = DictionaryArray::<Int8Type>::try_new(&keys, &values).unwrap();
+
+        let boxed: ArrayRef = Arc::new(dict_array);
+        let col: DictionaryArray<Int8Type> = as_dictionary_array(&boxed).clone();
+
+        drop(boxed);
+        drop(keys);
+        drop(values);
+
+        let mut builder = col.into_primitive_dict_builder::<Int32Type>().unwrap();
+
+        let slice = builder.values_slice_mut();
+        assert_eq!(slice, &[10, 12, 15]);
+
+        slice[0] = 4;
+        slice[1] = 2;
+        slice[2] = 1;
+
+        let values = Int32Array::from_iter_values([4_i32, 2, 1]);
+        let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]);
+
+        let expected = DictionaryArray::<Int8Type>::try_new(&keys, &values).unwrap();
+
+        let new_array = builder.finish();
+        assert_eq!(expected, new_array);
+    }
+
+    #[test]
+    fn test_into_primitive_dict_builder_cloned_array() {
+        let values = Int32Array::from_iter_values([10_i32, 12, 15]);
+        let keys = Int8Array::from_iter_values([1_i8, 0, 2, 0]);
+
+        let dict_array = DictionaryArray::<Int8Type>::try_new(&keys, &values).unwrap();
+
+        let boxed: ArrayRef = Arc::new(dict_array);
+
+        let col: DictionaryArray<Int8Type> =
+            DictionaryArray::<Int8Type>::from(boxed.data().clone());
+        let err = col.into_primitive_dict_builder::<Int32Type>();
+
+        match err {
+            Ok(_) => panic!("Should not get builder from cloned array"),
+            Err(returned) => {

Review Comment:
   You could also write this like `let returned = err.unwrap_err()`



##########
arrow-array/src/array/dictionary_array.rs:
##########
@@ -402,6 +403,44 @@ impl<K: ArrowPrimitiveType> DictionaryArray<K> {
         // Offsets were valid before and verified length is greater than or equal
         Self::from(unsafe { builder.build_unchecked() })
     }
+
+    /// Returns `PrimitiveDictionaryBuilder` of this dictionary array for mutating
+    /// its keys and values if the underlying data buffer is not shared by others.
+    pub fn into_primitive_dict_builder<V>(
+        self,
+    ) -> Result<PrimitiveDictionaryBuilder<K, V>, Self>
+    where
+        V: ArrowPrimitiveType,
+    {
+        if !self.value_type().is_primitive() {
+            return Err(self);
+        }
+
+        let key_array = as_primitive_array::<K>(self.keys()).clone();
+        let value_array = as_primitive_array::<V>(self.values()).clone();
+
+        drop(self.data);

Review Comment:
   is this needed to drop the ref counts to be able to call `into_builder()` without copying data (as self has a ref count)?



-- 
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-rs] viirya commented on a diff in pull request #3715: Add `into_primitive_dict_builder` to `DictionaryArray`

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #3715:
URL: https://github.com/apache/arrow-rs/pull/3715#discussion_r1123583931


##########
arrow-array/src/array/dictionary_array.rs:
##########
@@ -402,6 +403,44 @@ impl<K: ArrowPrimitiveType> DictionaryArray<K> {
         // Offsets were valid before and verified length is greater than or equal
         Self::from(unsafe { builder.build_unchecked() })
     }
+
+    /// Returns `PrimitiveDictionaryBuilder` of this dictionary array for mutating
+    /// its keys and values if the underlying data buffer is not shared by others.
+    pub fn into_primitive_dict_builder<V>(
+        self,
+    ) -> Result<PrimitiveDictionaryBuilder<K, V>, Self>
+    where
+        V: ArrowPrimitiveType,
+    {
+        if !self.value_type().is_primitive() {
+            return Err(self);
+        }
+
+        let key_array = as_primitive_array::<K>(self.keys()).clone();
+        let value_array = as_primitive_array::<V>(self.values()).clone();
+
+        drop(self.data);

Review Comment:
   Yea, this drops self ref count.



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