[enhancement](array-type) Handle cast empty string value to array (#13028)

Handle empty value between two comma when cast string to array type.

before:
mysql> select cast("[a,b,c,,,,]" as array<string>);
+-----------------------------------+
| CAST('[a,b,c,,,,]' AS ARRAY<TEXT>) |
+-----------------------------------+
| ['a', 'b', 'c', ',', ',']                |
+-----------------------------------+
1 row in set (0.01 sec)

after:
mysql> select cast("[a,b,c,,,,]" as array<string>);
+-----------------------------------+
| CAST('[a,b,c,,,,]' AS ARRAY<TEXT>) |
+-----------------------------------+
| ['a', 'b', 'c', '', '', '']                |
+-----------------------------------+
1 row in set (0.01 sec)
This commit is contained in:
xy720
2022-10-08 21:45:42 +08:00
committed by GitHub
parent 869fe2bc5d
commit b8b18e5153
6 changed files with 84 additions and 2 deletions

View File

@ -201,15 +201,28 @@ Status DataTypeArray::from_string(ReadBuffer& rb, IColumn* column) const {
if (*rb.position() == ']') {
break;
}
size_t nested_str_len = 1;
size_t nested_str_len = 0;
char* temp_char = rb.position() + nested_str_len;
while (*(temp_char) != ']' && *(temp_char) != ',' && temp_char != rb.end()) {
++nested_str_len;
temp_char = rb.position() + nested_str_len;
}
// dispose the case of ["123"] or ['123']
// dispose the case of [123,,,]
if (nested_str_len == 0) {
if (nested_column.is_nullable()) {
auto& nested_null_col = reinterpret_cast<ColumnNullable&>(nested_column);
nested_null_col.get_nested_column().insert_default();
nested_null_col.get_null_map_data().push_back(0);
} else {
nested_column.insert_default();
}
++size;
continue;
}
ReadBuffer read_buffer(rb.position(), nested_str_len);
// dispose the case of ["123"] or ['123']
auto begin_char = *rb.position();
auto end_char = *(rb.position() + nested_str_len - 1);
if (begin_char == end_char && (begin_char == '"' || begin_char == '\'')) {

View File

@ -0,0 +1,27 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
[1, 2, 3]
-- !sql --
['a', 'b', 'c']
-- !sql --
[1.34, 2.001]
-- !sql --
[1.34, 2.001]
-- !sql --
[2022-09-01]
-- !sql --
[1, 2, 3, 0, 0]
-- !sql --
['a', 'b', 'c', '', '']
-- !sql --
[1.34, 2.01, 0, 0]
-- !sql --
[2022-09-01, 0000-00-00]

View File

@ -0,0 +1,42 @@
// 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.
suite("test_cast_string_to_array") {
sql "set enable_vectorized_engine = true"
sql "ADMIN SET FRONTEND CONFIG ('enable_array_type' = 'true')"
// cast string to array<int>
qt_sql """ select cast ("[1,2,3]" as array<int>) """
// cast string to array<string>
qt_sql """ select cast ("['a','b','c']" as array<string>) """
// cast string to array<double>
qt_sql """ select cast ("[1.34,2.001]" as array<double>) """
// cast string to array<decimal>
qt_sql """ select cast ("[1.34,2.001]" as array<decimal>) """
// cast string to array<date>
qt_sql """ select cast ("[2022-09-01]" as array<date>) """
// cast empty value
qt_sql """ select cast ("[1,2,3,,,]" as array<int>) """
qt_sql """ select cast ("[a,b,c,,,]" as array<string>) """
qt_sql """ select cast ("[1.34,2.01,,,]" as array<decimal>) """
qt_sql """ select cast ("[2022-09-01,,]" as array<date>) """
}