[fix](Nereids) unix_timestamp compute signature and fold const is wrong (#35727)

1. compute signature should call super#computeSignature first
2. fold const return type not changed after signature changed in #26827

we already have p0 for this case, but our regression framework has bug
that it report success when compare decimal type if real result lose
scale
This commit is contained in:
morrySnow
2024-06-03 18:56:11 +08:00
committed by yiguolei
parent f80b856405
commit 958687f7d4
3 changed files with 50 additions and 50 deletions

View File

@ -493,17 +493,17 @@ public class DateTimeExtractAndTransform {
*/
@ExecFunction(name = "unix_timestamp", argTypes = {"DATE"}, returnType = "INT")
public static Expression unixTimestamp(DateLiteral date) {
return new IntegerLiteral(getTimestamp(date.toJavaDateType()));
return new IntegerLiteral(Integer.parseInt(getTimestamp(date.toJavaDateType())));
}
@ExecFunction(name = "unix_timestamp", argTypes = {"DATETIME"}, returnType = "INT")
public static Expression unixTimestamp(DateTimeLiteral date) {
return new IntegerLiteral(getTimestamp(date.toJavaDateType()));
return new IntegerLiteral(Integer.parseInt(getTimestamp(date.toJavaDateType())));
}
@ExecFunction(name = "unix_timestamp", argTypes = {"DATEV2"}, returnType = "INT")
public static Expression unixTimestamp(DateV2Literal date) {
return new IntegerLiteral(getTimestamp(date.toJavaDateType()));
return new IntegerLiteral(Integer.parseInt(getTimestamp(date.toJavaDateType())));
}
/**
@ -513,18 +513,17 @@ public class DateTimeExtractAndTransform {
public static Expression unixTimestamp(DateTimeV2Literal date) {
if (date.getMicroSecond() == 0) {
return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck(10, 0),
new BigDecimal(getTimestamp(date.toJavaDateType()).toString()));
new BigDecimal(getTimestamp(date.toJavaDateType())));
}
int scale = date.getDataType().getScale();
String val = getTimestamp(date.toJavaDateType()).toString() + "." + date.getMicrosecondString();
return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck(10 + scale, scale),
new BigDecimal(val));
new BigDecimal(getTimestamp(date.toJavaDateType())));
}
/**
* date transformation function: unix_timestamp
*/
@ExecFunction(name = "unix_timestamp", argTypes = {"VARCHAR", "VARCHAR"}, returnType = "INT")
@ExecFunction(name = "unix_timestamp", argTypes = {"VARCHAR", "VARCHAR"}, returnType = "DECIMALV3")
public static Expression unixTimestamp(StringLikeLiteral date, StringLikeLiteral format) {
DateTimeFormatter formatter = DateUtils.formatBuilder(format.getValue()).toFormatter();
LocalDateTime dateObj;
@ -534,20 +533,26 @@ public class DateTimeExtractAndTransform {
// means the date string doesn't contain time fields.
dateObj = LocalDate.parse(date.getValue(), formatter).atStartOfDay();
}
return new IntegerLiteral(getTimestamp(dateObj));
return new DecimalV3Literal(DecimalV3Type.createDecimalV3TypeLooseCheck(16, 6),
new BigDecimal(getTimestamp(dateObj)));
}
private static Integer getTimestamp(LocalDateTime dateTime) {
private static String getTimestamp(LocalDateTime dateTime) {
LocalDateTime specialUpperBound = LocalDateTime.of(2038, 1, 19, 3, 14, 7);
LocalDateTime specialLowerBound = LocalDateTime.of(1970, 1, 1, 0, 0, 0);
if (dateTime.isBefore(specialLowerBound) || dateTime.isAfter(specialUpperBound)) {
return 0;
return "0";
}
return ((int) Duration.between(
Duration duration = Duration.between(
specialLowerBound,
dateTime.atZone(DateUtils.getTimeZone())
.toOffsetDateTime().atZoneSameInstant(ZoneId.of("UTC+0"))
.toLocalDateTime()).getSeconds());
.toLocalDateTime());
if (duration.getNano() == 0) {
return String.valueOf(duration.getSeconds());
} else {
return duration.getSeconds() + "." + (duration.getNano() / 1000);
}
}
/**

View File

@ -46,7 +46,7 @@ public class UnixTimestamp extends ScalarFunction
// we got changes when computeSignature
private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IntegerType.INSTANCE).args(),
FunctionSignature.ret(DecimalV3Type.createDecimalV3Type(16, 6)).args(DateTimeV2Type.SYSTEM_DEFAULT),
FunctionSignature.ret(DecimalV3Type.WILDCARD).args(DateTimeV2Type.SYSTEM_DEFAULT),
FunctionSignature.ret(IntegerType.INSTANCE).args(DateV2Type.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE).args(DateTimeType.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE).args(DateType.INSTANCE),
@ -102,6 +102,7 @@ public class UnixTimestamp extends ScalarFunction
@Override
public FunctionSignature computeSignature(FunctionSignature signature) {
signature = super.computeSignature(signature);
if (arity() != 1) {
return signature;
}
@ -120,7 +121,7 @@ public class UnixTimestamp extends ScalarFunction
*/
@Override
public UnixTimestamp withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 0
Preconditions.checkArgument(children.isEmpty()
|| children.size() == 1
|| children.size() == 2);
if (children.isEmpty() && arity() == 0) {