Skip to content

Commit cdaa8cf

Browse files
authored
fix(common): interval should have microsecond precision (risingwavelabs#8501)
1 parent b235f68 commit cdaa8cf

File tree

21 files changed

+258
-191
lines changed

21 files changed

+258
-191
lines changed

dashboard/proto/gen/data.ts

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

e2e_test/batch/types/interval.slt.part

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ select interval '1 year 1 month 1 day 1:1:1.009';
109109
----
110110
1 year 1 mon 1 day 01:01:01.009
111111

112-
# issue#7059, if we improve precision, then this should be removed.
113-
query TTTTTT
112+
# issue #7059
113+
query T
114114
select '1 mons 1 days 00:00:00.000001'::INTERVAL;
115115
----
116+
1 mon 1 day 00:00:00.000001
117+
118+
query T
119+
select '1 mons 1 days 00:00:00.0000001'::INTERVAL;
120+
----
116121
1 mon 1 day

e2e_test/batch/types/temporal_arithmetic.slt.part

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ select interval '20' / float '12.5';
6666
query T
6767
select interval '12 days' / 4.2;
6868
----
69-
2 days 20:34:17.143
69+
2 days 20:34:17.142857
7070

7171
query T
7272
SELECT interval '1 month' / 2000;
@@ -136,7 +136,7 @@ select real '6.1' * interval '1' second;
136136
query T
137137
select real '61.1' * interval '1' second;
138138
----
139-
00:01:01.1
139+
00:01:01.099998
140140

141141
query T
142142
select real '0.0' * interval '1' second;
@@ -161,7 +161,7 @@ select interval '1' second * real '6.1';
161161
query T
162162
select interval '1' second * real '61.1';
163163
----
164-
00:01:01.1
164+
00:01:01.099998
165165

166166
query T
167167
select interval '1' second * real '0.0';

proto/data.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ option optimize_for = SPEED;
1010
message IntervalUnit {
1111
int32 months = 1;
1212
int32 days = 2;
13-
int64 ms = 3;
13+
int64 usecs = 3;
1414
}
1515

1616
message DataType {

src/batch/src/executor/order_by.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,9 @@ mod tests {
464464
None] },
465465
column! { IntervalArray, [
466466
None,
467-
Some(IntervalUnit::new(1, 2, 3)),
467+
Some(IntervalUnit::from_month_day_usec(1, 2, 3)),
468468
None,
469-
Some(IntervalUnit::new(4, 5, 6)),
469+
Some(IntervalUnit::from_month_day_usec(4, 5, 6)),
470470
None] },
471471
],
472472
5,
@@ -491,8 +491,8 @@ mod tests {
491491
Some(NaiveDateTimeWrapper::with_secs_nsecs(1, 23).unwrap()),
492492
Some(NaiveDateTimeWrapper::with_secs_nsecs(7, 89).unwrap())] },
493493
column! { IntervalArray, [
494-
Some(IntervalUnit::new(4, 5, 6)),
495-
Some(IntervalUnit::new(1, 2, 3)),
494+
Some(IntervalUnit::from_month_day_usec(4, 5, 6)),
495+
Some(IntervalUnit::from_month_day_usec(1, 2, 3)),
496496
None,
497497
None,
498498
None] },

src/common/src/array/arrow.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,15 @@ impl FromIntoArrow for IntervalUnit {
348348

349349
fn from_arrow(value: Self::ArrowType) -> Self {
350350
let (months, days, ns) = arrow_array::types::IntervalMonthDayNanoType::to_parts(value);
351-
IntervalUnit::new(months, days, ns / 1_000_000)
351+
IntervalUnit::from_month_day_usec(months, days, ns / 1000)
352352
}
353353

354354
fn into_arrow(self) -> Self::ArrowType {
355355
arrow_array::types::IntervalMonthDayNanoType::make_value(
356356
self.get_months(),
357357
self.get_days(),
358-
self.get_ms() * 1_000_000,
358+
// TODO: this may overflow and we need `try_into`
359+
self.get_usecs() * 1000,
359360
)
360361
}
361362
}

