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
)

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 7e2bcb5 commit e4f41db
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,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 @@ -251,8 +251,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 @@ -67,7 +67,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 @@ -83,7 +83,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 @@ -131,8 +131,8 @@ void testRound() {
Expression expression = new GreaterThan(left, right);
Expression rewrittenExpression = executor.rewrite(typeCoercion(expression), context);

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,51 @@ 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, 111111);
Assertions.assertEquals(111111, literal.roundCeiling(6).microSecond);
Assertions.assertEquals(111120, literal.roundCeiling(5).microSecond);
Assertions.assertEquals(111200, literal.roundCeiling(4).microSecond);
Assertions.assertEquals(112000, literal.roundCeiling(3).microSecond);
Assertions.assertEquals(120000, literal.roundCeiling(2).microSecond);
Assertions.assertEquals(200000, 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);

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 e4f41db

Please sign in to comment.