Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a faster sort #803

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions src/builtins/filters/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;

use crate::context::{get_json_pointer, ValueRender};
use crate::errors::{Error, Result};
use crate::filter_utils::{get_sort_strategy_for_type, get_unique_strategy_for_type};
use crate::filter_utils::{compare_values, get_unique_strategy_for_type};
use crate::utils::render_to_string;
use serde_json::value::{to_value, Map, Value};

Expand Down Expand Up @@ -65,11 +65,10 @@ pub fn join(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
/// Sorts the array in ascending order.
/// Use the 'attribute' argument to define a field to sort by.
pub fn sort(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
let arr = try_get_value!("sort", "value", Vec<Value>, value);
let mut arr = try_get_value!("sort", "value", Vec<Value>, value);
if arr.is_empty() {
return Ok(arr.into());
}

let attribute = match args.get("attribute") {
Some(val) => try_get_value!("sort", "attribute", String, val),
None => String::new(),
Expand All @@ -79,20 +78,40 @@ pub fn sort(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
s => get_json_pointer(s),
};

// make sure the types are identical, and the field exists
let first = arr[0].pointer(&ptr).ok_or_else(|| {
Error::msg(format!("attribute '{}' does not reference a field", attribute))
})?;

let mut strategy = get_sort_strategy_for_type(first)?;
for v in &arr {
let key = v.pointer(&ptr).ok_or_else(|| {
Error::msg(format!("attribute '{}' does not reference a field", attribute))
})?;
strategy.try_add_pair(v, key)?;
}
let sorted = strategy.sort();

Ok(sorted.into())
let discriminant = std::mem::discriminant(first);
let misfits: Result<Vec<_>> = arr[1..]
.iter()
.map(|item| {
let a = item.pointer(&ptr).ok_or(Error::msg(format!(
"attribute '{}' does not reference a field for {}",
attribute, item
)))?;

if std::mem::discriminant(a) != discriminant {
return Err(Error::msg(format!(
"expected same types but got {} and {}",
first, item
)));
}
Ok(())
})
.collect();
misfits?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to put ? after the collect.


arr.sort_unstable_by(|a, b| {
compare_values(
a.pointer(&ptr)
.expect(&format!("attribute '{}' does not reference a field", attribute)),
b.pointer(&ptr)
.expect(&format!("attribute '{}' does not reference a field", attribute)),
)
.expect("values should be compareable")
});
Ok(arr.into())
}

/// Remove duplicates from an array.
Expand Down Expand Up @@ -469,7 +488,7 @@ mod tests {

let result = sort(&v, &args);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "expected number got []");
assert_eq!(result.unwrap_err().to_string(), "expected same types but got 12 and []");
}

#[test]
Expand All @@ -482,8 +501,15 @@ mod tests {
let args = HashMap::new();

let result = sort(&v, &args);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Null is not a sortable value");
assert!(result.is_ok());
assert_eq!(
result.unwrap(),
to_value(vec![
::std::f64::NAN, // NaN and friends get deserialized as Null by serde.
::std::f64::NAN,
])
.unwrap()
);
}

#[derive(Deserialize, Eq, Hash, PartialEq, Serialize)]
Expand Down
81 changes: 31 additions & 50 deletions src/filter_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@ use crate::errors::{Error, Result};
use serde_json::Value;
use std::cmp::Ordering;

pub fn compare_values(a: &Value, b: &Value) -> Result<Ordering> {
if std::mem::discriminant(a) != std::mem::discriminant(b) {
return Err(Error::msg(format!("expected same types but got {a} and {b}")));
}
match a {
Value::Null => Ok(Ordering::Equal),
Value::Bool(a) => Ok(a.cmp(&b.as_bool().unwrap())),
Value::Number(a) => {
if let Some(a) = a.as_f64() {
if let Some(b) = b.as_f64() {
return Ok(a.total_cmp(&b));
}
}
if let Some(a) = a.as_i64() {
if let Some(b) = b.as_i64() {
return Ok(a.cmp(&b));
}
}
if let Some(a) = a.as_u64() {
if let Some(b) = b.as_u64() {
return Ok(a.cmp(&b));
}
}
Err(Error::msg(format!("{a} cannot be sorted")))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely going to error on the MSRV

}
Value::String(a) => Ok(a.as_str().cmp(b.as_str().unwrap())),
Value::Array(a) => Ok(a.len().cmp(&b.as_array().unwrap().len())),
Value::Object(a) => Ok(a.len().cmp(&b.as_array().unwrap().len())),
}
}

#[derive(PartialEq, PartialOrd, Default, Copy, Clone)]
pub struct OrderedF64(f64);

Expand Down Expand Up @@ -66,56 +97,6 @@ impl GetValue for ArrayLen {
}
}

#[derive(Default)]
pub struct SortPairs<K: Ord> {
pairs: Vec<(Value, K)>,
}

type SortNumbers = SortPairs<OrderedF64>;
type SortBools = SortPairs<bool>;
type SortStrings = SortPairs<String>;
type SortArrays = SortPairs<ArrayLen>;

impl<K: GetValue> SortPairs<K> {
fn try_add_pair(&mut self, val: &Value, key: &Value) -> Result<()> {
let key = K::get_value(key)?;
self.pairs.push((val.clone(), key));
Ok(())
}

fn sort(&mut self) -> Vec<Value> {
self.pairs.sort_by_key(|a| a.1.clone());
self.pairs.iter().map(|a| a.0.clone()).collect()
}
}

pub trait SortStrategy {
fn try_add_pair(&mut self, val: &Value, key: &Value) -> Result<()>;
fn sort(&mut self) -> Vec<Value>;
}

impl<K: GetValue> SortStrategy for SortPairs<K> {
fn try_add_pair(&mut self, val: &Value, key: &Value) -> Result<()> {
SortPairs::try_add_pair(self, val, key)
}

fn sort(&mut self) -> Vec<Value> {
SortPairs::sort(self)
}
}

pub fn get_sort_strategy_for_type(ty: &Value) -> Result<Box<dyn SortStrategy>> {
use crate::Value::*;
match *ty {
Null => Err(Error::msg("Null is not a sortable value")),
Bool(_) => Ok(Box::new(SortBools::default())),
Number(_) => Ok(Box::new(SortNumbers::default())),
String(_) => Ok(Box::new(SortStrings::default())),
Array(_) => Ok(Box::new(SortArrays::default())),
Object(_) => Err(Error::msg("Object is not a sortable value")),
}
}

#[derive(Default)]
pub struct Unique<K: Eq + std::hash::Hash> {
unique: std::collections::HashSet<K>,
Expand Down