Skip to content

Commit

Permalink
Port create_drop.rs tests to sqllogictest framework
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Dec 7, 2022
1 parent 8547fd8 commit 1ffe19b
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 272 deletions.
272 changes: 0 additions & 272 deletions datafusion/core/tests/sql/create_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,278 +24,6 @@ use tempfile::TempDir;

use super::*;

#[tokio::test]
async fn create_table_as() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_simple_csv(&ctx).await?;

let sql = "CREATE TABLE my_table AS SELECT * FROM aggregate_simple";
ctx.sql(sql).await.unwrap();

let sql_all = "SELECT * FROM my_table order by c1 LIMIT 1";
let results_all = execute_to_batches(&ctx, sql_all).await;

let expected = vec![
"+---------+----------------+------+",
"| c1 | c2 | c3 |",
"+---------+----------------+------+",
"| 0.00001 | 0.000000000001 | true |",
"+---------+----------------+------+",
];

assert_batches_eq!(expected, &results_all);

Ok(())
}

#[tokio::test]
async fn create_table_with_schema_as_select() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_simple_csv(&ctx).await?;

let sql = "CREATE TABLE my_table(c1 float, c2 double, c3 boolean, c4 varchar) \
AS SELECT *,c3 as c4_tmp FROM aggregate_simple";
ctx.sql(sql).await.unwrap();

let sql_all = "SELECT * FROM my_table order by c1 LIMIT 1";
let results_all = execute_to_batches(&ctx, sql_all).await;

let expected = vec![
"+---------+----------------+------+----+",
"| c1 | c2 | c3 | c4 |",
"+---------+----------------+------+----+",
"| 0.00001 | 0.000000000001 | true | 1 |",
"+---------+----------------+------+----+",
];

assert_batches_eq!(expected, &results_all);

Ok(())
}

#[tokio::test]
async fn create_table_with_schema_as_select_mismatch() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_simple_csv(&ctx).await?;

let sql = "CREATE TABLE my_table(c1 float, c2 double, c3 boolean, c4 varchar) \
AS SELECT * FROM aggregate_simple";
let expected_err = ctx.sql(sql).await.unwrap_err();
assert_contains!(
expected_err.to_string(),
"Mismatch: 4 columns specified, but result has 3 columns"
);
Ok(())
}

#[tokio::test]
async fn create_table_with_schema_as_values() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_simple_csv(&ctx).await?;

let sql =
"CREATE TABLE my_table(c1 int, c2 float, c3 varchar) AS VALUES(1, 2, 'hello')";
ctx.sql(sql).await.unwrap();

let sql_all = "SELECT * FROM my_table";
let results_all = execute_to_batches(&ctx, sql_all).await;

let expected = vec![
"+----+----+-------+",
"| c1 | c2 | c3 |",
"+----+----+-------+",
"| 1 | 2 | hello |",
"+----+----+-------+",
];

assert_batches_eq!(expected, &results_all);

Ok(())
}

#[tokio::test]
async fn create_or_replace_table_as() -> Result<()> {
// the information schema used to introduce cyclic Arcs
let ctx =
SessionContext::with_config(SessionConfig::new().with_information_schema(true));

// Create table
let result = ctx
.sql("CREATE TABLE y AS VALUES (1,2),(3,4)")
.await
.unwrap()
.collect()
.await
.unwrap();
assert!(result.is_empty());

// Replace table
ctx.sql("CREATE OR REPLACE TABLE y AS VALUES (5,6)")
.await
.unwrap()
.collect()
.await
.unwrap();

let sql_all = "SELECT * FROM y";
let results_all = execute_to_batches(&ctx, sql_all).await;

let expected = vec![
"+---------+---------+",
"| column1 | column2 |",
"+---------+---------+",
"| 5 | 6 |",
"+---------+---------+",
];

assert_batches_eq!(expected, &results_all);

// 'IF NOT EXISTS' cannot coexist with 'REPLACE'
let result = ctx
.sql("CREATE OR REPLACE TABLE if not exists y AS VALUES (7,8)")
.await;
assert!(
result.is_err(),
"'IF NOT EXISTS' cannot coexist with 'REPLACE'"
);

Ok(())
}

