Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): add interval type #16990

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/common/column/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ column-default = [
]

[dependencies]

borsh = { workspace = true, features = ["derive"] }
databend-common-base = { workspace = true }
databend-common-exception = { workspace = true }

Expand Down
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 {}
}
141 changes: 49 additions & 92 deletions src/common/column/src/types/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ use std::convert::TryFrom;
use std::ops::Neg;
use std::panic::RefUnwindSafe;

use borsh::BorshDeserialize;
use borsh::BorshSerialize;
use bytemuck::Pod;
use bytemuck::Zeroable;
use databend_common_base::base::OrderedFloat;
use serde_derive::Deserialize;
use serde_derive::Serialize;

use super::PrimitiveType;

Expand Down Expand Up @@ -243,124 +247,71 @@ impl NativeType for days_ms {
}

/// The in-memory representation of the MonthDayNano variant of the "Interval" logical type.
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, Zeroable, Pod)]
#[derive(
Debug,
Copy,
Clone,
Default,
PartialEq,
PartialOrd,
Ord,
Eq,
Hash,
Zeroable,
Pod,
Serialize,
Deserialize,
BorshSerialize,
BorshDeserialize,
)]
#[allow(non_camel_case_types)]
#[repr(C)]
pub struct months_days_ns(pub i32, pub i32, pub i64);
pub struct months_days_micros(pub i128);

impl months_days_ns {
/// A new [`months_days_ns`].
#[inline]
pub fn new(months: i32, days: i32, nanoseconds: i64) -> Self {
Self(months, days, nanoseconds)
impl months_days_micros {
pub fn new(months: i32, days: i32, microseconds: i64) -> Self {
let months_bits = (months as i128) << 96;
let days_bits = (days as i128) << 64;
let micros_bits = microseconds as i128;

Self(months_bits | days_bits | micros_bits)
}

/// The number of months
#[inline]
pub fn months(&self) -> i32 {
self.0
// Decoding logic
((self.0 >> 96) & 0xFFFFFFFF) as i32
}

/// The number of days
#[inline]
pub fn days(&self) -> i32 {
self.1
((self.0 >> 64) & 0xFFFFFFFF) as i32
}

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

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 mut result = [0; 16];
result[0] = months[0];
result[1] = months[1];
result[2] = months[2];
result[3] = months[3];
result[4] = days[0];
result[5] = days[1];
result[6] = days[2];
result[7] = days[3];
(0..8).for_each(|i| {
result[8 + i] = ns[i];
});
result
self.0.to_le_bytes()
}

#[inline]
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 mut result = [0; 16];
result[0] = months[0];
result[1] = months[1];
result[2] = months[2];
result[3] = months[3];
result[4] = days[0];
result[5] = days[1];
result[6] = days[2];
result[7] = days[3];
(0..8).for_each(|i| {
result[8 + i] = ns[i];
});
result
self.0.to_be_bytes()
}

#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
let mut months = [0; 4];
months[0] = bytes[0];
months[1] = bytes[1];
months[2] = bytes[2];
months[3] = bytes[3];
let mut days = [0; 4];
days[0] = bytes[4];
days[1] = bytes[5];
days[2] = bytes[6];
days[3] = bytes[7];
let mut ns = [0; 8];
(0..8).for_each(|i| {
ns[i] = bytes[8 + i];
});
Self(
i32::from_le_bytes(months),
i32::from_le_bytes(days),
i64::from_le_bytes(ns),
)
Self(i128::from_le_bytes(bytes))
}

#[inline]
fn from_be_bytes(bytes: Self::Bytes) -> Self {
let mut months = [0; 4];
months[0] = bytes[0];
months[1] = bytes[1];
months[2] = bytes[2];
months[3] = bytes[3];
let mut days = [0; 4];
days[0] = bytes[4];
days[1] = bytes[5];
days[2] = bytes[6];
days[3] = bytes[7];
let mut ns = [0; 8];
(0..8).for_each(|i| {
ns[i] = bytes[8 + i];
});
Self(
i32::from_be_bytes(months),
i32::from_be_bytes(days),
i64::from_be_bytes(ns),
)
Self(i128::from_be_bytes(bytes))
}
}

Expand All @@ -370,9 +321,15 @@ 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.microseconds()
)
}
}

Expand All @@ -385,12 +342,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.microseconds())
}
}

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_microsx8, 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_microsx8);
Loading
Loading