Skip to content

Commit 26f07e8

Browse files
committed
fmt
1 parent c8bcc21 commit 26f07e8

File tree

7 files changed

+55
-66
lines changed

7 files changed

+55
-66
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ databend-common-storages-stage = { path = "src/query/storages/stage" }
176176
databend-common-storages-stream = { path = "src/query/storages/stream" }
177177
databend-common-storages-system = { path = "src/query/storages/system" }
178178
databend-common-telemetry = { path = "src/common/telemetry" }
179+
databend-common-timezone = { path = "src/common/timezone" }
179180
databend-common-tracing = { path = "src/common/tracing" }
180181
databend-common-users = { path = "src/query/users" }
181182
databend-common-vector = { path = "src/common/vector" }
182-
databend-common-timezone = { path = "src/common/timezone" }
183183
databend-common-version = { path = "src/common/version" }
184184
databend-enterprise-attach-table = { path = "src/query/ee_features/attach_table" }
185185
databend-enterprise-data-mask-feature = { path = "src/query/ee_features/data_mask" }

src/common/io/src/cursor_ext/cursor_read_datetime_ext.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,15 @@ where T: AsRef<[u8]>
322322
// only date part
323323
if need_date {
324324
Ok(DateTimeResType::Date(d))
325+
} else if let Some(zoned) = fast_local_to_zoned(tz, &d, 0, 0, 0, 0) {
326+
Ok(DateTimeResType::Datetime(zoned))
325327
} else {
326-
if let Some(zoned) = fast_local_to_zoned(tz, &d, 0, 0, 0, 0) {
327-
Ok(DateTimeResType::Datetime(zoned))
328-
} else {
329-
Ok(DateTimeResType::Datetime(
330-
d.to_zoned(tz.clone())
331-
.map_err_to_code(ErrorCode::BadBytes, || {
332-
format!("Failed to parse date {} as timestamp.", d)
333-
})?,
334-
))
335-
}
328+
Ok(DateTimeResType::Datetime(
329+
d.to_zoned(tz.clone())
330+
.map_err_to_code(ErrorCode::BadBytes, || {
331+
format!("Failed to parse date {} as timestamp.", d)
332+
})?,
333+
))
336334
}
337335
}
338336
}

src/common/timezone/src/lib.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,10 @@ impl DayEntry {
126126
} else {
127127
return None;
128128
}
129+
} else if local_seconds >= transition_elapsed {
130+
elapsed = local_seconds - offset_change;
129131
} else {
130-
if local_seconds >= transition_elapsed {
131-
elapsed = local_seconds - offset_change;
132-
} else {
133-
elapsed = local_seconds;
134-
}
132+
elapsed = local_seconds;
135133
}
136134
}
137135
}
@@ -204,8 +202,9 @@ impl TimeZoneLut {
204202
}
205203
}
206204

207-
static TZ_LUTS: LazyLock<RwLock<Vec<(TimeZone, Arc<TimeZoneLut>)>>> =
208-
LazyLock::new(|| RwLock::new(Vec::new()));
205+
type LutCache = RwLock<Vec<(TimeZone, Arc<TimeZoneLut>)>>;
206+
207+
static TZ_LUTS: LazyLock<LutCache> = LazyLock::new(|| RwLock::new(Vec::new()));
209208

