drusso commented on a change in pull request #8222:
URL: https://github.com/apache/arrow/pull/8222#discussion_r500992058



##########
File path: rust/datafusion/src/physical_plan/group_scalar.rs
##########
@@ -0,0 +1,133 @@
+// 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.
+
+//! Defines scalars used to construct groups, ex. in GROUP BY clauses.
+
+use std::convert::{From, TryFrom};
+
+use crate::error::{ExecutionError, Result};
+use crate::scalar::ScalarValue;
+
+/// Enumeration of types that can be used in a GROUP BY expression (all 
primitives except
+/// for floating point numerics)
+#[derive(Debug, PartialEq, Eq, Hash, Clone)]
+pub(crate) enum GroupByScalar {
+    UInt8(u8),
+    UInt16(u16),
+    UInt32(u32),
+    UInt64(u64),
+    Int8(i8),
+    Int16(i16),
+    Int32(i32),
+    Int64(i64),
+    Utf8(String),
+}
+
+impl TryFrom<&ScalarValue> for GroupByScalar {
+    type Error = ExecutionError;
+
+    fn try_from(scalar_value: &ScalarValue) -> Result<Self> {
+        Ok(match scalar_value {
+            ScalarValue::Int8(Some(v)) => GroupByScalar::Int8(*v),
+            ScalarValue::Int16(Some(v)) => GroupByScalar::Int16(*v),
+            ScalarValue::Int32(Some(v)) => GroupByScalar::Int32(*v),
+            ScalarValue::Int64(Some(v)) => GroupByScalar::Int64(*v),
+            ScalarValue::UInt8(Some(v)) => GroupByScalar::UInt8(*v),
+            ScalarValue::UInt16(Some(v)) => GroupByScalar::UInt16(*v),
+            ScalarValue::UInt32(Some(v)) => GroupByScalar::UInt32(*v),
+            ScalarValue::UInt64(Some(v)) => GroupByScalar::UInt64(*v),
+            ScalarValue::Utf8(Some(v)) => GroupByScalar::Utf8(v.clone()),
+            ScalarValue::Int8(None)
+            | ScalarValue::Int16(None)
+            | ScalarValue::Int32(None)
+            | ScalarValue::Int64(None)
+            | ScalarValue::UInt8(None)
+            | ScalarValue::UInt16(None)
+            | ScalarValue::UInt32(None)
+            | ScalarValue::UInt64(None)
+            | ScalarValue::Utf8(None) => {
+                return Err(ExecutionError::InternalError(format!(
+                    "Cannot convert a ScalarValue holding NULL ({:?})",
+                    scalar_value
+                )));
+            }
+            v => {
+                return Err(ExecutionError::InternalError(format!(
+                    "Cannot convert a ScalarValue with associated DataType 
{:?}",
+                    v.get_datatype()
+                )))
+            }
+        })
+    }
+}
+
+impl From<&GroupByScalar> for ScalarValue {
+    fn from(group_by_scalar: &GroupByScalar) -> Self {
+        match group_by_scalar {
+            GroupByScalar::Int8(v) => ScalarValue::Int8(Some(*v)),
+            GroupByScalar::Int16(v) => ScalarValue::Int16(Some(*v)),
+            GroupByScalar::Int32(v) => ScalarValue::Int32(Some(*v)),
+            GroupByScalar::Int64(v) => ScalarValue::Int64(Some(*v)),
+            GroupByScalar::UInt8(v) => ScalarValue::UInt8(Some(*v)),
+            GroupByScalar::UInt16(v) => ScalarValue::UInt16(Some(*v)),
+            GroupByScalar::UInt32(v) => ScalarValue::UInt32(Some(*v)),
+            GroupByScalar::UInt64(v) => ScalarValue::UInt64(Some(*v)),
+            GroupByScalar::Utf8(v) => ScalarValue::Utf8(Some(v.clone())),
+        }
+    }
+}
+
+mod tests {
+    use super::*;

Review comment:
       These are flagged as unused imports, will fix. 




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to