Apache DataFusion SQL Query Engine
ARROW-6487: [Rust] [DataFusion] Introduce common test module
Andy Grove265a336c9790
diff --git a/rust/datafusion/src/execution/aggregate.rs b/rust/datafusion/src/execution/aggregate.rsindex fc1223c4a7..5fd887451d 100644--- a/rust/datafusion/src/execution/aggregate.rs+++ b/rust/datafusion/src/execution/aggregate.rs@@ -1196,14 +1196,14 @@ mod tests {use crate::execution::expression;use crate::execution::relation::DataSourceRelation;use crate::logicalplan::Expr;+ use crate::test;use arrow::datatypes::{DataType, Field, Schema};- use std::env;use std::sync::Mutex;#[test]fn min_f64_group_by_string() {- let schema = aggr_test_schema();- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");+ let schema = test::aggr_test_schema();+ let testdata = test::arrow_testdata_path();let relation =load_csv(&format!("{}/csv/aggregate_test_100.csv", testdata), &schema);let context = ExecutionContext::new();@@ -1239,8 +1239,8 @@ mod tests {#[test]fn count() {- let schema = aggr_test_schema();- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");+ let schema = test::aggr_test_schema();+ let testdata = test::arrow_testdata_path();let relation =load_csv(&format!("{}/csv/aggregate_test_100.csv", testdata), &schema);let context = ExecutionContext::new();@@ -1276,8 +1276,8 @@ mod tests {#[test]fn max_f64_group_by_string() {- let schema = aggr_test_schema();- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");+ let schema = test::aggr_test_schema();+ let testdata = test::arrow_testdata_path();let relation =load_csv(&format!("{}/csv/aggregate_test_100.csv", testdata), &schema);let context = ExecutionContext::new();@@ -1313,8 +1313,8 @@ mod tests {#[test]fn test_min_max_sum_count_avg_f64_group_by_uint32() {- let schema = aggr_test_schema();- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");+ let schema = test::aggr_test_schema();+ let testdata = test::arrow_testdata_path();let relation =load_csv(&format!("{}/csv/aggregate_test_100.csv", testdata), &schema);@@ -1464,24 +1464,6 @@ mod tests {assert_eq!(0.5108939802619781, avg.value(4));}- fn aggr_test_schema() -> Arc<Schema> {- Arc::new(Schema::new(vec![- Field::new("c1", DataType::Utf8, false),- Field::new("c2", DataType::UInt32, false),- Field::new("c3", DataType::Int8, false),- Field::new("c3", DataType::Int16, false),- Field::new("c4", DataType::Int32, false),- Field::new("c5", DataType::Int64, false),- Field::new("c6", DataType::UInt8, false),- Field::new("c7", DataType::UInt16, false),- Field::new("c8", DataType::UInt32, false),- Field::new("c9", DataType::UInt64, false),- Field::new("c10", DataType::Float32, false),- Field::new("c11", DataType::Float64, false),- Field::new("c12", DataType::Utf8, false),- ]))- }-fn load_csv(filename: &str, schema: &Arc<Schema>) -> Rc<RefCell<dyn Relation>> {let ds = CsvBatchIterator::new(filename, schema.clone(), true, &None, 10);Rc::new(RefCell::new(DataSourceRelation::new(Arc::new(Mutex::new(diff --git a/rust/datafusion/src/execution/physical_plan/projection.rs b/rust/datafusion/src/execution/physical_plan/projection.rsindex 563fc348b9..be88977ad6 100644--- a/rust/datafusion/src/execution/physical_plan/projection.rs+++ b/rust/datafusion/src/execution/physical_plan/projection.rs@@ -141,13 +141,8 @@ mod tests {use super::*;use crate::execution::physical_plan::csv::CsvExec;use crate::execution::physical_plan::expressions::Column;+ use crate::test;use arrow::datatypes::{DataType, Field, Schema};- use std::env;- use std::fs;- use std::fs::File;- use std::io::prelude::*;- use std::io::{BufReader, BufWriter};- use std::path::Path;#[test]fn project_first_column() -> Result<()> {@@ -168,7 +163,7 @@ mod tests {]));let partitions = 4;- let path = create_partitioned_csv("aggregate_test_100.csv", partitions)?;+ let path = test::create_partitioned_csv("aggregate_test_100.csv", partitions)?;let csv = CsvExec::try_new(&path, schema, true, None, 1024)?;@@ -192,52 +187,4 @@ mod tests {Ok(())}- /// Generated partitioned copy of a CSV file- fn create_partitioned_csv(filename: &str, partitions: usize) -> Result<String> {- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");- let path = format!("{}/csv/{}", testdata, filename);-- let mut dir = env::temp_dir();- dir.push(&format!("{}-{}", filename, partitions));-- if Path::new(&dir).exists() {- fs::remove_dir_all(&dir).unwrap();- }- fs::create_dir(dir.clone()).unwrap();-- let mut writers = vec![];- for i in 0..partitions {- let mut filename = dir.clone();- filename.push(format!("part{}.csv", i));- let writer = BufWriter::new(File::create(&filename).unwrap());- writers.push(writer);- }-- let f = File::open(&path)?;- let f = BufReader::new(f);- let mut i = 0;- for line in f.lines() {- let line = line.unwrap();-- if i == 0 {- // write header to all partitions- for w in writers.iter_mut() {- w.write(line.as_bytes()).unwrap();- w.write(b"\n").unwrap();- }- } else {- // write data line to single partition- let partition = i % partitions;- writers[partition].write(line.as_bytes()).unwrap();- writers[partition].write(b"\n").unwrap();- }-- i += 1;- }- for w in writers.iter_mut() {- w.flush().unwrap();- }-- Ok(dir.as_os_str().to_str().unwrap().to_string())- }}diff --git a/rust/datafusion/src/execution/projection.rs b/rust/datafusion/src/execution/projection.rsindex 80510a1b98..7048683482 100644--- a/rust/datafusion/src/execution/projection.rs+++ b/rust/datafusion/src/execution/projection.rs@@ -92,8 +92,8 @@ mod tests {use crate::execution::expression;use crate::execution::relation::DataSourceRelation;use crate::logicalplan::Expr;+ use crate::test;use arrow::datatypes::{DataType, Field, Schema};- use std::env;use std::sync::Mutex;#[test]@@ -114,7 +114,7 @@ mod tests {Field::new("c12", DataType::Utf8, false),]));- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");+ let testdata = test::arrow_testdata_path();let ds = CsvBatchIterator::new(&format!("{}/csv/aggregate_test_100.csv", testdata),diff --git a/rust/datafusion/src/execution/table_impl.rs b/rust/datafusion/src/execution/table_impl.rsindex 6b7c5bd30c..cb2cb1aa0a 100644--- a/rust/datafusion/src/execution/table_impl.rs+++ b/rust/datafusion/src/execution/table_impl.rs@@ -183,7 +183,7 @@ impl TableImpl {mod tests {use super::*;use crate::execution::context::ExecutionContext;- use std::env;+ use crate::test;#[test]fn column_index() {@@ -306,8 +306,8 @@ mod tests {}fn register_aggregate_csv(ctx: &mut ExecutionContext) {- let schema = aggr_test_schema();- let testdata = env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined");+ let schema = test::aggr_test_schema();+ let testdata = test::arrow_testdata_path();ctx.register_csv("aggregate_test_100",&format!("{}/csv/aggregate_test_100.csv", testdata),@@ -316,22 +316,4 @@ mod tests {);}- fn aggr_test_schema() -> Arc<Schema> {- Arc::new(Schema::new(vec![- Field::new("c1", DataType::Utf8, false),- Field::new("c2", DataType::UInt32, false),- Field::new("c3", DataType::Int8, false),- Field::new("c4", DataType::Int16, false),- Field::new("c5", DataType::Int32, false),- Field::new("c6", DataType::Int64, false),- Field::new("c7", DataType::UInt8, false),- Field::new("c8", DataType::UInt16, false),- Field::new("c9", DataType::UInt32, false),- Field::new("c10", DataType::UInt64, false),- Field::new("c11", DataType::Float32, false),- Field::new("c12", DataType::Float64, false),- Field::new("c13", DataType::Utf8, false),- ]))- }-}diff --git a/rust/datafusion/src/lib.rs b/rust/datafusion/src/lib.rsindex 5f4d5ce388..f6bbdb2ab8 100644--- a/rust/datafusion/src/lib.rs+++ b/rust/datafusion/src/lib.rs@@ -30,3 +30,6 @@ pub mod logicalplan;pub mod optimizer;pub mod sql;pub mod table;++#[cfg(test)]+pub mod test;diff --git a/rust/datafusion/src/test/mod.rs b/rust/datafusion/src/test/mod.rsnew file mode 100644index 0000000000..8fabb18339--- /dev/null+++ b/rust/datafusion/src/test/mod.rs@@ -0,0 +1,101 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++//! Common unit test utility methods++use crate::error::Result;+use arrow::datatypes::{DataType, Field, Schema};+use std::env;+use std::fs;+use std::fs::File;+use std::io::prelude::*;+use std::io::{BufReader, BufWriter};+use std::path::Path;+use std::sync::Arc;++/// Get the value of the ARROW_TEST_DATA environment variable+pub fn arrow_testdata_path() -> String {+ env::var("ARROW_TEST_DATA").expect("ARROW_TEST_DATA not defined")+}++/// Generated partitioned copy of a CSV file+pub fn create_partitioned_csv(filename: &str, partitions: usize) -> Result<String> {+ let testdata = arrow_testdata_path();+ let path = format!("{}/csv/{}", testdata, filename);++ let mut dir = env::temp_dir();+ dir.push(&format!("{}-{}", filename, partitions));++ if Path::new(&dir).exists() {+ fs::remove_dir_all(&dir).unwrap();+ }+ fs::create_dir(dir.clone()).unwrap();++ let mut writers = vec![];+ for i in 0..partitions {+ let mut filename = dir.clone();+ filename.push(format!("part{}.csv", i));+ let writer = BufWriter::new(File::create(&filename).unwrap());+ writers.push(writer);+ }++ let f = File::open(&path)?;+ let f = BufReader::new(f);+ let mut i = 0;+ for line in f.lines() {+ let line = line.unwrap();++ if i == 0 {+ // write header to all partitions+ for w in writers.iter_mut() {+ w.write(line.as_bytes()).unwrap();+ w.write(b"\n").unwrap();+ }+ } else {+ // write data line to single partition+ let partition = i % partitions;+ writers[partition].write(line.as_bytes()).unwrap();+ writers[partition].write(b"\n").unwrap();+ }++ i += 1;+ }+ for w in writers.iter_mut() {+ w.flush().unwrap();+ }++ Ok(dir.as_os_str().to_str().unwrap().to_string())+}++/// Get the schema for the aggregate_test_* csv files+pub fn aggr_test_schema() -> Arc<Schema> {+ Arc::new(Schema::new(vec![+ Field::new("c1", DataType::Utf8, false),+ Field::new("c2", DataType::UInt32, false),+ Field::new("c3", DataType::Int8, false),+ Field::new("c4", DataType::Int16, false),+ Field::new("c5", DataType::Int32, false),+ Field::new("c6", DataType::Int64, false),+ Field::new("c7", DataType::UInt8, false),+ Field::new("c8", DataType::UInt16, false),+ Field::new("c9", DataType::UInt32, false),+ Field::new("c10", DataType::UInt64, false),+ Field::new("c11", DataType::Float32, false),+ Field::new("c12", DataType::Float64, false),+ Field::new("c13", DataType::Utf8, false),+ ]))+}