[BUG](date_trunc) fix date_trunc function only handle lower string (#22602)

fix date_trunc function only handle lower string
This commit is contained in:
zhangstar333
2023-08-05 12:53:13 +08:00
committed by GitHub
parent fe6bae2924
commit d3b50e3b2a
7 changed files with 310 additions and 58 deletions

View File

@ -1536,6 +1536,20 @@ public class FunctionCallExpr extends Expr {
.toSql());
}
}
if (fnName.getFunction().equalsIgnoreCase("date_trunc")) {
if ((children.size() != 2) || (getChild(1).isConstant() == false)
|| !(getChild(1) instanceof StringLiteral)) {
throw new AnalysisException(
fnName.getFunction() + " needs two params, and the second is must be a string constant: "
+ this.toSql());
}
final String constParam = ((StringLiteral) getChild(1)).getValue().toLowerCase();
if (!Lists.newArrayList("year", "quarter", "month", "week", "day", "hour", "minute", "second")
.contains(constParam)) {
throw new AnalysisException("date_trunc function second param only support argument is "
+ "year|quarter|month|week|day|hour|minute|second");
}
}
if (fnName.getFunction().equalsIgnoreCase("char")) {
if (!getChild(0).isConstant()) {
throw new AnalysisException(

View File

@ -18,9 +18,11 @@
package org.apache.doris.nereids.trees.expressions.functions.scalar;
import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.DateTimeType;
@ -29,6 +31,7 @@ import org.apache.doris.nereids.types.VarcharType;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.List;
@ -51,6 +54,20 @@ public class DateTrunc extends ScalarFunction
super("date_trunc", arg0, arg1);
}
@Override
public void checkLegalityBeforeTypeCoercion() {
if (getArgument(1).isConstant() == false || !(getArgument(1) instanceof VarcharLiteral)) {
throw new AnalysisException("the second parameter of "
+ getName() + " function must be a string constant: " + toSql());
}
final String constParam = ((VarcharLiteral) getArgument(1)).getStringValue().toLowerCase();
if (!Lists.newArrayList("year", "quarter", "month", "week", "day", "hour", "minute", "second")
.contains(constParam)) {
throw new AnalysisException("date_trunc function second param only support argument is"
+ "year|quarter|month|week|day|hour|minute|second");
}
}
/**
* withChildren.
*/