liukun4515 commented on a change in pull request #1223:
URL: https://github.com/apache/arrow-rs/pull/1223#discussion_r790371475
##########
File path: arrow/src/array/array_binary.rs
##########
@@ -816,13 +827,80 @@ impl DecimalArray {
let array_data = unsafe { builder.build_unchecked() };
Self::from(array_data)
}
+
+ /// Creates a [DecimalArray] with default precision and scale,
+ /// based on an iterator of `i128` values without nulls
+ pub fn from_iter_values<I: IntoIterator<Item = i128>>(iter: I) -> Self {
+ let val_buf: Buffer = iter.into_iter().collect();
+ let data = unsafe {
+ ArrayData::new_unchecked(
+ Self::default_type(),
+ val_buf.len() / std::mem::size_of::<i128>(),
+ None,
+ None,
+ 0,
+ vec![val_buf],
+ vec![],
+ )
+ };
+ DecimalArray::from(data)
+ }
+
+ /// Return the precision (total digits) that can be stored by this array
pub fn precision(&self) -> usize {
self.precision
}
+ /// Return the scale (digits after the decimal) that can be stored by this
array
pub fn scale(&self) -> usize {
self.scale
}
+
+ /// Returns a DecimalArray with the same data as self, with the
+ /// specified precision.
+ ///
+ /// panic's if:
+ /// 1. the precision is larger than [`DECIMAL_MAX_PRECISION`]
+ /// 2. scale is larger than [`DECIMAL_MAX_SCALE`];
+ /// 3. scale is > precision
+ pub fn with_precision_and_scale(mut self, precision: usize, scale: usize)
-> Self {
+ assert!(
+ precision <= DECIMAL_MAX_PRECISION,
+ "precision {} is greater than max {}",
+ precision,
+ DECIMAL_MAX_PRECISION
+ );
+ assert!(
+ scale <= DECIMAL_MAX_SCALE,
+ "scale {} is greater than max {}",
+ scale,
+ DECIMAL_MAX_SCALE
+ );
+ assert!(
+ scale <= precision,
+ "scale {} is greater than precision {}",
+ scale,
+ precision
+ );
+
+ assert_eq!(
+ self.data.data_type(),
+ &DataType::Decimal(self.precision, self.scale)
+ );
+
+ // safety: self.data is valid DataType::Decimal
+ let new_data_type = DataType::Decimal(precision, scale);
+ self.precision = precision;
+ self.scale = scale;
+ self.data = self.data.with_data_type(new_data_type);
+ self
+ }
+
+ /// The default precision and scale used when not specified
+ pub fn default_type() -> DataType {
+ // Keep maximum precision
+ DataType::Decimal(38, 10)
Review comment:
we can replace 38 to `DECIMAL_MAX_PRECISION`, and 10 to
`OTHER_DEFAULT_VALUE`.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]