diff --git a/Cargo.lock b/Cargo.lock index b2fe8f6592a7..d1977b49a552 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2897,7 +2897,6 @@ dependencies = [ "itoa", "num-traits", "polars-arrow", - "polars-core", "polars-error", "polars-utils", "ryu", diff --git a/crates/polars-arrow/src/compute/decimal.rs b/crates/polars-arrow/src/compute/decimal.rs index d4199d260362..d1fc24472b8f 100644 --- a/crates/polars-arrow/src/compute/decimal.rs +++ b/crates/polars-arrow/src/compute/decimal.rs @@ -1,5 +1,16 @@ +use std::sync::atomic::{AtomicBool, Ordering}; + use atoi::FromRadix10SignedChecked; +static TRIM_DECIMAL_ZEROS: AtomicBool = AtomicBool::new(false); + +pub fn get_trim_decimal_zeros() -> bool { + TRIM_DECIMAL_ZEROS.load(Ordering::Relaxed) +} +pub fn set_trim_decimal_zeros(trim: Option) { + TRIM_DECIMAL_ZEROS.store(trim.unwrap_or(false), Ordering::Relaxed) +} + /// Count the number of b'0's at the beginning of a slice. fn leading_zeros(bytes: &[u8]) -> u8 { bytes.iter().take_while(|byte| **byte == b'0').count() as u8 diff --git a/crates/polars-core/src/fmt.rs b/crates/polars-core/src/fmt.rs index 2ced3591c0a3..0b9435f9cedb 100644 --- a/crates/polars-core/src/fmt.rs +++ b/crates/polars-core/src/fmt.rs @@ -1,7 +1,7 @@ #[cfg(any(feature = "fmt", feature = "fmt_no_tty"))] use std::borrow::Cow; use std::fmt::{Debug, Display, Formatter, Write}; -use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::RwLock; use std::{fmt, str}; @@ -39,7 +39,6 @@ pub enum FloatFmt { static FLOAT_PRECISION: RwLock> = RwLock::new(None); static FLOAT_FMT: AtomicU8 = AtomicU8::new(FloatFmt::Mixed as u8); -static TRIM_DECIMAL_ZEROS: AtomicBool = AtomicBool::new(false); static THOUSANDS_SEPARATOR: AtomicU8 = AtomicU8::new(b'\0'); static DECIMAL_SEPARATOR: AtomicU8 = AtomicU8::new(b'.'); @@ -65,8 +64,9 @@ pub fn get_thousands_separator() -> String { sep.to_string() } } +#[cfg(feature = "dtype-decimal")] pub fn get_trim_decimal_zeros() -> bool { - TRIM_DECIMAL_ZEROS.load(Ordering::Relaxed) + arrow::compute::decimal::get_trim_decimal_zeros() } // Numeric formatting setters @@ -82,8 +82,9 @@ pub fn set_decimal_separator(dec: Option) { pub fn set_thousands_separator(sep: Option) { THOUSANDS_SEPARATOR.store(sep.unwrap_or('\0') as u8, Ordering::Relaxed) } +#[cfg(feature = "dtype-decimal")] pub fn set_trim_decimal_zeros(trim: Option) { - TRIM_DECIMAL_ZEROS.store(trim.unwrap_or(false), Ordering::Relaxed) + arrow::compute::decimal::set_trim_decimal_zeros(trim) } macro_rules! format_array { diff --git a/crates/polars-io/src/csv/write_impl.rs b/crates/polars-io/src/csv/write_impl.rs index 53580b5d4e87..93fc847e871d 100644 --- a/crates/polars-io/src/csv/write_impl.rs +++ b/crates/polars-io/src/csv/write_impl.rs @@ -160,8 +160,8 @@ unsafe fn write_any_value( write!(f, "{quote}")?; end_with_quote = true } - let trim_zeros = polars_core::fmt::get_trim_decimal_zeros(); - let buf = arrow::legacy::compute::decimal::format_decimal(v, scale, trim_zeros); + let trim_zeros = arrow::compute::decimal::get_trim_decimal_zeros(); + let buf = arrow::compute::decimal::format_decimal(v, scale, trim_zeros); write!(f, "{}", buf.as_str()) }, _ => { diff --git a/crates/polars-json/Cargo.toml b/crates/polars-json/Cargo.toml index 4cf0cf5eea80..0b9159cadccd 100644 --- a/crates/polars-json/Cargo.toml +++ b/crates/polars-json/Cargo.toml @@ -9,7 +9,6 @@ repository = { workspace = true } description = "JSON related logic for the Polars DataFrame library" [dependencies] -polars-core = { workspace = true } polars-error = { workspace = true } polars-utils = { workspace = true } diff --git a/crates/polars-json/src/json/write/serialize.rs b/crates/polars-json/src/json/write/serialize.rs index 95be95816514..483b4d15fdac 100644 --- a/crates/polars-json/src/json/write/serialize.rs +++ b/crates/polars-json/src/json/write/serialize.rs @@ -2,10 +2,10 @@ use std::io::Write; use arrow::array::*; use arrow::bitmap::utils::ZipValidity; +#[cfg(feature = "dtype-decimal")] +use arrow::compute::decimal::{format_decimal, get_trim_decimal_zeros}; use arrow::datatypes::{ArrowDataType, IntegerType, TimeUnit}; use arrow::io::iterator::BufStreamingIterator; -#[cfg(feature = "dtype-decimal")] -use arrow::legacy::compute::decimal::format_decimal; use arrow::offset::Offset; #[cfg(feature = "chrono-tz")] use arrow::temporal_conversions::parse_offset_tz; @@ -121,7 +121,7 @@ fn decimal_serializer<'a>( offset: usize, take: usize, ) -> Box + 'a + Send + Sync> { - let trim_zeros = polars_core::fmt::get_trim_decimal_zeros(); + let trim_zeros = get_trim_decimal_zeros(); let f = move |x: Option<&i128>, buf: &mut Vec| { if let Some(x) = x { utf8::write_str(buf, format_decimal(*x, scale, trim_zeros).as_str()).unwrap()