This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-3.1 by this push:
new 96acf86b2b6 branch-3.1: [fix](prepare statement)Fix date_trunc using
prepare statement parameter type bug. #54847 (#54920)
96acf86b2b6 is described below
commit 96acf86b2b6b44ece01c16f298ba513e6c1cf047
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Aug 19 11:16:23 2025 +0800
branch-3.1: [fix](prepare statement)Fix date_trunc using prepare statement
parameter type bug. #54847 (#54920)
Cherry-picked from #54847
Co-authored-by: James <[email protected]>
---
.../trees/expressions/functions/scalar/DateTrunc.java | 13 ++++++-------
.../data/prepared_stmt_p0/prepared_stmt.out | Bin 5355 -> 5389 bytes
.../suites/prepared_stmt_p0/prepared_stmt.groovy | 6 ++++++
3 files changed, 12 insertions(+), 7 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/DateTrunc.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/DateTrunc.java
index 86a6bfccf56..59e7175c3f3 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/DateTrunc.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/DateTrunc.java
@@ -24,7 +24,6 @@ import
org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.CustomSignature;
import org.apache.doris.nereids.trees.expressions.functions.Monotonic;
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
-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.DateTimeV2Type;
@@ -60,14 +59,14 @@ public class DateTrunc extends ScalarFunction
throw new AnalysisException("the time unit parameter of "
+ getName() + " function must be a string constant: " +
toSql());
} else if (firstArgIsStringLiteral && secondArgIsStringLiteral) {
- if (!LEGAL_TIME_UNIT.contains(((VarcharLiteral)
getArgument(0)).getStringValue().toLowerCase())
- && !LEGAL_TIME_UNIT.contains(((VarcharLiteral)
getArgument(1))
+ if (!LEGAL_TIME_UNIT.contains(((StringLikeLiteral)
getArgument(0)).getStringValue().toLowerCase())
+ && !LEGAL_TIME_UNIT.contains(((StringLikeLiteral)
getArgument(1))
.getStringValue().toLowerCase())) {
throw new AnalysisException("date_trunc function time unit
param only support argument is "
+ String.join("|", LEGAL_TIME_UNIT));
}
} else {
- final String constParam = ((VarcharLiteral)
getArgument(firstArgIsStringLiteral ? 0 : 1))
+ final String constParam = ((StringLikeLiteral)
getArgument(firstArgIsStringLiteral ? 0 : 1))
.getStringValue().toLowerCase();
if (!LEGAL_TIME_UNIT.contains(constParam)) {
throw new AnalysisException("date_trunc function time unit
param only support argument is "
@@ -95,9 +94,9 @@ public class DateTrunc extends ScalarFunction
.args(VarcharType.SYSTEM_DEFAULT,
getArgument(1).getDataType());
}
boolean firstArgIsStringLiteral =
- getArgument(0).isConstant() && getArgument(0) instanceof
VarcharLiteral;
+ getArgument(0).isConstant() && getArgument(0) instanceof
StringLikeLiteral;
boolean secondArgIsStringLiteral =
- getArgument(1).isConstant() && getArgument(1) instanceof
VarcharLiteral;
+ getArgument(1).isConstant() && getArgument(1) instanceof
StringLikeLiteral;
if (firstArgIsStringLiteral && !secondArgIsStringLiteral) {
return FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT)
.args(VarcharType.SYSTEM_DEFAULT,
DateTimeV2Type.SYSTEM_DEFAULT);
@@ -105,7 +104,7 @@ public class DateTrunc extends ScalarFunction
return FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT)
.args(DateTimeV2Type.SYSTEM_DEFAULT,
VarcharType.SYSTEM_DEFAULT);
} else if (firstArgIsStringLiteral && secondArgIsStringLiteral) {
- boolean timeUnitIsFirst =
LEGAL_TIME_UNIT.contains(((VarcharLiteral) getArgument(0))
+ boolean timeUnitIsFirst =
LEGAL_TIME_UNIT.contains(((StringLikeLiteral) getArgument(0))
.getStringValue().toLowerCase());
return timeUnitIsFirst ?
FunctionSignature.ret(DateTimeV2Type.SYSTEM_DEFAULT)
.args(VarcharType.SYSTEM_DEFAULT,
DateTimeV2Type.SYSTEM_DEFAULT)
diff --git a/regression-test/data/prepared_stmt_p0/prepared_stmt.out
b/regression-test/data/prepared_stmt_p0/prepared_stmt.out
index 9cadb98813a..a940c924e64 100644
Binary files a/regression-test/data/prepared_stmt_p0/prepared_stmt.out and
b/regression-test/data/prepared_stmt_p0/prepared_stmt.out differ
diff --git a/regression-test/suites/prepared_stmt_p0/prepared_stmt.groovy
b/regression-test/suites/prepared_stmt_p0/prepared_stmt.groovy
index 80058e72894..9a350ce6dc6 100644
--- a/regression-test/suites/prepared_stmt_p0/prepared_stmt.groovy
+++ b/regression-test/suites/prepared_stmt_p0/prepared_stmt.groovy
@@ -313,6 +313,12 @@ suite("test_prepared_stmt", "nonConcurrent") {
stmt_read = prepareStatement("""SELECT 1, null, [{'id': 1, 'name' :
'doris'}, {'id': 2, 'name': 'apache'}, null], null""")
assertEquals(com.mysql.cj.jdbc.ServerPreparedStatement,
stmt_read.class)
qe_select24 stmt_read
+
+ // test date_trunc
+ stmt_read = prepareStatement "select date_trunc (? , ?)"
+ stmt_read.setString(1, "2025-08-15 11:22:33")
+ stmt_read.setString(2, "DAY")
+ qe_select25 stmt_read
}
// test stmtId overflow
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]