Apache DataFusion SQL Query Engine
ARROW-11355: [Rust] Aligned Date DataType with specification.
Jorge C. Leitaoa53c76ff3403
diff --git a/rust/benchmarks/src/bin/tpch.rs b/rust/benchmarks/src/bin/tpch.rsindex 8f87ca3e26..c3352efb40 100644--- a/rust/benchmarks/src/bin/tpch.rs+++ b/rust/benchmarks/src/bin/tpch.rs@@ -20,7 +20,7 @@use std::path::{Path, PathBuf};use std::time::Instant;-use arrow::datatypes::{DataType, DateUnit, Field, Schema};+use arrow::datatypes::{DataType, Field, Schema};use arrow::util::pretty;use datafusion::datasource::parquet::ParquetTable;use datafusion::datasource::{CsvFile, MemTable, TableProvider};@@ -1185,7 +1185,7 @@ fn get_schema(table: &str) -> Schema {Field::new("o_custkey", DataType::Int32, false),Field::new("o_orderstatus", DataType::Utf8, false),Field::new("o_totalprice", DataType::Float64, false),- Field::new("o_orderdate", DataType::Date32(DateUnit::Day), false),+ Field::new("o_orderdate", DataType::Date32, false),Field::new("o_orderpriority", DataType::Utf8, false),Field::new("o_clerk", DataType::Utf8, false),Field::new("o_shippriority", DataType::Int32, false),@@ -1203,9 +1203,9 @@ fn get_schema(table: &str) -> Schema {Field::new("l_tax", DataType::Float64, false),Field::new("l_returnflag", DataType::Utf8, false),Field::new("l_linestatus", DataType::Utf8, false),- Field::new("l_shipdate", DataType::Date32(DateUnit::Day), false),- Field::new("l_commitdate", DataType::Date32(DateUnit::Day), false),- Field::new("l_receiptdate", DataType::Date32(DateUnit::Day), false),+ Field::new("l_shipdate", DataType::Date32, false),+ Field::new("l_commitdate", DataType::Date32, false),+ Field::new("l_receiptdate", DataType::Date32, false),Field::new("l_shipinstruct", DataType::Utf8, false),Field::new("l_shipmode", DataType::Utf8, false),Field::new("l_comment", DataType::Utf8, false),@@ -1421,7 +1421,7 @@ mod tests {3 => Schema::new(vec![Field::new("l_orderkey", DataType::Int32, true),Field::new("revenue", DataType::Float64, true),- Field::new("o_orderdat", DataType::Date32(DateUnit::Day), true),+ Field::new("o_orderdat", DataType::Date32, true),Field::new("o_shippriority", DataType::Int32, true),]),@@ -1499,7 +1499,7 @@ mod tests {Field::new("c_name", DataType::Utf8, true),Field::new("c_custkey", DataType::Int32, true),Field::new("o_orderkey", DataType::Int32, true),- Field::new("o_orderdat", DataType::Date32(DateUnit::Day), true),+ Field::new("o_orderdat", DataType::Date32, true),Field::new("o_totalprice", DataType::Float64, true),Field::new("sum_l_quantity", DataType::Float64, true),]),diff --git a/rust/datafusion/README.md b/rust/datafusion/README.mdindex ee757a59ed..0a6638ccbd 100644--- a/rust/datafusion/README.md+++ b/rust/datafusion/README.md@@ -123,9 +123,9 @@ are mapped to Arrow types according to the following table| `REAL` | `Float64` || `DOUBLE` | `Float64` || `BOOLEAN` | `Boolean` |-| `DATE` | `Date64(DateUnit::Day)` |+| `DATE` | `Date32` || `TIME` | `Time64(TimeUnit::Millisecond)` |-| `TIMESTAMP` | `Date64(DateUnit::Millisecond)` |+| `TIMESTAMP` | `Date64` || `INTERVAL` | *Not yet supported* || `REGCLASS` | *Not yet supported* || `TEXT` | *Not yet supported* |diff --git a/rust/datafusion/src/physical_plan/expressions.rs b/rust/datafusion/src/physical_plan/expressions.rsindex 3ef240dd70..485a578ed2 100644--- a/rust/datafusion/src/physical_plan/expressions.rs+++ b/rust/datafusion/src/physical_plan/expressions.rs@@ -48,7 +48,7 @@ use arrow::compute::kernels::comparison::{neq_utf8_scalar,};use arrow::compute::kernels::sort::{SortColumn, SortOptions};-use arrow::datatypes::{DataType, DateUnit, Schema, TimeUnit};+use arrow::datatypes::{DataType, Schema, TimeUnit};use arrow::record_batch::RecordBatch;use arrow::{array::{@@ -1089,7 +1089,7 @@ macro_rules! binary_array_op_scalar {DataType::Timestamp(TimeUnit::Nanosecond, None) => {compute_op_scalar!($LEFT, $RIGHT, $OP, TimestampNanosecondArray)}- DataType::Date32(DateUnit::Day) => {+ DataType::Date32 => {compute_op_scalar!($LEFT, $RIGHT, $OP, Date32Array)}other => Err(DataFusionError::Internal(format!(@@ -1120,10 +1120,10 @@ macro_rules! binary_array_op {DataType::Timestamp(TimeUnit::Nanosecond, None) => {compute_op!($LEFT, $RIGHT, $OP, TimestampNanosecondArray)}- DataType::Date32(DateUnit::Day) => {+ DataType::Date32 => {compute_op!($LEFT, $RIGHT, $OP, Date32Array)}- DataType::Date64(DateUnit::Millisecond) => {+ DataType::Date64 => {compute_op!($LEFT, $RIGHT, $OP, Date64Array)}other => Err(DataFusionError::Internal(format!(@@ -1238,10 +1238,10 @@ fn string_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>fn temporal_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {use arrow::datatypes::DataType::*;match (lhs_type, rhs_type) {- (Utf8, Date32(DateUnit::Day)) => Some(Date32(DateUnit::Day)),- (Date32(DateUnit::Day), Utf8) => Some(Date32(DateUnit::Day)),- (Utf8, Date64(DateUnit::Millisecond)) => Some(Date64(DateUnit::Millisecond)),- (Date64(DateUnit::Millisecond), Utf8) => Some(Date64(DateUnit::Millisecond)),+ (Utf8, Date32) => Some(Date32),+ (Date32, Utf8) => Some(Date32),+ (Utf8, Date64) => Some(Date64),+ (Date64, Utf8) => Some(Date64),_ => None,}}@@ -3014,7 +3014,7 @@ mod tests {DataType::Utf8,vec!["1994-12-13", "1995-01-26"],Date32Array,- DataType::Date32(DateUnit::Day),+ DataType::Date32,vec![9112, 9156],Operator::Eq,BooleanArray,@@ -3026,7 +3026,7 @@ mod tests {DataType::Utf8,vec!["1994-12-13", "1995-01-26"],Date32Array,- DataType::Date32(DateUnit::Day),+ DataType::Date32,vec![9113, 9154],Operator::Lt,BooleanArray,@@ -3038,7 +3038,7 @@ mod tests {DataType::Utf8,vec!["1994-12-13T12:34:56", "1995-01-26T01:23:45"],Date64Array,- DataType::Date64(DateUnit::Millisecond),+ DataType::Date64,vec![787322096000, 791083425000],Operator::Eq,BooleanArray,@@ -3050,7 +3050,7 @@ mod tests {DataType::Utf8,vec!["1994-12-13T12:34:56", "1995-01-26T01:23:45"],Date64Array,- DataType::Date64(DateUnit::Millisecond),+ DataType::Date64,vec![787322096001, 791083424999],Operator::Lt,BooleanArray,diff --git a/rust/datafusion/src/scalar.rs b/rust/datafusion/src/scalar.rsindex 8623edc6ca..b86f4e5c08 100644--- a/rust/datafusion/src/scalar.rs+++ b/rust/datafusion/src/scalar.rs@@ -19,6 +19,11 @@use std::{convert::TryFrom, fmt, iter::repeat, sync::Arc};+use arrow::array::{+ Array, BooleanArray, Date32Array, Float32Array, Float64Array, Int16Array, Int32Array,+ Int64Array, Int8Array, LargeStringArray, ListArray, StringArray, UInt16Array,+ UInt32Array, UInt64Array, UInt8Array,+};use arrow::array::{Int16Builder, Int32Builder, Int64Builder, Int8Builder, ListBuilder,TimestampMicrosecondArray, TimestampNanosecondArray, UInt16Builder, UInt32Builder,@@ -28,14 +33,6 @@ use arrow::{array::ArrayRef,datatypes::{DataType, Field},};-use arrow::{- array::{- Array, BooleanArray, Date32Array, Float32Array, Float64Array, Int16Array,- Int32Array, Int64Array, Int8Array, LargeStringArray, ListArray, StringArray,- UInt16Array, UInt32Array, UInt64Array, UInt8Array,- },- datatypes::DateUnit,-};use crate::error::{DataFusionError, Result};use arrow::datatypes::TimeUnit;@@ -150,7 +147,7 @@ impl ScalarValue {ScalarValue::List(_, data_type) => {DataType::List(Box::new(Field::new("item", data_type.clone(), true)))}- ScalarValue::Date32(_) => DataType::Date32(DateUnit::Day),+ ScalarValue::Date32(_) => DataType::Date32,}}@@ -358,7 +355,7 @@ impl ScalarValue {};ScalarValue::List(value, nested_type.data_type().clone())}- DataType::Date32(DateUnit::Day) => {+ DataType::Date32 => {typed_cast!(array, index, Date32Array, Date32)}other => {diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rsindex d2cbd7c4f6..d97b52dbe1 100644--- a/rust/datafusion/src/sql/planner.rs+++ b/rust/datafusion/src/sql/planner.rs@@ -214,9 +214,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {SQLDataType::Float(_) => Ok(DataType::Float32),SQLDataType::Real | SQLDataType::Double => Ok(DataType::Float64),SQLDataType::Boolean => Ok(DataType::Boolean),- SQLDataType::Date => Ok(DataType::Date32(DateUnit::Day)),+ SQLDataType::Date => Ok(DataType::Date32),SQLDataType::Time => Ok(DataType::Time64(TimeUnit::Millisecond)),- SQLDataType::Timestamp => Ok(DataType::Date64(DateUnit::Millisecond)),+ SQLDataType::Timestamp => Ok(DataType::Date64),_ => Err(DataFusionError::NotImplemented(format!("The SQL data type {:?} is not implemented",sql_type@@ -996,7 +996,7 @@ pub fn convert_data_type(sql: &SQLDataType) -> Result<DataType> {SQLDataType::Double => Ok(DataType::Float64),SQLDataType::Char(_) | SQLDataType::Varchar(_) => Ok(DataType::Utf8),SQLDataType::Timestamp => Ok(DataType::Timestamp(TimeUnit::Nanosecond, None)),- SQLDataType::Date => Ok(DataType::Date32(DateUnit::Day)),+ SQLDataType::Date => Ok(DataType::Date32),other => Err(DataFusionError::NotImplemented(format!("Unsupported SQL type {:?}",other@@ -1147,7 +1147,7 @@ mod tests {"SELECT state FROM person WHERE birth_date < CAST ('2020-01-01' as date)";let expected = "Projection: #state\- \n Filter: #birth_date Lt CAST(Utf8(\"2020-01-01\") AS Date32(Day))\+ \n Filter: #birth_date Lt CAST(Utf8(\"2020-01-01\") AS Date32)\\n TableScan: person projection=None";quick_test(sql, expected);@@ -1765,7 +1765,7 @@ mod tests {#[test]fn select_typedstring() {let sql = "SELECT date '2020-12-10' AS date FROM person";- let expected = "Projection: CAST(Utf8(\"2020-12-10\") AS Date32(Day)) AS date\+ let expected = "Projection: CAST(Utf8(\"2020-12-10\") AS Date32) AS date\\n TableScan: person projection=None";quick_test(sql, expected);}