github-actions[bot] commented on code in PR #28361:
URL: https://github.com/apache/doris/pull/28361#discussion_r1425286458


##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,

Review Comment:
   warning: function 'convertToIPv6' exceeds recommended size/complexity 
thresholds [readability-function-size]
   ```cpp
   ColumnPtr convertToIPv6(const StringColumnType& string_column,
             ^
   ```
   <details>
   <summary>Additional context</summary>
   
   **be/src/vec/functions/function_ip.h:353:** 165 lines including whitespace 
and comments (threshold 80)
   ```cpp
   ColumnPtr convertToIPv6(const StringColumnType& string_column,
             ^
   ```
   
   </details>
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@ class FunctionIPv6NumToString : public IFunction {
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,

Review Comment:
   warning: function 'convertToIPv6' has cognitive complexity of 71 (threshold 
50) [readability-function-cognitive-complexity]
   ```cpp
   ColumnPtr convertToIPv6(const StringColumnType& string_column,
             ^
   ```
   <details>
   <summary>Additional context</summary>
   
   **be/src/vec/functions/function_ip.h:355:** +1, including nesting penalty of 
0, nesting level increased to 1
   ```cpp
       if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)
       ^
   ```
   **be/src/vec/functions/function_ip.h:365:** +1, including nesting penalty of 
0, nesting level increased to 1
   ```cpp
       if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
       ^
   ```
   **be/src/vec/functions/function_ip.h:372:** +1, including nesting penalty of 
0, nesting level increased to 1
   ```cpp
       if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
       ^
   ```
   **be/src/vec/functions/function_ip.h:373:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if (string_column.get_offsets()[0] - 1 == IPV6_BINARY_LENGTH) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:374:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if constexpr (std::is_same_v<ToColumn, ColumnString>) {
               ^
   ```
   **be/src/vec/functions/function_ip.h:377:** +4, including nesting penalty of 
3, nesting level increased to 4
   ```cpp
                   if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
                   ^
   ```
   **be/src/vec/functions/function_ip.h:379:** +5, including nesting penalty of 
4, nesting level increased to 5
   ```cpp
                       if (null_map)
                       ^
   ```
   **be/src/vec/functions/function_ip.h:386:** +1, nesting level increased to 3
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/functions/function_ip.h:394:** +4, including nesting penalty of 
3, nesting level increased to 4
   ```cpp
                   if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
                   ^
   ```
   **be/src/vec/functions/function_ip.h:397:** +5, including nesting penalty of 
4, nesting level increased to 5
   ```cpp
                       if (null_map)
                       ^
   ```
   **be/src/vec/functions/function_ip.h:407:** nesting level increased to 1
   ```cpp
       auto column_create = [](size_t column_size) -> typename 
ToColumn::MutablePtr {
                            ^
   ```
   **be/src/vec/functions/function_ip.h:408:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if constexpr (std::is_same_v<ToColumn, ColumnString>) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:413:** +1, nesting level increased to 2
   ```cpp
           } else {
             ^
   ```
   **be/src/vec/functions/function_ip.h:418:** nesting level increased to 1
   ```cpp
       auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
                         ^
   ```
   **be/src/vec/functions/function_ip.h:419:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if constexpr (std::is_same_v<ToColumn, ColumnString>) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:423:** +1, nesting level increased to 2
   ```cpp
           } else {
             ^
   ```
   **be/src/vec/functions/function_ip.h:444:** +1, including nesting penalty of 
0, nesting level increased to 1
   ```cpp
       if constexpr (std::is_same_v<ToColumn, ColumnString>) offset_inc = 
IPV6_BINARY_LENGTH;
       ^
   ```
   **be/src/vec/functions/function_ip.h:446:** +1, including nesting penalty of 
0, nesting level increased to 1
   ```cpp
       for (size_t out_offset = 0, i = 0; i < column_size; out_offset += 
offset_inc, ++i) {
       ^
   ```
   **be/src/vec/functions/function_ip.h:453:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:461:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if (null_map && (*null_map)[i]) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:461:** +1
   ```cpp
           if (null_map && (*null_map)[i]) {
                        ^
   ```
   **be/src/vec/functions/function_ip.h:464:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
               ^
   ```
   **be/src/vec/functions/function_ip.h:466:** +4, including nesting penalty of 
3, nesting level increased to 4
   ```cpp
                   if constexpr (std::is_same_v<ToColumn, ColumnString>) {
                   ^
   ```
   **be/src/vec/functions/function_ip.h:482:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if (string_length != 0) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:483:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if (tryParseIPv4(src_value, dummy_result)) {
               ^
   ```
   **be/src/vec/functions/function_ip.h:486:** +1, nesting level increased to 3
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/functions/function_ip.h:491:** +2, including nesting penalty of 
1, nesting level increased to 2
   ```cpp
           if (parse_result && string_length != 0) {
           ^
   ```
   **be/src/vec/functions/function_ip.h:491:** +1
   ```cpp
           if (parse_result && string_length != 0) {
                            ^
   ```
   **be/src/vec/functions/function_ip.h:492:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if constexpr (std::is_same_v<ToColumn, ColumnString>) {
               ^
   ```
   **be/src/vec/functions/function_ip.h:497:** +1, nesting level increased to 3
   ```cpp
               } else {
                 ^
   ```
   **be/src/vec/functions/function_ip.h:500:** +1, nesting level increased to 2
   ```cpp
           } else {
             ^
   ```
   **be/src/vec/functions/function_ip.h:501:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if (exception_mode == IPStringToNumExceptionMode::Throw) {
               ^
   ```
   **be/src/vec/functions/function_ip.h:505:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if constexpr (std::is_same_v<ToColumn, ColumnString>) {
               ^
   ```
   **be/src/vec/functions/function_ip.h:509:** +3, including nesting penalty of 
2, nesting level increased to 3
   ```cpp
               if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
               ^
   ```
   **be/src/vec/functions/function_ip.h:515:** +1, including nesting penalty of 
0, nesting level increased to 1
   ```cpp
       if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
       ^
   ```
   
   </details>
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,
+                        const PaddedPODArray<UInt8>* null_map = nullptr) {
+    if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)
+        throw Exception(ErrorCode::INVALID_ARGUMENT,
+                        "Illegal return column type {}. Expected IPv6 or 
String",
+                        TypeName<typename ToColumn::ValueType>::get());
+
+    const size_t column_size = string_column.size();
+
+    ColumnUInt8::MutablePtr col_null_map_to;
+    ColumnUInt8::Container* vec_null_map_to = nullptr;
+
+    if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
+        col_null_map_to = ColumnUInt8::create(column_size, false);
+        vec_null_map_to = &col_null_map_to->get_data();
+    }
+
+    /// This is a special treatment for source column of type String
+    /// to preserve previous behavior when IPv6 was a domain type of String
+    if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
+        if (string_column.get_offsets()[0] - 1 == IPV6_BINARY_LENGTH) {
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto col_res = ColumnString::create();
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            } else {
+                auto col_res = ColumnIPv6::create();
+                auto& vec_res = col_res->get_data();
+
+                vec_res.resize(column_size);
+                memcpy(vec_res.data(), string_column.get_chars().data(),
+                       column_size * IPV6_BINARY_LENGTH);
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);

Review Comment:
   warning: statement should be inside braces 
[readability-braces-around-statements]
   
   ```suggestion
                       if (null_map) {
                           memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
   }
   ```
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,
+                        const PaddedPODArray<UInt8>* null_map = nullptr) {
+    if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)
+        throw Exception(ErrorCode::INVALID_ARGUMENT,
+                        "Illegal return column type {}. Expected IPv6 or 
String",
+                        TypeName<typename ToColumn::ValueType>::get());
+
+    const size_t column_size = string_column.size();
+
+    ColumnUInt8::MutablePtr col_null_map_to;
+    ColumnUInt8::Container* vec_null_map_to = nullptr;
+
+    if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
+        col_null_map_to = ColumnUInt8::create(column_size, false);
+        vec_null_map_to = &col_null_map_to->get_data();
+    }
+
+    /// This is a special treatment for source column of type String
+    /// to preserve previous behavior when IPv6 was a domain type of String
+    if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
+        if (string_column.get_offsets()[0] - 1 == IPV6_BINARY_LENGTH) {
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto col_res = ColumnString::create();
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            } else {
+                auto col_res = ColumnIPv6::create();
+                auto& vec_res = col_res->get_data();
+
+                vec_res.resize(column_size);
+                memcpy(vec_res.data(), string_column.get_chars().data(),
+                       column_size * IPV6_BINARY_LENGTH);
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            }
+        }
+    }
+
+    auto column_create = [](size_t column_size) -> typename 
ToColumn::MutablePtr {
+        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+            auto column_string = ColumnString::create();
+            column_string->get_chars().reserve(column_size * 
IPV6_BINARY_LENGTH);
+            column_string->get_offsets().reserve(column_size);
+            return column_string;
+        } else {
+            return ColumnIPv6::create();
+        }
+    };
+
+    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
+        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+            auto& vec_res = col_res->get_chars();
+            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
+            return (vec_res);
+        } else {
+            auto& vec_res = col_res->get_data();
+            vec_res.resize(col_size);
+            return (vec_res);
+        }
+    };
+
+    auto col_res = column_create(column_size);
+    auto& vec_res = get_vector(col_res, column_size);
+
+    using Chars = typename StringColumnType::Chars;
+    const Chars& vec_src = string_column.get_chars();
+
+    size_t src_offset = 0;
+    char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = 
"::ffff:";
+
+    /// ColumnString contains not null terminated strings. But functions 
parseIPv6, parseIPv4 expect null terminated string.
+    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be 
parsed in-place
+    std::string string_buffer;
+
+    int offset_inc = 1;
+    if constexpr (std::is_same_v<ToColumn, ColumnString>) offset_inc = 
IPV6_BINARY_LENGTH;
+
+    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += 
offset_inc, ++i) {
+
+        size_t src_next_offset = src_offset;
+
+        const char* src_value = nullptr;
+        unsigned char* res_value = reinterpret_cast<unsigned 
char*>(&vec_res[out_offset]);

Review Comment:
   warning: use auto when initializing with a cast to avoid duplicating the 
type name [modernize-use-auto]
   
   ```suggestion
           auto* res_value = reinterpret_cast<unsigned 
char*>(&vec_res[out_offset]);
   ```
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,
+                        const PaddedPODArray<UInt8>* null_map = nullptr) {
+    if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)

