Skip to content

Commit 3026520

Browse files
committed
refactor: months_days_ns(pub i32,pub i32,pub i64) -> months_days_micros(pub i128)
1 parent 0ecbfe1 commit 3026520

File tree

35 files changed

+422
-455
lines changed

35 files changed

+422
-455
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: 27 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ use borsh::BorshSerialize;
2222
use bytemuck::Pod;
2323
use bytemuck::Zeroable;
2424
use databend_common_base::base::OrderedFloat;
25-
use num_traits::NumCast;
26-
use num_traits::ToPrimitive;
2725
use serde_derive::Deserialize;
2826
use serde_derive::Serialize;
2927

@@ -268,137 +266,52 @@ impl NativeType for days_ms {
268266
)]
269267
#[allow(non_camel_case_types)]
270268
#[repr(C)]
271-
pub struct months_days_ns(pub i32, pub i32, pub i64);
269+
pub struct months_days_micros(pub i128);
272270

273-
impl NumCast for months_days_ns {
274-
fn from<T: ToPrimitive>(n: T) -> Option<Self> {
275-
n.to_i64().map(|n| Self::new(0, 0, n))
276-
}
277-
}
278-
279-
impl ToPrimitive for months_days_ns {
280-
fn to_i64(&self) -> Option<i64> {
281-
Some(self.2)
282-
}
283-
284-
fn to_u64(&self) -> Option<u64> {
285-
self.2.to_u64()
286-
}
287-
}
271+
impl months_days_micros {
272+
pub fn new(months: i32, days: i32, microseconds: i64) -> Self {
273+
let months_bits = (months as i128) << 96;
274+
let days_bits = (days as i128) << 64;
275+
let micros_bits = microseconds as i128;
288276

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)
277+
Self(months_bits | days_bits | micros_bits)
294278
}
295279

296-
/// The number of months
297-
#[inline]
298280
pub fn months(&self) -> i32 {
299-
self.0
281+
// Decoding logic
282+
((self.0 >> 96) & 0xFFFFFFFF) as i32
300283
}
301284

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

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

315-
impl NativeType for months_days_ns {
294+
impl NativeType for months_days_micros {
316295
const PRIMITIVE: PrimitiveType = PrimitiveType::MonthDayNano;
317296
type Bytes = [u8; 16];
318297
#[inline]
319298
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
299+
self.0.to_le_bytes()
336300
}
337301

338302
#[inline]
339303
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
304+
self.0.to_be_bytes()
356305
}
357306

358307
#[inline]
359308
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-
)
309+
Self(i128::from_le_bytes(bytes))
379310
}
380311

381312
#[inline]
382313
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-
)
314+
Self(i128::from_be_bytes(bytes))
402315
}
403316
}
404317

@@ -408,9 +321,15 @@ impl std::fmt::Display for days_ms {
408321
}
409322
}
410323

411-
impl std::fmt::Display for months_days_ns {
324+
impl std::fmt::Display for months_days_micros {
412325
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
413-
write!(f, "{}m {}d {}ns", self.months(), self.days(), self.ns())
326+
write!(
327+
f,
328+
"{}m {}d {}micros",
329+
self.months(),
330+
self.days(),
331+
self.microseconds()
332+
)
414333
}
415334
}
416335

@@ -423,12 +342,12 @@ impl Neg for days_ms {
423342
}
424343
}
425344

426-
impl Neg for months_days_ns {
345+
impl Neg for months_days_micros {
427346
type Output = Self;
428347

429348
#[inline(always)]
430349
fn neg(self) -> Self::Output {
431-
Self::new(-self.months(), -self.days(), -self.ns())
350+
Self::new(-self.months(), -self.days(), -self.microseconds())
432351
}
433352
}
434353

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)