src/common/src/array/column_proto_readers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ pub fn read_interval_unit(cursor: &mut Cursor<&[u8]>) -> ArrayResult<IntervalUni
101101
let mut read = || {
102102
let months = cursor.read_i32::<BigEndian>()?;
103103
let days = cursor.read_i32::<BigEndian>()?;
104-
let ms = cursor.read_i64::<BigEndian>()?;
104+
let usecs = cursor.read_i64::<BigEndian>()?;
105105

106-
Ok::<_, std::io::Error>(IntervalUnit::new(months, days, ms))
106+
Ok::<_, std::io::Error>(IntervalUnit::from_month_day_usec(months, days, usecs))
107107
};
108108

109109
match read() {

src/common/src/hash/key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,14 @@ impl HashKeySerDe<'_> for IntervalUnit {
365365
let mut ret = [0; 16];
366366
ret[0..4].copy_from_slice(&self.get_months().to_ne_bytes());
367367
ret[4..8].copy_from_slice(&self.get_days().to_ne_bytes());
368-
ret[8..16].copy_from_slice(&self.get_ms().to_ne_bytes());
368+
ret[8..16].copy_from_slice(&self.get_usecs().to_ne_bytes());
369369

370370
ret
371371
}
372372

373373
fn deserialize<R: Read>(source: &mut R) -> Self {
374374
let value = Self::read_fixed_size_bytes::<R, 16>(source);
375-
IntervalUnit::new(
375+
IntervalUnit::from_month_day_usec(
376376
i32::from_ne_bytes(value[0..4].try_into().unwrap()),
377377
i32::from_ne_bytes(value[4..8].try_into().unwrap()),
378378
i64::from_ne_bytes(value[8..16].try_into().unwrap()),

src/common/src/row/owned_row.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ mod tests {
228228
Some(ScalarImpl::Float32(4.0.into())),
229229
Some(ScalarImpl::Float64(5.0.into())),
230230
Some(ScalarImpl::Decimal("-233.3".parse().unwrap())),
231-
Some(ScalarImpl::Interval(IntervalUnit::new(7, 8, 9))),
231+
Some(ScalarImpl::Interval(IntervalUnit::from_month_day_usec(
232+
7, 8, 9,
233+
))),
232234
]);
233235
let value_indices = (0..9).collect_vec();
234236
let bytes = (&row).project(&value_indices).value_serialize();
@@ -261,10 +263,14 @@ mod tests {
261263
Some(ScalarImpl::Float32(4.0.into())),
262264
Some(ScalarImpl::Float64(5.0.into())),
263265
Some(ScalarImpl::Decimal("-233.3".parse().unwrap())),
264-
Some(ScalarImpl::Interval(IntervalUnit::new(7, 8, 9))),
266+
Some(ScalarImpl::Interval(IntervalUnit::from_month_day_usec(
267+
7, 8, 9,
268+
))),
265269
]);
266270
let row2 = OwnedRow::new(vec![
267-
Some(ScalarImpl::Interval(IntervalUnit::new(7, 8, 9))),
271+
Some(ScalarImpl::Interval(IntervalUnit::from_month_day_usec(
272+
7, 8, 9,
273+
))),
268274
Some(ScalarImpl::Utf8("string".into())),
269275
Some(ScalarImpl::Bool(true)),
270276
Some(ScalarImpl::Int16(1)),

src/common/src/test_utils/rand_array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ impl RandValue for IntervalUnit {
7878
fn rand_value<R: Rng>(rand: &mut R) -> Self {
7979
let months = rand.gen_range(0..100);
8080
let days = rand.gen_range(0..200);
81-
let ms = rand.gen_range(0..100_000);
82-
IntervalUnit::new(months, days, ms)
81+
let usecs = rand.gen_range(0..100_000);
82+
IntervalUnit::from_month_day_usec(months, days, usecs)
8383
}
8484
}
8585

src/common/src/types/chrono_wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl CheckedAdd<IntervalUnit> for NaiveDateTimeWrapper {
565565
}
566566
let mut datetime = NaiveDateTime::new(date, self.0.time());
567567
datetime = datetime.checked_add_signed(Duration::days(rhs.get_days().into()))?;
568-
datetime = datetime.checked_add_signed(Duration::milliseconds(rhs.get_ms()))?;
568+
datetime = datetime.checked_add_signed(Duration::microseconds(rhs.get_usecs()))?;
569569

570570
Some(NaiveDateTimeWrapper::new(datetime))
571571
}

0 commit comments

Comments
 (0)