Skip to content

Apache DataFusion SQL Query Engine

ARROW-12278: [Rust][DataFusion] Use Timestamp(Nanosecond, None) for SQL TIMESTAMP Type

Andrew Lamb78b9c0cd05f0
diff --git a/rust/datafusion/src/execution/context.rs b/rust/datafusion/src/execution/context.rs
index aabe587a0d..ce0ea6d005 100644
--- a/rust/datafusion/src/execution/context.rs
+++ b/rust/datafusion/src/execution/context.rs
@@ -2837,6 +2837,52 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn create_external_table_with_timestamps() {
+ let mut ctx = ExecutionContext::new();
+
+ let data = "Jorge,2018-12-13T12:12:10.011\n\
+ Andrew,2018-11-13T17:11:10.011";
+
+ let tmp_dir = TempDir::new().unwrap();
+ let file_path = tmp_dir.path().join("timestamps.csv");
+
+ // scope to ensure the file is closed and written
+ {
+ File::create(&file_path)
+ .expect("creating temp file")
+ .write_all(data.as_bytes())
+ .expect("writing data");
+ }
+
+ let sql = format!(
+ "CREATE EXTERNAL TABLE csv_with_timestamps (
+ name VARCHAR,
+ ts TIMESTAMP
+ )
+ STORED AS CSV
+ LOCATION '{}'
+ ",
+ file_path.to_str().expect("path is utf8")
+ );
+
+ plan_and_collect(&mut ctx, &sql)
+ .await
+ .expect("Executing CREATE EXTERNAL TABLE");
+
+ let sql = "SELECT * from csv_with_timestamps";
+ let result = plan_and_collect(&mut ctx, &sql).await.unwrap();
+ let expected = vec![
+ "+--------+-------------------------+",
+ "| name | ts |",
+ "+--------+-------------------------+",
+ "| Andrew | 2018-11-13 17:11:10.011 |",
+ "| Jorge | 2018-12-13 12:12:10.011 |",
+ "+--------+-------------------------+",
+ ];
+ assert_batches_sorted_eq!(expected, &result);
+ }
+
struct MyPhysicalPlanner {}
impl PhysicalPlanner for MyPhysicalPlanner {
@@ -2869,10 +2915,7 @@ mod tests {
ctx: &mut ExecutionContext,
sql: &str,
) -> Result<Vec<RecordBatch>> {
- let logical_plan = ctx.create_logical_plan(sql)?;
- let logical_plan = ctx.optimize(&logical_plan)?;
- let physical_plan = ctx.create_physical_plan(&logical_plan)?;
- collect(physical_plan).await
+ ctx.sql(sql)?.collect().await
}
/// Execute SQL and return results
diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rs
index f3ea7c9e34..f3cba232a2 100644
--- a/rust/datafusion/src/sql/planner.rs
+++ b/rust/datafusion/src/sql/planner.rs
@@ -298,7 +298,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
SQLDataType::Boolean => Ok(DataType::Boolean),
SQLDataType::Date => Ok(DataType::Date32),
SQLDataType::Time => Ok(DataType::Time64(TimeUnit::Millisecond)),
- SQLDataType::Timestamp => Ok(DataType::Date64),
+ SQLDataType::Timestamp => Ok(DataType::Timestamp(TimeUnit::Nanosecond, None)),
_ => Err(DataFusionError::NotImplemented(format!(
"The SQL data type {:?} is not implemented",
sql_type