FIX: fix datetimev2 decimal error. (#10736)

This commit is contained in:
Stalary
2022-07-13 08:32:26 +08:00
committed by GitHub
parent a9a08d3d0b
commit f9f711cd16
2 changed files with 18 additions and 1 deletions

View File

@ -285,6 +285,7 @@ public class DateLiteral extends LiteralExpr {
this.hour = dateTime.getHourOfDay();
this.minute = dateTime.getMinuteOfHour();
this.second = dateTime.getSecondOfMinute();
this.microsecond = dateTime.getMillisOfSecond() * 1000L;
this.type = type;
}
@ -433,12 +434,16 @@ public class DateLiteral extends LiteralExpr {
if (((ScalarType) type).decimalScale() == 0) {
return s;
}
return s + "." + microsecond / (10L * (6 - ((ScalarType) type).decimalScale()));
return s + "." + getDecimalNumber();
} else {
return String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
}
}
public long getDecimalNumber() {
return Double.valueOf(microsecond / (Math.pow(10, 6 - ((ScalarType) type).decimalScale()))).longValue();
}
private String convertToString(PrimitiveType type) {
if (type == PrimitiveType.DATE || type == PrimitiveType.DATEV2) {
return String.format("%04d-%02d-%02d", year, month, day);

View File

@ -23,6 +23,7 @@ import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.InvalidFormatException;
import org.apache.doris.common.jmockit.Deencapsulation;
import org.joda.time.LocalDateTime;
import org.junit.Assert;
import org.junit.Test;
@ -344,4 +345,15 @@ public class DateLiteralTest {
}
Assert.assertFalse(hasException);
}
@Test
public void testDateTimeV2Decimal() throws AnalysisException {
DateLiteral dateLiteral1 = new DateLiteral(LocalDateTime.now(),
DateLiteral.getDefaultDateType(ScalarType.createDatetimeV2Type(3)));
Assert.assertTrue(dateLiteral1.getDecimalNumber() >= 100 && dateLiteral1.getDecimalNumber() < 1000);
DateLiteral dateLiteral2 = new DateLiteral(LocalDateTime.now(),
DateLiteral.getDefaultDateType(ScalarType.createDatetimeV2Type(5)));
Assert.assertTrue(dateLiteral2.getDecimalNumber() >= 10000 && dateLiteral2.getDecimalNumber() < 100000);
}
}