You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by xu...@apache.org on 2022/10/08 13:45:51 UTC

[doris] branch master updated: [enhancement](array-type) Handle cast empty string value to array (#13028)

This is an automated email from the ASF dual-hosted git repository.

xuyang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new b8b18e5153 [enhancement](array-type) Handle cast empty string value to array (#13028)
b8b18e5153 is described below

commit b8b18e51533bb59044df0fc35ec61fc54d928907
Author: xy720 <22...@users.noreply.github.com>
AuthorDate: Sat Oct 8 21:45:42 2022 +0800

    [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)
---
 be/src/vec/data_types/data_type_array.cpp          | 17 +++++++--
 .../test_cast_array_function.out                   |  0
 .../cast_function/test_cast_string_to_array.out    | 27 ++++++++++++++
 .../test_cast_array_function.groovy                |  0
 .../test_cast_array_functions_by_literal.groovy    |  0
 .../cast_function/test_cast_string_to_array.groovy | 42 ++++++++++++++++++++++
 6 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/be/src/vec/data_types/data_type_array.cpp b/be/src/vec/data_types/data_type_array.cpp
index 61fb4c04a5..4a0c1ae049 100644
--- a/be/src/vec/data_types/data_type_array.cpp
+++ b/be/src/vec/data_types/data_type_array.cpp
@@ -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 == '\'')) {
diff --git a/regression-test/data/query_p0/sql_functions/array_functions/test_cast_array_function.out b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_array_function.out
similarity index 100%
rename from regression-test/data/query_p0/sql_functions/array_functions/test_cast_array_function.out
rename to regression-test/data/query_p0/sql_functions/cast_function/test_cast_array_function.out
diff --git a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_string_to_array.out b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_string_to_array.out
new file mode 100644
index 0000000000..1eb1418926
--- /dev/null
+++ b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_string_to_array.out
@@ -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]
diff --git a/regression-test/suites/query_p0/sql_functions/array_functions/test_cast_array_function.groovy b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_array_function.groovy
similarity index 100%
rename from regression-test/suites/query_p0/sql_functions/array_functions/test_cast_array_function.groovy
rename to regression-test/suites/query_p0/sql_functions/cast_function/test_cast_array_function.groovy
diff --git a/regression-test/suites/query_p0/sql_functions/array_functions/test_cast_array_functions_by_literal.groovy b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_array_functions_by_literal.groovy
similarity index 100%
rename from regression-test/suites/query_p0/sql_functions/array_functions/test_cast_array_functions_by_literal.groovy
rename to regression-test/suites/query_p0/sql_functions/cast_function/test_cast_array_functions_by_literal.groovy
diff --git a/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_string_to_array.groovy b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_string_to_array.groovy
new file mode 100644
index 0000000000..ab31f54031
--- /dev/null
+++ b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_string_to_array.groovy
@@ -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>) """
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org