diff --git a/datafusion/core/tests/sql/mod.rs b/datafusion/core/tests/sql/mod.rs index cbccf333b304..529d0313cac2 100644 --- a/datafusion/core/tests/sql/mod.rs +++ b/datafusion/core/tests/sql/mod.rs @@ -106,8 +106,6 @@ pub mod parquet_schema; pub mod partitioned_csv; pub mod set_variable; pub mod subqueries; -#[cfg(feature = "unicode_expressions")] -pub mod unicode; fn assert_float_eq(expected: &[Vec], received: &[Vec]) where diff --git a/datafusion/core/tests/sql/unicode.rs b/datafusion/core/tests/sql/unicode.rs deleted file mode 100644 index b787b5a328cb..000000000000 --- a/datafusion/core/tests/sql/unicode.rs +++ /dev/null @@ -1,128 +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::*; - -#[tokio::test] -async fn query_length() -> Result<()> { - generic_query_length::(DataType::Utf8).await -} - -#[tokio::test] -async fn query_large_length() -> Result<()> { - generic_query_length::(DataType::LargeUtf8).await -} - -#[tokio::test] -async fn test_unicode_expressions() -> Result<()> { - test_expression!("char_length('')", "0"); - test_expression!("char_length('chars')", "5"); - test_expression!("char_length('josé')", "4"); - test_expression!("char_length(NULL)", "NULL"); - test_expression!("character_length('')", "0"); - test_expression!("character_length('chars')", "5"); - test_expression!("character_length('josé')", "4"); - test_expression!("character_length(NULL)", "NULL"); - test_expression!("left('abcde', -2)", "abc"); - test_expression!("left('abcde', -200)", ""); - test_expression!("left('abcde', 0)", ""); - test_expression!("left('abcde', 2)", "ab"); - test_expression!("left('abcde', 200)", "abcde"); - test_expression!("left('abcde', CAST(NULL AS INT))", "NULL"); - test_expression!("left(NULL, 2)", "NULL"); - test_expression!("left(NULL, CAST(NULL AS INT))", "NULL"); - test_expression!("length('')", "0"); - test_expression!("length('chars')", "5"); - test_expression!("length('josé')", "4"); - test_expression!("length(NULL)", "NULL"); - test_expression!("lpad('hi', -1, 'xy')", ""); - test_expression!("lpad('hi', 5, 'xy')", "xyxhi"); - test_expression!("lpad('hi', -1)", ""); - test_expression!("lpad('hi', 0)", ""); - test_expression!("lpad('hi', 21, 'abcdef')", "abcdefabcdefabcdefahi"); - test_expression!("lpad('hi', 5, 'xy')", "xyxhi"); - test_expression!("lpad('hi', 5, NULL)", "NULL"); - test_expression!("lpad('hi', 5)", " hi"); - test_expression!("lpad('hi', CAST(NULL AS INT), 'xy')", "NULL"); - test_expression!("lpad('hi', CAST(NULL AS INT))", "NULL"); - test_expression!("lpad('xyxhi', 3)", "xyx"); - test_expression!("lpad(NULL, 0)", "NULL"); - test_expression!("lpad(NULL, 5, 'xy')", "NULL"); - test_expression!("reverse('abcde')", "edcba"); - test_expression!("reverse('loẅks')", "sk̈wol"); // Compatible with PostgreSQL - test_expression!("reverse(NULL)", "NULL"); - test_expression!("right('abcde', -2)", "cde"); - test_expression!("right('abcde', -200)", ""); - test_expression!("right('abcde', 0)", ""); - test_expression!("right('abcde', 2)", "de"); - test_expression!("right('abcde', 200)", "abcde"); - test_expression!("right('abcde', CAST(NULL AS INT))", "NULL"); - test_expression!("right(NULL, 2)", "NULL"); - test_expression!("right(NULL, CAST(NULL AS INT))", "NULL"); - test_expression!("rpad('hi', -1, 'xy')", ""); - test_expression!("rpad('hi', 5, 'xy')", "hixyx"); - test_expression!("rpad('hi', -1)", ""); - test_expression!("rpad('hi', 0)", ""); - test_expression!("rpad('hi', 21, 'abcdef')", "hiabcdefabcdefabcdefa"); - test_expression!("rpad('hi', 5, 'xy')", "hixyx"); - test_expression!("rpad('hi', 5, NULL)", "NULL"); - test_expression!("rpad('hi', 5)", "hi "); - test_expression!("rpad('hi', CAST(NULL AS INT), 'xy')", "NULL"); - test_expression!("rpad('hi', CAST(NULL AS INT))", "NULL"); - test_expression!("rpad('xyxhi', 3)", "xyx"); - test_expression!("strpos('abc', 'c')", "3"); - test_expression!("strpos('josé', 'é')", "4"); - test_expression!("strpos('joséésoj', 'so')", "6"); - test_expression!("strpos('joséésoj', 'abc')", "0"); - test_expression!("strpos(NULL, 'abc')", "NULL"); - test_expression!("strpos('joséésoj', NULL)", "NULL"); - test_expression!("substr('alphabet', -3)", "alphabet"); - test_expression!("substr('alphabet', 0)", "alphabet"); - test_expression!("substr('alphabet', 1)", "alphabet"); - test_expression!("substr('alphabet', 2)", "lphabet"); - test_expression!("substr('alphabet', 3)", "phabet"); - test_expression!("substr('alphabet', 30)", ""); - test_expression!("substr('alphabet', CAST(NULL AS int))", "NULL"); - test_expression!("substr('alphabet', 3, 2)", "ph"); - test_expression!("substr('alphabet', 3, 20)", "phabet"); - test_expression!("substr('alphabet', CAST(NULL AS int), 20)", "NULL"); - test_expression!("substr('alphabet', 3, CAST(NULL AS int))", "NULL"); - test_expression!("translate('12345', '143', 'ax')", "a2x5"); - test_expression!("translate(NULL, '143', 'ax')", "NULL"); - test_expression!("translate('12345', NULL, 'ax')", "NULL"); - test_expression!("translate('12345', '143', NULL)", "NULL"); - Ok(()) -} - -async fn generic_query_length>>( - datatype: DataType, -) -> Result<()> { - let schema = Arc::new(Schema::new(vec![Field::new("c1", datatype, false)])); - - let data = RecordBatch::try_new( - schema.clone(), - vec![Arc::new(T::from(vec!["", "a", "aa", "aaa"]))], - )?; - - let ctx = SessionContext::new(); - ctx.register_batch("test", data)?; - let sql = "SELECT length(c1) FROM test"; - let actual = execute(&ctx, sql).await; - let expected = vec![vec!["0"], vec!["1"], vec!["2"], vec!["3"]]; - assert_eq!(expected, actual); - Ok(()) -} diff --git a/datafusion/core/tests/sqllogictests/test_files/functions.slt b/datafusion/core/tests/sqllogictests/test_files/functions.slt new file mode 100644 index 000000000000..c6f2db8eb233 --- /dev/null +++ b/datafusion/core/tests/sqllogictests/test_files/functions.slt @@ -0,0 +1,420 @@ +# 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. + + +# unicode expressions + +query I +SELECT char_length('') +---- +0 + +query I +SELECT char_length('chars') +---- +5 + +query I +SELECT char_length('josé') +---- +4 + +query ? +SELECT char_length(NULL) +---- +NULL + +query I +SELECT character_length('') +---- +0 + +query I +SELECT character_length('chars') +---- +5 + +query I +SELECT character_length('josé') +---- +4 + +query ? +SELECT character_length(NULL) +---- +NULL + +query T +SELECT left('abcde', -2) +---- +abc + +query T +SELECT left('abcde', -200) +---- +(empty) + +query T +SELECT left('abcde', 0) +---- +(empty) + +query T +SELECT left('abcde', 2) +---- +ab + +query T +SELECT left('abcde', 200) +---- +abcde + +query T +SELECT left('abcde', CAST(NULL AS INT)) +---- +NULL + +query ? +SELECT left(NULL, 2) +---- +NULL + +query ? +SELECT left(NULL, CAST(NULL AS INT)) +---- +NULL + +query I +SELECT length('') +---- +0 + +query I +SELECT length('chars') +---- +5 + +query I +SELECT length('josé') +---- +4 + +query ? +SELECT length(NULL) +---- +NULL + +query T +SELECT lpad('hi', -1, 'xy') +---- +(empty) + +query T +SELECT lpad('hi', 5, 'xy') +---- +xyxhi + +query T +SELECT lpad('hi', -1) +---- +(empty) + +query T +SELECT lpad('hi', 0) +---- +(empty) + +query T +SELECT lpad('hi', 21, 'abcdef') +---- +abcdefabcdefabcdefahi + +query T +SELECT lpad('hi', 5, 'xy') +---- +xyxhi + +query T +SELECT lpad('hi', 5, NULL) +---- +NULL + +query T +SELECT lpad('hi', 5) +---- + hi + +query T +SELECT lpad('hi', CAST(NULL AS INT), 'xy') +---- +NULL + +query T +SELECT lpad('hi', CAST(NULL AS INT)) +---- +NULL + +query T +SELECT lpad('xyxhi', 3) +---- +xyx + +query ? +SELECT lpad(NULL, 0) +---- +NULL + +query ? +SELECT lpad(NULL, 5, 'xy') +---- +NULL + +query T +SELECT reverse('abcde') +---- +edcba + +query T +SELECT reverse('loẅks') +---- +sk̈wol + +query ? +SELECT reverse(NULL) +---- +NULL + +query T +SELECT right('abcde', -2) +---- +cde + +query T +SELECT right('abcde', -200) +---- +(empty) + +query T +SELECT right('abcde', 0) +---- +(empty) + +query T +SELECT right('abcde', 2) +---- +de + +query T +SELECT right('abcde', 200) +---- +abcde + +query T +SELECT right('abcde', CAST(NULL AS INT)) +---- +NULL + +query ? +SELECT right(NULL, 2) +---- +NULL + +query ? +SELECT right(NULL, CAST(NULL AS INT)) +---- +NULL + +query T +SELECT rpad('hi', -1, 'xy') +---- +(empty) + +query T +SELECT rpad('hi', 5, 'xy') +---- +hixyx + +query T +SELECT rpad('hi', -1) +---- +(empty) + +query T +SELECT rpad('hi', 0) +---- +(empty) + +query T +SELECT rpad('hi', 21, 'abcdef') +---- +hiabcdefabcdefabcdefa + +query T +SELECT rpad('hi', 5, 'xy') +---- +hixyx + +query T +SELECT rpad('hi', 5, NULL) +---- +NULL + +query T +SELECT rpad('hi', 5) +---- +hi + +query T +SELECT rpad('hi', CAST(NULL AS INT), 'xy') +---- +NULL + +query T +SELECT rpad('hi', CAST(NULL AS INT)) +---- +NULL + +query T +SELECT rpad('xyxhi', 3) +---- +xyx + +query I +SELECT strpos('abc', 'c') +---- +3 + +query I +SELECT strpos('josé', 'é') +---- +4 + +query I +SELECT strpos('joséésoj', 'so') +---- +6 + +query I +SELECT strpos('joséésoj', 'abc') +---- +0 + +query ? +SELECT strpos(NULL, 'abc') +---- +NULL + +query I +SELECT strpos('joséésoj', NULL) +---- +NULL + +query T +SELECT substr('alphabet', -3) +---- +alphabet + +query T +SELECT substr('alphabet', 0) +---- +alphabet + +query T +SELECT substr('alphabet', 1) +---- +alphabet + +query T +SELECT substr('alphabet', 2) +---- +lphabet + +query T +SELECT substr('alphabet', 3) +---- +phabet + +query T +SELECT substr('alphabet', 30) +---- +(empty) + +query T +SELECT substr('alphabet', CAST(NULL AS int)) +---- +NULL + +query T +SELECT substr('alphabet', 3, 2) +---- +ph + +query T +SELECT substr('alphabet', 3, 20) +---- +phabet + +query T +SELECT substr('alphabet', CAST(NULL AS int), 20) +---- +NULL + +query T +SELECT substr('alphabet', 3, CAST(NULL AS int)) +---- +NULL + +query T +SELECT translate('12345', '143', 'ax') +---- +a2x5 + +query ? +SELECT translate(NULL, '143', 'ax') +---- +NULL + +query T +SELECT translate('12345', NULL, 'ax') +---- +NULL + +query T +SELECT translate('12345', '143', NULL) +---- +NULL + +statement ok +CREATE TABLE test( + c1 VARCHAR +) as VALUES +(''), +('a'), +('aa'), +('aaa'); + +# generic query length +query I rowsort +SELECT length(c1) FROM test +---- +0 +1 +2 +3 + +statement ok +drop table test