Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query): Disabled the shrink_scalar optimization for cast expressions during the resolve procedure args #17119

Merged
merged 2 commits into from
Dec 27, 2024
Merged
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
18 changes: 11 additions & 7 deletions src/query/sql/src/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ impl<'a> TypeChecker<'a> {
let registry = &BUILTIN_FUNCTIONS;
let checked_expr = type_check::check(&raw_expr, registry)?;

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

if let Some(constant) = self.try_fold_constant(&checked_expr) {
if let Some(constant) = self.try_fold_constant(&checked_expr, false) {
return Ok(constant);
}

Expand Down Expand Up @@ -1112,7 +1112,6 @@ impl<'a> TypeChecker<'a> {

Expr::Hole { .. } => unreachable!("hole is impossible in trivial query"),
};

Ok(Box::new((scalar, data_type)))
}

Expand Down Expand Up @@ -2775,7 +2774,7 @@ impl<'a> TypeChecker<'a> {
} => {
let mut folded_args = Vec::with_capacity(args.len());
for (checked_arg, arg) in checked_args.iter().zip(args.iter()) {
match self.try_fold_constant(checked_arg) {
match self.try_fold_constant(checked_arg, true) {
Some(constant) if arg.evaluable() => {
folded_args.push(constant.0);
}
Expand All @@ -2793,7 +2792,7 @@ impl<'a> TypeChecker<'a> {
self.ctx.set_cacheable(false);
}

if let Some(constant) = self.try_fold_constant(&expr) {
if let Some(constant) = self.try_fold_constant(&expr, true) {
return Ok(constant);
}

Expand Down Expand Up @@ -5166,12 +5165,17 @@ impl<'a> TypeChecker<'a> {
fn try_fold_constant<Index: ColumnIndex>(
&self,
expr: &EExpr<Index>,
enable_shrink: bool,
) -> Option<Box<(ScalarExpr, DataType)>> {
if expr.is_deterministic(&BUILTIN_FUNCTIONS) {
if expr.is_deterministic(&BUILTIN_FUNCTIONS) && enable_shrink {
TCeason marked this conversation as resolved.
Show resolved Hide resolved
if let (EExpr::Constant { scalar, .. }, _) =
ConstantFolder::fold(expr, &self.func_ctx, &BUILTIN_FUNCTIONS)
{
let scalar = shrink_scalar(scalar);
let scalar = if enable_shrink {
shrink_scalar(scalar)
} else {
scalar
};
let ty = scalar.as_ref().infer_data_type();
return Some(Box::new((
ConstantExpr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,36 @@ drop procedure if exists not_exists_p();
statement error 3130
drop procedure not_exists_p();

statement ok
drop procedure if exists sum_even_numbers(Int, Int);

statement ok
CREATE PROCEDURE sum_even_numbers(start_val Int, end_val Int)
RETURNS UInt8 NOT NULL
LANGUAGE SQL
COMMENT='Calculate the sum of all even numbers'
AS $$
BEGIN
LET sum := 0;
FOR i IN start_val TO end_val DO
IF i % 2 = 0 THEN
sum := sum + i;
END IF;
END FOR;
RETURN sum;
END;
$$;

statement error 3130
call procedure sum_even_numbers(1, 2)

query T
call procedure sum_even_numbers(1::INT, 2::INT)
----
2

statement ok
drop procedure sum_even_numbers(Int, Int);

statement ok
unset global enable_experimental_procedure;
Loading