Skip to content

Commit

Permalink
[Bug](date) Fix wrong result produced by date function (apache#12720)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel39 authored and Yijia Su committed Oct 8, 2022
1 parent 6524017 commit a9f3e32
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,11 @@ private String convertToString(PrimitiveType type) {

@Override
public long getLongValue() {
return (year * 10000 + month * 100 + day) * 1000000L + hour * 10000 + minute * 100 + second;
if (this.getType().isDate() || this.getType().isDateV2()) {
return year * 10000 + month * 100 + day;
} else {
return (year * 10000 + month * 100 + day) * 1000000L + hour * 10000 + minute * 100 + second;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void testUnionWithDateV2() throws AnalysisException {
InPredicate union = inPredicate1.union(inPredicate2);
Assert.assertEquals(slotRef1, union.getChild(0));
Assert.assertTrue(union.isLiteralChildren());
Assert.assertEquals(2, union.getListChildren().size());
Assert.assertEquals(3, union.getListChildren().size());
Assert.assertTrue(union.getListChildren().contains(literalChild1));
Assert.assertTrue(union.getListChildren().contains(literalChild2));
Assert.assertTrue(union.getListChildren().contains(literalChild5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,79 +146,46 @@ public void dateV2DiffTest() throws AnalysisException {
public void dateAddTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE),
new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME);
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME);
expectedResult = new DateLiteral("2018-08-07", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void dateV2AddTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIMEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIMEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIMEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIMEV2);
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME);
expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void addDateTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME);
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME);
expectedResult = new DateLiteral("2018-08-07", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);

}

@Test
public void addDateV2Test() throws AnalysisException {
DateLiteral actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIMEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIMEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(1));
expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIMEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIMEV2);
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATEV2);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATEV2), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME);
expectedResult = new DateLiteral("2018-08-07", Type.DATEV2);
Assert.assertEquals(expectedResult, actualResult);

}

@Test
Expand Down

0 comments on commit a9f3e32

Please sign in to comment.