Skip to content

Commit f9e7e96

Browse files
committed
refactor: months_days_ns(pub i32,pub i32,pub i64) -> months_days_micros(pub i128)
1 parent a3bb9f8 commit f9e7e96

File tree

33 files changed

+437
-377
lines changed

33 files changed

+437
-377
lines changed

src/common/column/src/types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ mod private {
114114
impl Sealed for OrderedFloat<f32> {}
115115
impl Sealed for OrderedFloat<f64> {}
116116
impl Sealed for super::days_ms {}
117-
impl Sealed for super::months_days_ns {}
117+
impl Sealed for super::months_days_micros {}
118118
impl Sealed for View {}
119119
}

src/common/column/src/types/native.rs

Lines changed: 45 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::convert::TryFrom;
1717
use std::ops::Neg;
1818
use std::panic::RefUnwindSafe;
1919

20+
use arrow_buffer::ArrowNativeType;
2021
use borsh::BorshDeserialize;
2122
use borsh::BorshSerialize;
2223
use bytemuck::Pod;
@@ -268,137 +269,80 @@ impl NativeType for days_ms {
268269
)]
269270
#[allow(non_camel_case_types)]
270271
#[repr(C)]
271-
pub struct months_days_ns(pub i32, pub i32, pub i64);
272+
pub struct months_days_micros(pub i128);
272273

