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

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3769: ArrayData Enumeration for Remaining Types

tustvold commented on code in PR #3769:
URL: https://github.com/apache/arrow-rs/pull/3769#discussion_r1119195911


##########
arrow-data/src/data/run.rs:
##########
@@ -0,0 +1,149 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::data::types::RunEndType;
+use crate::ArrayData;
+use arrow_buffer::buffer::ScalarBuffer;
+use arrow_buffer::ArrowNativeType;
+use arrow_schema::DataType;
+use std::marker::PhantomData;
+
+mod private {
+    use super::*;
+
+    pub trait RunEndSealed {
+        /// Downcast [`ArrayDataRun`] to `[RunArrayData`]
+        fn downcast_ref(data: &ArrayDataRun) -> Option<&RunArrayData<Self>>
+        where
+            Self: RunEnd;
+
+        /// Downcast [`ArrayDataRun`] to `[RunArrayData`]
+        fn downcast(data: ArrayDataRun) -> Option<RunArrayData<Self>>
+        where
+            Self: RunEnd;
+
+        /// Cast [`RunArrayData`] to [`ArrayDataRun`]
+        fn upcast(v: RunArrayData<Self>) -> ArrayDataRun
+        where
+            Self: RunEnd;
+    }
+}
+
+pub trait RunEnd: private::RunEndSealed + ArrowNativeType {
+    const TYPE: RunEndType;
+}
+
+macro_rules! run_end {
+    ($t:ty,$v:ident) => {
+        impl RunEnd for $t {
+            const TYPE: RunEndType = RunEndType::$v;
+        }
+        impl private::RunEndSealed for $t {
+            fn downcast_ref(data: &ArrayDataRun) -> Option<&RunArrayData<Self>> {
+                match data {
+                    ArrayDataRun::$v(v) => Some(v),
+                    _ => None,
+                }
+            }
+
+            fn downcast(data: ArrayDataRun) -> Option<RunArrayData<Self>> {
+                match data {
+                    ArrayDataRun::$v(v) => Some(v),
+                    _ => None,
+                }
+            }
+
+            fn upcast(v: RunArrayData<Self>) -> ArrayDataRun {
+                ArrayDataRun::$v(v)
+            }
+        }
+    };
+}
+
+run_end!(i16, Int16);
+run_end!(i32, Int32);
+run_end!(i64, Int64);
+
+/// An enumeration of the types of [`RunArrayData`]
+pub enum ArrayDataRun {
+    Int16(RunArrayData<i16>),
+    Int32(RunArrayData<i32>),
+    Int64(RunArrayData<i64>),
+}
+
+impl ArrayDataRun {
+    /// Downcast this [`ArrayDataRun`] to the corresponding [`RunArrayData`]
+    pub fn downcast_ref<E: RunEnd>(&self) -> Option<&RunArrayData<E>> {
+        E::downcast_ref(self)
+    }
+
+    /// Downcast this [`ArrayDataRun`] to the corresponding [`RunArrayData`]
+    pub fn downcast<E: RunEnd>(self) -> Option<RunArrayData<E>> {
+        E::downcast(self)
+    }
+}
+
+impl<E: RunEnd> From<RunArrayData<E>> for ArrayDataRun {
+    fn from(value: RunArrayData<E>) -> Self {
+        E::upcast(value)
+    }
+}
+
+/// ArrayData for [run-end encoded arrays](https://arrow.apache.org/docs/format/Columnar.html#run-end-encoded-layout)
+pub struct RunArrayData<E: RunEnd> {

Review Comment:
   This is a slight deviation from the spec as defined in https://arrow.apache.org/docs/format/Columnar.html#run-end-encoded-layout in that it "inlines" the run ends. This is consistent with `RunEndArray` and I think makes for a significantly less confusing abstraction.



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