Skip to content

Commit

Permalink
Move identifer case tests to sql_integ, add negative cases, Debug f…
Browse files Browse the repository at this point in the history
…or `DataFrame` (#2243)

* Move normalized_column_identier test to sql_integ

* Add test for invalid identifiers

* Add Minimal Debug impls for DataFrame

* RAT
  • Loading branch information
alamb authored Apr 16, 2022
1 parent 9f2ed42 commit f5592cf
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 168 deletions.
1 change: 1 addition & 0 deletions datafusion/core/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use std::any::Any;
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct DataFrame {
session_state: Arc<RwLock<SessionState>>,
plan: LogicalPlan,
Expand Down
181 changes: 13 additions & 168 deletions datafusion/core/src/execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ use crate::{
};
use log::{debug, trace};
use parking_lot::RwLock;
use std::collections::{HashMap, HashSet};
use std::string::String;
use std::sync::Arc;
use std::{
collections::{HashMap, HashSet},
fmt::Debug,
};

use arrow::datatypes::{DataType, SchemaRef};

Expand Down Expand Up @@ -1148,6 +1151,15 @@ pub struct SessionState {
pub runtime_env: Arc<RuntimeEnv>,
}

impl Debug for SessionState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SessionState")
.field("session_id", &self.session_id)
// TODO should we print out more?
.finish()
}
}

/// Default session builder using the provided configuration
pub fn default_session_builder(config: SessionConfig) -> SessionState {
SessionState::with_config_rt(
Expand Down Expand Up @@ -3322,173 +3334,6 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn normalized_column_identifiers() {
// create local execution context
let ctx = SessionContext::new();

// register csv file with the execution context
ctx.register_csv(
"case_insensitive_test",
"tests/example.csv",
CsvReadOptions::new(),
)
.await
.unwrap();

let sql = "SELECT A, b FROM case_insensitive_test";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| a | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

let sql = "SELECT t.A, b FROM case_insensitive_test AS t";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| a | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

// Aliases

let sql = "SELECT t.A as x, b FROM case_insensitive_test AS t";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| x | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

let sql = "SELECT t.A AS X, b FROM case_insensitive_test AS t";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| x | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

let sql = r#"SELECT t.A AS "X", b FROM case_insensitive_test AS t"#;
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| X | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

// Order by

let sql = "SELECT t.A AS x, b FROM case_insensitive_test AS t ORDER BY x";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| x | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

let sql = "SELECT t.A AS x, b FROM case_insensitive_test AS t ORDER BY X";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| x | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

let sql = r#"SELECT t.A AS "X", b FROM case_insensitive_test AS t ORDER BY "X""#;
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| X | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

// Where

let sql = "SELECT a, b FROM case_insensitive_test where A IS NOT null";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| a | b |",
"+---+---+",
"| 1 | 2 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

// Group by

let sql = "SELECT a as x, count(*) as c FROM case_insensitive_test GROUP BY X";
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| x | c |",
"+---+---+",
"| 1 | 1 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);

let sql =
r#"SELECT a as "X", count(*) as c FROM case_insensitive_test GROUP BY "X""#;
let result = plan_and_collect(&ctx, sql)
.await
.expect("ran plan correctly");
let expected = vec![
"+---+---+",
"| X | c |",
"+---+---+",
"| 1 | 1 |",
"+---+---+",
];
assert_batches_sorted_eq!(expected, &result);
}

struct MyPhysicalPlanner {}

#[async_trait]
Expand Down
Loading

0 comments on commit f5592cf

Please sign in to comment.