Skip to content

Commit 5294f43

Browse files
authored
refactor(common): cleanup unused methods on IntervalUnit (risingwavelabs#8456)
1 parent f12b263 commit 5294f43

File tree

3 files changed

+17
-56
lines changed

3 files changed

+17
-56
lines changed

src/common/src/array/interval_array.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@ mod tests {
3434
}
3535
let ret_arr = array_builder.finish();
3636
for v in ret_arr.iter().flatten() {
37-
assert_eq!(v.get_years(), 1);
3837
assert_eq!(v.get_months(), 12);
3938
assert_eq!(v.get_days(), 0);
4039
}
4140
let ret_arr = IntervalArray::from_iter([Some(IntervalUnit::from_ymd(1, 0, 0)), None]);
4241
let v = ret_arr.value_at(0).unwrap();
43-
assert_eq!(v.get_years(), 1);
4442
assert_eq!(v.get_months(), 12);
4543
assert_eq!(v.get_days(), 0);
4644
let v = ret_arr.value_at(1);
4745
assert_eq!(v, None);
4846
let v = unsafe { ret_arr.value_at_unchecked(0).unwrap() };
49-
assert_eq!(v.get_years(), 1);
5047
assert_eq!(v.get_months(), 12);
5148
assert_eq!(v.get_days(), 0);
5249
}

src/common/src/types/interval.rs

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::hash::{Hash, Hasher};
1919
use std::io::Write;
2020
use std::ops::{Add, Neg, Sub};
2121

22-
use anyhow::anyhow;
2322
use byteorder::{BigEndian, NetworkEndian, ReadBytesExt, WriteBytesExt};
2423
use bytes::BytesMut;
2524
use num_traits::{CheckedAdd, CheckedNeg, CheckedSub, Zero};
@@ -69,10 +68,6 @@ impl IntervalUnit {
6968
self.months
7069
}
7170

72-
pub fn get_years(&self) -> i32 {
73-
self.months / 12
74-
}
75-
7671
pub fn get_ms(&self) -> i64 {
7772
self.ms
7873
}
@@ -81,40 +76,12 @@ impl IntervalUnit {
8176
self.ms.rem_euclid(DAY_MS) as u64
8277
}
8378

