Skip to content

Commit 519e4c5

Browse files
authored
fix: propagate CASE unparser errors (apache#26311)
## Which issue does this PR close? - Closes apache#26295. ## Rationale for this change When a simple CASE operand or ELSE branch cannot be unparsed, the unparser currently drops that child and returns valid-looking SQL with different semantics. Callers should receive the original unparse error instead. ## What changes are included in this PR? Propagate errors from CASE operands and ELSE branches, matching the existing WHEN/THEN behavior. Add regression coverage for both paths. ## What is the testing strategy for this PR? - `cargo test -p datafusion-sql --lib` (95 passed) - `cargo clippy -p datafusion-sql --all-targets --all-features -- -D warnings` - `cargo fmt --all --check` The regression test was also run against the pre-fix implementation and failed because unparsing returned a CASE AST instead of an error. ## Are there any user-facing changes? Yes. CASE expressions with unsupported operands or ELSE branches now return an error instead of producing SQL with altered semantics.
1 parent c52eb97 commit 519e4c5

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

‎datafusion/sql/src/unparser/expr.rs‎

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,14 @@ impl Unparser<'_> {
246246
})
247247
.collect::<Result<Vec<CaseWhen>>>()?;
248248

249-
let operand = match expr.as_ref() {
250-
Some(e) => match self.expr_to_sql_inner(e) {
251-
Ok(sql_expr) => Some(Box::new(sql_expr)),
252-
Err(_) => None,
253-
},
254-
None => None,
255-
};
256-
let else_result = match else_expr.as_ref() {
257-
Some(e) => match self.expr_to_sql_inner(e) {
258-
Ok(sql_expr) => Some(Box::new(sql_expr)),
259-
Err(_) => None,
260-
},
261-
None => None,
262-
};
249+
let operand = expr
250+
.as_ref()
251+
.map(|e| self.expr_to_sql_inner(e).map(Box::new))
252+
.transpose()?;
253+
let else_result = else_expr
254+
.as_ref()
255+
.map(|e| self.expr_to_sql_inner(e).map(Box::new))
256+
.transpose()?;
263257

264258
Ok(ast::Expr::Case {
265259
operand,
@@ -3453,6 +3447,28 @@ mod tests {
34533447
Ok(())
34543448
}
34553449

3450+
#[test]
3451+
fn case_propagates_operand_and_else_errors() {
3452+
let unsupported = Expr::Literal(ScalarValue::DurationSecond(Some(1)), None);
3453+
let expressions = [
3454+
Expr::Case(Case::new(
3455+
Some(Box::new(unsupported.clone())),
3456+
vec![(Box::new(lit(1)), Box::new(lit(2)))],
3457+
None,
3458+
)),
3459+
Expr::Case(Case::new(
3460+
None,
3461+
vec![(Box::new(lit(true)), Box::new(lit(1)))],
3462+
Some(Box::new(unsupported)),
3463+
)),
3464+
];
3465+
3466+
for expr in expressions {
3467+
let error = expr_to_sql(&expr).expect_err("unsupported CASE child");
3468+
assert!(error.to_string().contains("Unsupported scalar"));
3469+
}
3470+
}
3471+
34563472
#[test]
34573473
fn test_window_func_support_window_frame() -> Result<()> {
34583474
let default_dialect: Arc<dyn Dialect> =

0 commit comments

Comments
 (0)