@@ -261,89 +261,90 @@ impl TopK {
261261 let Some ( filter) = & self . filter else {
262262 return Ok ( ( ) ) ;
263263 } ;
264- if let Some ( thresholds) = self . heap . get_threshold_values ( & self . expr ) ? {
265- // Create filter expressions for each threshold
266- let mut filters: Vec < Arc < dyn PhysicalExpr > > =
267- Vec :: with_capacity ( thresholds. len ( ) ) ;
268-
269- let mut prev_sort_expr: Option < Arc < dyn PhysicalExpr > > = None ;
270- for ( sort_expr, value) in self . expr . iter ( ) . zip ( thresholds. iter ( ) ) {
271- // Create the appropriate operator based on sort order
272- let op = if sort_expr. options . descending {
273- // For descending sort, we want col > threshold (exclude smaller values)
274- Operator :: Gt
275- } else {
276- // For ascending sort, we want col < threshold (exclude larger values)
277- Operator :: Lt
278- } ;
264+ let Some ( thresholds) = self . heap . get_threshold_values ( & self . expr ) ? else {
265+ return Ok ( ( ) ) ;
266+ } ;
279267
280- let value_null = value. is_null ( ) ;
268+ // Create filter expressions for each threshold
269+ let mut filters: Vec < Arc < dyn PhysicalExpr > > =
270+ Vec :: with_capacity ( thresholds. len ( ) ) ;
281271
282- let comparison = Arc :: new ( BinaryExpr :: new (
283- Arc :: clone ( & sort_expr. expr ) ,
284- op,
285- lit ( value. clone ( ) ) ,
286- ) ) ;
272+ let mut prev_sort_expr: Option < Arc < dyn PhysicalExpr > > = None ;
273+ for ( sort_expr, value) in self . expr . iter ( ) . zip ( thresholds. iter ( ) ) {
274+ // Create the appropriate operator based on sort order
275+ let op = if sort_expr. options . descending {
276+ // For descending sort, we want col > threshold (exclude smaller values)
277+ Operator :: Gt
278+ } else {
279+ // For ascending sort, we want col < threshold (exclude larger values)
280+ Operator :: Lt
281+ } ;
287282
288- let comparison_with_null =
289- match ( sort_expr. options . nulls_first , value_null) {
290- // For nulls first, transform to (threshold.value is not null) and (threshold.expr is null or comparison)
291- ( true , true ) => lit ( false ) ,
292- ( true , false ) => Arc :: new ( BinaryExpr :: new (
293- is_null ( Arc :: clone ( & sort_expr. expr ) ) ?,
294- Operator :: Or ,
295- comparison,
296- ) ) ,
297- // For nulls last, transform to (threshold.value is null and threshold.expr is not null)
298- // or (threshold.value is not null and comparison)
299- ( false , true ) => is_not_null ( Arc :: clone ( & sort_expr. expr ) ) ?,
300- ( false , false ) => comparison,
301- } ;
302-
303- let mut eq_expr = Arc :: new ( BinaryExpr :: new (
304- Arc :: clone ( & sort_expr. expr ) ,
305- Operator :: Eq ,
306- lit ( value. clone ( ) ) ,
283+ let value_null = value. is_null ( ) ;
284+
285+ let comparison = Arc :: new ( BinaryExpr :: new (
286+ Arc :: clone ( & sort_expr. expr ) ,
287+ op,
288+ lit ( value. clone ( ) ) ,
289+ ) ) ;
290+
291+ let comparison_with_null = match ( sort_expr. options . nulls_first , value_null) {
292+ // For nulls first, transform to (threshold.value is not null) and (threshold.expr is null or comparison)
293+ ( true , true ) => lit ( false ) ,
294+ ( true , false ) => Arc :: new ( BinaryExpr :: new (
295+ is_null ( Arc :: clone ( & sort_expr. expr ) ) ?,
296+ Operator :: Or ,
297+ comparison,
298+ ) ) ,
299+ // For nulls last, transform to (threshold.value is null and threshold.expr is not null)
300+ // or (threshold.value is not null and comparison)
301+ ( false , true ) => is_not_null ( Arc :: clone ( & sort_expr. expr ) ) ?,
302+ ( false , false ) => comparison,
303+ } ;
304+
305+ let mut eq_expr = Arc :: new ( BinaryExpr :: new (
306+ Arc :: clone ( & sort_expr. expr ) ,
307+ Operator :: Eq ,
308+ lit ( value. clone ( ) ) ,
309+ ) ) ;
310+
311+ if value_null {
312+ eq_expr = Arc :: new ( BinaryExpr :: new (
313+ is_null ( Arc :: clone ( & sort_expr. expr ) ) ?,
314+ Operator :: Or ,
315+ eq_expr,
307316 ) ) ;
317+ }
308318
309- if value_null {
310- eq_expr = Arc :: new ( BinaryExpr :: new (
311- is_null ( Arc :: clone ( & sort_expr. expr ) ) ?,
312- Operator :: Or ,
313- eq_expr,
314- ) ) ;
319+ // For a query like order by a, b, the filter for column `b` is only applied if
320+ // the condition a = threshold.value (considering null equality) is met.
321+ // Therefore, we add equality predicates for all preceding fields to the filter logic of the current field,
322+ // and include the current field's equality predicate in `prev_sort_expr` for use with subsequent fields.
323+ match prev_sort_expr. take ( ) {
324+ None => {
325+ prev_sort_expr = Some ( eq_expr) ;
326+ filters. push ( comparison_with_null) ;
315327 }
316-
317- // For a query like order by a, b, the filter for column `b` is only applied if
318- // the condition a = threshold.value (considering null equality) is met.
319- // Therefore, we add equality predicates for all preceding fields to the filter logic of the current field,
320- // and include the current field's equality predicate in `prev_sort_expr` for use with subsequent fields.
321- match prev_sort_expr. take ( ) {
322- None => {
323- prev_sort_expr = Some ( eq_expr) ;
324- filters. push ( comparison_with_null) ;
325- }
326- Some ( p) => {
327- filters. push ( Arc :: new ( BinaryExpr :: new (
328- Arc :: clone ( & p) ,
329- Operator :: And ,
330- comparison_with_null,
331- ) ) ) ;
332-
333- prev_sort_expr =
334- Some ( Arc :: new ( BinaryExpr :: new ( p, Operator :: And , eq_expr) ) ) ;
335- }
328+ Some ( p) => {
329+ filters. push ( Arc :: new ( BinaryExpr :: new (
330+ Arc :: clone ( & p) ,
331+ Operator :: And ,
332+ comparison_with_null,
333+ ) ) ) ;
334+
335+ prev_sort_expr =
336+ Some ( Arc :: new ( BinaryExpr :: new ( p, Operator :: And , eq_expr) ) ) ;
336337 }
337338 }
339+ }
338340
339- let dynamic_predicate = filters
340- . into_iter ( )
341- . reduce ( |a, b| Arc :: new ( BinaryExpr :: new ( a, Operator :: Or , b) ) ) ;
341+ let dynamic_predicate = filters
342+ . into_iter ( )
343+ . reduce ( |a, b| Arc :: new ( BinaryExpr :: new ( a, Operator :: Or , b) ) ) ;
342344
343- if let Some ( predicate) = dynamic_predicate {
344- if !predicate. eq ( & lit ( true ) ) {
345- filter. update ( predicate) ?;
346- }
345+ if let Some ( predicate) = dynamic_predicate {
346+ if !predicate. eq ( & lit ( true ) ) {
347+ filter. update ( predicate) ?;
347348 }
348349 }
349350
0 commit comments