Skip to content

Commit cce8c74

Browse files
TCeasonsundy-li
andauthored
fix(query): Disabled the shrink_scalar optimization for cast expressions during the resolve procedure args (#17119)
fix(query): Disabled the shrink_scalar optimization for cast expressions Co-authored-by: sundyli <[email protected]>
1 parent 15ca07c commit cce8c74

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/query/sql/src/planner/semantic/type_check.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl<'a> TypeChecker<'a> {
599599
let registry = &BUILTIN_FUNCTIONS;
600600
let checked_expr = type_check::check(&raw_expr, registry)?;
601601

602-
if let Some(constant) = self.try_fold_constant(&checked_expr) {
602+
if let Some(constant) = self.try_fold_constant(&checked_expr, false) {
603603
return Ok(constant);
604604
}
605605
// if the source type is nullable, cast target type should also be nullable.
@@ -642,7 +642,7 @@ impl<'a> TypeChecker<'a> {
642642
let registry = &BUILTIN_FUNCTIONS;
643643
let checked_expr = type_check::check(&raw_expr, registry)?;
644644

645-
if let Some(constant) = self.try_fold_constant(&checked_expr) {
645+
if let Some(constant) = self.try_fold_constant(&checked_expr, false) {
646646
return Ok(constant);
647647
}
648648

@@ -1112,7 +1112,6 @@ impl<'a> TypeChecker<'a> {
11121112

11131113
Expr::Hole { .. } => unreachable!("hole is impossible in trivial query"),
11141114
};
1115-
11161115
Ok(Box::new((scalar, data_type)))
11171116
}
11181117

@@ -2775,7 +2774,7 @@ impl<'a> TypeChecker<'a> {
27752774
} => {
27762775
let mut folded_args = Vec::with_capacity(args.len());
27772776
for (checked_arg, arg) in checked_args.iter().zip(args.iter()) {
2778-
match self.try_fold_constant(checked_arg) {
2777+
match self.try_fold_constant(checked_arg, true) {
27792778
Some(constant) if arg.evaluable() => {
27802779
folded_args.push(constant.0);
27812780
}
@@ -2793,7 +2792,7 @@ impl<'a> TypeChecker<'a> {
27932792
self.ctx.set_cacheable(false);
27942793
}
27952794

2796-
if let Some(constant) = self.try_fold_constant(&expr) {
2795+
if let Some(constant) = self.try_fold_constant(&expr, true) {
27972796
return Ok(constant);
27982797
}
27992798

@@ -5166,12 +5165,17 @@ impl<'a> TypeChecker<'a> {
51665165
fn try_fold_constant<Index: ColumnIndex>(
51675166
&self,
51685167
expr: &EExpr<Index>,
5168+
enable_shrink: bool,
51695169
) -> Option<Box<(ScalarExpr, DataType)>> {
5170-
if expr.is_deterministic(&BUILTIN_FUNCTIONS) {
5170+
if expr.is_deterministic(&BUILTIN_FUNCTIONS) && enable_shrink {
51715171
if let (EExpr::Constant { scalar, .. }, _) =
51725172
ConstantFolder::fold(expr, &self.func_ctx, &BUILTIN_FUNCTIONS)
51735173
{
5174-
let scalar = shrink_scalar(scalar);
5174+
let scalar = if enable_shrink {
5175+
shrink_scalar(scalar)
5176+
} else {
5177+
scalar
5178+
};
51755179
let ty = scalar.as_ref().infer_data_type();
51765180
return Some(Box::new((
51775181
ConstantExpr {

tests/sqllogictests/suites/base/15_procedure/15_0002_procedure.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,36 @@ drop procedure if exists not_exists_p();
124124
statement error 3130
125125
drop procedure not_exists_p();
126126

127+
statement ok
128+
drop procedure if exists sum_even_numbers(Int, Int);
129+
130+
statement ok
131+
CREATE PROCEDURE sum_even_numbers(start_val Int, end_val Int)
132+
RETURNS UInt8 NOT NULL
133+
LANGUAGE SQL
134+
COMMENT='Calculate the sum of all even numbers'
135+
AS $$
136+
BEGIN
137+
LET sum := 0;
138+
FOR i IN start_val TO end_val DO
139+
IF i % 2 = 0 THEN
140+
sum := sum + i;
141+
END IF;
142+
END FOR;
143+
RETURN sum;
144+
END;
145+
$$;
146+
147+
statement error 3130
148+
call procedure sum_even_numbers(1, 2)
149+
150+
query T
151+
call procedure sum_even_numbers(1::INT, 2::INT)
152+
----
153+
2
154+
155+
statement ok
156+
drop procedure sum_even_numbers(Int, Int);
157+
127158
statement ok
128159
unset global enable_experimental_procedure;

0 commit comments

Comments
 (0)