210209
fn get_or_init_lut(tz: &TimeZone) -> Arc<TimeZoneLut> {
211210
{
@@ -315,7 +314,7 @@ fn day_of_year(year: i32, month: u8, day: u8) -> u16 {
315314
}
316315

317316
fn day_index_for_date(year: i32, month: u8, day: u8) -> Option<usize> {
318-
if year < LUT_MIN_YEAR || year > LUT_MAX_YEAR {
317+
if !(LUT_MIN_YEAR..=LUT_MAX_YEAR).contains(&year) {
319318
return None;
320319
}
321320
if month == 0 || month > 12 {
@@ -361,13 +360,14 @@ fn weeks_in_year(year: i32) -> u32 {
361360

362361
#[cfg(test)]
363362
mod tests {
364-
use super::*;
365363
use jiff::tz::TimeZone;
366364
use jiff::Timestamp;
367365
use rand::rngs::StdRng;
368366
use rand::Rng;
369367
use rand::SeedableRng;
370368

369+
use super::*;
370+
371371
const START_SECS: i64 = -2_208_988_800;
372372
const END_SECS: i64 = 10_413_792_000;
373373

@@ -406,7 +406,10 @@ mod tests {
406406
assert_eq!(components.minute, zoned.minute() as u8);
407407
assert_eq!(components.second, zoned.second() as u8);
408408
assert_eq!(components.offset_seconds, zoned.offset().seconds());
409-
assert_eq!(components.day_of_year as usize, zoned.day_of_year() as usize);
409+
assert_eq!(
410+
components.day_of_year as usize,
411+
zoned.day_of_year() as usize
412+
);
410413
assert_eq!(components.weekday, zoned.weekday());
411414

412415
let (iso_year, iso_week) = components.iso_year_week();
@@ -428,7 +431,11 @@ mod tests {
428431
) {
429432
let time = jiff::civil::Time::new(hour as i8, minute as i8, second as i8, 0).unwrap();
430433
let dt = jiff::civil::date(year as i16, month as i8, day as i8).to_datetime(time);
431-
let expected = dt.to_zoned(tz.clone()).unwrap().timestamp().as_microsecond();
434+
let expected = dt
435+
.to_zoned(tz.clone())
436+
.unwrap()
437+
.timestamp()
438+
.as_microsecond();
432439
let actual = fast_utc_from_local(tz, year, month, day, hour, minute, second, 0)
433440
.expect("fast path should succeed");
434441
assert_eq!(expected, actual);

src/query/expression/src/utils/date_helper.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use std::sync::LazyLock;
1616

1717
use databend_common_exception::Result;
18+
use databend_common_timezone::fast_components_from_timestamp;
19+
use databend_common_timezone::fast_utc_from_local;
20+
use databend_common_timezone::DateTimeComponents;
1821
use jiff::civil::date;
1922
use jiff::civil::datetime;
2023
use jiff::civil::Date;
@@ -33,9 +36,6 @@ use num_traits::AsPrimitive;
3336
use crate::types::date::clamp_date;
3437
use crate::types::timestamp::clamp_timestamp;
3538
use crate::types::timestamp::MICROS_PER_SEC;
36-
use databend_common_timezone::fast_components_from_timestamp;
37-
use databend_common_timezone::fast_utc_from_local;
38-
use databend_common_timezone::DateTimeComponents;
3939

4040
// jiff's `Timestamp` only accepts UTC seconds in
4141
// [-377705023201, 253402207200] so that any ±25:59:59 offset still
@@ -221,12 +221,7 @@ impl_interval_year_month!(EvalYearsImpl, eval_years_base);
221221
impl_interval_year_month!(EvalMonthsImpl, eval_months_base);
222222

223223
fn components_time_is_before(a: &DateTimeComponents, b: &DateTimeComponents) -> bool {
224-
(
225-
a.hour,
226-
a.minute,
227-
a.second,
228-
a.micro,
229-
) < (b.hour, b.minute, b.second, b.micro)
224+
(a.hour, a.minute, a.second, a.micro) < (b.hour, b.minute, b.second, b.micro)
230225
}
231226

232227
fn date_from_components(c: &DateTimeComponents) -> Option<Date> {
@@ -235,14 +230,12 @@ fn date_from_components(c: &DateTimeComponents) -> Option<Date> {
235230

236231
fn datetime_from_components(c: &DateTimeComponents) -> Option<DateTime> {
237232
let date = date_from_components(c)?;
238-
Some(
239-
date.at(
240-
c.hour as i8,
241-
c.minute as i8,
242-
c.second as i8,
243-
(c.micro * 1_000) as i32,
244-
),
245-
)
233+
Some(date.at(
234+
c.hour as i8,
235+
c.minute as i8,
236+
c.second as i8,
237+
(c.micro * 1_000) as i32,
238+
))
246239
}
247240

248241
impl EvalYearsImpl {
@@ -1237,7 +1230,7 @@ impl ToNumber<u32> for ToYYYYWW {
12371230

12381231
fn from_components(components: &DateTimeComponents) -> Option<u32> {
12391232
let (iso_year, iso_week) = components.iso_year_week();
1240-
Some(iso_year as u32 * 100 + iso_week as u32)
1233+
Some(iso_year as u32 * 100 + iso_week)
12411234
}
12421235
}
12431236

@@ -1248,7 +1241,7 @@ impl ToNumber<u8> for ToQuarter {
12481241
}
12491242

12501243
fn from_components(components: &DateTimeComponents) -> Option<u8> {
1251-
Some(((components.month - 1) / 3 + 1) as u8)
1244+
Some((components.month - 1) / 3 + 1)
12521245
}
12531246
}
12541247

src/query/expression/tests/it/types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ fn test_parse_jiff() {
6161
// that only specify a Unix timestamp (`%s`), verify via `to_timestamp`.
6262
let (mut tm, _) = BrokenDownTime::parse_prefix("%s", "200").unwrap();
6363
tm.set_offset(Some(tz::offset(0 as _)));
64-
assert_eq!("1970-01-01T00:03:20Z", tm.to_timestamp().unwrap().to_string());
64+
assert_eq!(
65+
"1970-01-01T00:03:20Z",
66+
tm.to_timestamp().unwrap().to_string()
67+
);
6568
}
6669

6770
#[test]

src/query/functions/src/scalars/timestamp/src/datetime.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ use databend_common_expression::types::StringType;
6161
use databend_common_expression::types::TimestampType;
6262
use databend_common_expression::types::F64;
6363
use databend_common_expression::utils::date_helper::*;
64-
use databend_common_timezone::fast_components_from_timestamp;
65-
use databend_common_timezone::fast_utc_from_local;
6664
use databend_common_expression::vectorize_1_arg;
6765
use databend_common_expression::vectorize_2_arg;
6866
use databend_common_expression::vectorize_4_arg;
@@ -75,6 +73,8 @@ use databend_common_expression::FunctionDomain;
7573
use databend_common_expression::FunctionProperty;
7674
use databend_common_expression::FunctionRegistry;
7775
use databend_common_expression::Value;
76+
use databend_common_timezone::fast_components_from_timestamp;
77+
use databend_common_timezone::fast_utc_from_local;
7878
use dtparse::parse;
7979
use jiff::civil::date;
8080
use jiff::civil::Date;
@@ -613,16 +613,7 @@ fn fast_timestamp_from_tm(tm: &BrokenDownTime, tz: &TimeZone) -> Option<i64> {
613613
let second: u8 = tm.second().unwrap_or(0).try_into().ok()?;
614614
let nanos = tm.subsec_nanosecond().unwrap_or(0);
615615
let micro = (nanos / 1_000).max(0) as u32;
616-
fast_utc_from_local(
617-
tz,
618-
year,
619-
month,
620-
day,
621-
hour,
622-
minute,
623-
second,
624-
micro,
625-
)
616+
fast_utc_from_local(tz, year, month, day, hour, minute, second, micro)
626617
}
627618

628619
fn register_date_to_timestamp(registry: &mut FunctionRegistry) {
@@ -977,9 +968,8 @@ fn timestamp_days_via_lut(value: i64, tz: &TimeZone) -> Option<i32> {
977968
}
978969

979970
fn days_from_components(year: i32, month: u8, day: u8) -> Option<i32> {
980-
NaiveDate::from_ymd_opt(year, month as u32, day as u32).map(|d| {
981-
clamp_date((d.num_days_from_ce() - EPOCH_DAYS_FROM_CE) as i64)
982-
})
971+
NaiveDate::from_ymd_opt(year, month as u32, day as u32)
972+
.map(|d| clamp_date((d.num_days_from_ce() - EPOCH_DAYS_FROM_CE) as i64))
983973
}
984974

985975
fn timestamp_days_via_jiff(value: i64, tz: &TimeZone) -> i32 {
@@ -993,10 +983,11 @@ fn timestamp_days_via_jiff(value: i64, tz: &TimeZone) -> i32 {
993983

994984
#[cfg(test)]
995985
mod tests {
996-
use super::*;
997986
use chrono::NaiveDate;
998987
use jiff::tz::TimeZone;
999988

989+
use super::*;
990+
1000991
fn micros_for_utc(year: i32, month: u32, day: u32, hour: u32, minute: u32, second: u32) -> i64 {
1001992
let naive = NaiveDate::from_ymd_opt(year, month, day)
1002993
.unwrap()
@@ -1025,10 +1016,7 @@ mod tests {
10251016
let tz = TimeZone::UTC;
10261017
let past = micros_for_utc(1800, 7, 1, 0, 0, 0);
10271018
assert!(timestamp_days_via_lut(past, &tz).is_none());
1028-
assert_eq!(
1029-
timestamp_to_date_days(past, &tz),
1030-
expected_days(1800, 7, 1)
1031-
);
1019+
assert_eq!(timestamp_to_date_days(past, &tz), expected_days(1800, 7, 1));
10321020

10331021
let future = micros_for_utc(2400, 3, 15, 12, 0, 0);
10341022
assert!(timestamp_days_via_lut(future, &tz).is_none());

src/query/functions/src/scalars/timestamp/src/interval.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ use databend_common_expression::types::Int64Type;
3030
use databend_common_expression::types::IntervalType;
3131
use databend_common_expression::types::StringType;
3232
use databend_common_expression::types::TimestampType;
33-
use databend_common_timezone::fast_components_from_timestamp;
34-
use databend_common_timezone::DateTimeComponents;
3533
use databend_common_expression::vectorize_2_arg;
3634
use databend_common_expression::vectorize_with_builder_1_arg;
3735
use databend_common_expression::vectorize_with_builder_2_arg;
3836
use databend_common_expression::EvalContext;
3937
use databend_common_expression::FunctionDomain;
4038
use databend_common_expression::FunctionRegistry;
4139
use databend_common_expression::Value;
40+
use databend_common_timezone::fast_components_from_timestamp;
41+
use databend_common_timezone::DateTimeComponents;
4242
use jiff::tz::Offset;
4343
use jiff::tz::TimeZone;
4444
use jiff::Timestamp;
@@ -290,7 +290,7 @@ fn register_interval_add_sub_mul(registry: &mut FunctionRegistry) {
290290
is_negative = true;
291291
}
292292
let zone1 = match Offset::from_seconds(t1.seconds_offset())
293-
.map(|offset| TimeZone::fixed(offset))
293+
.map(TimeZone::fixed)
294294
{
295295
Ok(zone) => zone,
296296
Err(err) => {
@@ -299,7 +299,7 @@ fn register_interval_add_sub_mul(registry: &mut FunctionRegistry) {
299299
}
300300
};
301301
let zone2 = match Offset::from_seconds(t2.seconds_offset())
302-
.map(|offset| TimeZone::fixed(offset))
302+
.map(TimeZone::fixed)
303303
{
304304
Ok(zone) => zone,
305305
Err(err) => {

0 commit comments

Comments
 (0)