84-
pub fn from_protobuf_bytes(bytes: &[u8], ty: IntervalType) -> ArrayResult<Self> {
85-
// TODO: remove IntervalType later.
86-
match ty {
87-
// the unit is months
88-
Year | YearToMonth | Month => {
89-
let bytes = bytes
90-
.try_into()
91-
.map_err(|e| anyhow!("Failed to deserialize i32: {:?}", e))?;
92-
let mouths = i32::from_be_bytes(bytes);
93-
Ok(IntervalUnit::from_month(mouths))
94-
}
95-
// the unit is ms
96-
Day | DayToHour | DayToMinute | DayToSecond | Hour | HourToMinute | HourToSecond
97-
| Minute | MinuteToSecond | Second => {
98-
let bytes = bytes
99-
.try_into()
100-
.map_err(|e| anyhow!("Failed to deserialize i64: {:?}", e))?;
101-
let ms = i64::from_be_bytes(bytes);
102-
Ok(IntervalUnit::from_millis(ms))
103-
}
104-
Unspecified => {
105-
// Invalid means the interval is from the new frontend.
106-
// TODO: make this default path later.
107-
let mut cursor = Cursor::new(bytes);
108-
read_interval_unit(&mut cursor)
109-
}
110-
}
111-
}
112-
11379
/// Justify interval, convert 1 month to 30 days and 86400 ms to 1 day.
11480
/// If day is positive, complement the ms negative value.
11581
/// These rules only use in interval comparison.
11682
pub fn justify_interval(&mut self) {
117-
let total_ms = self.total_ms();
83+
#[expect(deprecated)]
84+
let total_ms = self.as_ms_i64();
11885
*self = Self {
11986
months: 0,
12087
days: (total_ms / DAY_MS) as i32,
@@ -128,8 +95,8 @@ impl IntervalUnit {
12895
interval
12996
}
13097

131-
#[must_use]
132-
pub fn from_total_ms(ms: i64) -> Self {
98+
#[deprecated]
99+
fn from_total_ms(ms: i64) -> Self {
133100
let mut remaining_ms = ms;
134101
let months = remaining_ms / MONTH_MS;
135102
remaining_ms -= months * MONTH_MS;
@@ -142,10 +109,6 @@ impl IntervalUnit {
142109
}
143110
}
144111

145-
pub fn total_ms(&self) -> i64 {
146-
self.months as i64 * MONTH_MS + self.days as i64 * DAY_MS + self.ms
147-
}
148-
149112
#[must_use]
150113
pub fn from_ymd(year: i32, month: i32, days: i32) -> Self {
151114
let months = year * 12 + month;
@@ -186,13 +149,6 @@ impl IntervalUnit {
186149
}
187150
}
188151

189-
pub fn to_protobuf_owned(self) -> Vec<u8> {
190-
let buf = BytesMut::with_capacity(16);
191-
let mut writer = buf.writer();
192-
self.to_protobuf(&mut writer).unwrap();
193-
writer.into_inner().to_vec()
194-
}
195-
196152
pub fn to_protobuf<T: Write>(self, output: &mut T) -> ArrayResult<usize> {
197153
output.write_i32::<BigEndian>(self.months)?;
198154
output.write_i32::<BigEndian>(self.days)?;
@@ -225,10 +181,13 @@ impl IntervalUnit {
225181
return None;
226182
}
227183

184+
#[expect(deprecated)]
228185
let ms = self.as_ms_i64();
186+
#[expect(deprecated)]
229187
Some(IntervalUnit::from_total_ms((ms as f64 / rhs).round() as i64))
230188
}
231189

190+
#[deprecated]
232191
fn as_ms_i64(&self) -> i64 {
233192
self.months as i64 * MONTH_MS + self.days as i64 * DAY_MS + self.ms
234193
}
@@ -241,7 +200,9 @@ impl IntervalUnit {
241200
let rhs = rhs.try_into().ok()?;
242201
let rhs = rhs.0;
243202

203+
#[expect(deprecated)]
244204
let ms = self.as_ms_i64();
205+
#[expect(deprecated)]
245206
Some(IntervalUnit::from_total_ms((ms as f64 * rhs).round() as i64))
246207
}
247208

@@ -474,6 +435,8 @@ impl IntervalUnit {
474435
}
475436
}
476437

438+
/// Wrapper so that `Debug for IntervalUnitDisplay` would use the concise format of `Display for
439+
/// IntervalUnit`.
477440
#[derive(Clone, Copy)]
478441
pub struct IntervalUnitDisplay<'a> {
479442
pub core: &'a IntervalUnit,
@@ -491,6 +454,8 @@ impl std::fmt::Debug for IntervalUnitDisplay<'_> {
491454
}
492455
}
493456

457+
/// Loss of information during the process due to `justify`. Only intended for memcomparable
458+
/// encoding.
494459
impl Serialize for IntervalUnit {
495460
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
496461
where
@@ -512,6 +477,7 @@ impl<'de> Deserialize<'de> for IntervalUnit {
512477
}
513478
}
514479

480+
/// Duplicated logic only used by `HopWindow`. See #8452.
515481
#[expect(clippy::from_over_into)]
516482
impl Into<IntervalUnitProto> for IntervalUnit {
517483
fn into(self) -> IntervalUnitProto {

src/common/src/types/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ mod scalar_impl;
3232
mod successor;
3333

3434
use std::fmt::Debug;
35-
use std::io::Cursor;
3635
use std::str::{FromStr, Utf8Error};
3736

3837
pub use native_type::*;
39-
use risingwave_pb::data::data_type::IntervalType::*;
40-
use risingwave_pb::data::data_type::{IntervalType, TypeName};
38+
use risingwave_pb::data::data_type::TypeName;
4139
pub use scalar_impl::*;
4240
pub use successor::*;
4341
pub mod chrono_wrapper;
@@ -68,8 +66,8 @@ use self::to_binary::ToBinary;
6866
use self::to_text::ToText;
6967
use crate::array::serial_array::Serial;
7068
use crate::array::{
71-
read_interval_unit, ArrayBuilderImpl, JsonbRef, JsonbVal, ListRef, ListValue,
72-
PrimitiveArrayItemType, StructRef, StructValue,
69+
ArrayBuilderImpl, JsonbRef, JsonbVal, ListRef, ListValue, PrimitiveArrayItemType, StructRef,
70+
StructValue,
7371
};
7472
use crate::error::Result as RwResult;
7573

0 commit comments

Comments
 (0)