Skip to content

Apache DataFusion SQL Query Engine

Minor: port more create_drop table tests to sqllogictests (#6031)

zhenxing jiangceb23e8cb12d
diff --git a/datafusion/core/tests/sql/create_drop.rs b/datafusion/core/tests/sql/create_drop.rs
index 5bb8abb14a..aa34552044 100644
--- a/datafusion/core/tests/sql/create_drop.rs
+++ b/datafusion/core/tests/sql/create_drop.rs
@@ -21,90 +21,6 @@ use datafusion::test_util::TestTableFactory;
use super::*;
-#[tokio::test]
-async fn sql_create_table_if_not_exists() -> Result<()> {
- // the information schema used to introduce cyclic Arcs
- let ctx =
- SessionContext::with_config(SessionConfig::new().with_information_schema(true));
-
- // Create table
- ctx.sql("CREATE TABLE y AS VALUES (1,2,3)")
- .await?
- .collect()
- .await?;
-
- // Create table again
- let result = ctx
- .sql("CREATE TABLE IF NOT EXISTS y AS VALUES (1,2,3)")
- .await?
- .collect()
- .await?;
-
- assert_eq!(result, Vec::new());
-
- // Create external table
- ctx.sql("CREATE EXTERNAL TABLE aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'")
- .await?
- .collect()
- .await?;
-
- // Create external table again
- let result = ctx.sql("CREATE EXTERNAL TABLE IF NOT EXISTS aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'")
- .await?
- .collect()
- .await?;
-
- assert_eq!(result, Vec::new());
-
- Ok(())
-}
-
-#[tokio::test]
-async fn sql_create_table_exists() -> Result<()> {
- // the information schema used to introduce cyclic Arcs
- let ctx =
- SessionContext::with_config(SessionConfig::new().with_information_schema(true));
-
- // Create table
- ctx.sql("CREATE TABLE y AS VALUES (1,2,3)")
- .await?
- .collect()
- .await?;
-
- // Create table again without if not exist
- let result = ctx.sql("CREATE TABLE y AS VALUES (1,2,3)").await;
-
- match result {
- Err(DataFusionError::Execution(err_msg)) => {
- assert_eq!(err_msg, "Table 'y' already exists");
- }
- _ => {
- panic!("expect create table failed");
- }
- }
-
- // Create external table
- ctx.sql("CREATE EXTERNAL TABLE aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'")
- .await?
- .collect()
- .await?;
-
- // Create external table again without if not exist
- let result = ctx.sql("CREATE EXTERNAL TABLE aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'")
- .await;
-
- match result {
- Err(DataFusionError::Execution(err_msg)) => {
- assert_eq!(err_msg, "Table 'aggregate_simple' already exists");
- }
- _ => {
- panic!("expect create table failed");
- }
- }
-
- Ok(())
-}
-
#[tokio::test]
async fn create_custom_table() -> Result<()> {
let cfg = RuntimeConfig::new();
@@ -157,46 +73,3 @@ async fn create_external_table_with_ddl() -> Result<()> {
Ok(())
}
-
-#[tokio::test]
-async fn create_bad_custom_table() {
- let ctx = SessionContext::new();
-
- let sql = "CREATE EXTERNAL TABLE dt STORED AS DELTATABLE LOCATION 's3://bucket/schema/table';";
- let res = ctx.sql(sql).await;
- match res {
- Ok(_) => panic!("Registration of tables without factories should fail"),
- Err(e) => {
- assert!(
- e.to_string().contains("Unable to find factory for"),
- "Registration of tables without factories should throw correct error"
- )
- }
- }
-}
-
-#[tokio::test]
-async fn create_csv_table_empty_file() -> Result<()> {
- let ctx =
- SessionContext::with_config(SessionConfig::new().with_information_schema(true));
-
- let sql = "CREATE EXTERNAL TABLE empty STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/empty.csv'";
- ctx.sql(sql).await.unwrap();
- let sql =
- "select column_name, data_type, ordinal_position from information_schema.columns";
- let results = execute_to_batches(&ctx, sql).await;
-
- let expected = vec![
- "+-------------+-----------+------------------+",
- "| column_name | data_type | ordinal_position |",
- "+-------------+-----------+------------------+",
- "| c1 | Utf8 | 0 |",
- "| c2 | Utf8 | 1 |",
- "| c3 | Utf8 | 2 |",
- "+-------------+-----------+------------------+",
- ];
-
- assert_batches_eq!(expected, &results);
-
- Ok(())
-}
diff --git a/datafusion/core/tests/sqllogictests/test_files/ddl.slt b/datafusion/core/tests/sqllogictests/test_files/ddl.slt
index addc20af99..49ec168d70 100644
--- a/datafusion/core/tests/sqllogictests/test_files/ddl.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/ddl.slt
@@ -485,6 +485,20 @@ CREATE TABLE table_without_values(field1 BIGINT NULL, field2 BIGINT NULL);
statement ok
CREATE TABLE IF NOT EXISTS table_without_values(field1 BIGINT, field2 BIGINT);
+statement ok
+CREATE EXTERNAL TABLE aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'
+
+# Should not recreate the same EXTERNAL table
+statement error Execution error: Table 'aggregate_simple' already exists
+CREATE EXTERNAL TABLE aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'
+
+statement ok
+CREATE EXTERNAL TABLE IF NOT EXISTS aggregate_simple STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/aggregate_simple.csv'
+
+# create bad custom table
+statement error DataFusion error: Execution error: Unable to find factory for DELTATABLE
+CREATE EXTERNAL TABLE dt STORED AS DELTATABLE LOCATION 's3://bucket/schema/table';
+
# Should not recreate the same table
statement error Table 'table_without_values' already exists
@@ -606,6 +620,17 @@ NULL
statement ok
drop table foo;
+# craete csv table with empty csv file
+statement ok
+CREATE EXTERNAL TABLE empty STORED AS CSV WITH HEADER ROW LOCATION 'tests/data/empty.csv';
+
+query TTI
+select column_name, data_type, ordinal_position from information_schema.columns where table_name='empty';;
+----
+c1 Utf8 0
+c2 Utf8 1
+c3 Utf8 2
+
## should allow any type of exprs as values
statement ok