From 4bc3b299aaf1bdccdc8c783ace5e98a918a26eda Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 27 Apr 2022 02:30:03 +0100 Subject: [PATCH] Implement RAND() ordering in ORDER BY clauses --- lib/src/dbs/iterator.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/src/dbs/iterator.rs b/lib/src/dbs/iterator.rs index bc842613..5f6916ec 100644 --- a/lib/src/dbs/iterator.rs +++ b/lib/src/dbs/iterator.rs @@ -20,6 +20,7 @@ use crate::sql::statements::update::UpdateStatement; use crate::sql::table::Table; use crate::sql::thing::Thing; use crate::sql::value::Value; +use rand::Rng; use std::cmp::Ordering; use std::collections::BTreeMap; use std::mem; @@ -288,10 +289,18 @@ impl Iterator { // Loop over each order clause for order in orders.iter() { // Reverse the ordering if DESC - let o = match order.direction { - true => a.compare(b, &order.order), - false => b.compare(a, &order.order), + let o = match order.random { + true => { + let a = rand::random::(); + let b = rand::random::(); + a.partial_cmp(&b) + } + false => match order.direction { + true => a.compare(b, &order.order), + false => b.compare(a, &order.order), + }, }; + // match o { Some(Ordering::Greater) => return Ordering::Greater, Some(Ordering::Equal) => continue,