Review Comment:
   warning: statement should be inside braces 
[readability-braces-around-statements]
   
   ```suggestion
       if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>) {
   ```
   
   be/src/vec/functions/function_ip.h:358:
   ```diff
   -                         TypeName<typename ToColumn::ValueType>::get());
   +                         TypeName<typename ToColumn::ValueType>::get());
   + }
   ```
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,
+                        const PaddedPODArray<UInt8>* null_map = nullptr) {
+    if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)
+        throw Exception(ErrorCode::INVALID_ARGUMENT,
+                        "Illegal return column type {}. Expected IPv6 or 
String",
+                        TypeName<typename ToColumn::ValueType>::get());
+
+    const size_t column_size = string_column.size();
+
+    ColumnUInt8::MutablePtr col_null_map_to;
+    ColumnUInt8::Container* vec_null_map_to = nullptr;
+
+    if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
+        col_null_map_to = ColumnUInt8::create(column_size, false);
+        vec_null_map_to = &col_null_map_to->get_data();
+    }
+
+    /// This is a special treatment for source column of type String
+    /// to preserve previous behavior when IPv6 was a domain type of String
+    if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
+        if (string_column.get_offsets()[0] - 1 == IPV6_BINARY_LENGTH) {
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto col_res = ColumnString::create();
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            } else {
+                auto col_res = ColumnIPv6::create();
+                auto& vec_res = col_res->get_data();
+
+                vec_res.resize(column_size);
+                memcpy(vec_res.data(), string_column.get_chars().data(),
+                       column_size * IPV6_BINARY_LENGTH);
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            }
+        }
+    }
+
+    auto column_create = [](size_t column_size) -> typename 
ToColumn::MutablePtr {
+        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+            auto column_string = ColumnString::create();
+            column_string->get_chars().reserve(column_size * 
IPV6_BINARY_LENGTH);
+            column_string->get_offsets().reserve(column_size);
+            return column_string;
+        } else {
+            return ColumnIPv6::create();
+        }
+    };
+
+    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
+        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+            auto& vec_res = col_res->get_chars();
+            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
+            return (vec_res);
+        } else {
+            auto& vec_res = col_res->get_data();
+            vec_res.resize(col_size);
+            return (vec_res);
+        }
+    };
+
+    auto col_res = column_create(column_size);
+    auto& vec_res = get_vector(col_res, column_size);
+
+    using Chars = typename StringColumnType::Chars;
+    const Chars& vec_src = string_column.get_chars();
+
+    size_t src_offset = 0;
+    char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = 
"::ffff:";
+
+    /// ColumnString contains not null terminated strings. But functions 
parseIPv6, parseIPv4 expect null terminated string.
+    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be 
parsed in-place
+    std::string string_buffer;
+
+    int offset_inc = 1;
+    if constexpr (std::is_same_v<ToColumn, ColumnString>) offset_inc = 
IPV6_BINARY_LENGTH;

