[Bug](date) Fix invalid date (#16205)

Issue Number: close #15777
This commit is contained in:
Gabriel
2023-01-31 10:08:44 +08:00
committed by GitHub
parent a7b030778a
commit 471db80f69
6 changed files with 95 additions and 13 deletions

View File

@ -627,6 +627,13 @@ public class DateLiteral extends LiteralExpr {
}
msg.node_type = TExprNodeType.DATE_LITERAL;
msg.date_literal = new TDateLiteral(getStringValue());
try {
checkValueValid();
} catch (AnalysisException e) {
// If date value is invalid, set this to null
msg.node_type = TExprNodeType.NULL_LITERAL;
msg.setIsNullable(true);
}
}
@Override
@ -775,6 +782,11 @@ public class DateLiteral extends LiteralExpr {
}
}
private boolean isLeapYear() {
return ((year % 4) == 0) && ((year % 100 != 0) || ((year % 400) == 0 && year > 0));
}
// Validation check should be same as DateV2Value<T>::is_invalid in BE
@Override
public void checkValueValid() throws AnalysisException {
if (year < 0 || year > 9999) {
@ -783,8 +795,10 @@ public class DateLiteral extends LiteralExpr {
if (month < 1 || month > 12) {
throw new AnalysisException("DateLiteral has invalid month value: " + month);
}
if (day < 1 || day > 31) {
throw new AnalysisException("DateLiteral has invalid day value: " + day);
if (day < 1 || day > DAYS_IN_MONTH[(int) month]) {
if (!(month == 2 && day == 29 && isLeapYear())) {
throw new AnalysisException("DateLiteral has invalid day value: " + day);
}
}
if (type.isDatetimeV2() || type.isDatetime()) {
if (hour < 0 || hour > 24) {

View File

@ -98,7 +98,18 @@ public enum ExpressionFunctions {
FEFunctionInvoker invoker = getFunction(signature);
if (invoker != null) {
try {
return invoker.invoke(constExpr.getChildrenWithoutCast());
if (fn.getReturnType().isDateType()) {
Expr dateLiteral = invoker.invoke(constExpr.getChildrenWithoutCast());
Preconditions.checkArgument(dateLiteral instanceof DateLiteral);
try {
((DateLiteral) dateLiteral).checkValueValid();
} catch (AnalysisException e) {
return NullLiteral.create(dateLiteral.getType());
}
return dateLiteral;
} else {
return invoker.invoke(constExpr.getChildrenWithoutCast());
}
} catch (AnalysisException e) {
LOG.debug("failed to invoke", e);
return constExpr;

View File

@ -194,6 +194,11 @@ public class StringLiteral extends LiteralExpr {
throw e;
}
}
try {
newLiteral.checkValueValid();
} catch (AnalysisException e) {
return NullLiteral.create(newLiteral.getType());
}
return newLiteral;
}