forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.rs
128 lines (121 loc) · 5.83 KB
/
unicode.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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::<StringArray>(DataType::Utf8).await
}
#[tokio::test]
async fn query_large_length() -> Result<()> {
generic_query_length::<LargeStringArray>(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<T: 'static + Array + From<Vec<&'static str>>>(
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(())
}