Skip to content

Commit

Permalink
fix: resolve data calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
chagelo committed Oct 3, 2024
1 parent aa130f1 commit dd0d158
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
44 changes: 42 additions & 2 deletions src/query/expression/src/utils/date_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,26 @@ impl EvalDaysImpl {
}

pub fn eval_timestamp_diff(date_start: i64, date_end: i64) -> i64 {
(date_end - date_start) / MICROSECS_PER_DAY
let (mut date_start, mut date_end) = (date_start, date_end);
let sign = if date_start > date_end {
(date_start, date_end) = (date_end, date_start);
-1
} else {
1
};

let (date_start_days, date_start_micros) = (
date_start / MICROSECS_PER_DAY,
date_start % MICROSECS_PER_DAY,
);
let (date_end_days, date_end_micros) =
(date_end / MICROSECS_PER_DAY, date_end % MICROSECS_PER_DAY);
let mut diff_days = date_end_days - date_start_days;

if date_start_micros > date_end_micros {
diff_days -= 1;
}
diff_days * sign
}
}

Expand All @@ -555,7 +574,28 @@ impl EvalTimesImpl {
}

pub fn eval_timestamp_diff(date_start: i64, date_end: i64, factor: i64) -> i64 {
(date_end - date_start) / factor
let (mut date_start, mut date_end) = (date_start, date_end);
let sign = if date_start > date_end {
(date_start, date_end) = (date_end, date_start);
-1
} else {
1
};

let (date_start_days, date_start_micros) = (
date_start / (MICROS_PER_SEC * factor),
date_start % (MICROS_PER_SEC * factor),
);
let (date_end_days, date_end_micros) = (
date_end / (MICROS_PER_SEC * factor),
date_end % (MICROS_PER_SEC * factor),
);
let mut diff_days = date_end_days - date_start_days;

if date_start_micros > date_end_micros {
diff_days -= 1;
}
diff_days * sign
}
}

Expand Down
36 changes: 18 additions & 18 deletions src/query/functions/tests/it/scalars/testdata/datetime.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3573,54 +3573,54 @@ output : -365
ast : date_diff(hour, to_timestamp('2023-11-12 09:38:18.165575'), to_timestamp('2025-03-27 21:01:35.423179'))
raw expr : diff_hours(to_timestamp('2025-03-27 21:01:35.423179'), to_timestamp('2023-11-12 09:38:18.165575'))
checked expr : diff_hours<Timestamp, Timestamp>(to_timestamp<String>("2025-03-27 21:01:35.423179"), to_timestamp<String>("2023-11-12 09:38:18.165575"))
optimized expr : 12035388127_i64
optimized expr : 12035_i64
output type : Int64
output domain : {12035388127..=12035388127}
output : 12035388127
output domain : {12035..=12035}
output : 12035


ast : date_diff(hour, to_timestamp('2020-02-29 23:59:59.165575'), to_timestamp('2019-02-28 23:59:59.423179'))
raw expr : diff_hours(to_timestamp('2019-02-28 23:59:59.423179'), to_timestamp('2020-02-29 23:59:59.165575'))
checked expr : diff_hours<Timestamp, Timestamp>(to_timestamp<String>("2019-02-28 23:59:59.423179"), to_timestamp<String>("2020-02-29 23:59:59.165575"))
optimized expr : -8783999928_i64
optimized expr : -8783_i64
output type : Int64
output domain : {-8783999928..=-8783999928}
output : -8783999928
output domain : {-8783..=-8783}
output : -8783


ast : date_diff(minute, to_timestamp('2023-11-12 09:38:18.165575'), to_timestamp('2025-03-27 21:01:35.423179'))
raw expr : diff_minutes(to_timestamp('2025-03-27 21:01:35.423179'), to_timestamp('2023-11-12 09:38:18.165575'))
checked expr : diff_minutes<Timestamp, Timestamp>(to_timestamp<String>("2025-03-27 21:01:35.423179"), to_timestamp<String>("2023-11-12 09:38:18.165575"))
optimized expr : 722123287626_i64
optimized expr : 722123_i64
output type : Int64
output domain : {722123287626..=722123287626}
output : 722123287626
output domain : {722123..=722123}
output : 722123


ast : date_diff(minute, to_timestamp('2020-02-29 23:59:59.165575'), to_timestamp('2019-02-28 23:59:59.423179'))
raw expr : diff_minutes(to_timestamp('2019-02-28 23:59:59.423179'), to_timestamp('2020-02-29 23:59:59.165575'))
checked expr : diff_minutes<Timestamp, Timestamp>(to_timestamp<String>("2019-02-28 23:59:59.423179"), to_timestamp<String>("2020-02-29 23:59:59.165575"))
optimized expr : -527039995706_i64
optimized expr : -527039_i64
output type : Int64
output domain : {-527039995706..=-527039995706}
output : -527039995706
output domain : {-527039..=-527039}
output : -527039


ast : date_diff(second, to_timestamp('2023-11-12 09:38:18.165575'), to_timestamp('2025-03-27 21:01:35.423179'))
raw expr : diff_seconds(to_timestamp('2025-03-27 21:01:35.423179'), to_timestamp('2023-11-12 09:38:18.165575'))
checked expr : diff_seconds<Timestamp, Timestamp>(to_timestamp<String>("2025-03-27 21:01:35.423179"), to_timestamp<String>("2023-11-12 09:38:18.165575"))
optimized expr : 43327397257604_i64
optimized expr : 43327397_i64
output type : Int64
output domain : {43327397257604..=43327397257604}
output : 43327397257604
output domain : {43327397..=43327397}
output : 43327397


ast : date_diff(second, to_timestamp('2020-02-29 23:59:59.165575'), to_timestamp('2019-02-28 23:59:59.423179'))
raw expr : diff_seconds(to_timestamp('2019-02-28 23:59:59.423179'), to_timestamp('2020-02-29 23:59:59.165575'))
checked expr : diff_seconds<Timestamp, Timestamp>(to_timestamp<String>("2019-02-28 23:59:59.423179"), to_timestamp<String>("2020-02-29 23:59:59.165575"))
optimized expr : -31622399742396_i64
optimized expr : -31622399_i64
output type : Int64
output domain : {-31622399742396..=-31622399742396}
output : -31622399742396
output domain : {-31622399..=-31622399}
output : -31622399


Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ SELECT DATE_DIFF(DAY, to_date('2000-01-02'), to_date('2024-01-01'))
query T
SELECT DATE_DIFF(DAY, to_date('2012-02-29'), to_date('2013-02-28'))
----
366
365

query T
select date_sub(QUARTER, 1, to_date('2018-01-02'))
Expand Down

0 comments on commit dd0d158

Please sign in to comment.