diff --git a/datafusion/physical-plan/src/topk/mod.rs b/datafusion/physical-plan/src/topk/mod.rs index b017b2c9b428..6c0479a108e8 100644 --- a/datafusion/physical-plan/src/topk/mod.rs +++ b/datafusion/physical-plan/src/topk/mod.rs @@ -350,12 +350,6 @@ impl TopK { let new_threshold_row = &max_row.row; - // Extract scalar values BEFORE acquiring lock to reduce critical section - let thresholds = match self.heap.get_threshold_values(&self.expr)? { - Some(t) => t, - None => return Ok(()), - }; - // Fast path: check if the current value in topk is better than what is // currently set in the filter with a read only lock let needs_update = self @@ -374,6 +368,12 @@ impl TopK { return Ok(()); } + // Extract scalar values BEFORE acquiring lock to reduce critical section + let thresholds = match self.heap.get_threshold_values(&self.expr)? { + Some(t) => t, + None => return Ok(()), + }; + // Build the filter expression OUTSIDE any synchronization let predicate = Self::build_filter_expression(&self.expr, thresholds)?; let new_threshold = new_threshold_row.to_vec();