Apache DataFusion SQL Query Engine
ARROW-9895: [Rust] Improve sorting kernels
Jörn Horstmannce89ab540d06
diff --git a/rust/datafusion/README.md b/rust/datafusion/README.mdindex a9dce33d79..015f5f7da6 100644--- a/rust/datafusion/README.md+++ b/rust/datafusion/README.md@@ -81,7 +81,7 @@ This library currently supports the following SQL constructs:* most mathematical unary and binary expressions such as `+`, `/`, `sqrt`, `tan`, `>=`.* `WHERE` to filter* `GROUP BY` together with one of the following aggregations: `MIN`, `MAX`, `COUNT`, `SUM`, `AVG`-* `ORDER BY` together with an expression and optional `DESC`+* `ORDER BY` together with an expression and optional `ASC` or `DESC` and also optional `NULLS FIRST` or `NULLS LAST`## Supported Data Typesdiff --git a/rust/datafusion/src/physical_plan/sort.rs b/rust/datafusion/src/physical_plan/sort.rsindex 498cbd7cb5..c58347a408 100644--- a/rust/datafusion/src/physical_plan/sort.rs+++ b/rust/datafusion/src/physical_plan/sort.rs@@ -169,6 +169,7 @@ mod tests {use super::*;use crate::physical_plan::csv::{CsvExec, CsvReadOptions};use crate::physical_plan::expressions::col;+ use crate::physical_plan::memory::MemoryExec;use crate::physical_plan::merge::MergeExec;use crate::test;use arrow::array::*;@@ -223,4 +224,106 @@ mod tests {Ok(())}++ #[test]+ fn test_lex_sort_by_float() -> Result<()> {+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Float32, true),+ Field::new("b", DataType::Float64, true),+ ]));++ // define data.+ let batch = RecordBatch::try_new(+ schema.clone(),+ vec![+ Arc::new(Float32Array::from(vec![+ Some(f32::NAN),+ None,+ None,+ Some(f32::NAN),+ Some(1.0_f32),+ Some(1.0_f32),+ Some(2.0_f32),+ Some(3.0_f32),+ ])),+ Arc::new(Float64Array::from(vec![+ Some(200.0_f64),+ Some(20.0_f64),+ Some(10.0_f64),+ Some(100.0_f64),+ Some(f64::NAN),+ None,+ None,+ Some(f64::NAN),+ ])),+ ],+ )?;++ let sort_exec = Arc::new(SortExec::try_new(+ vec![+ PhysicalSortExpr {+ expr: col("a"),+ options: SortOptions {+ descending: true,+ nulls_first: true,+ },+ },+ PhysicalSortExpr {+ expr: col("b"),+ options: SortOptions {+ descending: false,+ nulls_first: false,+ },+ },+ ],+ Arc::new(MemoryExec::try_new(&vec![vec![batch]], schema, None)?),+ 2,+ )?);++ assert_eq!(DataType::Float32, *sort_exec.schema().field(0).data_type());+ assert_eq!(DataType::Float64, *sort_exec.schema().field(1).data_type());++ let result: Vec<RecordBatch> = test::execute(sort_exec)?;+ assert_eq!(result.len(), 1);++ let columns = result[0].columns();++ assert_eq!(DataType::Float32, *columns[0].data_type());+ assert_eq!(DataType::Float64, *columns[1].data_type());++ let a = as_primitive_array::<Float32Type>(&columns[0]);+ let b = as_primitive_array::<Float64Type>(&columns[1]);++ // convert result to strings to allow comparing to expected result containing NaN+ let result: Vec<(Option<String>, Option<String>)> = (0..result[0].num_rows())+ .map(|i| {+ let aval = if a.is_valid(i) {+ Some(a.value(i).to_string())+ } else {+ None+ };+ let bval = if b.is_valid(i) {+ Some(b.value(i).to_string())+ } else {+ None+ };+ (aval, bval)+ })+ .collect();++ let expected: Vec<(Option<String>, Option<String>)> = vec![+ (None, Some("10".to_owned())),+ (None, Some("20".to_owned())),+ (Some("NaN".to_owned()), Some("100".to_owned())),+ (Some("NaN".to_owned()), Some("200".to_owned())),+ (Some("3".to_owned()), Some("NaN".to_owned())),+ (Some("2".to_owned()), None),+ (Some("1".to_owned()), Some("NaN".to_owned())),+ (Some("1".to_owned()), None),+ ];++ assert_eq!(expected, result);++ Ok(())+ }}