You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by yi...@apache.org on 2023/06/21 16:41:00 UTC
[doris] branch master updated: [fix](nereids)the microseconds value is wrong when create datatimev2 literal from LocalDateTime (#21089)
This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new fff308352f [fix](nereids)the microseconds value is wrong when create datatimev2 literal from LocalDateTime (#21089)
fff308352f is described below
commit fff308352feec551bc8fe797d52adf3c42a07f4a
Author: starocean999 <40...@users.noreply.github.com>
AuthorDate: Thu Jun 22 00:40:53 2023 +0800
[fix](nereids)the microseconds value is wrong when create datatimev2 literal from LocalDateTime (#21089)
* [fix](nereids)the microseconds value is wrong when create datatimev2 literal from LocalDateTime
* fix code style
---
.../expressions/literal/DateTimeV2Literal.java | 42 ++++++----------------
.../org/apache/doris/nereids/util/DateUtils.java | 2 +-
2 files changed, 11 insertions(+), 33 deletions(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
index e180c667e1..b3152b0bc1 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateTimeV2Literal.java
@@ -62,27 +62,9 @@ public class DateTimeV2Literal extends DateTimeLiteral {
return visitor.visitDateTimeV2Literal(this, context);
}
- private long formatMicroSecond(long microSecond) {
- if (microSecond == 0) {
- return microSecond;
- }
- while (10 * microSecond <= 999999) {
- microSecond = 10 * microSecond;
- }
- return microSecond;
- }
-
- /*
- * Legacy and Nereids have different approaches to handling accuracy in scale.
- * For example, with a scale of 5, Legacy would store 123450, while Nereids
- * would store it as 12345.
- * Therefore, we need to perform a conversion.
- * Alternatively, can we standardize the format?
- */
@Override
public LiteralExpr toLegacyLiteral() {
- return new org.apache.doris.analysis.DateLiteral(year, month, day, hour, minute, second,
- formatMicroSecond(microSecond),
+ return new org.apache.doris.analysis.DateLiteral(year, month, day, hour, minute, second, microSecond,
getDataType().toCatalogDataType());
}
@@ -93,15 +75,10 @@ public class DateTimeV2Literal extends DateTimeLiteral {
@Override
public String getStringValue() {
- int scale = getDataType().getScale();
- if (scale == 0) {
- return String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
- } else {
- int microsecond = (int) (microSecond / Math.pow(10, DateTimeV2Type.MAX_SCALE - scale));
- return String.format("%04d-%02d-%02d %02d:%02d:%02d"
- + ".%0" + scale + "d",
- year, month, day, hour, minute, second, microsecond);
- }
+ return String.format("%04d-%02d-%02d %02d:%02d:%02d"
+ + (getDataType().getScale() > 0 ? ".%0" + getDataType().getScale() + "d" : ""),
+ year, month, day, hour, minute, second,
+ (int) (microSecond / Math.pow(10, DateTimeV2Type.MAX_SCALE - getDataType().getScale())));
}
@Override
@@ -153,11 +130,12 @@ public class DateTimeV2Literal extends DateTimeLiteral {
* convert java LocalDateTime object to DateTimeV2Literal object.
*/
public static Expression fromJavaDateType(LocalDateTime dateTime, int precision) {
+ long value = (long) Math.pow(10, DateTimeV2Type.MAX_SCALE - precision);
return isDateOutOfRange(dateTime)
? new NullLiteral(DateTimeV2Type.of(precision))
- : new DateTimeV2Literal(DateTimeV2Type.of(precision),
- dateTime.getYear(), dateTime.getMonthValue(), dateTime.getDayOfMonth(),
- dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
- dateTime.getNano() / (long) Math.pow(10, 9 - precision));
+ : new DateTimeV2Literal(DateTimeV2Type.of(precision), dateTime.getYear(),
+ dateTime.getMonthValue(), dateTime.getDayOfMonth(), dateTime.getHour(),
+ dateTime.getMinute(), dateTime.getSecond(),
+ (dateTime.getNano() / 1000) / value * value);
}
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java
index 56201460e7..1ae262a172 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java
@@ -158,7 +158,7 @@ public class DateUtils {
getOrDefault(accessor, ChronoField.HOUR_OF_DAY),
getOrDefault(accessor, ChronoField.MINUTE_OF_HOUR),
getOrDefault(accessor, ChronoField.SECOND_OF_MINUTE),
- 1000 * getOrDefault(accessor, ChronoField.MICRO_OF_SECOND));
+ getOrDefault(accessor, ChronoField.NANO_OF_SECOND));
}
public static int getOrDefault(final TemporalAccessor accessor, final ChronoField field) {
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org