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: rewrite ifnull and nvl as coalesce #14877

Merged
merged 7 commits into from
Mar 9, 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
36 changes: 14 additions & 22 deletions src/query/sql/src/planner/semantic/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,34 +2510,18 @@ impl<'a> TypeChecker<'a> {
)
}
("ifnull", &[arg_x, arg_y]) => {
// Rewrite ifnull(x, y) to if(is_null(x), y, x)
// Rewrite ifnull(x, y) to coalesce(x, y)
Some(
self.resolve_function(span, "if", vec![], &[
&Expr::IsNull {
span,
expr: Box::new(arg_x.clone()),
not: false,
},
arg_y,
arg_x,
])
.await,
self.resolve_function(span, "coalesce", vec![], &[arg_x, arg_y])
.await,
)
}
("nvl", &[arg_x, arg_y]) => {
// Rewrite nvl(x, y) to if(is_not_null(x), x, y)
// Rewrite nvl(x, y) to coalesce(x, y)
// nvl is essentially an alias for ifnull.
Some(
self.resolve_function(span, "if", vec![], &[
&Expr::IsNull {
span,
expr: Box::new(arg_x.clone()),
not: true,
},
arg_x,
arg_y,
])
.await,
self.resolve_function(span, "coalesce", vec![], &[arg_x, arg_y])
.await,
)
}
("nvl2", &[arg_x, arg_y, arg_z]) => {
Expand Down Expand Up @@ -2620,6 +2604,14 @@ impl<'a> TypeChecker<'a> {
span,
lit: Literal::Null,
});
new_args.push(Expr::Literal {
span,
lit: Literal::Null,
});
new_args.push(Expr::Literal {
span,
lit: Literal::Null,
});
PsiACE marked this conversation as resolved.
Show resolved Hide resolved
let args_ref: Vec<&Expr> = new_args.iter().collect();
Some(self.resolve_function(span, "if", vec![], &args_ref).await)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ AggregateFinal
├── estimated rows: 0.00
├── EvalScalar
│ ├── output columns: [t.id (#0), de (#8)]
│ ├── expressions: [if(CAST(is_not_null(sum(tb.de) (#7)) AS Boolean NULL), CAST(assume_not_null(sum(tb.de) (#7)) AS Int64 NULL), true, 0, NULL)]
│ ├── expressions: [if(CAST(is_not_null(sum(tb.de) (#7)) AS Boolean NULL), CAST(assume_not_null(sum(tb.de) (#7)) AS Int64 NULL), true, 0, NULL, NULL, NULL)]
│ ├── estimated rows: 0.00
│ └── AggregateFinal
│ ├── output columns: [sum(tb.de) (#7), t.id (#0)]
Expand Down Expand Up @@ -177,7 +177,7 @@ AggregateFinal
│ │ ├── estimated rows: 0.00
│ │ └── EvalScalar
│ │ ├── output columns: [t2.sid (#1), sum_arg_0 (#4)]
│ │ ├── expressions: [if(CAST(is_not_null(t3.val (#2)) AS Boolean NULL), CAST(assume_not_null(t3.val (#2)) AS Int32 NULL), true, 0, NULL)]
│ │ ├── expressions: [if(CAST(is_not_null(t3.val (#2)) AS Boolean NULL), CAST(assume_not_null(t3.val (#2)) AS Int32 NULL), true, 0, NULL, NULL, NULL)]
│ │ ├── estimated rows: 0.00
│ │ └── TableScan
│ │ ├── table: default.default.t2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ AggregateFinal
├── estimated rows: 0.00
├── EvalScalar
│ ├── output columns: [t.id (#0), de (#8)]
│ ├── expressions: [if(CAST(is_not_null(sum(tb.de) (#7)) AS Boolean NULL), CAST(assume_not_null(sum(tb.de) (#7)) AS Int64 NULL), true, 0, NULL)]
│ ├── expressions: [if(CAST(is_not_null(sum(tb.de) (#7)) AS Boolean NULL), CAST(assume_not_null(sum(tb.de) (#7)) AS Int64 NULL), true, 0, NULL, NULL, NULL)]
│ ├── estimated rows: 0.00
│ └── AggregateFinal
│ ├── output columns: [sum(tb.de) (#7), t.id (#0)]
Expand Down Expand Up @@ -169,7 +169,7 @@ AggregateFinal
│ │ ├── estimated rows: 0.00
│ │ └── EvalScalar
│ │ ├── output columns: [t2.sid (#1), sum_arg_0 (#4)]
│ │ ├── expressions: [if(CAST(is_not_null(t3.val (#2)) AS Boolean NULL), CAST(assume_not_null(t3.val (#2)) AS Int32 NULL), true, 0, NULL)]
│ │ ├── expressions: [if(CAST(is_not_null(t3.val (#2)) AS Boolean NULL), CAST(assume_not_null(t3.val (#2)) AS Int32 NULL), true, 0, NULL, NULL, NULL)]
│ │ ├── estimated rows: 0.00
│ │ └── TableScan
│ │ ├── table: default.default.t2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
query B
select nvl(nullif(rand()>0.5, true), false) is null from (unnest(range(1,10)));
----
0
0
0
0
0
0
0
0
0

query I
SELECT NVL(1, 1)
----
Expand Down
Loading