Skip to content

Commit a8d1c6c

Browse files
committed
[fix](nereids)fold constant rule can't fold from_unixtime when its 1st param is decimalv3 literal
1 parent f66e517 commit a8d1c6c

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,11 @@ public static Expression fromUnixTime(DecimalLiteral second) {
541541
return fromUnixTime(second, new VarcharLiteral("%Y-%m-%d %H:%i:%s.%f"));
542542
}
543543

544+
@ExecFunction(name = "from_unixtime")
545+
public static Expression fromUnixTime(DecimalV3Literal second) {
546+
return fromUnixTime(second, new VarcharLiteral("%Y-%m-%d %H:%i:%s.%f"));
547+
}
548+
544549
/**
545550
* date transformation function: from_unixtime
546551
*/
@@ -591,6 +596,32 @@ public static Expression fromUnixTime(DecimalLiteral second, StringLikeLiteral f
591596
return dateFormat(datetime, format);
592597
}
593598

599+
/**
600+
* date transformation function: from_unixtime
601+
*/
602+
@ExecFunction(name = "from_unixtime")
603+
public static Expression fromUnixTime(DecimalV3Literal second, StringLikeLiteral format) {
604+
format = (StringLikeLiteral) SupportJavaDateFormatter.translateJavaFormatter(format);
605+
606+
if (second.getValue().signum() < 0) {
607+
throw new AnalysisException("Operation from_unixtime of " + second.getValue() + " out of range");
608+
}
609+
610+
ZonedDateTime dateTime = LocalDateTime.of(1970, 1, 1, 0, 0, 0)
611+
.plusSeconds(second.getValue().longValue())
612+
.atZone(ZoneId.of("UTC+0"))
613+
.toOffsetDateTime()
614+
.atZoneSameInstant(DateUtils.getTimeZone());
615+
DateTimeV2Literal datetime = new DateTimeV2Literal(DateTimeV2Type.of(6), dateTime.getYear(),
616+
dateTime.getMonthValue(),
617+
dateTime.getDayOfMonth(), dateTime.getHour(), dateTime.getMinute(), dateTime.getSecond(),
618+
dateTime.getNano() / 1000);
619+
if (datetime.checkRange()) {
620+
throw new AnalysisException("Operation from_unixtime of " + second.getValue() + " out of range");
621+
}
622+
return dateFormat(datetime, format);
623+
}
624+
594625
/**
595626
* date transformation function: unix_timestamp
596627
*/

fe/fe-core/src/main/java/org/apache/doris/nereids/util/DateUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,12 @@ public static DateTimeFormatter dateTimeFormatterChecklength(String pattern, Dat
219219
builder.appendValueReduced(ChronoField.YEAR, 2, 2, 1970);
220220
length += 2;
221221
break;
222+
case 'f':
223+
builder.appendValue(ChronoField.MICRO_OF_SECOND, 6);
224+
length += 6;
225+
break;
222226
// TODO(Gabriel): support microseconds in date literal
223227
case 'D': // %D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
224-
case 'f': // %f Microseconds (000000..999999)
225228
case 'U': // %U Week (00..53), where Sunday is the first day of the week
226229
case 'u': // %u Week (00..53), where Monday is the first day of the week
227230
case 'w': // %w Day of the week (0=Sunday..6=Saturday)

fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral;
108108
import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal;
109109
import org.apache.doris.nereids.trees.expressions.literal.DateV2Literal;
110+
import org.apache.doris.nereids.trees.expressions.literal.DecimalLiteral;
110111
import org.apache.doris.nereids.trees.expressions.literal.DecimalV3Literal;
111112
import org.apache.doris.nereids.trees.expressions.literal.DoubleLiteral;
112113
import org.apache.doris.nereids.trees.expressions.literal.FloatLiteral;
@@ -473,6 +474,14 @@ void testFoldString() {
473474
rewritten = executor.rewrite(f, context);
474475
Assertions.assertEquals(new VarcharLiteral("73 11 30"), rewritten);
475476

477+
f = new FromUnixtime(DecimalLiteral.of(new BigDecimal("1761548288.000000")));
478+
rewritten = executor.rewrite(f, context);
479+
Assertions.assertEquals(new VarcharLiteral("2025-10-27 14:58:08.000000"), rewritten);
480+
481+
f = new FromUnixtime(DecimalV3Literal.of(new BigDecimal("1761548288.000000")));
482+
rewritten = executor.rewrite(f, context);
483+
Assertions.assertEquals(new VarcharLiteral("2025-10-27 14:58:08.000000"), rewritten);
484+
476485
UnixTimestamp ut = new UnixTimestamp(StringLiteral.of("2021-11-11"), StringLiteral.of("%Y-%m-%d"));
477486
rewritten = executor.rewrite(ut, context);
478487
Assertions.assertEquals(new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeNoCheck(18, 6),

0 commit comments

Comments
 (0)