MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / find_minmax

Function find_minmax

nodedb-query/src/msgpack_scan/aggregate.rs:292–367  ·  view source on GitHub ↗

Find min or max across docs by comparing raw field bytes.

(
    docs: &[&[u8]],
    field: &str,
    expr: Option<&crate::expr::SqlExpr>,
    want_max: bool,
)

Source from the content-addressed store, hash-verified

290
291/// Find min or max across docs by comparing raw field bytes.
292fn find_minmax(
293 docs: &[&[u8]],
294 field: &str,
295 expr: Option<&crate::expr::SqlExpr>,
296 want_max: bool,
297) -> Value {
298 if let Some(expr) = expr {
299 // Evaluate expression once per doc; compare on Value
300 // since the result may be any type (not a raw field).
301 let mut best: Option<Value> = None;
302 for doc in docs {
303 let Some(value) = eval_expr_on_doc(doc, expr) else {
304 continue;
305 };
306 if value.is_null() {
307 continue;
308 }
309 let replace = match &best {
310 None => true,
311 Some(current) => {
312 let ord = value_ops::compare_values(&value, current);
313 if want_max {
314 ord == Ordering::Greater
315 } else {
316 ord == Ordering::Less
317 }
318 }
319 };
320 if replace {
321 best = Some(value);
322 }
323 }
324 return best.unwrap_or(Value::Null);
325 }
326
327 let mut best_doc: Option<&[u8]> = None;
328 let mut best_range: Option<(usize, usize)> = None;
329
330 for doc in docs {
331 if let Some(range) = extract_field(doc, 0, field) {
332 if read_null(doc, range.0) {
333 continue;
334 }
335 match best_range {
336 None => {
337 best_doc = Some(doc);
338 best_range = Some(range);
339 }
340 Some(br) => {
341 let Some(bd) = best_doc else { continue };
342 let cmp = compare_field_bytes(doc, range, bd, br);
343 let replace = if want_max {
344 cmp == Ordering::Greater
345 } else {
346 cmp == Ordering::Less
347 };
348 if replace {
349 best_doc = Some(doc);

Callers 1

compute_aggregate_binaryFunction · 0.85

Calls 8

eval_expr_on_docFunction · 0.85
extract_fieldFunction · 0.85
read_nullFunction · 0.85
compare_field_bytesFunction · 0.85
value_from_msgpackFunction · 0.85
read_valueFunction · 0.70
compare_valuesFunction · 0.50
is_nullMethod · 0.45

Tested by

no test coverage detected