Apache DataFusion SQL Query Engine
ARROW-11421: [Rust][DataFusion] Support GROUP BY Date32
Heres, Danielcfa5b1d6e8e8
diff --git a/rust/datafusion/src/physical_plan/group_scalar.rs b/rust/datafusion/src/physical_plan/group_scalar.rsindex 6aa699b364..51e7c4b807 100644--- a/rust/datafusion/src/physical_plan/group_scalar.rs+++ b/rust/datafusion/src/physical_plan/group_scalar.rs@@ -40,6 +40,7 @@ pub(crate) enum GroupByScalar {Boolean(bool),TimeMicrosecond(i64),TimeNanosecond(i64),+ Date32(i32),}impl TryFrom<&ScalarValue> for GroupByScalar {@@ -107,6 +108,7 @@ impl From<&GroupByScalar> for ScalarValue {GroupByScalar::Utf8(v) => ScalarValue::Utf8(Some(v.to_string())),GroupByScalar::TimeMicrosecond(v) => ScalarValue::TimeMicrosecond(Some(*v)),GroupByScalar::TimeNanosecond(v) => ScalarValue::TimeNanosecond(Some(*v)),+ GroupByScalar::Date32(v) => ScalarValue::Date32(Some(*v)),}}}diff --git a/rust/datafusion/src/physical_plan/hash_aggregate.rs b/rust/datafusion/src/physical_plan/hash_aggregate.rsindex b893f52634..677578c5e3 100644--- a/rust/datafusion/src/physical_plan/hash_aggregate.rs+++ b/rust/datafusion/src/physical_plan/hash_aggregate.rs@@ -31,7 +31,7 @@ use crate::error::{DataFusionError, Result};use crate::physical_plan::{Accumulator, AggregateExpr};use crate::physical_plan::{Distribution, ExecutionPlan, Partitioning, PhysicalExpr};-use arrow::array::BooleanArray;+use arrow::array::{BooleanArray, Date32Array};use arrow::{array::{Array, UInt32Builder},error::{ArrowError, Result as ArrowResult},@@ -473,6 +473,10 @@ pub(crate) fn create_key(// store the string valuevec.extend_from_slice(value.as_bytes());}+ DataType::Date32 => {+ let array = col.as_any().downcast_ref::<Date32Array>().unwrap();+ vec.extend_from_slice(&array.value(row).to_le_bytes());+ }_ => {// This is internal because we should have caught this before.return Err(DataFusionError::Internal(format!(@@ -844,6 +848,7 @@ fn create_batch_from_map(GroupByScalar::TimeNanosecond(n) => {Arc::new(TimestampNanosecondArray::from_vec(vec![*n], None))}+ GroupByScalar::Date32(n) => Arc::new(Date32Array::from(vec![*n])),}).collect::<Vec<ArrayRef>>();@@ -978,6 +983,11 @@ pub(crate) fn create_group_by_values(.unwrap();vec[i] = GroupByScalar::TimeNanosecond(array.value(row))}+ DataType::Date32 => {+ let array = col.as_any().downcast_ref::<Date32Array>().unwrap();+ vec[i] = GroupByScalar::Date32(array.value(row));+ }+_ => {// This is internal because we should have caught this before.return Err(DataFusionError::Internal(format!(diff --git a/rust/datafusion/tests/sql.rs b/rust/datafusion/tests/sql.rsindex c3c524686d..82b58bb051 100644--- a/rust/datafusion/tests/sql.rs+++ b/rust/datafusion/tests/sql.rs@@ -710,7 +710,8 @@ async fn csv_query_cast() -> Result<()> {async fn csv_query_cast_literal() -> Result<()> {let mut ctx = ExecutionContext::new();register_aggregate_csv(&mut ctx)?;- let sql = "SELECT c12, CAST(1 AS float) FROM aggregate_test_100 WHERE c12 > CAST(0 AS float) LIMIT 2";+ let sql =+ "SELECT c12, CAST(1 AS float) FROM aggregate_test_100 WHERE c12 > CAST(0 AS float) LIMIT 2";let actual = execute(&mut ctx, sql).await;let expected = vec![vec!["0.9294097332465232", "1"],@@ -1123,8 +1124,7 @@ async fn equijoin() -> Result<()> {#[tokio::test]async fn left_join() -> Result<()> {let mut ctx = create_join_context("t1_id", "t2_id")?;- let sql =- "SELECT t1_id, t1_name, t2_name FROM t1 LEFT JOIN t2 ON t1_id = t2_id ORDER BY t1_id";+ let sql = "SELECT t1_id, t1_name, t2_name FROM t1 LEFT JOIN t2 ON t1_id = t2_id ORDER BY t1_id";let actual = execute(&mut ctx, sql).await;let expected = vec![vec!["11", "a", "z"],@@ -1900,6 +1900,46 @@ async fn csv_between_expr_negated() -> Result<()> {Ok(())}+#[tokio::test]+async fn csv_group_by_date() -> Result<()> {+ let mut ctx = ExecutionContext::new();+ let schema = Arc::new(Schema::new(vec![+ Field::new("date", DataType::Date32, false),+ Field::new("cnt", DataType::Int32, false),+ ]));+ let data = RecordBatch::try_new(+ schema.clone(),+ vec![+ Arc::new(Date32Array::from(vec![+ Some(100),+ Some(100),+ Some(100),+ Some(101),+ Some(101),+ Some(101),+ ])),+ Arc::new(Int32Array::from(vec![+ Some(1),+ Some(2),+ Some(3),+ Some(3),+ Some(3),+ Some(3),+ ])),+ ],+ )?;+ let table = MemTable::try_new(schema, vec![vec![data]])?;++ ctx.register_table("dates", Box::new(table));+ let sql = "SELECT SUM(cnt) FROM dates GROUP BY date";+ let actual = execute(&mut ctx, sql).await;+ let mut actual: Vec<String> = actual.iter().flatten().cloned().collect();+ actual.sort();+ let expected = vec!["6", "9"];+ assert_eq!(expected, actual);+ Ok(())+}+#[tokio::test]async fn string_expressions() -> Result<()> {let mut ctx = ExecutionContext::new();