273-
impl NumCast for months_days_ns {
274+
impl NumCast for months_days_micros {
274275
fn from<T: ToPrimitive>(n: T) -> Option<Self> {
275276
n.to_i64().map(|n| Self::new(0, 0, n))
276277
}
277278
}
278279

279-
impl ToPrimitive for months_days_ns {
280+
impl ToPrimitive for months_days_micros {
280281
fn to_i64(&self) -> Option<i64> {
281-
Some(self.2)
282+
self.0.to_i64()
282283
}
283284

284285
fn to_u64(&self) -> Option<u64> {
285-
self.2.to_u64()
286+
self.0.to_u64()
286287
}
287288
}
288289

289-
impl months_days_ns {
290-
/// A new [`months_days_ns`].
291-
#[inline]
292-
pub fn new(months: i32, days: i32, nanoseconds: i64) -> Self {
293-
Self(months, days, nanoseconds)
290+
impl months_days_micros {
291+
pub fn new(months: i32, days: i32, microseconds: i64) -> Self {
292+
let months_bits = (months as i128) << 96;
293+
let days_bits = (days as i128) << 64;
294+
let micros_bits = microseconds as i128;
295+
296+
Self(months_bits | days_bits | micros_bits)
294297
}
295298

296-
/// The number of months
297-
#[inline]
298299
pub fn months(&self) -> i32 {
299-
self.0
300+
// Decoding logic
301+
((self.0 >> 96) & 0xFFFFFFFF) as i32
300302
}
301303

302-
/// The number of days
303-
#[inline]
304304
pub fn days(&self) -> i32 {
305-
self.1
305+
((self.0 >> 64) & 0xFFFFFFFF) as i32
306306
}
307307

308-
/// The number of nanoseconds
309-
#[inline]
310-
pub fn ns(&self) -> i64 {
311-
self.2
308+
pub fn microseconds(&self) -> i64 {
309+
(self.0 & 0xFFFFFFFFFFFFFFFF) as i64
312310
}
313311
}
314312

315-
impl NativeType for months_days_ns {
313+
impl NativeType for months_days_micros {
316314
const PRIMITIVE: PrimitiveType = PrimitiveType::MonthDayNano;
317315
type Bytes = [u8; 16];
318316
#[inline]
319317
fn to_le_bytes(&self) -> Self::Bytes {
320-
let months = self.months().to_le_bytes();
321-
let days = self.days().to_le_bytes();
322-
let ns = self.ns().to_le_bytes();
323-
let mut result = [0; 16];
324-
result[0] = months[0];
325-
result[1] = months[1];
326-
result[2] = months[2];
327-
result[3] = months[3];
328-
result[4] = days[0];
329-
result[5] = days[1];
330-
result[6] = days[2];
331-
result[7] = days[3];
332-
(0..8).for_each(|i| {
333-
result[8 + i] = ns[i];
334-
});
335-
result
318+
self.0.to_le_bytes()
319+
// let a = self.0.to_le_bytes();
320+
// let mut result = [0; 16];
321+
// (0..16).for_each(|i| {
322+
// result[0+i] = a[i];
323+
// });
324+
// result
336325
}
337326

338327
#[inline]
339328
fn to_be_bytes(&self) -> Self::Bytes {
340-
let months = self.months().to_be_bytes();
341-
let days = self.days().to_be_bytes();
342-
let ns = self.ns().to_be_bytes();
343-
let mut result = [0; 16];
344-
result[0] = months[0];
345-
result[1] = months[1];
346-
result[2] = months[2];
347-
result[3] = months[3];
348-
result[4] = days[0];
349-
result[5] = days[1];
350-
result[6] = days[2];
351-
result[7] = days[3];
352-
(0..8).for_each(|i| {
353-
result[8 + i] = ns[i];
354-
});
355-
result
329+
self.0.to_be_bytes()
330+
// let a = self.0.to_be_bytes();
331+
// let mut result = [0; 16];
332+
// (0..16).for_each(|i| {
333+
// result[0+i] = a[i];
334+
// });
335+
// result
356336
}
357337

358338
#[inline]
359339
fn from_le_bytes(bytes: Self::Bytes) -> Self {
360-
let mut months = [0; 4];
361-
months[0] = bytes[0];
362-
months[1] = bytes[1];
363-
months[2] = bytes[2];
364-
months[3] = bytes[3];
365-
let mut days = [0; 4];
366-
days[0] = bytes[4];
367-
days[1] = bytes[5];
368-
days[2] = bytes[6];
369-
days[3] = bytes[7];
370-
let mut ns = [0; 8];
371-
(0..8).for_each(|i| {
372-
ns[i] = bytes[8 + i];
373-
});
374-
Self(
375-
i32::from_le_bytes(months),
376-
i32::from_le_bytes(days),
377-
i64::from_le_bytes(ns),
378-
)
340+
Self(i128::from_le_bytes(bytes))
379341
}
380342

381343
#[inline]
382344
fn from_be_bytes(bytes: Self::Bytes) -> Self {
383-
let mut months = [0; 4];
384-
months[0] = bytes[0];
385-
months[1] = bytes[1];
386-
months[2] = bytes[2];
387-
months[3] = bytes[3];
388-
let mut days = [0; 4];
389-
days[0] = bytes[4];
390-
days[1] = bytes[5];
391-
days[2] = bytes[6];
392-
days[3] = bytes[7];
393-
let mut ns = [0; 8];
394-
(0..8).for_each(|i| {
395-
ns[i] = bytes[8 + i];
396-
});
397-
Self(
398-
i32::from_be_bytes(months),
399-
i32::from_be_bytes(days),
400-
i64::from_be_bytes(ns),
401-
)
345+
Self(i128::from_be_bytes(bytes))
402346
}
403347
}
404348

@@ -408,9 +352,15 @@ impl std::fmt::Display for days_ms {
408352
}
409353
}
410354

411-
impl std::fmt::Display for months_days_ns {
355+
impl std::fmt::Display for months_days_micros {
412356
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
413-
write!(f, "{}m {}d {}ns", self.months(), self.days(), self.ns())
357+
write!(
358+
f,
359+
"{}m {}d {}micros",
360+
self.months(),
361+
self.days(),
362+
self.microseconds()
363+
)
414364
}
415365
}
416366

@@ -423,12 +373,12 @@ impl Neg for days_ms {
423373
}
424374
}
425375

426-
impl Neg for months_days_ns {
376+
impl Neg for months_days_micros {
427377
type Output = Self;
428378

429379
#[inline(always)]
430380
fn neg(self) -> Self::Output {
431-
Self::new(-self.months(), -self.days(), -self.ns())
381+
Self::new(-self.months(), -self.days(), -self.microseconds())
432382
}
433383
}
434384

src/common/column/src/types/simd/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use super::days_ms;
2020
use super::f16;
2121
use super::i256;
22-
use super::months_days_ns;
22+
use super::months_days_micros;
2323
use super::BitChunk;
2424
use super::BitChunkIter;
2525
use super::NativeType;
@@ -151,7 +151,7 @@ pub(super) use native_simd;
151151
// of how they are represented in the different channels.
152152
native_simd!(f16x32, f16, 32, u32);
153153
native_simd!(days_msx8, days_ms, 8, u8);
154-
native_simd!(months_days_nsx8, months_days_ns, 8, u8);
154+
native_simd!(months_days_microsx8, months_days_micros, 8, u8);
155155
native_simd!(i128x8, i128, 8, u8);
156156
native_simd!(i256x8, i256, 8, u8);
157157

@@ -185,4 +185,4 @@ native!(f64, f64x8);
185185
native!(i128, i128x8);
186186
native!(i256, i256x8);
187187
native!(days_ms, days_msx8);
188-
native!(months_days_ns, months_days_nsx8);
188+
native!(months_days_micros, months_days_microsx8);

0 commit comments

Comments
 (0)