Skip to content

Commit 2e5fec4

Browse files
authored
feat(query): add interval type (#16990)
* support Interval DataType :) select to_interval('02:01'), to_interval('1 year 1 day 1 hour'); โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ to_interval('02:01') โ”‚ to_interval('1 year 1 day 1 hour') โ”‚ โ”‚ Interval โ”‚ Interval โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ 2:01:00 โ”‚ 1year 1day 1:00:00 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ * refactor: months_days_ns(pub i32,pub i32,pub i64) -> months_days_micros(pub i128)
1 parent 85f4771 commit 2e5fec4

File tree

69 files changed

+1869
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1869
-123
lines changed

โ€ŽCargo.lockโ€Ž

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

โ€Žsrc/common/column/Cargo.tomlโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ column-default = [
2121
]
2222

2323
[dependencies]
24-
24+
borsh = { workspace = true, features = ["derive"] }
2525
databend-common-base = { workspace = true }
2626
databend-common-exception = { workspace = true }
2727

โ€Ž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: 49 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ use std::convert::TryFrom;
1717
use std::ops::Neg;
1818
use std::panic::RefUnwindSafe;
1919

20+
use borsh::BorshDeserialize;
21+
use borsh::BorshSerialize;
2022
use bytemuck::Pod;
2123
use bytemuck::Zeroable;
2224
use databend_common_base::base::OrderedFloat;
25+
use serde_derive::Deserialize;
26+
use serde_derive::Serialize;
2327

2428
use super::PrimitiveType;
2529

@@ -243,124 +247,71 @@ impl NativeType for days_ms {
243247
}
244248

245249
/// The in-memory representation of the MonthDayNano variant of the "Interval" logical type.
246-
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, Hash, Zeroable, Pod)]
250+
#[derive(
251+
Debug,
252+
Copy,
253+
Clone,
254+
Default,
255+
PartialEq,
256+
PartialOrd,
257+
Ord,
258+
Eq,
259+
Hash,
260+
Zeroable,
261+
Pod,
262+
Serialize,
263+
Deserialize,
264+
BorshSerialize,
265+
BorshDeserialize,
266+
)]
247267
#[allow(non_camel_case_types)]
248268
#[repr(C)]
249-
pub struct months_days_ns(pub i32, pub i32, pub i64);
269+
pub struct months_days_micros(pub i128);
250270

251-
impl months_days_ns {
252-
/// A new [`months_days_ns`].
253-
#[inline]
254-
pub fn new(months: i32, days: i32, nanoseconds: i64) -> Self {
255-
Self(months, days, nanoseconds)
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;
276+
277+
Self(months_bits | days_bits | micros_bits)
256278
}
257279

258-
/// The number of months
259-
#[inline]
260280
pub fn months(&self) -> i32 {
261-
self.0
281+
// Decoding logic
282+
((self.0 >> 96) & 0xFFFFFFFF) as i32
262283
}
263284

264-
/// The number of days
265-
#[inline]
266285
pub fn days(&self) -> i32 {
267-
self.1
286+
((self.0 >> 64) & 0xFFFFFFFF) as i32
268287
}
269288

270-
/// The number of nanoseconds
271-
#[inline]
272-
pub fn ns(&self) -> i64 {
273-
self.2
289+
pub fn microseconds(&self) -> i64 {
290+
(self.0 & 0xFFFFFFFFFFFFFFFF) as i64
274291
}
275292
}
276293

277-
impl NativeType for months_days_ns {
294+
impl NativeType for months_days_micros {
278295
const PRIMITIVE: PrimitiveType = PrimitiveType::MonthDayNano;
279296
type Bytes = [u8; 16];
280297
#[inline]
281298
fn to_le_bytes(&self) -> Self::Bytes {
282-
let months = self.months().to_le_bytes();
283-
let days = self.days().to_le_bytes();
284-
let ns = self.ns().to_le_bytes();
285-
let mut result = [0; 16];
286-
result[0] = months[0];
287-
result[1] = months[1];
288-
result[2] = months[2];
289-
result[3] = months[3];
290-
result[4] = days[0];
291-
result[5] = days[1];
292-
result[6] = days[2];
293-
result[7] = days[3];
294-
(0..8).for_each(|i| {
295-
result[8 + i] = ns[i];
296-
});
297-
result
299+
self.0.to_le_bytes()
298300
}
299301

300302
#[inline]
301303
fn to_be_bytes(&self) -> Self::Bytes {
302-
let months = self.months().to_be_bytes();
303-
let days = self.days().to_be_bytes();
304-
let ns = self.ns().to_be_bytes();
305-
let mut result = [0; 16];
306-
result[0] = months[0];
307-
result[1] = months[1];
308-
result[2] = months[2];
309-
result[3] = months[3];
310-
result[4] = days[0];
311-
result[5] = days[1];
312-
result[6] = days[2];
313-
result[7] = days[3];
314-
(0..8).for_each(|i| {
315-
result[8 + i] = ns[i];
316-
});
317-
result
304+
self.0.to_be_bytes()
318305
}
319306

320307
#[inline]
321308
fn from_le_bytes(bytes: Self::Bytes) -> Self {
322-
let mut months = [0; 4];
323-
months[0] = bytes[0];
324-
months[1] = bytes[1];
325-
months[2] = bytes[2];
326-
months[3] = bytes[3];
327-
let mut days = [0; 4];
328-
days[0] = bytes[4];
329-
days[1] = bytes[5];
330-
days[2] = bytes[6];
331-
days[3] = bytes[7];
332-
let mut ns = [0; 8];
333-
(0..8).for_each(|i| {
334-
ns[i] = bytes[8 + i];
335-
});
336-
Self(
337-
i32::from_le_bytes(months),
338-
i32::from_le_bytes(days),
339-
i64::from_le_bytes(ns),
340-
)
309+
Self(i128::from_le_bytes(bytes))
341310
}
342311

343312
#[inline]
344313
fn from_be_bytes(bytes: Self::Bytes) -> Self {
345-
let mut months = [0; 4];
346-
months[0] = bytes[0];
347-
months[1] = bytes[1];
348-
months[2] = bytes[2];
349-
months[3] = bytes[3];
350-
let mut days = [0; 4];
351-
days[0] = bytes[4];
352-
days[1] = bytes[5];
353-
days[2] = bytes[6];
354-
days[3] = bytes[7];
355-
let mut ns = [0; 8];
356-
(0..8).for_each(|i| {
357-
ns[i] = bytes[8 + i];
358-
});
359-
Self(
360-
i32::from_be_bytes(months),
361-
i32::from_be_bytes(days),
362-
i64::from_be_bytes(ns),
363-
)
314+
Self(i128::from_be_bytes(bytes))
364315
}
365316
}
366317

@@ -370,9 +321,15 @@ impl std::fmt::Display for days_ms {
370321
}
371322
}
372323

373-
impl std::fmt::Display for months_days_ns {
324+
impl std::fmt::Display for months_days_micros {
374325
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
375-
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+
)
376333
}
377334
}
378335

@@ -385,12 +342,12 @@ impl Neg for days_ms {
385342
}
386343
}
387344

388-
impl Neg for months_days_ns {
345+
impl Neg for months_days_micros {
389346
type Output = Self;
390347

391348
#[inline(always)]
392349
fn neg(self) -> Self::Output {
393-
Self::new(-self.months(), -self.days(), -self.ns())
350+
Self::new(-self.months(), -self.days(), -self.microseconds())
394351
}
395352
}
396353

โ€Ž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)