Skip to content

Commit

Permalink
Minor: Move test code from context.rs into sql_integration (#2473)
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb authored May 6, 2022
1 parent 940fbf0 commit 32cf354
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 99 deletions.
100 changes: 2 additions & 98 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,14 +1603,13 @@ impl FunctionRegistry for TaskContext {
mod tests {
use super::*;
use crate::execution::context::QueryPlanner;
use crate::logical_plan::{binary_expr, lit, Operator};
use crate::physical_plan::functions::make_scalar_function;
use crate::test;
use crate::test_util::parquet_test_data;
use crate::variable::VarType;
use crate::{
assert_batches_eq, assert_batches_sorted_eq,
logical_plan::{col, create_udf, sum, Expr},
assert_batches_eq,
logical_plan::{create_udf, Expr},
};
use crate::{logical_plan::create_udaf, physical_plan::expressions::AvgAccumulator};
use arrow::array::ArrayRef;
Expand Down Expand Up @@ -1701,56 +1700,6 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn unprojected_filter() {
let ctx = SessionContext::new();
let df = ctx
.read_table(test::table_with_sequence(1, 3).unwrap())
.unwrap();

let df = df
.select(vec![binary_expr(col("i"), Operator::Plus, col("i"))])
.unwrap()
.filter(col("i").gt(lit(2)))
.unwrap();
let results = df.collect().await.unwrap();

let expected = vec![
"+--------------------------+",
"| ?table?.i Plus ?table?.i |",
"+--------------------------+",
"| 6 |",
"+--------------------------+",
];
assert_batches_sorted_eq!(expected, &results);
}

#[tokio::test]
async fn aggregate_with_alias() -> Result<()> {
let tmp_dir = TempDir::new()?;
let ctx = create_ctx(&tmp_dir, 1).await?;

let schema = Arc::new(Schema::new(vec![
Field::new("c1", DataType::Utf8, false),
Field::new("c2", DataType::UInt32, false),
]));

let plan = LogicalPlanBuilder::scan_empty(None, schema.as_ref(), None)?
.aggregate(vec![col("c1")], vec![sum(col("c2"))])?
.project(vec![col("c1"), sum(col("c2")).alias("total_salary")])?
.build()?;

let plan = ctx.optimize(&plan)?;

let physical_plan = ctx.create_physical_plan(&Arc::new(plan)).await?;
assert_eq!("c1", physical_plan.schema().field(0).name().as_str());
assert_eq!(
"total_salary",
physical_plan.schema().field(1).name().as_str()
);
Ok(())
}

#[tokio::test]
async fn case_sensitive_identifiers_user_defined_functions() -> Result<()> {
let mut ctx = SessionContext::new();
Expand Down Expand Up @@ -2097,51 +2046,6 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn union_test() -> Result<()> {
let ctx = SessionContext::with_config(
SessionConfig::new().with_information_schema(true),
);

let result = ctx
.sql("SELECT 1 A UNION ALL SELECT 2")
.await
.unwrap()
.collect()
.await
.unwrap();

#[rustfmt::skip]
let expected = vec![
"+---+",
"| a |",
"+---+",
"| 1 |",
"| 2 |",
"+---+"
];
assert_batches_eq!(expected, &result);

let result = ctx
.sql("SELECT 1 UNION SELECT 2")
.await
.unwrap()
.collect()
.await
.unwrap();

let expected = vec![
"+----------+",
"| Int64(1) |",
"+----------+",
"| 1 |",
"| 2 |",
"+----------+",
];
assert_batches_eq!(expected, &result);
Ok(())
}

#[tokio::test]
async fn read_with_glob_path() -> Result<()> {
let ctx = SessionContext::new();
Expand Down
27 changes: 26 additions & 1 deletion datafusion/core/tests/sql/aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

use super::*;
use datafusion::scalar::ScalarValue;
use datafusion::{logical_plan::LogicalPlanBuilder, scalar::ScalarValue};

#[tokio::test]
async fn csv_query_avg_multi_batch() -> Result<()> {
Expand Down Expand Up @@ -1447,3 +1447,28 @@ async fn count_distinct_integers_aggregated_multiple_partitions() -> Result<()>

Ok(())
}

#[tokio::test]
async fn aggregate_with_alias() -> Result<()> {
let ctx = SessionContext::new();

let schema = Arc::new(Schema::new(vec![
Field::new("c1", DataType::Utf8, false),
Field::new("c2", DataType::UInt32, false),
]));

let plan = LogicalPlanBuilder::scan_empty(None, schema.as_ref(), None)?
.aggregate(vec![col("c1")], vec![sum(col("c2"))])?
.project(vec![col("c1"), sum(col("c2")).alias("total_salary")])?
.build()?;

let plan = ctx.optimize(&plan)?;

let physical_plan = ctx.create_physical_plan(&Arc::new(plan)).await?;
assert_eq!("c1", physical_plan.schema().field(0).name().as_str());
assert_eq!(
"total_salary",
physical_plan.schema().field(1).name().as_str()
);
Ok(())
}
22 changes: 22 additions & 0 deletions datafusion/core/tests/sql/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,3 +1188,25 @@ async fn boolean_literal() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn unprojected_filter() {
let ctx = SessionContext::new();
let df = ctx.read_table(table_with_sequence(1, 3).unwrap()).unwrap();

let df = df
.select(vec![col("i") + col("i")])
.unwrap()
.filter(col("i").gt(lit(2)))
.unwrap();
let results = df.collect().await.unwrap();

let expected = vec![
"+--------------------------+",
"| ?table?.i Plus ?table?.i |",
"+--------------------------+",
"| 6 |",
"+--------------------------+",
];
assert_batches_sorted_eq!(expected, &results);
}
44 changes: 44 additions & 0 deletions datafusion/core/tests/sql/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,47 @@ async fn union_all_with_aggregate() -> Result<()> {
assert_batches_eq!(expected, &actual);
Ok(())
}

#[tokio::test]
async fn union_schemas() -> Result<()> {
let ctx =
SessionContext::with_config(SessionConfig::new().with_information_schema(true));

let result = ctx
.sql("SELECT 1 A UNION ALL SELECT 2")
.await
.unwrap()
.collect()
.await
.unwrap();

#[rustfmt::skip]
let expected = vec![
"+---+",
"| a |",
"+---+",
"| 1 |",
"| 2 |",
"+---+"
];
assert_batches_eq!(expected, &result);

let result = ctx
.sql("SELECT 1 UNION SELECT 2")
.await
.unwrap()
.collect()
.await
.unwrap();

let expected = vec![
"+----------+",
"| Int64(1) |",
"+----------+",
"| 1 |",
"| 2 |",
"+----------+",
];
assert_batches_eq!(expected, &result);
Ok(())
}

0 comments on commit 32cf354

Please sign in to comment.