@@ -280,11 +280,27 @@ pub(crate) fn case_expression(payload: &Value, site: Site<'_>) -> Result<Emitted
280280 require_understood ( payload, & [ "operand" , "whens" , "else_" ] , & construct) ?;
281281 let whens = list ( payload, "whens" ) ;
282282 let operand_node = payload. get ( "operand" ) . filter ( |operand| !operand. is_null ( ) ) ;
283+ let otherwise_node = payload. get ( "else_" ) . filter ( |value| !value. is_null ( ) ) ;
284+ let mut carried: Option < Vec < String > > = None ;
283285 let ( subject, conditions, mut branches) = match operand_node {
284286 Some ( node) if !plain ( node) && whens. iter ( ) . all ( |when| when. get ( 0 ) . is_some_and ( plain) ) => {
285- let ( subject, results) = bound_whens ( node, whens, site) ?;
286- let positions = ( 1 ..=results. len ( ) ) . map ( |position| position. to_string ( ) ) ;
287- ( format ! ( " {subject}" ) , positions. collect ( ) , results)
287+ let carry = whens. iter ( ) . all ( |when| when. get ( 1 ) . is_some_and ( plain) )
288+ && otherwise_node. is_none_or ( plain) ;
289+ let bound = bound_whens ( node, whens, carry, site) ?;
290+ match carry {
291+ true => {
292+ carried = Some ( bound. packed ) ;
293+ ( String :: new ( ) , bound. tests , bound. results )
294+ }
295+ false => {
296+ let positions = ( 1 ..=bound. results . len ( ) ) . map ( |position| position. to_string ( ) ) ;
297+ (
298+ format ! ( " {}" , bound. subject( ) ) ,
299+ positions. collect ( ) ,
300+ bound. results ,
301+ )
302+ }
303+ }
288304 }
289305 Some ( node) => {
290306 let operand = convert_expression ( node, site) ?;
@@ -296,8 +312,12 @@ pub(crate) fn case_expression(payload: &Value, site: Site<'_>) -> Result<Emitted
296312 ( String :: new ( ) , conditions, results)
297313 }
298314 } ;
299- let otherwise = match payload. get ( "else_" ) . filter ( |value| !value. is_null ( ) ) {
300- Some ( otherwise) => Some ( convert_expression ( otherwise, site) ?) ,
315+ let otherwise = match otherwise_node {
316+ Some ( otherwise) => Some ( carried_branch (
317+ convert_expression ( otherwise, site) ?,
318+ "e" ,
319+ carried. as_mut ( ) ,
320+ ) ) ,
301321 None => None ,
302322 } ;
303323 branches. extend ( otherwise. iter ( ) . cloned ( ) ) ;
@@ -310,6 +330,9 @@ pub(crate) fn case_expression(payload: &Value, site: Site<'_>) -> Result<Emitted
310330 sql. push_str ( & format ! ( " ELSE {last}" ) ) ;
311331 }
312332 sql. push_str ( " END)" ) ;
333+ if let Some ( packed) = carried {
334+ sql = bound_subject ( & packed, & sql) ;
335+ }
313336 let mut values = marks ( & branches) ;
314337 if otherwise. is_none ( ) {
315338 values. push ( ( SnowflakeType :: Null , false ) ) ;
@@ -338,12 +361,56 @@ fn plain(node: &Value) -> bool {
338361 }
339362}
340363
341- /// A simple CASE's operand bound once beside its WHEN values: the first equal WHEN's position.
364+ /// A simple CASE's operand packed once, as DuckDB repeats a simple CASE's operand in every WHEN.
365+ struct BoundWhens {
366+ /// The `struct_pack` fields: the operand, the WHEN values and any carried results.
367+ packed : Vec < String > ,
368+ /// Each WHEN's test of the packed operand against its packed value.
369+ tests : Vec < String > ,
370+ /// The results, read from the pack when carried.
371+ results : Vec < Emitted > ,
372+ }
373+
374+ impl BoundWhens {
375+ /// The first equal WHEN's position, for a CASE over the positions choosing uncarried results.
376+ fn subject ( & self ) -> String {
377+ let tests: Vec < String > = self
378+ . tests
379+ . iter ( )
380+ . enumerate ( )
381+ . map ( |( position, test) | format ! ( " WHEN {test} THEN {}" , position + 1 ) )
382+ . collect ( ) ;
383+ bound_subject ( & self . packed , & format ! ( "CASE{} END" , tests. concat( ) ) )
384+ }
385+ }
386+
387+ /// `body` over the packed fields, read through the lambda parameter once per row.
388+ fn bound_subject ( packed : & [ String ] , body : & str ) -> String {
389+ format ! (
390+ "list_transform([struct_pack({})], lambda {CASE_SUBJECT}: {body})[1]" ,
391+ packed. join( ", " )
392+ )
393+ }
394+
395+ /// `branch` read from the pack as `field` when the results are carried in it.
396+ fn carried_branch ( branch : Emitted , field : & str , packed : Option < & mut Vec < String > > ) -> Emitted {
397+ let Some ( packed) = packed else {
398+ return branch;
399+ } ;
400+ packed. push ( format ! ( "{field} := {}" , branch. sql) ) ;
401+ Emitted {
402+ sql : format ! ( "{CASE_SUBJECT}['{field}']" ) ,
403+ ..branch
404+ }
405+ }
406+
407+ /// A simple CASE's operand packed once with its WHEN values and, when `carry`, its plain results.
342408fn bound_whens (
343409 operand : & Value ,
344410 whens : & [ Value ] ,
411+ carry : bool ,
345412 site : Site < ' _ > ,
346- ) -> Result < ( String , Vec < Emitted > ) , Refusal > {
413+ ) -> Result < BoundWhens , Refusal > {
347414 let construct = node_construct ( "case" ) ;
348415 let operand = convert_expression ( operand, site) ?;
349416 let mut packed = vec ! [ format!( "o := {}" , operand. sql) ] ;
@@ -367,25 +434,22 @@ fn bound_whens(
367434 ..value
368435 } ;
369436 let ( left, right) = comparable ( subject. clone ( ) , value, & construct, site. context ) ?;
370- tests. push ( format ! (
371- " WHEN {} = {} THEN {}" ,
372- left. sql,
373- right. sql,
374- position + 1
437+ tests. push ( format ! ( "{} = {}" , left. sql, right. sql) ) ;
438+ let result = convert_expression ( result, site) ?;
439+ results. push ( carried_branch (
440+ result,
441+ & format ! ( "r{position}" ) ,
442+ carry. then_some ( & mut packed) ,
375443 ) ) ;
376- results. push ( convert_expression ( result, site) ?) ;
377444 }
378445 if tests. is_empty ( ) {
379446 return Err ( missing ( construct, "CASE without WHEN" . to_owned ( ) ) ) ;
380447 }
381- Ok ( (
382- format ! (
383- "list_transform([struct_pack({})], lambda {CASE_SUBJECT}: CASE{} END)[1]" ,
384- packed. join( ", " ) ,
385- tests. concat( )
386- ) ,
448+ Ok ( BoundWhens {
449+ packed,
450+ tests,
387451 results,
388- ) )
452+ } )
389453}
390454
391455fn case_whens (
0 commit comments