Skip to content

Commit

Permalink
Update more
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Dec 6, 2022
1 parent 02bdc82 commit 324557d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 186 deletions.
184 changes: 0 additions & 184 deletions datafusion/core/tests/sql/create_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,190 +24,6 @@ use tempfile::TempDir;

use super::*;


#[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
97 changes: 95 additions & 2 deletions datafusion/core/tests/sqllogictests/test_files/ddl.slt
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,22 @@ DROP VIEW foo, bar;
statement ok
DROP VIEW foo;

# Ok to drop
statement ok
DROP VIEW bar;

# not ok to drop after already dropped
statement error
DROP VIEW bar;

# ok to drop with IF EXISTS after already dropped
statement ok
DROP VIEW IF EXISTS bar;

# can't drop non existent view
statement error
DROP VIEW non_existent_view

##########
# Create / drop views agaist tables
##########
Expand All @@ -176,6 +189,18 @@ SELECT * FROM my_table order by c1 LIMIT 1
statement ok
DROP TABLE my_table;

# not ok to drop after already dropped
statement error
DROP TABLE my_table;

# ok to drop after already dropped with IF EXISTS
statement ok
DROP TABLE IF EXISTS my_table;

# Can't drop non existent table
statement error
DROP TABLE non_existent_table;


# create_table_with_schema_as_select
statement ok
Expand All @@ -190,12 +215,12 @@ statement ok
DROP TABLE my_table;


# create_table_with_schema_as_select_mismatch()
# create_table_with_schema_as_select_mismatch
statement error
CREATE TABLE my_table(c1 float, c2 double, c3 boolean, c4 varchar) AS SELECT * FROM aggregate_simple;


# create_table_with_schema_as_values()
# create_table_with_schema_as_values
statement ok
CREATE TABLE my_table(c1 int, c2 float, c3 varchar) AS VALUES(1, 2, 'hello')

Expand All @@ -206,3 +231,71 @@ SELECT * FROM my_table;

statement ok
DROP TABLE my_table;


# TODO enable information schema

statement ok
CREATE TABLE y AS VALUES (1,2),(3,4);

statement ok
CREATE OR REPLACE TABLE y AS VALUES (5,6);

query II
SELECT * FROM y
----
5 6

# 'IF NOT EXISTS' cannot coexist with 'REPLACE'
statement error
CREATE OR REPLACE TABLE if not exists y AS VALUES (7,8);




# drop_view_cant_drop_table
statement ok
CREATE TABLE t AS SELECT 1

statement error
DROP VIEW t

statement ok
DROP TABLE t

# drop_table_cant_drop_view
statement ok
CREATE VIEW v AS SELECT 1;

statement error
DROP TABLE v;

statement ok
DROP VIEW v;


# csv_query_create_external_table
statement ok
CREATE EXTERNAL TABLE aggregate_test_100 (
c1 VARCHAR NOT NULL,
c2 TINYINT NOT NULL,
c3 SMALLINT NOT NULL,
c4 SMALLINT NOT NULL,
c5 INTEGER NOT NULL,
c6 BIGINT NOT NULL,
c7 SMALLINT NOT NULL,
c8 INT NOT NULL,
c9 INT UNSIGNED NOT NULL,
c10 BIGINT UNSIGNED NOT NULL,
c11 FLOAT NOT NULL,
c12 DOUBLE NOT NULL,
c13 VARCHAR NOT NULL
)
STORED AS CSV
WITH HEADER ROW
LOCATION '../../testing/data/csv/aggregate_test_100.csv';

query IIIIIIIIIIIII
SELECT c1, c2, c3, c4, c5, c6, c7, c8, c9, 10, c11, c12, c13 FROM aggregate_test_100 LIMIT 1;
----
c 2 1 18109 2033001162 -6513304855495910254 25 43062 1491205016 10 0.110830784 0.9294097332465232 6WfVFBVGJSQb7FhA7E0lBwdvjfZnSW

0 comments on commit 324557d

Please sign in to comment.