[fix](datetime) fix unstable str_to_date function result (#25707)

fix unstable str_to_date function result
This commit is contained in:
zclllyybb
2023-10-23 11:52:08 +08:00
committed by GitHub
parent 4889841ff7
commit cbc5c91aec
8 changed files with 38 additions and 52 deletions

View File

@ -1276,9 +1276,20 @@ public class DateLiteral extends LiteralExpr {
break;
// Micro second
case 'f':
tmp = vp + Math.min(6, vend - vp);
intValue = strToLong(value.substring(vp, tmp));
this.microsecond = (long) (intValue * Math.pow(10, 6 - Math.min(6, vend - vp)));
// FIXME: fix same with BE
tmp = vp;
// when there's still something to the end, fix the scale of ms.
while (tmp < vend && Character.isDigit(value.charAt(tmp))) {
tmp += 1;
}
if (tmp - vp > 6) {
int tmp2 = vp + 6;
intValue = strToLong(value.substring(vp, tmp2));
} else {
intValue = strToLong(value.substring(vp, tmp));
}
this.microsecond = (long) (intValue * Math.pow(10, 6 - Math.min(6, tmp - vp)));
timePartUsed = true;
microSecondPartUsed = true;
vp = tmp;
@ -1476,7 +1487,7 @@ public class DateLiteral extends LiteralExpr {
// we think it's stable enough
if (datePartUsed) {
if (microSecondPartUsed) {
this.type = Type.DATETIMEV2;
this.type = Type.DATETIMEV2_WITH_MAX_SCALAR;
} else if (timePartUsed) {
this.type = ScalarType.getDefaultDateType(Type.DATETIME);
} else {

View File

@ -1900,12 +1900,12 @@ public class FunctionCallExpr extends Expr {
Expr child1Result = getChild(1).getResultValue(false);
if (child1Result instanceof StringLiteral) {
if (DateLiteral.hasTimePart(child1Result.getStringValue())) {
this.type = Type.DATETIME;
this.type = Type.DATETIMEV2_WITH_MAX_SCALAR;
} else {
this.type = Type.DATE;
this.type = Type.DATEV2;
}
} else {
this.type = Type.DATETIME;
this.type = Type.DATETIMEV2_WITH_MAX_SCALAR;
}
} else if (TIME_FUNCTIONS_WITH_PRECISION.contains(fnName.getFunction().toLowerCase())
&& fn.getReturnType().isDatetimeV2()) {

View File

@ -44,9 +44,9 @@ public class StrToDate extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature, AlwaysNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT).args(VarcharType.SYSTEM_DEFAULT,
FunctionSignature.ret(DateTimeV2Type.MAX).args(VarcharType.SYSTEM_DEFAULT,
VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT).args(StringType.INSTANCE, StringType.INSTANCE)
FunctionSignature.ret(DateTimeV2Type.MAX).args(StringType.INSTANCE, StringType.INSTANCE)
);
/**
@ -95,6 +95,9 @@ public class StrToDate extends ScalarFunction
}
} else {
returnType = DataType.fromCatalogType(ScalarType.getDefaultDateType(Type.DATETIME));
if (returnType.isDateTimeV2Type()) {
returnType = DataType.fromCatalogType(Type.DATETIMEV2_WITH_MAX_SCALAR);
}
}
return signature.withReturnType(returnType);
}

View File

@ -151,7 +151,7 @@ public class FEFunctions {
return new StringLiteral(result);
}
@FEFunction(name = "str_to_date", argTypes = { "VARCHAR", "VARCHAR" }, returnType = "DATETIME")
@FEFunction(name = "str_to_date", argTypes = { "VARCHAR", "VARCHAR" }, returnType = "DATETIMEV2")
public static DateLiteral dateParse(StringLiteral date, StringLiteral fmtLiteral) throws AnalysisException {
DateLiteral dateLiteral = new DateLiteral();
try {