Skip to content

Commit

Permalink
[fix](Nereids) DatetimeV2 round floor and round ceiling is wrong (#35153
Browse files Browse the repository at this point in the history
) (#35154)

pick from master #35153

1.  round floor was incorrectly implemented as round
2. round ceiling not really round because use double type when divide
  • Loading branch information
morrySnow authored May 22, 2024
1 parent 7fa317d commit e495dd3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public DateTimeV2Literal roundCeiling(int newScale) {
long newYear = year;
if (remain != 0) {
newMicroSecond = Double
.valueOf((microSecond + (Math.pow(10, 6 - newScale)))
.valueOf((microSecond + (int) (Math.pow(10, 6 - newScale)))
/ (int) (Math.pow(10, 6 - newScale)) * (Math.pow(10, 6 - newScale)))
.longValue();
}
Expand All @@ -199,8 +199,8 @@ public DateTimeV2Literal roundCeiling(int newScale) {
}

public DateTimeV2Literal roundFloor(int newScale) {
// use roundMicroSecond in constructor
return new DateTimeV2Literal(DateTimeV2Type.of(newScale), year, month, day, hour, minute, second, microSecond);
return new DateTimeV2Literal(DateTimeV2Type.of(newScale), year, month, day, hour, minute, second,
microSecond / (int) Math.pow(10, 6 - newScale) * (int) Math.pow(10, 6 - newScale));
}

public static Expression fromJavaDateType(LocalDateTime dateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void testSql() {
.rewrite()
.matches(
logicalFilter()
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a <= '2023-06-16 00:00:00')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a <= '2023-06-15 23:59:59')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b <= 111.11)")))
);

Expand All @@ -82,7 +82,7 @@ void testSql() {
.rewrite()
.matches(
logicalFilter()
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a > '2023-06-16 00:00:00')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(a > '2023-06-15 23:59:59')")))
.when(f -> f.getConjuncts().stream().anyMatch(e -> e.toSql().equals("(b > 111.11)")))
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void testRound() {
Expression rewrittenExpression = executor.rewrite(typeCoercion(expression), context);

// right should round to be 2021-01-02 00:00:00.00
Assertions.assertEquals(new DateTimeV2Literal("2021-01-02 00:00:00"), rewrittenExpression.child(1));
Assertions.assertEquals(new DateTimeV2Literal("2021-01-01 23:59:59.9"), rewrittenExpression.child(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,36 @@ void testDateTimeV2Scale() {
new DateTimeV2Literal(DateTimeV2Type.of(5), "2016-12-31 23:59:59.999999"),
new DateTimeV2Literal("2017-01-01 00:00:00.0"));
}
}

@Test
void testRoundFloor() {
DateTimeV2Literal literal;
literal = new DateTimeV2Literal(DateTimeV2Type.of(6), 2000, 2, 2, 2, 2, 2, 222222);
Assertions.assertEquals(222222, literal.roundFloor(6).microSecond);
Assertions.assertEquals(222220, literal.roundFloor(5).microSecond);
Assertions.assertEquals(222200, literal.roundFloor(4).microSecond);
Assertions.assertEquals(222000, literal.roundFloor(3).microSecond);
Assertions.assertEquals(220000, literal.roundFloor(2).microSecond);
Assertions.assertEquals(200000, literal.roundFloor(1).microSecond);
Assertions.assertEquals(0, literal.roundFloor(0).microSecond);
}

@Test
void testRoundCeiling() {
DateTimeV2Literal literal;
literal = new DateTimeV2Literal(DateTimeV2Type.of(6), 2000, 12, 31, 23, 59, 59, 888888);
Assertions.assertEquals(888888, literal.roundCeiling(6).microSecond);
Assertions.assertEquals(888890, literal.roundCeiling(5).microSecond);
Assertions.assertEquals(888900, literal.roundCeiling(4).microSecond);
Assertions.assertEquals(889000, literal.roundCeiling(3).microSecond);
Assertions.assertEquals(890000, literal.roundCeiling(2).microSecond);
Assertions.assertEquals(900000, literal.roundCeiling(1).microSecond);
Assertions.assertEquals(0, literal.roundCeiling(0).microSecond);
Assertions.assertEquals(0, literal.roundCeiling(0).second);
Assertions.assertEquals(0, literal.roundCeiling(0).minute);
Assertions.assertEquals(0, literal.roundCeiling(0).hour);
Assertions.assertEquals(1, literal.roundCeiling(0).day);
Assertions.assertEquals(1, literal.roundCeiling(0).month);
Assertions.assertEquals(2001, literal.roundCeiling(0).year);
}
}

0 comments on commit e495dd3

Please sign in to comment.