@@ -372,42 +372,37 @@ pub(crate) fn window_equivalence_properties(
372372 let partitioning_exprs = expr. partition_by ( ) ;
373373 let no_partitioning = partitioning_exprs. is_empty ( ) ;
374374
375- // Use incremental approach to avoid O(4^n) exponential complexity:
376- // Instead of generating all combinations upfront via multi_cartesian_product,
377- // we build orderings incrementally and prune invalid paths early.
375+ // Find "one" valid ordering for partition columns to avoid exponential complexity.
378376 let mut all_satisfied_lexs = vec ! [ ] ;
379377 if !no_partitioning {
380- // Start with empty orderings that we'll extend incrementally
381- let mut current_orderings = vec ! [ vec! [ ] ] ;
378+ // Find a single valid ordering using a greedy approach
379+ let mut ordering = vec ! [ ] ;
382380 for partition_expr in partitioning_exprs. iter ( ) {
383- let mut next_orderings = vec ! [ ] ;
384-
385381 let sort_options =
386382 sort_options_resolving_constant ( Arc :: clone ( partition_expr) , true ) ;
387383
388- // For each current partial ordering, try extending with each sort option
389- for current in current_orderings . iter ( ) {
390- for sort_expr in sort_options. iter ( ) {
391- let mut extended = current . clone ( ) ;
392- extended . push ( sort_expr. clone ( ) ) ;
393-
394- // Check if this partial ordering can potentially satisfy requirements
395- if let Some ( lex) = LexOrdering :: new ( extended . clone ( ) ) {
396- if window_eq_properties . ordering_satisfy ( lex . clone ( ) ) ? {
397- next_orderings . push ( extended ) ;
398- }
384+ // Try each sort option and pick the first one that works
385+ let mut found = false ;
386+ for sort_expr in sort_options. iter ( ) {
387+ let mut candidate_ordering = ordering . clone ( ) ;
388+ candidate_ordering . push ( sort_expr. clone ( ) ) ;
389+
390+ if let Some ( lex ) = LexOrdering :: new ( candidate_ordering . clone ( ) ) {
391+ if window_eq_properties . ordering_satisfy ( lex) ? {
392+ ordering . push ( sort_expr . clone ( ) ) ;
393+ found = true ;
394+ break ;
399395 }
400396 }
401397 }
402- // If no valid orderings remain, stop early
403- if next_orderings. is_empty ( ) {
398+ // If no sort option works for this column, we can't build a valid ordering
399+ if !found {
400+ ordering. clear ( ) ;
404401 break ;
405402 }
406- current_orderings = next_orderings;
407403 }
408-
409- // Convert final orderings to LexOrdering and add to all_satisfied_lexs
410- for ordering in current_orderings {
404+ // If we successfully built an ordering for all columns, use it
405+ if ordering. len ( ) == partitioning_exprs. len ( ) {
411406 if let Some ( lex) = LexOrdering :: new ( ordering) {
412407 all_satisfied_lexs. push ( lex) ;
413408 }
0 commit comments