From c2f24b79dc3602388a696ffd0d269edbafcdf048 Mon Sep 17 00:00:00 2001 From: jiangzhx Date: Mon, 17 Apr 2023 18:38:53 +0800 Subject: [PATCH 1/2] Minor: port more create_drop table tests to sqllogictests --- datafusion/core/tests/sql/create_drop.rs | 127 ------------------ .../tests/sqllogictests/test_files/ddl.slt | 27 +++- 2 files changed, 26 insertions(+), 128 deletions(-) diff --git a/datafusion/core/tests/sql/create_drop.rs b/datafusion/core/tests/sql/create_drop.rs index 5bb8abb14a01..aa34552044d4 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 d21d85267a04..cf00e6f6a530 100644 --- a/datafusion/core/tests/sqllogictests/test_files/ddl.slt +++ b/datafusion/core/tests/sqllogictests/test_files/ddl.slt @@ -448,6 +448,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 @@ -569,6 +583,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 @@ -580,4 +605,4 @@ select * from t; 0 years 0 mons 5 days 0 hours 0 mins 0.000000003 secs 1 statement ok -drop table t; +drop table t; \ No newline at end of file From 6056ce67585b5d722e30f7a1326e09ab875b5daf Mon Sep 17 00:00:00 2001 From: zhenxing jiang Date: Mon, 17 Apr 2023 22:14:13 +0800 Subject: [PATCH 2/2] Update datafusion/core/tests/sqllogictests/test_files/ddl.slt Co-authored-by: Ruihang Xia --- datafusion/core/tests/sqllogictests/test_files/ddl.slt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datafusion/core/tests/sqllogictests/test_files/ddl.slt b/datafusion/core/tests/sqllogictests/test_files/ddl.slt index cf00e6f6a530..2f4dc7f95788 100644 --- a/datafusion/core/tests/sqllogictests/test_files/ddl.slt +++ b/datafusion/core/tests/sqllogictests/test_files/ddl.slt @@ -605,4 +605,4 @@ select * from t; 0 years 0 mons 5 days 0 hours 0 mins 0.000000003 secs 1 statement ok -drop table t; \ No newline at end of file +drop table t;