[bug](function) fix date_sub function failed when arg type is datev2 (#30443)
* [bug](function) fix date_sub function failed when arg type is datev2 * update
This commit is contained in:
@ -146,6 +146,18 @@ public class TimestampArithmeticExpr extends Expr {
|
||||
if (t1 == PrimitiveType.DATEV2) {
|
||||
return Type.DATEV2;
|
||||
}
|
||||
// could try cast to date first, then cast to datetime
|
||||
if (t1 == PrimitiveType.VARCHAR || t1 == PrimitiveType.STRING) {
|
||||
Expr expr = getChild(0);
|
||||
if ((expr instanceof StringLiteral) && ((StringLiteral) expr).canConvertToDateType(Type.DATEV2)) {
|
||||
try {
|
||||
setChild(0, new DateLiteral(((StringLiteral) expr).getValue(), Type.DATEV2));
|
||||
} catch (AnalysisException e) {
|
||||
return Type.INVALID;
|
||||
}
|
||||
return Type.DATEV2;
|
||||
}
|
||||
}
|
||||
if (PrimitiveType.isImplicitCast(t1, PrimitiveType.DATETIME)) {
|
||||
if (Config.enable_date_conversion) {
|
||||
if (t1 == PrimitiveType.NULL_TYPE) {
|
||||
|
||||
@ -168,6 +168,11 @@ public class FEFunctions {
|
||||
return dateAdd(date, new IntLiteral(-(int) day.getLongValue()));
|
||||
}
|
||||
|
||||
@FEFunction(name = "date_sub", argTypes = { "DATEV2", "INT" }, returnType = "DATEV2")
|
||||
public static DateLiteral dateSubDateV2(LiteralExpr date, LiteralExpr day) throws AnalysisException {
|
||||
return dateAdd(date, new IntLiteral(-(int) day.getLongValue()));
|
||||
}
|
||||
|
||||
@FEFunction(name = "years_sub", argTypes = { "DATETIME", "INT" }, returnType = "DATETIME")
|
||||
public static DateLiteral yearsSub(LiteralExpr date, LiteralExpr year) throws AnalysisException {
|
||||
return yearsAdd(date, new IntLiteral(-(int) year.getLongValue()));
|
||||
@ -183,6 +188,16 @@ public class FEFunctions {
|
||||
return daysAdd(date, new IntLiteral(-(int) day.getLongValue()));
|
||||
}
|
||||
|
||||
@FEFunction(name = "days_sub", argTypes = { "DATETIMEV2", "INT" }, returnType = "DATETIMEV2")
|
||||
public static DateLiteral daysSubDateTimeV2(LiteralExpr date, LiteralExpr day) throws AnalysisException {
|
||||
return daysAdd(date, new IntLiteral(-(int) day.getLongValue()));
|
||||
}
|
||||
|
||||
@FEFunction(name = "days_sub", argTypes = { "DATEV2", "INT" }, returnType = "DATEV2")
|
||||
public static DateLiteral daysSubDateV2(LiteralExpr date, LiteralExpr day) throws AnalysisException {
|
||||
return daysAdd(date, new IntLiteral(-(int) day.getLongValue()));
|
||||
}
|
||||
|
||||
@FEFunction(name = "hours_sub", argTypes = { "DATETIME", "INT" }, returnType = "DATETIME")
|
||||
public static DateLiteral hoursSub(LiteralExpr date, LiteralExpr hour) throws AnalysisException {
|
||||
return hoursAdd(date, new IntLiteral(-(int) hour.getLongValue()));
|
||||
|
||||
@ -22,14 +22,17 @@ package org.apache.doris.rewrite;
|
||||
|
||||
|
||||
import org.apache.doris.analysis.Analyzer;
|
||||
import org.apache.doris.analysis.ArithmeticExpr;
|
||||
import org.apache.doris.analysis.BetweenPredicate;
|
||||
import org.apache.doris.analysis.CaseExpr;
|
||||
import org.apache.doris.analysis.CastExpr;
|
||||
import org.apache.doris.analysis.Expr;
|
||||
import org.apache.doris.analysis.FunctionCallExpr;
|
||||
import org.apache.doris.analysis.InformationFunction;
|
||||
import org.apache.doris.analysis.LiteralExpr;
|
||||
import org.apache.doris.analysis.NullLiteral;
|
||||
import org.apache.doris.analysis.SlotRef;
|
||||
import org.apache.doris.analysis.TimestampArithmeticExpr;
|
||||
import org.apache.doris.analysis.VariableExpr;
|
||||
import org.apache.doris.catalog.Env;
|
||||
import org.apache.doris.catalog.PrimitiveType;
|
||||
@ -96,7 +99,8 @@ public class FoldConstantsRule implements ExprRewriteRule {
|
||||
// of the Expr tree. Assumes the bottom-up application of this rule. Constant
|
||||
// children should have been folded at this point.
|
||||
for (Expr child : expr.getChildren()) {
|
||||
if (!child.isLiteral() && !(child instanceof CastExpr)) {
|
||||
if (!child.isLiteral() && !(child instanceof CastExpr) && !((child instanceof FunctionCallExpr
|
||||
|| child instanceof ArithmeticExpr || child instanceof TimestampArithmeticExpr))) {
|
||||
return expr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user