Apache DataFusion SQL Query Engine
Port tests in set_variable.rs to sqllogictest (#6255)
parkma99684289c8880e
diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rsindex cbccf333b3..cda01903c3 100644--- a/datafusion/core/tests/sql/mod.rs+++ b/datafusion/core/tests/sql/mod.rs@@ -104,7 +104,6 @@ pub mod explain;pub mod information_schema;pub mod parquet_schema;pub mod partitioned_csv;-pub mod set_variable;pub mod subqueries;#[cfg(feature = "unicode_expressions")]pub mod unicode;diff --git a/datafusion/core/tests/sql/set_variable.rs b/datafusion/core/tests/sql/set_variable.rsdeleted file mode 100644index b89264ebba..0000000000--- a/datafusion/core/tests/sql/set_variable.rs+++ /dev/null@@ -1,476 +0,0 @@-// Licensed to the Apache Software Foundation (ASF) under one-// or more contributor license agreements. See the NOTICE file-// distributed with this work for additional information-// regarding copyright ownership. The ASF licenses this file-// to you under the Apache License, Version 2.0 (the-// "License"); you may not use this file except in compliance-// with the License. You may obtain a copy of the License at-//-// http://www.apache.org/licenses/LICENSE-2.0-//-// Unless required by applicable law or agreed to in writing,-// software distributed under the License is distributed on an-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY-// KIND, either express or implied. See the License for the-// specific language governing permissions and limitations-// under the License.--use super::*;-use arrow::util::pretty::pretty_format_batches;--#[tokio::test]-async fn set_variable_to_value() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- ctx.sql("SET datafusion.execution.batch_size to 1")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 1 |",- "+---------------------------------+---------+",- ];- assert_batches_sorted_eq!(expected, &result);-}--#[tokio::test]-async fn set_variable_to_value_with_equal_sign() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- ctx.sql("SET datafusion.execution.batch_size = 1")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 1 |",- "+---------------------------------+---------+",- ];- assert_batches_sorted_eq!(expected, &result);-}--#[tokio::test]-async fn set_variable_to_value_with_single_quoted_string() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- ctx.sql("SET datafusion.execution.batch_size to '1'")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 1 |",- "+---------------------------------+---------+",- ];- assert_batches_sorted_eq!(expected, &result);-}--#[tokio::test]-async fn set_variable_to_value_case_insensitive() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- ctx.sql("SET datafusion.EXECUTION.batch_size to '1'")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 1 |",- "+---------------------------------+---------+",- ];- assert_batches_sorted_eq!(expected, &result);-}--#[tokio::test]-async fn set_variable_unknown_variable() {- let ctx = SessionContext::new();-- let err = plan_and_collect(&ctx, "SET aabbcc to '1'")- .await- .unwrap_err();- assert_eq!(- err.to_string(),- "External error: could not find config namespace for key \"aabbcc\""- );-}--#[tokio::test]-async fn set_bool_variable() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- ctx.sql("SET datafusion.execution.coalesce_batches to true")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.coalesce_batches")- .await- .unwrap();- let expected = vec![- "+---------------------------------------+---------+",- "| name | setting |",- "+---------------------------------------+---------+",- "| datafusion.execution.coalesce_batches | true |",- "+---------------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-- ctx.sql("SET datafusion.execution.coalesce_batches to 'false'")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.coalesce_batches")- .await- .unwrap();- let expected = vec![- "+---------------------------------------+---------+",- "| name | setting |",- "+---------------------------------------+---------+",- "| datafusion.execution.coalesce_batches | false |",- "+---------------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-}--#[tokio::test]-async fn set_bool_variable_bad_value() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- let err = plan_and_collect(&ctx, "SET datafusion.execution.coalesce_batches to 1")- .await- .unwrap_err();-- assert_eq!(- err.to_string(),- "Error parsing 1 as bool\ncaused by\nExternal error: provided string was not `true` or `false`"- );-- let err = plan_and_collect(&ctx, "SET datafusion.execution.coalesce_batches to abc")- .await- .unwrap_err();-- assert_eq!(- err.to_string(),- "Error parsing abc as bool\ncaused by\nExternal error: provided string was not `true` or `false`"- );-}--#[tokio::test]-async fn set_u64_variable() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- ctx.sql("SET datafusion.execution.batch_size to 0")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 0 |",- "+---------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-- ctx.sql("SET datafusion.execution.batch_size to '1'")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 1 |",- "+---------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-- ctx.sql("SET datafusion.execution.batch_size to +2")- .await- .unwrap();- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.batch_size")- .await- .unwrap();- let expected = vec![- "+---------------------------------+---------+",- "| name | setting |",- "+---------------------------------+---------+",- "| datafusion.execution.batch_size | 2 |",- "+---------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-}--#[tokio::test]-async fn set_u64_variable_bad_value() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- let err = plan_and_collect(&ctx, "SET datafusion.execution.batch_size to -1")- .await- .unwrap_err();-- assert_eq!(- err.to_string(),- "Error parsing -1 as usize\ncaused by\nExternal error: invalid digit found in string"- );-- let err = plan_and_collect(&ctx, "SET datafusion.execution.batch_size to abc")- .await- .unwrap_err();-- assert_eq!(- err.to_string(),- "Error parsing abc as usize\ncaused by\nExternal error: invalid digit found in string"- );-- let err = plan_and_collect(&ctx, "SET datafusion.execution.batch_size to 0.1")- .await- .unwrap_err();-- assert_eq!(- err.to_string(),- "Error parsing 0.1 as usize\ncaused by\nExternal error: invalid digit found in string"- );-}--#[tokio::test]-async fn set_time_zone() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- plan_and_collect(&ctx, "SET datafusion.execution.time_zone = '+08:00'")- .await- .unwrap();-- let result = plan_and_collect(&ctx, "SHOW datafusion.execution.time_zone")- .await- .unwrap();- let expected = vec![- "+--------------------------------+---------+",- "| name | setting |",- "+--------------------------------+---------+",- "| datafusion.execution.time_zone | +08:00 |",- "+--------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-}--#[tokio::test]-async fn set_time_zone_with_alias_variable_name() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- // TIME ZONE with space- plan_and_collect(&ctx, "SET TIME ZONE = '+08:00'")- .await- .unwrap();-- let result = plan_and_collect(&ctx, "SHOW TIME ZONE").await.unwrap();- let expected = vec![- "+--------------------------------+---------+",- "| name | setting |",- "+--------------------------------+---------+",- "| datafusion.execution.time_zone | +08:00 |",- "+--------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-- // TIMEZONE without space- plan_and_collect(&ctx, "SET TIMEZONE = '+07:00'")- .await- .unwrap();-- let result = plan_and_collect(&ctx, "SHOW TIMEZONE").await.unwrap();- let expected = vec![- "+--------------------------------+---------+",- "| name | setting |",- "+--------------------------------+---------+",- "| datafusion.execution.time_zone | +07:00 |",- "+--------------------------------+---------+",- ];- assert_batches_eq!(expected, &result);-}--#[tokio::test]-async fn set_time_zone_good_time_zone_format() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- plan_and_collect(&ctx, "SET TIME ZONE = '+08:00'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let expected = vec![- "+-----------------------------+",- "| Utf8(\"2000-01-01T00:00:00\") |",- "+-----------------------------+",- "| 2000-01-01T08:00:00+08:00 |",- "+-----------------------------+",- ];- // this might break once https://github.com/apache/arrow-rs/issues/1936 fixed- assert_batches_eq!(expected, &result);-- plan_and_collect(&ctx, "SET TIME ZONE = '-08:00'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let expected = vec![- "+-----------------------------+",- "| Utf8(\"2000-01-01T00:00:00\") |",- "+-----------------------------+",- "| 1999-12-31T16:00:00-08:00 |",- "+-----------------------------+",- ];- // this might break once https://github.com/apache/arrow-rs/issues/1936 fixed- assert_batches_eq!(expected, &result);-- plan_and_collect(&ctx, "SET TIME ZONE = '+0800'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let expected = vec![- "+-----------------------------+",- "| Utf8(\"2000-01-01T00:00:00\") |",- "+-----------------------------+",- "| 2000-01-01T08:00:00+08:00 |",- "+-----------------------------+",- ];- // this might break once https://github.com/apache/arrow-rs/issues/1936 fixed- assert_batches_eq!(expected, &result);-- plan_and_collect(&ctx, "SET TIME ZONE = '+08'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let expected = vec![- "+-----------------------------+",- "| Utf8(\"2000-01-01T00:00:00\") |",- "+-----------------------------+",- "| 2000-01-01T08:00:00+08:00 |",- "+-----------------------------+",- ];- // this might break once https://github.com/apache/arrow-rs/issues/1936 fixed- assert_batches_eq!(expected, &result);-}--#[tokio::test]-async fn set_time_zone_bad_time_zone_format() {- let ctx =- SessionContext::with_config(SessionConfig::new().with_information_schema(true));-- plan_and_collect(&ctx, "SET TIME ZONE = '+08:00:00'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let err = pretty_format_batches(&result).err().unwrap().to_string();- assert_eq!(err, "Parser error: Invalid timezone \"+08:00:00\": '+08:00:00' is not a valid timezone");-- plan_and_collect(&ctx, "SET TIME ZONE = '08:00'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();-- let err = pretty_format_batches(&result).err().unwrap().to_string();- assert_eq!(- err,- "Parser error: Invalid timezone \"08:00\": '08:00' is not a valid timezone"- );-- plan_and_collect(&ctx, "SET TIME ZONE = '08'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();-- let err = pretty_format_batches(&result).err().unwrap().to_string();- assert_eq!(- err,- "Parser error: Invalid timezone \"08\": '08' is not a valid timezone"- );-- // we support named timezones- plan_and_collect(&ctx, "SET TIME ZONE = 'Asia/Taipei'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let batch_pretty = pretty_format_batches(&result).unwrap().to_string();- assert_eq!(batch_pretty, "+-----------------------------+\n| Utf8(\"2000-01-01T00:00:00\") |\n+-----------------------------+\n| 2000-01-01T08:00:00+08:00 |\n+-----------------------------+");-- // this is invalid even after we support named time zone- plan_and_collect(&ctx, "SET TIME ZONE = 'Asia/Taipei2'")- .await- .unwrap();-- // casting UTF-8 to TimestampTZ isn't supported yet, add Timestamp as the middle layer for now- let result =- plan_and_collect(&ctx, "SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ")- .await- .unwrap();- let err = pretty_format_batches(&result).err().unwrap().to_string();- assert_eq!(err, "Parser error: Invalid timezone \"Asia/Taipei2\": 'Asia/Taipei2' is not a valid timezone");-}diff --git a/datafusion/core/tests/sqllogictests/test_files/set_variable.slt b/datafusion/core/tests/sqllogictests/test_files/set_variable.sltnew file mode 100644index 0000000000..3c276f346e--- /dev/null+++ b/datafusion/core/tests/sqllogictests/test_files/set_variable.slt@@ -0,0 +1,256 @@+# Licensed to the Apache Software Foundation (ASF) under one+# or more contributor license agreements. See the NOTICE file+# distributed with this work for additional information+# regarding copyright ownership. The ASF licenses this file+# to you under the Apache License, Version 2.0 (the+# "License"); you may not use this file except in compliance+# with the License. You may obtain a copy of the License at++# http://www.apache.org/licenses/LICENSE-2.0++# Unless required by applicable law or agreed to in writing,+# software distributed under the License is distributed on an+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+# KIND, either express or implied. See the License for the+# specific language governing permissions and limitations+# under the License.++# set variable to value+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.execution.batch_size to 1++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 1++# set variable to value with equal sign++statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.execution.batch_size = 1++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 1++# set variable to value with single quoted string+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.execution.batch_size to '1'++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 1++# set variable to value case insensitive+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.EXECUTION.batch_size to '1'++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 1++# set variable unknown variable+statement error DataFusion error: External error: could not find config namespace for key "aabbcc"+SET aabbcc to '1'++# set bool variable+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.execution.coalesce_batches to true++query TT+SHOW datafusion.execution.coalesce_batches+----+datafusion.execution.coalesce_batches true++statement ok+SET datafusion.execution.coalesce_batches to 'false'++query TT+SHOW datafusion.execution.coalesce_batches+----+datafusion.execution.coalesce_batches false++# set bool variable bad value++statement ok+set datafusion.catalog.information_schema = true++statement error DataFusion error: SQL error: ParserError\("Expected an SQL statement, found: caused"\)+caused by+External error: provided string was not `true` or `false`+SET datafusion.execution.coalesce_batches to 1++statement error DataFusion error: SQL error: ParserError\("Expected an SQL statement, found: caused"\)+caused by+External error: provided string was not `true` or `false`+SET datafusion.execution.coalesce_batches to abc++# set u64 variable+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.execution.batch_size to 0++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 0++statement ok+SET datafusion.execution.batch_size to '1'++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 1++statement ok+SET datafusion.execution.batch_size to +2++query TT+SHOW datafusion.execution.batch_size+----+datafusion.execution.batch_size 2++# set u64 variable bad value++statement ok+set datafusion.catalog.information_schema = true++statement error DataFusion error: SQL error: ParserError\("Expected an SQL statement, found: caused"\)+caused by+External error: invalid digit found in string+SET datafusion.execution.batch_size to -1++statement error DataFusion error: SQL error: ParserError\("Expected an SQL statement, found: caused"\)+caused by+External error: invalid digit found in string+SET datafusion.execution.batch_size to abc++statement error DataFusion error: SQL error: ParserError\("Expected an SQL statement, found: caused"\)+caused by+External error: invalid digit found in string+SET datafusion.execution.batch_size to 0.1++# set time zone+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET datafusion.execution.time_zone = '+08:00'++query TT+SHOW datafusion.execution.time_zone+----+datafusion.execution.time_zone +08:00++# set time zone with alias variable name+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET TIME ZONE = '+08:00'++query TT+SHOW TIME ZONE+----+datafusion.execution.time_zone +08:00++statement ok+SET TIMEZONE = '+07:00'++query TT+SHOW TIMEZONE+----+datafusion.execution.time_zone +07:00++# set time zone good time zone format+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET TIME ZONE = '+08:00'++query P+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ+----+2000-01-01T08:00:00+08:00++statement ok+SET TIME ZONE = '-08:00'++query P+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ+----+1999-12-31T16:00:00-08:00++statement ok+SET TIME ZONE = '+0800'++query P+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ+----+2000-01-01T08:00:00+08:00++statement ok+SET TIME ZONE = '+08'++query P+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ+----+2000-01-01T08:00:00+08:00++# set time zone bad time zone format+statement ok+set datafusion.catalog.information_schema = true++statement ok+SET TIME ZONE = '+08:00:00'++statement error Arrow error: Parser error: Invalid timezone "\+08:00:00": '\+08:00:00' is not a valid timezone+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ++statement ok+SET TIME ZONE = '08:00'++statement error Arrow error: Parser error: Invalid timezone "08:00": '08:00' is not a valid timezone+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ++statement ok+SET TIME ZONE = '08'++statement error Arrow error: Parser error: Invalid timezone "08": '08' is not a valid timezone+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ++statement ok+SET TIME ZONE = 'Asia/Taipei'++query P+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ+----+2000-01-01T08:00:00+08:00++statement ok+SET TIME ZONE = 'Asia/Taipei2'++statement error Arrow error: Parser error: Invalid timezone "Asia/Taipei2": 'Asia/Taipei2' is not a valid timezone+SELECT '2000-01-01T00:00:00'::TIMESTAMP::TIMESTAMPTZ