Skip to content

Commit

Permalink
Interval -> LargeBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Dec 6, 2024
1 parent 9e58cd9 commit 0f40b74
Show file tree
Hide file tree
Showing 40 changed files with 385 additions and 475 deletions.
2 changes: 1 addition & 1 deletion src/common/column/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ mod private {
impl Sealed for OrderedFloat<f32> {}
impl Sealed for OrderedFloat<f64> {}
impl Sealed for super::days_ms {}
impl Sealed for super::months_days_ns {}
impl Sealed for super::months_days_micros {}
impl Sealed for View {}
}
48 changes: 24 additions & 24 deletions src/common/column/src/types/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ impl NativeType for days_ms {
)]
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct months_days_ns(pub i32, pub i32, pub i64);
pub struct months_days_micros(pub i32, pub i32, pub i64);

impl NumCast for months_days_ns {
impl NumCast for months_days_micros {
fn from<T: ToPrimitive>(n: T) -> Option<Self> {
n.to_i64().map(|n| Self::new(0, 0, n))
}
}

impl ToPrimitive for months_days_ns {
impl ToPrimitive for months_days_micros {
fn to_i64(&self) -> Option<i64> {
Some(self.2)
}
Expand All @@ -286,11 +286,11 @@ impl ToPrimitive for months_days_ns {
}
}

impl months_days_ns {
/// A new [`months_days_ns`].
impl months_days_micros {
/// A new [`months_days_micros`].
#[inline]
pub fn new(months: i32, days: i32, nanoseconds: i64) -> Self {
Self(months, days, nanoseconds)
pub fn new(months: i32, days: i32, micros: i64) -> Self {
Self(months, days, micros)
}

/// The number of months
Expand All @@ -305,21 +305,21 @@ impl months_days_ns {
self.1
}

/// The number of nanoseconds
/// The number of microseconds
#[inline]
pub fn ns(&self) -> i64 {
pub fn micros(&self) -> i64 {
self.2
}
}

impl NativeType for months_days_ns {
impl NativeType for months_days_micros {
const PRIMITIVE: PrimitiveType = PrimitiveType::MonthDayNano;
type Bytes = [u8; 16];
#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
let months = self.months().to_le_bytes();
let days = self.days().to_le_bytes();
let ns = self.ns().to_le_bytes();
let micros = self.micros().to_le_bytes();
let mut result = [0; 16];
result[0] = months[0];
result[1] = months[1];
Expand All @@ -330,7 +330,7 @@ impl NativeType for months_days_ns {
result[6] = days[2];
result[7] = days[3];
(0..8).for_each(|i| {
result[8 + i] = ns[i];
result[8 + i] = micros[i];
});
result
}
Expand All @@ -339,7 +339,7 @@ impl NativeType for months_days_ns {
fn to_be_bytes(&self) -> Self::Bytes {
let months = self.months().to_be_bytes();
let days = self.days().to_be_bytes();
let ns = self.ns().to_be_bytes();
let micros = self.micros().to_be_bytes();
let mut result = [0; 16];
result[0] = months[0];
result[1] = months[1];
Expand All @@ -350,7 +350,7 @@ impl NativeType for months_days_ns {
result[6] = days[2];
result[7] = days[3];
(0..8).for_each(|i| {
result[8 + i] = ns[i];
result[8 + i] = micros[i];
});
result
}
Expand All @@ -367,14 +367,14 @@ impl NativeType for months_days_ns {
days[1] = bytes[5];
days[2] = bytes[6];
days[3] = bytes[7];
let mut ns = [0; 8];
let mut micros = [0; 8];
(0..8).for_each(|i| {
ns[i] = bytes[8 + i];
micros[i] = bytes[8 + i];
});
Self(
i32::from_le_bytes(months),
i32::from_le_bytes(days),
i64::from_le_bytes(ns),
i64::from_le_bytes(micros),
)
}

Expand All @@ -390,14 +390,14 @@ impl NativeType for months_days_ns {
days[1] = bytes[5];
days[2] = bytes[6];
days[3] = bytes[7];
let mut ns = [0; 8];
let mut micros = [0; 8];
(0..8).for_each(|i| {
ns[i] = bytes[8 + i];
micros[i] = bytes[8 + i];
});
Self(
i32::from_be_bytes(months),
i32::from_be_bytes(days),
i64::from_be_bytes(ns),
i64::from_be_bytes(micros),
)
}
}
Expand All @@ -408,9 +408,9 @@ impl std::fmt::Display for days_ms {
}
}

impl std::fmt::Display for months_days_ns {
impl std::fmt::Display for months_days_micros {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}m {}d {}ns", self.months(), self.days(), self.ns())
write!(f, "{}m {}d {}micros", self.months(), self.days(), self.micros())
}
}

Expand All @@ -423,12 +423,12 @@ impl Neg for days_ms {
}
}

impl Neg for months_days_ns {
impl Neg for months_days_micros {
type Output = Self;

#[inline(always)]
fn neg(self) -> Self::Output {
Self::new(-self.months(), -self.days(), -self.ns())
Self::new(-self.months(), -self.days(), -self.micros())
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/common/column/src/types/simd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use super::days_ms;
use super::f16;
use super::i256;
use super::months_days_ns;
use super::months_days_micros;
use super::BitChunk;
use super::BitChunkIter;
use super::NativeType;
Expand Down Expand Up @@ -151,7 +151,7 @@ pub(super) use native_simd;
// of how they are represented in the different channels.
native_simd!(f16x32, f16, 32, u32);
native_simd!(days_msx8, days_ms, 8, u8);
native_simd!(months_days_nsx8, months_days_ns, 8, u8);
native_simd!(months_days_nsx8, months_days_micros, 8, u8);
native_simd!(i128x8, i128, 8, u8);
native_simd!(i256x8, i256, 8, u8);

Expand Down Expand Up @@ -185,4 +185,4 @@ native!(f64, f64x8);
native!(i128, i128x8);
native!(i256, i256x8);
native!(days_ms, days_msx8);
native!(months_days_ns, months_days_nsx8);
native!(months_days_micros, months_days_nsx8);
87 changes: 41 additions & 46 deletions src/common/io/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait BufferReadIntervalExt {
pub struct Interval {
pub months: i32,
pub days: i32,
pub nanos: i64,
pub micros: i64,
}

impl Display for Interval {
Expand Down Expand Up @@ -72,11 +72,11 @@ impl IntervalToStringCast {
}
}

fn format_nanos(mut nanos: i64, buffer: &mut [u8], length: &mut usize) {
if nanos < 0 {
nanos = -nanos;
fn format_micros(mut micros: i64, buffer: &mut [u8], length: &mut usize) {
if micros < 0 {
micros = -micros;
}
let s = format!("{:09}", nanos);
let s = format!("{:06}", micros);
let bytes = s.as_bytes();
buffer[*length..*length + bytes.len()].copy_from_slice(bytes);
*length += bytes.len();
Expand All @@ -97,23 +97,23 @@ impl IntervalToStringCast {
if interval.days != 0 {
Self::format_interval_value(interval.days, buffer, &mut length, " day");
}
if interval.nanos != 0 {
if interval.micros != 0 {
if length != 0 {
buffer[length] = b' ';
length += 1;
}
let mut nanos = interval.nanos;
let mut nanos = interval.micros;
if nanos < 0 {
buffer[length] = b'-';
length += 1;
nanos = -nanos;
}
let hour = nanos / NANO_PER_HOUR;
nanos -= hour * NANO_PER_HOUR;
let min = nanos / NANO_PER_MINUTE;
nanos -= min * NANO_PER_MINUTE;
let sec = nanos / NANO_PER_SEC;
nanos -= sec * NANO_PER_SEC;
let hour = nanos / MICROS_PER_HOUR;
nanos -= hour * MICROS_PER_HOUR;
let min = nanos / MICROS_PER_MINUTE;
nanos -= min * MICROS_PER_MINUTE;
let sec = nanos / MICROS_PER_SEC;
nanos -= sec * MICROS_PER_SEC;

Self::format_signed_number(hour, buffer, &mut length);
buffer[length] = b':';
Expand All @@ -125,7 +125,7 @@ impl IntervalToStringCast {
if nanos != 0 {
buffer[length] = b'.';
length += 1;
Self::format_nanos(nanos, buffer, &mut length);
Self::format_micros(nanos, buffer, &mut length);
}
} else if length == 0 {
buffer[..8].copy_from_slice(b"00:00:00");
Expand Down Expand Up @@ -213,7 +213,7 @@ impl Interval {
}
result.months = -result.months;
result.days = -result.days;
result.nanos = -result.nanos;
result.micros = -result.micros;
return Ok(result);
}
_ => {
Expand Down Expand Up @@ -261,12 +261,12 @@ fn parse_number(bytes: &[u8]) -> Result<(i64, i64, usize)> {
if pos < bytes.len() && bytes[pos] == b':' {
let time_bytes = &bytes[pos..];
let mut time_pos = 0;
let mut total_nanos: i64 = number * 60 * 60 * NANO_PER_SEC;
let mut total_nanos: i64 = number * 60 * 60 * MICROS_PER_SEC;
let mut colon_count = 0;

while colon_count < 2 && time_bytes.len() > time_pos {
let (minute, _, next_pos) = parse_time_part(&time_bytes[time_pos..])?;
let minute_nanos = minute * 60 * NANO_PER_SEC;
let minute_nanos = minute * 60 * MICROS_PER_SEC;
total_nanos += minute_nanos;
time_pos += next_pos;

Expand All @@ -279,7 +279,7 @@ fn parse_number(bytes: &[u8]) -> Result<(i64, i64, usize)> {
}
if time_bytes.len() > time_pos {
let (seconds, nanos, next_pos) = parse_time_part_with_nanos(&time_bytes[time_pos..])?;
total_nanos += seconds * NANO_PER_SEC + nanos;
total_nanos += seconds * MICROS_PER_SEC + nanos;
time_pos += next_pos;
}
return Ok((total_nanos, 0, pos + time_pos));
Expand Down Expand Up @@ -390,11 +390,10 @@ fn try_get_date_part_specifier(specifier_str: &str) -> Result<DatePartSpecifier>
}
}

const NANO_PER_SEC: i64 = 1_000_000_000;
const NANO_PER_MSEC: i64 = 1_000_000;
const NANO_PER_MICROS: i64 = 1_000;
const NANO_PER_MINUTE: i64 = 60 * NANO_PER_SEC;
const NANO_PER_HOUR: i64 = 60 * NANO_PER_MINUTE;
const MICROS_PER_SEC: i64 = 1_000_000;
const MICROS_PER_MSEC: i64 = 1_000;
const MICROS_PER_MINUTE: i64 = 60 * MICROS_PER_SEC;
const MICROS_PER_HOUR: i64 = 60 * MICROS_PER_MINUTE;
const DAYS_PER_WEEK: i32 = 7;
const MONTHS_PER_QUARTER: i32 = 3;
const MONTHS_PER_YEAR: i32 = 12;
Expand All @@ -409,12 +408,12 @@ fn apply_specifier(
specifier_str: &str,
) -> Result<()> {
if specifier_str.is_empty() {
result.nanos = result
.nanos
result.micros = result
.micros
.checked_add(number)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
result.nanos = result
.nanos
result.micros = result
.micros
.checked_add(fraction)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
return Ok(());
Expand Down Expand Up @@ -515,51 +514,47 @@ fn apply_specifier(
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
}
DatePartSpecifier::Microseconds => {
result.nanos = result
.nanos
.checked_add(
number
.checked_mul(NANO_PER_MICROS)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?,
)
result.micros = result
.micros
.checked_add(number)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
}
DatePartSpecifier::Milliseconds => {
result.nanos = result
.nanos
result.micros = result
.micros
.checked_add(
number
.checked_mul(NANO_PER_MSEC)
.checked_mul(MICROS_PER_MSEC)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?,
)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
}
DatePartSpecifier::Second => {
result.nanos = result
.nanos
result.micros = result
.micros
.checked_add(
number
.checked_mul(NANO_PER_SEC)
.checked_mul(MICROS_PER_SEC)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?,
)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
}
DatePartSpecifier::Minute => {
result.nanos = result
.nanos
result.micros = result
.micros
.checked_add(
number
.checked_mul(NANO_PER_MINUTE)
.checked_mul(MICROS_PER_MINUTE)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?,
)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
}
DatePartSpecifier::Hour => {
result.nanos = result
.nanos
result.micros = result
.micros
.checked_add(
number
.checked_mul(NANO_PER_HOUR)
.checked_mul(MICROS_PER_HOUR)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?,
)
.ok_or(ErrorCode::BadArguments("Overflow".to_string()))?;
Expand Down
Loading

0 comments on commit 0f40b74

Please sign in to comment.