Skip to content

Commit ac85fb8

Browse files
committed
fix(query): Disabled the shrink_scalar optimization for cast expressions
1 parent b72f3e1 commit ac85fb8

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'a> TypeChecker<'a> {
594594
let registry = &BUILTIN_FUNCTIONS;
595595
let checked_expr = type_check::check(&raw_expr, registry)?;
596596

597-
if let Some(constant) = self.try_fold_constant(&checked_expr) {
597+
if let Some(constant) = self.try_fold_constant(&checked_expr, false) {
598598
return Ok(constant);
599599
}
600600
// if the source type is nullable, cast target type should also be nullable.
@@ -637,7 +637,7 @@ impl<'a> TypeChecker<'a> {
637637
let registry = &BUILTIN_FUNCTIONS;
638638
let checked_expr = type_check::check(&raw_expr, registry)?;
639639

640-
if let Some(constant) = self.try_fold_constant(&checked_expr) {
640+
if let Some(constant) = self.try_fold_constant(&checked_expr, false) {
641641
return Ok(constant);
642642
}
643643

@@ -1086,7 +1086,6 @@ impl<'a> TypeChecker<'a> {
10861086

10871087
Expr::Hole { .. } => unreachable!("hole is impossible in trivial query"),
10881088
};
1089-
10901089
Ok(Box::new((scalar, data_type)))
10911090
}
10921091

@@ -2749,7 +2748,7 @@ impl<'a> TypeChecker<'a> {
27492748
} => {
27502749
let mut folded_args = Vec::with_capacity(args.len());
27512750
for (checked_arg, arg) in checked_args.iter().zip(args.iter()) {
2752-
match self.try_fold_constant(checked_arg) {
2751+
match self.try_fold_constant(checked_arg, true) {
27532752
Some(constant) if arg.evaluable() => {
27542753
folded_args.push(constant.0);
27552754
}
@@ -2767,7 +2766,7 @@ impl<'a> TypeChecker<'a> {
27672766
self.ctx.set_cacheable(false);
27682767
}
27692768

2770-
if let Some(constant) = self.try_fold_constant(&expr) {
2769+
if let Some(constant) = self.try_fold_constant(&expr, true) {
27712770
return Ok(constant);
27722771
}
27732772

@@ -5067,8 +5066,9 @@ impl<'a> TypeChecker<'a> {
50675066
fn try_fold_constant<Index: ColumnIndex>(
50685067
&self,
50695068
expr: &EExpr<Index>,
5069+
enable_shrink: bool,
50705070
) -> Option<Box<(ScalarExpr, DataType)>> {
5071-
if expr.is_deterministic(&BUILTIN_FUNCTIONS) {
5071+
if expr.is_deterministic(&BUILTIN_FUNCTIONS) && enable_shrink {
50725072
if let (EExpr::Constant { scalar, .. }, _) =
50735073
ConstantFolder::fold(expr, &self.func_ctx, &BUILTIN_FUNCTIONS)
50745074
{

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)