Review Comment:
   warning: statement should be inside braces 
[readability-braces-around-statements]
   
   ```suggestion
       if constexpr (std::is_same_v<ToColumn, ColumnString>) { offset_inc = 
IPV6_BINARY_LENGTH;
   }
   ```
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,
+                        const PaddedPODArray<UInt8>* null_map = nullptr) {
+    if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)
+        throw Exception(ErrorCode::INVALID_ARGUMENT,
+                        "Illegal return column type {}. Expected IPv6 or 
String",
+                        TypeName<typename ToColumn::ValueType>::get());
+
+    const size_t column_size = string_column.size();
+
+    ColumnUInt8::MutablePtr col_null_map_to;
+    ColumnUInt8::Container* vec_null_map_to = nullptr;
+
+    if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
+        col_null_map_to = ColumnUInt8::create(column_size, false);
+        vec_null_map_to = &col_null_map_to->get_data();
+    }
+
+    /// This is a special treatment for source column of type String
+    /// to preserve previous behavior when IPv6 was a domain type of String
+    if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
+        if (string_column.get_offsets()[0] - 1 == IPV6_BINARY_LENGTH) {
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto col_res = ColumnString::create();
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);

Review Comment:
   warning: statement should be inside braces 
[readability-braces-around-statements]
   
   ```suggestion
                       if (null_map) {
                           memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
   }
   ```
   



