Implement RAND() ordering in ORDER BY clauses

This commit is contained in:
Tobie Morgan Hitchcock 2022-04-27 02:30:03 +01:00
parent e6fe7e675b
commit 4bc3b299aa

View file

@ -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 {
let o = match order.random {
true => {
let a = rand::random::<f64>();
let b = rand::random::<f64>();
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,