#[tokio::test]
async fn drop_table() -> Result<()> {
let ctx = SessionContext::new();
register_aggregate_simple_csv(&ctx).await?;

let sql = "CREATE TABLE my_table AS SELECT * FROM aggregate_simple";
ctx.sql(sql).await.unwrap();

let sql = "DROP TABLE my_table";
ctx.sql(sql).await.unwrap();

let result = ctx.table("my_table");
assert!(result.is_err(), "drop table should deregister table.");

let sql = "DROP TABLE IF EXISTS my_table";
ctx.sql(sql).await.unwrap();

Ok(())
}

#[tokio::test]
async fn drop_view() -> Result<()> {
let ctx =
SessionContext::with_config(SessionConfig::new().with_information_schema(true));
plan_and_collect(&ctx, "CREATE VIEW v AS SELECT 1").await?;
let rb = plan_and_collect(
&ctx,
"select * from information_schema.tables where table_name = 'v' and table_type = 'VIEW'",
)
.await?;
assert_eq!(rb[0].num_rows(), 1);

plan_and_collect(&ctx, "DROP VIEW v").await?;
let rb = plan_and_collect(
&ctx,
"select * from information_schema.tables where table_name = 'v' and table_type = 'VIEW'",
)
.await?;
assert!(rb.is_empty());
Ok(())
}

#[tokio::test]
#[should_panic(expected = "doesn't exist")]
async fn drop_view_nonexistent() {
let ctx = SessionContext::new();
ctx.sql("DROP VIEW non_existent_view")
.await
.unwrap()
.collect()
.await
.unwrap();
}

#[tokio::test]
#[should_panic(expected = "doesn't exist")]
async fn drop_view_cant_drop_table() {
let ctx = SessionContext::new();
ctx.sql("CREATE TABLE t AS SELECT 1")
.await
.unwrap()
.collect()
.await
.unwrap();
ctx.sql("DROP VIEW t")
.await
.unwrap()
.collect()
.await
.unwrap();
}

#[tokio::test]
#[should_panic(expected = "doesn't exist")]
async fn drop_table_cant_drop_view() {
let ctx = SessionContext::new();
ctx.sql("CREATE VIEW v AS SELECT 1")
.await
.unwrap()
.collect()
.await
.unwrap();
ctx.sql("DROP TABLE v")
.await
.unwrap()
.collect()
.await
.unwrap();
}

#[tokio::test]
async fn drop_view_if_exists() -> Result<()> {
let ctx =
SessionContext::with_config(SessionConfig::new().with_information_schema(true));
plan_and_collect(&ctx, "CREATE VIEW v AS SELECT 1").await?;
let rb = plan_and_collect(&ctx, "DROP VIEW IF EXISTS non_existent_view").await?;
// make sure we get an empty response back
assert!(rb.is_empty());

let rb = plan_and_collect(
&ctx,
"select * from information_schema.views where table_name = 'v'",
)
.await?;
// confirm view exists
assert_eq!(rb[0].num_rows(), 1);

plan_and_collect(&ctx, "DROP VIEW IF EXISTS v").await?;
let rb = plan_and_collect(
&ctx,
"select * from information_schema.views where table_name = 'v'",
)
.await?;
// confirm view is gone
assert!(rb.is_empty());
Ok(())
}

#[tokio::test]
async fn csv_query_create_external_table() {
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
let sql = "SELECT c1, c2, c3, c4, c5, c6, c7, c8, c9, 10, c11, c12, c13 FROM aggregate_test_100 LIMIT 1";
let actual = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+----+----+----+-------+------------+----------------------+----+-------+------------+-----------+-------------+--------------------+--------------------------------+",
"| c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 | c9 | Int64(10) | c11 | c12 | c13 |",
"+----+----+----+-------+------------+----------------------+----+-------+------------+-----------+-------------+--------------------+--------------------------------+",
"| c | 2 | 1 | 18109 | 2033001162 | -6513304855495910254 | 25 | 43062 | 1491205016 | 10 | 0.110830784 | 0.9294097332465232 | 6WfVFBVGJSQb7FhA7E0lBwdvjfZnSW |",
"+----+----+----+-------+------------+----------------------+----+-------+------------+-----------+-------------+--------------------+--------------------------------+",
];
assert_batches_eq!(expected, &actual);
}

#[tokio::test]
async fn create_external_table_with_timestamps() {
let ctx = SessionContext::new();
Expand Down
Loading

0 comments on commit 1ffe19b

Please sign in to comment.