Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.PlaceholderLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
import org.apache.doris.nereids.trees.expressions.typecoercion.ImplicitCastInputTypes;
import org.apache.doris.nereids.trees.plans.PlaceholderId;
Expand Down Expand Up @@ -650,7 +651,7 @@ public Expression visitPlaceholder(Placeholder placeholder, ExpressionRewriteCon
// In prepare stage, the realExpr has not been set, set it to StringLiteral so that we can plan the statement
// and get the output slots in prepare stage, which is required by Mysql api definition.
if (realExpr == null && context.cascadesContext.getStatementContext().isPrepareStage()) {
realExpr = new StringLiteral(String.valueOf(placeholder.getPlaceholderId().asInt()));
realExpr = new PlaceholderLiteral(String.valueOf(placeholder.getPlaceholderId().asInt()));
}
return visit(realExpr, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.doris.nereids.trees.expressions.functions.Monotonic;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.literal.PlaceholderLiteral;
import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
Expand Down Expand Up @@ -65,16 +66,19 @@ public void checkLegalityBeforeTypeCoercion() {
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(((StringLikeLiteral) getArgument(0)).getStringValue().toLowerCase())
&& !LEGAL_TIME_UNIT.contains(((StringLikeLiteral) getArgument(1))
.getStringValue().toLowerCase())) {
StringLikeLiteral argument0 = (StringLikeLiteral) getArgument(0);
StringLikeLiteral argument1 = (StringLikeLiteral) getArgument(1);
if (!LEGAL_TIME_UNIT.contains(argument0.getStringValue().toLowerCase())
&& !(argument0 instanceof PlaceholderLiteral)
&& !LEGAL_TIME_UNIT.contains(argument1.getStringValue().toLowerCase())
&& !(argument1 instanceof PlaceholderLiteral)) {
throw new AnalysisException("date_trunc function time unit param only support argument is "
+ String.join("|", LEGAL_TIME_UNIT));
}
} else {
final String constParam = ((StringLikeLiteral) getArgument(firstArgIsStringLiteral ? 0 : 1))
.getStringValue().toLowerCase();
if (!LEGAL_TIME_UNIT.contains(constParam)) {
StringLikeLiteral argument = (StringLikeLiteral) getArgument(firstArgIsStringLiteral ? 0 : 1);
final String constParam = argument.getStringValue().toLowerCase();
if (!LEGAL_TIME_UNIT.contains(constParam) && !(argument instanceof PlaceholderLiteral)) {
throw new AnalysisException("date_trunc function time unit param only support argument is "
+ String.join("|", LEGAL_TIME_UNIT));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal;

/**
* Placeholder literal only used for prepare stage.
*/
public class PlaceholderLiteral extends StringLiteral {

/**
* Constructor for Literal.
*
* @param value real value stored in java object
*/
public PlaceholderLiteral(String value) {
super(value);
}
}
6 changes: 6 additions & 0 deletions regression-test/data/prepared_stmt_p0/prepared_stmt.out
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ a
0 1
1 2

-- !select27 --
2025-01-01T00:00

-- !select28 --
2020-01-01T00:00

-- !overflow_2 --
2

Expand Down
20 changes: 20 additions & 0 deletions regression-test/suites/prepared_stmt_p0/prepared_stmt.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ suite("test_prepared_stmt", "nonConcurrent") {
stmt_read.setString(2, "2")
stmt_read.setString(3, "3")
qe_select26 stmt_read

sql """drop table if exists date_trunc_test"""
sql """ CREATE TABLE IF NOT EXISTS `date_trunc_test` (
`pk` int NULL,
`value` datetime NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(`pk`)
DISTRIBUTED BY HASH(`pk`) BUCKETS 10
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
"""
sql """insert into date_trunc_test values (1, "2025-09-21 15:43:25") """
stmt_read = prepareStatement "select date_trunc(value, ?) from date_trunc_test"
stmt_read.setString(1, "year")
qe_select27 stmt_read
stmt_read = prepareStatement "select date_trunc(?, ?) from date_trunc_test"
stmt_read.setString(1, "year")
stmt_read.setString(2, "2020-01-01 11:22:33")
qe_select28 stmt_read
}

// test stmtId overflow
Expand Down