You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by tu...@apache.org on 2022/11/10 21:46:07 UTC

[arrow-rs] branch master updated: feat: add `OwnedRow` (#3079)

This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 885a3618f feat: add `OwnedRow` (#3079)
885a3618f is described below

commit 885a3618fc8739aaa7c1f9ff328a09a39d4c3af6
Author: Marco Neumann <ma...@crepererum.net>
AuthorDate: Thu Nov 10 21:46:02 2022 +0000

    feat: add `OwnedRow` (#3079)
    
    Closes #3078.
---
 arrow/src/row/mod.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)

diff --git a/arrow/src/row/mod.rs b/arrow/src/row/mod.rs
index 4fbaa3931..4dd2a33c0 100644
--- a/arrow/src/row/mod.rs
+++ b/arrow/src/row/mod.rs
@@ -451,6 +451,16 @@ pub struct Row<'a> {
     fields: &'a Arc<[SortField]>,
 }
 
+impl<'a> Row<'a> {
+    /// Create owned version of the row to detach it from the shared [`Rows`].
+    pub fn owned(&self) -> OwnedRow {
+        OwnedRow {
+            data: self.data.to_vec(),
+            fields: Arc::clone(self.fields),
+        }
+    }
+}
+
 // Manually derive these as don't wish to include `fields`
 
 impl<'a> PartialEq for Row<'a> {
@@ -490,6 +500,66 @@ impl<'a> AsRef<[u8]> for Row<'a> {
     }
 }
 
+/// Owned version of a [`Row`] that can be moved/cloned freely.
+///
+/// This contains the data for the one specific row (not the entire buffer of all rows).
+#[derive(Debug, Clone)]
+pub struct OwnedRow {
+    data: Vec<u8>,
+    fields: Arc<[SortField]>,
+}
+
+impl OwnedRow {
+    /// Get borrowed [`Row`] from owned version.
+    ///
+    /// This is helpful if you want to compare an [`OwnedRow`] with a [`Row`].
+    pub fn row(&self) -> Row<'_> {
+        Row {
+            data: &self.data,
+            fields: &self.fields,
+        }
+    }
+}
+
+// Manually derive these as don't wish to include `fields`. Also we just want to use the same `Row` implementations here.
+
+impl PartialEq for OwnedRow {
+    #[inline]
+    fn eq(&self, other: &Self) -> bool {
+        self.row().eq(&other.row())
+    }
+}
+
+impl Eq for OwnedRow {}
+
+impl PartialOrd for OwnedRow {
+    #[inline]
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        self.row().partial_cmp(&other.row())
+    }
+}
+
+impl Ord for OwnedRow {
+    #[inline]
+    fn cmp(&self, other: &Self) -> Ordering {
+        self.row().cmp(&other.row())
+    }
+}
+
+impl Hash for OwnedRow {
+    #[inline]
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        self.row().hash(state)
+    }
+}
+
+impl AsRef<[u8]> for OwnedRow {
+    #[inline]
+    fn as_ref(&self) -> &[u8] {
+        &self.data
+    }
+}
+
 /// Returns the null sentinel, negated if `invert` is true
 #[inline]
 fn null_sentinel(options: SortOptions) -> u8 {