提交 0d8a9676 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix division in Function.weekdiff() to avoid issue near start of epoch

上级 71fee8d6
...@@ -1915,11 +1915,13 @@ public class Function extends Expression implements FunctionCall { ...@@ -1915,11 +1915,13 @@ public class Function extends Expression implements FunctionCall {
} }
private static long weekdiff(long absolute1, long absolute2, int firstDayOfWeek) { private static long weekdiff(long absolute1, long absolute2, int firstDayOfWeek) {
long r1 = (absolute1 - firstDayOfWeek + 4) / 7; absolute1 += 4 - firstDayOfWeek;
long r1 = absolute1 / 7;
if (absolute1 < 0 && (r1 * 7 != absolute1)) { if (absolute1 < 0 && (r1 * 7 != absolute1)) {
r1--; r1--;
} }
long r2 = (absolute2 - firstDayOfWeek + 4) / 7; absolute2 += 4 - firstDayOfWeek;
long r2 = absolute2 / 7;
if (absolute2 < 0 && (r2 * 7 != absolute2)) { if (absolute2 < 0 && (r2 * 7 != absolute2)) {
r2--; r2--;
} }
......
...@@ -182,3 +182,15 @@ SELECT DATEDIFF('WEEK', DATE '2018-02-05', DATE '2018-02-06'), DATEDIFF('ISO_WEE ...@@ -182,3 +182,15 @@ SELECT DATEDIFF('WEEK', DATE '2018-02-05', DATE '2018-02-06'), DATEDIFF('ISO_WEE
> - - > - -
> 0 0 > 0 0
> rows: 1 > rows: 1
SELECT DATEDIFF('WEEK', DATE '1969-12-27', DATE '1969-12-28'), DATEDIFF('ISO_WEEK', DATE '1969-12-27', DATE '1969-12-28');
> 1 0
> - -
> 1 0
> rows: 1
SELECT DATEDIFF('WEEK', DATE '1969-12-28', DATE '1969-12-29'), DATEDIFF('ISO_WEEK', DATE '1969-12-28', DATE '1969-12-29');
> 0 1
> - -
> 0 1
> rows: 1
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论