##########
be/src/vec/functions/function_ip.h:
##########
@@ -348,4 +348,251 @@
     }
 };
 
+namespace detail {
+template <IPStringToNumExceptionMode exception_mode, typename ToColumn = 
ColumnIPv6,
+          typename StringColumnType>
+ColumnPtr convertToIPv6(const StringColumnType& string_column,
+                        const PaddedPODArray<UInt8>* null_map = nullptr) {
+    if constexpr (!std::is_same_v<ToColumn, ColumnString> && 
!std::is_same_v<ToColumn, ColumnIPv6>)
+        throw Exception(ErrorCode::INVALID_ARGUMENT,
+                        "Illegal return column type {}. Expected IPv6 or 
String",
+                        TypeName<typename ToColumn::ValueType>::get());
+
+    const size_t column_size = string_column.size();
+
+    ColumnUInt8::MutablePtr col_null_map_to;
+    ColumnUInt8::Container* vec_null_map_to = nullptr;
+
+    if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
+        col_null_map_to = ColumnUInt8::create(column_size, false);
+        vec_null_map_to = &col_null_map_to->get_data();
+    }
+
+    /// This is a special treatment for source column of type String
+    /// to preserve previous behavior when IPv6 was a domain type of String
+    if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
+        if (string_column.get_offsets()[0] - 1 == IPV6_BINARY_LENGTH) {
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto col_res = ColumnString::create();
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            } else {
+                auto col_res = ColumnIPv6::create();
+                auto& vec_res = col_res->get_data();
+
+                vec_res.resize(column_size);
+                memcpy(vec_res.data(), string_column.get_chars().data(),
+                       column_size * IPV6_BINARY_LENGTH);
+
+                if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
+
+                    col_null_map_to = ColumnUInt8::create(column_size, false);
+                    if (null_map)
+                        memcpy(col_null_map_to->get_data().data(), 
null_map->data(), column_size);
+                    return ColumnNullable::create(std::move(col_res), 
std::move(col_null_map_to));
+                }
+
+                return col_res;
+            }
+        }
+    }
+
+    auto column_create = [](size_t column_size) -> typename 
ToColumn::MutablePtr {
+        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+            auto column_string = ColumnString::create();
+            column_string->get_chars().reserve(column_size * 
IPV6_BINARY_LENGTH);
+            column_string->get_offsets().reserve(column_size);
+            return column_string;
+        } else {
+            return ColumnIPv6::create();
+        }
+    };
+
+    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
+        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+            auto& vec_res = col_res->get_chars();
+            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
+            return (vec_res);
+        } else {
+            auto& vec_res = col_res->get_data();
+            vec_res.resize(col_size);
+            return (vec_res);
+        }
+    };
+
+    auto col_res = column_create(column_size);
+    auto& vec_res = get_vector(col_res, column_size);
+
+    using Chars = typename StringColumnType::Chars;
+    const Chars& vec_src = string_column.get_chars();
+
+    size_t src_offset = 0;
+    char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = 
"::ffff:";
+
+    /// ColumnString contains not null terminated strings. But functions 
parseIPv6, parseIPv4 expect null terminated string.
+    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be 
parsed in-place
+    std::string string_buffer;
+
+    int offset_inc = 1;
+    if constexpr (std::is_same_v<ToColumn, ColumnString>) offset_inc = 
IPV6_BINARY_LENGTH;
+
+    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += 
offset_inc, ++i) {
+
+        size_t src_next_offset = src_offset;
+
+        const char* src_value = nullptr;
+        unsigned char* res_value = reinterpret_cast<unsigned 
char*>(&vec_res[out_offset]);
+
+        if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
+            src_value = reinterpret_cast<const char*>(&vec_src[src_offset]);
+            src_next_offset = string_column.get_offsets()[i];
+
+            string_buffer.assign(src_value, src_next_offset - src_offset);
+            src_value = string_buffer.c_str();
+        }
+
+        if (null_map && (*null_map)[i]) {
+            std::fill_n(&vec_res[out_offset], offset_inc, 0);
+            src_offset = src_next_offset;
+            if constexpr (exception_mode == IPStringToNumExceptionMode::Null) {
+                (*vec_null_map_to)[i] = true;
+                if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                    auto* column_string = 
assert_cast<ColumnString*>(col_res.get());
+                    column_string->get_offsets().push_back((i + 1) * 
IPV6_BINARY_LENGTH);
+                }
+            }
+            continue;
+        }
+
+        bool parse_result = false;
+        Int64 dummy_result = 0;
+
+        /// For both cases below: In case of failure, the function parseIPv6 
fills vec_res with zero bytes.
+
+        /// If the source IP address is parsable as an IPv4 address, then 
transform it into a valid IPv6 address.
+        /// Keeping it simple by just prefixing `::ffff:` to the IPv4 address 
to represent it as a valid IPv6 address.
+        size_t string_length = src_next_offset - src_offset;
+        if (string_length != 0) {
+            if (tryParseIPv4(src_value, dummy_result)) {
+                strcat(src_ipv4_buf, src_value);
+                parse_result = parseIPv6whole(src_ipv4_buf, res_value);
+            } else {
+                parse_result = parseIPv6whole(src_value, res_value);
+            }
+        }
+
+        if (parse_result && string_length != 0) {
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto* column_string = 
assert_cast<ColumnString*>(col_res.get());
+                std::copy(res_value, res_value + IPV6_BINARY_LENGTH,
+                          column_string->get_chars().begin() + i * 
IPV6_BINARY_LENGTH);
+                column_string->get_offsets().push_back((i + 1) * 
IPV6_BINARY_LENGTH);
+            } else {
+                col_res->insert_data(reinterpret_cast<const char*>(res_value), 
IPV6_BINARY_LENGTH);
+            }
+        } else {
+            if (exception_mode == IPStringToNumExceptionMode::Throw) {
+                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 
value");
+            }
+            std::fill_n(&vec_res[out_offset], offset_inc, 0);
+            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
+                auto* column_string = 
assert_cast<ColumnString*>(col_res.get());
+                column_string->get_offsets().push_back((i + 1) * 
IPV6_BINARY_LENGTH);
+            }
+            if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
+                (*vec_null_map_to)[i] = true;

Review Comment:
   warning: statement should be inside braces 
[readability-braces-around-statements]
   
   ```suggestion
               if constexpr (exception_mode == 
IPStringToNumExceptionMode::Null) {
                   (*vec_null_map_to)[i] = true;
   }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to