You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/06/16 07:53:20 UTC

[GitHub] [arrow-rs] martin-g commented on a diff in pull request #1871: Add Decimal128 API and use it in DecimalArray and DecimalBuilder

martin-g commented on code in PR #1871:
URL: https://github.com/apache/arrow-rs/pull/1871#discussion_r898800825


##########
arrow/src/util/decimal.rs:
##########
@@ -0,0 +1,150 @@
+// 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.
+
+//! Decimal related utils
+
+use std::cmp::Ordering;
+
+/// Represents a decimal value with precision and scale.
+/// The decimal value is represented by a signed 128-bit integer.
+#[derive(Debug)]
+pub struct Decimal128 {
+    #[allow(dead_code)]
+    precision: usize,
+    scale: usize,
+    value: i128,
+}
+
+impl PartialOrd for Decimal128 {
+    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+        assert_eq!(
+            self.scale, other.scale,
+            "Cannot compare two Decimal128 with different scale: {}, {}",
+            self.scale, other.scale
+        );
+        self.value.partial_cmp(&other.value)
+    }
+}
+
+impl Ord for Decimal128 {
+    fn cmp(&self, other: &Self) -> Ordering {
+        assert_eq!(
+            self.scale, other.scale,
+            "Cannot compare two Decimal128 with different scale: {}, {}",
+            self.scale, other.scale
+        );
+        self.value.cmp(&other.value)
+    }
+}
+
+impl PartialEq<Self> for Decimal128 {
+    fn eq(&self, other: &Self) -> bool {
+        assert_eq!(
+            self.scale, other.scale,
+            "Cannot compare two Decimal128 with different scale: {}, {}",
+            self.scale, other.scale
+        );
+        self.value.eq(&other.value)
+    }
+}
+
+impl Eq for Decimal128 {}
+
+impl Decimal128 {
+    pub fn new_from_bytes(precision: usize, scale: usize, bytes: &[u8]) -> Self {
+        let as_array = bytes.try_into();
+        let value = match as_array {
+            Ok(v) if bytes.len() == 16 => i128::from_le_bytes(v),
+            _ => panic!("Input to Decimal128 is not 128bit integer."),

Review Comment:
   Wouldn't it be better to return a `Result` instead of `panic`-ing here ?



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