Coverage Report

Created: 2025-05-19 20:49

/root/doris/be/src/util/string_parser.hpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/string-parser.hpp
19
// and modified by Doris
20
21
#pragma once
22
23
#include <fast_float/fast_float.h>
24
#include <fast_float/parse_number.h>
25
#include <glog/logging.h>
26
27
#include <cstdlib>
28
// IWYU pragma: no_include <bits/std_abs.h>
29
#include <cmath> // IWYU pragma: keep
30
#include <cstdint>
31
#include <limits>
32
#include <map>
33
#include <string>
34
#include <system_error>
35
#include <type_traits>
36
#include <utility>
37
38
#include "common/compiler_util.h" // IWYU pragma: keep
39
#include "common/status.h"
40
#include "runtime/large_int_value.h"
41
#include "runtime/primitive_type.h"
42
#include "vec/common/int_exp.h"
43
#include "vec/common/string_utils/string_utils.h"
44
#include "vec/core/extended_types.h"
45
#include "vec/core/wide_integer.h"
46
#include "vec/data_types/data_type_decimal.h"
47
#include "vec/data_types/number_traits.h"
48
49
namespace doris {
50
namespace vectorized {
51
template <DecimalNativeTypeConcept T>
52
struct Decimal;
53
} // namespace vectorized
54
55
// Utility functions for doing atoi/atof on non-null terminated strings.  On micro benchmarks,
56
// this is significantly faster than libc (atoi/strtol and atof/strtod).
57
//
58
// Strings with leading and trailing whitespaces are accepted.
59
// Branching is heavily optimized for the non-whitespace successful case.
60
// All the StringTo* functions first parse the input string assuming it has no leading whitespace.
61
// If that first attempt was unsuccessful, these functions retry the parsing after removing
62
// whitespace. Therefore, strings with whitespace take a perf hit on branch mis-prediction.
63
//
64
// For overflows, we are following the mysql behavior, to cap values at the max/min value for that
65
// data type.  This is different from hive, which returns NULL for overflow slots for int types
66
// and inf/-inf for float types.
67
//
68
// Things we tried that did not work:
69
//  - lookup table for converting character to digit
70
// Improvements (TODO):
71
//  - Validate input using _sidd_compare_ranges
72
//  - Since we know the length, we can parallelize this: i.e. result = 100*s[0] + 10*s[1] + s[2]
73
class StringParser {
74
public:
75
    enum ParseResult { PARSE_SUCCESS = 0, PARSE_FAILURE, PARSE_OVERFLOW, PARSE_UNDERFLOW };
76
77
    template <typename T>
78
646k
    static T numeric_limits(bool negative) {
79
646k
        if constexpr (std::is_same_v<T, __int128>) {
80
596k
            return negative ? MIN_INT128 : MAX_INT128;
81
596k
        } else {
82
596k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
596k
        }
84
646k
    }
_ZN5doris12StringParser14numeric_limitsIaEET_b
Line
Count
Source
78
332k
    static T numeric_limits(bool negative) {
79
332k
        if constexpr (std::is_same_v<T, __int128>) {
80
332k
            return negative ? MIN_INT128 : MAX_INT128;
81
332k
        } else {
82
332k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
332k
        }
84
332k
    }
_ZN5doris12StringParser14numeric_limitsIlEET_b
Line
Count
Source
78
91.6k
    static T numeric_limits(bool negative) {
79
91.6k
        if constexpr (std::is_same_v<T, __int128>) {
80
91.6k
            return negative ? MIN_INT128 : MAX_INT128;
81
91.6k
        } else {
82
91.6k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
91.6k
        }
84
91.6k
    }
_ZN5doris12StringParser14numeric_limitsIsEET_b
Line
Count
Source
78
80.2k
    static T numeric_limits(bool negative) {
79
80.2k
        if constexpr (std::is_same_v<T, __int128>) {
80
80.2k
            return negative ? MIN_INT128 : MAX_INT128;
81
80.2k
        } else {
82
80.2k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
80.2k
        }
84
80.2k
    }
_ZN5doris12StringParser14numeric_limitsIiEET_b
Line
Count
Source
78
70.5k
    static T numeric_limits(bool negative) {
79
70.5k
        if constexpr (std::is_same_v<T, __int128>) {
80
70.5k
            return negative ? MIN_INT128 : MAX_INT128;
81
70.5k
        } else {
82
70.5k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
70.5k
        }
84
70.5k
    }
_ZN5doris12StringParser14numeric_limitsInEET_b
Line
Count
Source
78
49.7k
    static T numeric_limits(bool negative) {
79
49.7k
        if constexpr (std::is_same_v<T, __int128>) {
80
49.7k
            return negative ? MIN_INT128 : MAX_INT128;
81
49.7k
        } else {
82
49.7k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
49.7k
        }
84
49.7k
    }
_ZN5doris12StringParser14numeric_limitsIhEET_b
Line
Count
Source
78
19.9k
    static T numeric_limits(bool negative) {
79
19.9k
        if constexpr (std::is_same_v<T, __int128>) {
80
19.9k
            return negative ? MIN_INT128 : MAX_INT128;
81
19.9k
        } else {
82
19.9k
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
19.9k
        }
84
19.9k
    }
_ZN5doris12StringParser14numeric_limitsIjEET_b
Line
Count
Source
78
692
    static T numeric_limits(bool negative) {
79
692
        if constexpr (std::is_same_v<T, __int128>) {
80
692
            return negative ? MIN_INT128 : MAX_INT128;
81
692
        } else {
82
692
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
692
        }
84
692
    }
_ZN5doris12StringParser14numeric_limitsImEET_b
Line
Count
Source
78
649
    static T numeric_limits(bool negative) {
79
649
        if constexpr (std::is_same_v<T, __int128>) {
80
649
            return negative ? MIN_INT128 : MAX_INT128;
81
649
        } else {
82
649
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
649
        }
84
649
    }
_ZN5doris12StringParser14numeric_limitsIoEET_b
Line
Count
Source
78
4
    static T numeric_limits(bool negative) {
79
4
        if constexpr (std::is_same_v<T, __int128>) {
80
4
            return negative ? MIN_INT128 : MAX_INT128;
81
4
        } else {
82
4
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
4
        }
84
4
    }
_ZN5doris12StringParser14numeric_limitsIN4wide7integerILm256EiEEEET_b
Line
Count
Source
78
4
    static T numeric_limits(bool negative) {
79
4
        if constexpr (std::is_same_v<T, __int128>) {
80
4
            return negative ? MIN_INT128 : MAX_INT128;
81
4
        } else {
82
4
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
4
        }
84
4
    }
_ZN5doris12StringParser14numeric_limitsItEET_b
Line
Count
Source
78
596
    static T numeric_limits(bool negative) {
79
596
        if constexpr (std::is_same_v<T, __int128>) {
80
596
            return negative ? MIN_INT128 : MAX_INT128;
81
596
        } else {
82
596
            return negative ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
83
596
        }
84
596
    }
85
86
    template <typename T>
87
551k
    static T get_scale_multiplier(int scale) {
88
551k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
551k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
551k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
551k
        if constexpr (std::is_same_v<T, int32_t>) {
92
435k
            return common::exp10_i32(scale);
93
435k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
326k
            return common::exp10_i64(scale);
95
326k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
218k
            return common::exp10_i128(scale);
97
218k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
218k
            return common::exp10_i256(scale);
99
218k
        }
100
551k
    }
_ZN5doris12StringParser20get_scale_multiplierIiEET_i
Line
Count
Source
87
116k
    static T get_scale_multiplier(int scale) {
88
116k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
116k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
116k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
116k
        if constexpr (std::is_same_v<T, int32_t>) {
92
116k
            return common::exp10_i32(scale);
93
116k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
116k
            return common::exp10_i64(scale);
95
116k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
116k
            return common::exp10_i128(scale);
97
116k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
116k
            return common::exp10_i256(scale);
99
116k
        }
100
116k
    }
_ZN5doris12StringParser20get_scale_multiplierIlEET_i
Line
Count
Source
87
108k
    static T get_scale_multiplier(int scale) {
88
108k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
108k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
108k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
108k
        if constexpr (std::is_same_v<T, int32_t>) {
92
108k
            return common::exp10_i32(scale);
93
108k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
108k
            return common::exp10_i64(scale);
95
108k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
108k
            return common::exp10_i128(scale);
97
108k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
108k
            return common::exp10_i256(scale);
99
108k
        }
100
108k
    }
_ZN5doris12StringParser20get_scale_multiplierInEET_i
Line
Count
Source
87
108k
    static T get_scale_multiplier(int scale) {
88
108k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
108k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
108k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
108k
        if constexpr (std::is_same_v<T, int32_t>) {
92
108k
            return common::exp10_i32(scale);
93
108k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
108k
            return common::exp10_i64(scale);
95
108k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
108k
            return common::exp10_i128(scale);
97
108k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
108k
            return common::exp10_i256(scale);
99
108k
        }
100
108k
    }
_ZN5doris12StringParser20get_scale_multiplierIN4wide7integerILm256EiEEEET_i
Line
Count
Source
87
218k
    static T get_scale_multiplier(int scale) {
88
218k
        static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
89
218k
                              std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
90
218k
                      "You can only instantiate as int32_t, int64_t, __int128.");
91
218k
        if constexpr (std::is_same_v<T, int32_t>) {
92
218k
            return common::exp10_i32(scale);
93
218k
        } else if constexpr (std::is_same_v<T, int64_t>) {
94
218k
            return common::exp10_i64(scale);
95
218k
        } else if constexpr (std::is_same_v<T, __int128>) {
96
218k
            return common::exp10_i128(scale);
97
218k
        } else if constexpr (std::is_same_v<T, wide::Int256>) {
98
218k
            return common::exp10_i256(scale);
99
218k
        }
100
218k
    }
101
102
    // This is considerably faster than glibc's implementation (25x).
103
    // In the case of overflow, the max/min value for the data type will be returned.
104
    // Assumes s represents a decimal number.
105
    template <typename T>
106
381k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
381k
        T ans = string_to_int_internal<T>(s, len, result);
108
381k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
320k
            return ans;
110
320k
        }
111
112
60.7k
        int i = skip_leading_whitespace(s, len);
113
60.7k
        return string_to_int_internal<T>(s + i, len - i, result);
114
381k
    }
_ZN5doris12StringParser13string_to_intIlEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
85.4k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
85.4k
        T ans = string_to_int_internal<T>(s, len, result);
108
85.4k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
80.3k
            return ans;
110
80.3k
        }
111
112
5.12k
        int i = skip_leading_whitespace(s, len);
113
5.12k
        return string_to_int_internal<T>(s + i, len - i, result);
114
85.4k
    }
_ZN5doris12StringParser13string_to_intIaEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
99.2k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
99.2k
        T ans = string_to_int_internal<T>(s, len, result);
108
99.2k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
69.4k
            return ans;
110
69.4k
        }
111
112
29.8k
        int i = skip_leading_whitespace(s, len);
113
29.8k
        return string_to_int_internal<T>(s + i, len - i, result);
114
99.2k
    }
_ZN5doris12StringParser13string_to_intIsEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
70.0k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
70.0k
        T ans = string_to_int_internal<T>(s, len, result);
108
70.0k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
61.1k
            return ans;
110
61.1k
        }
111
112
8.99k
        int i = skip_leading_whitespace(s, len);
113
8.99k
        return string_to_int_internal<T>(s + i, len - i, result);
114
70.0k
    }
_ZN5doris12StringParser13string_to_intIiEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
64.6k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
64.6k
        T ans = string_to_int_internal<T>(s, len, result);
108
64.6k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
57.7k
            return ans;
110
57.7k
        }
111
112
6.91k
        int i = skip_leading_whitespace(s, len);
113
6.91k
        return string_to_int_internal<T>(s + i, len - i, result);
114
64.6k
    }
_ZN5doris12StringParser13string_to_intInEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
47.9k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
47.9k
        T ans = string_to_int_internal<T>(s, len, result);
108
47.9k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
46.1k
            return ans;
110
46.1k
        }
111
112
1.80k
        int i = skip_leading_whitespace(s, len);
113
1.80k
        return string_to_int_internal<T>(s + i, len - i, result);
114
47.9k
    }
_ZN5doris12StringParser13string_to_intIhEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
11.9k
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
11.9k
        T ans = string_to_int_internal<T>(s, len, result);
108
11.9k
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
3.89k
            return ans;
110
3.89k
        }
111
112
8.03k
        int i = skip_leading_whitespace(s, len);
113
8.03k
        return string_to_int_internal<T>(s + i, len - i, result);
114
11.9k
    }
_ZN5doris12StringParser13string_to_intIjEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
690
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
690
        T ans = string_to_int_internal<T>(s, len, result);
108
690
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
688
            return ans;
110
688
        }
111
112
2
        int i = skip_leading_whitespace(s, len);
113
2
        return string_to_int_internal<T>(s + i, len - i, result);
114
690
    }
_ZN5doris12StringParser13string_to_intImEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
646
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
646
        T ans = string_to_int_internal<T>(s, len, result);
108
646
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
644
            return ans;
110
644
        }
111
112
2
        int i = skip_leading_whitespace(s, len);
113
2
        return string_to_int_internal<T>(s + i, len - i, result);
114
646
    }
_ZN5doris12StringParser13string_to_intIoEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
4
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
4
        T ans = string_to_int_internal<T>(s, len, result);
108
4
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
4
            return ans;
110
4
        }
111
112
0
        int i = skip_leading_whitespace(s, len);
113
0
        return string_to_int_internal<T>(s + i, len - i, result);
114
4
    }
_ZN5doris12StringParser13string_to_intIN4wide7integerILm256EiEEEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
4
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
4
        T ans = string_to_int_internal<T>(s, len, result);
108
4
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
4
            return ans;
110
4
        }
111
112
0
        int i = skip_leading_whitespace(s, len);
113
0
        return string_to_int_internal<T>(s + i, len - i, result);
114
4
    }
_ZN5doris12StringParser13string_to_intItEET_PKcmPNS0_11ParseResultE
Line
Count
Source
106
594
    static inline T string_to_int(const char* __restrict s, size_t len, ParseResult* result) {
107
594
        T ans = string_to_int_internal<T>(s, len, result);
108
594
        if (LIKELY(*result == PARSE_SUCCESS)) {
109
592
            return ans;
110
592
        }
111
112
2
        int i = skip_leading_whitespace(s, len);
113
2
        return string_to_int_internal<T>(s + i, len - i, result);
114
594
    }
115
116
    // This is considerably faster than glibc's implementation.
117
    // In the case of overflow, the max/min value for the data type will be returned.
118
    // Assumes s represents a decimal number.
119
    template <typename T>
120
1.57k
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
1.57k
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
1.57k
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
276
            return ans;
124
276
        }
125
126
1.29k
        int i = skip_leading_whitespace(s, len);
127
1.29k
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
1.57k
    }
_ZN5doris12StringParser22string_to_unsigned_intImEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
541
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
541
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
541
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
213
            return ans;
124
213
        }
125
126
328
        int i = skip_leading_whitespace(s, len);
127
328
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
541
    }
_ZN5doris12StringParser22string_to_unsigned_intIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
343
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
343
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
21
            return ans;
124
21
        }
125
126
322
        int i = skip_leading_whitespace(s, len);
127
322
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
343
    }
_ZN5doris12StringParser22string_to_unsigned_intItEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
343
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
343
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
21
            return ans;
124
21
        }
125
126
322
        int i = skip_leading_whitespace(s, len);
127
322
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
343
    }
_ZN5doris12StringParser22string_to_unsigned_intIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
120
343
    static inline T string_to_unsigned_int(const char* __restrict s, int len, ParseResult* result) {
121
343
        T ans = string_to_unsigned_int_internal<T>(s, len, result);
122
343
        if (LIKELY(*result == PARSE_SUCCESS)) {
123
21
            return ans;
124
21
        }
125
126
322
        int i = skip_leading_whitespace(s, len);
127
322
        return string_to_unsigned_int_internal<T>(s + i, len - i, result);
128
343
    }
129
130
    // Convert a string s representing a number in given base into a decimal number.
131
    template <typename T>
132
    static inline T string_to_int(const char* __restrict s, int64_t len, int base,
133
27.8k
                                  ParseResult* result) {
134
27.8k
        T ans = string_to_int_internal<T>(s, len, base, result);
135
27.8k
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
2.06k
            return ans;
137
2.06k
        }
138
139
25.7k
        int i = skip_leading_whitespace(s, len);
140
25.7k
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
27.8k
    }
_ZN5doris12StringParser13string_to_intIaEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
26.4k
                                  ParseResult* result) {
134
26.4k
        T ans = string_to_int_internal<T>(s, len, base, result);
135
26.4k
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
1.91k
            return ans;
137
1.91k
        }
138
139
24.5k
        int i = skip_leading_whitespace(s, len);
140
24.5k
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
26.4k
    }
_ZN5doris12StringParser13string_to_intIsEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
490
                                  ParseResult* result) {
134
490
        T ans = string_to_int_internal<T>(s, len, base, result);
135
490
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
56
            return ans;
137
56
        }
138
139
434
        int i = skip_leading_whitespace(s, len);
140
434
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
490
    }
_ZN5doris12StringParser13string_to_intIiEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
441
                                  ParseResult* result) {
134
441
        T ans = string_to_int_internal<T>(s, len, base, result);
135
441
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
49
            return ans;
137
49
        }
138
139
392
        int i = skip_leading_whitespace(s, len);
140
392
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
441
    }
_ZN5doris12StringParser13string_to_intIlEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
441
                                  ParseResult* result) {
134
441
        T ans = string_to_int_internal<T>(s, len, base, result);
135
441
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
49
            return ans;
137
49
        }
138
139
392
        int i = skip_leading_whitespace(s, len);
140
392
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
441
    }
_ZN5doris12StringParser13string_to_intImEET_PKcliPNS0_11ParseResultE
Line
Count
Source
133
1
                                  ParseResult* result) {
134
1
        T ans = string_to_int_internal<T>(s, len, base, result);
135
1
        if (LIKELY(*result == PARSE_SUCCESS)) {
136
1
            return ans;
137
1
        }
138
139
0
        int i = skip_leading_whitespace(s, len);
140
0
        return string_to_int_internal<T>(s + i, len - i, base, result);
141
1
    }
142
143
    template <typename T>
144
137k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
145
137k
        return string_to_float_internal<T>(s, len, result);
146
137k
    }
_ZN5doris12StringParser15string_to_floatIdEET_PKcmPNS0_11ParseResultE
Line
Count
Source
144
73.8k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
145
73.8k
        return string_to_float_internal<T>(s, len, result);
146
73.8k
    }
_ZN5doris12StringParser15string_to_floatIfEET_PKcmPNS0_11ParseResultE
Line
Count
Source
144
63.9k
    static inline T string_to_float(const char* __restrict s, size_t len, ParseResult* result) {
145
63.9k
        return string_to_float_internal<T>(s, len, result);
146
63.9k
    }
147
148
    // Parses a string for 'true' or 'false', case insensitive.
149
8.32k
    static inline bool string_to_bool(const char* __restrict s, int len, ParseResult* result) {
150
8.32k
        bool ans = string_to_bool_internal(s, len, result);
151
8.32k
        if (LIKELY(*result == PARSE_SUCCESS)) {
152
7.28k
            return ans;
153
7.28k
        }
154
155
1.04k
        int i = skip_leading_whitespace(s, len);
156
1.04k
        return string_to_bool_internal(s + i, len - i, result);
157
8.32k
    }
158
159
    template <PrimitiveType P, typename T = PrimitiveTypeTraits<P>::CppType::NativeType,
160
              typename DecimalType = PrimitiveTypeTraits<P>::ColumnType::value_type>
161
    static inline T string_to_decimal(const char* __restrict s, int len, int type_precision,
162
                                      int type_scale, ParseResult* result);
163
164
    template <typename T>
165
    static Status split_string_to_map(const std::string& base, const T element_separator,
166
                                      const T key_value_separator,
167
                                      std::map<std::string, std::string>* result) {
168
        int key_pos = 0;
169
        int key_end;
170
        int val_pos;
171
        int val_end;
172
173
        while ((key_end = base.find(key_value_separator, key_pos)) != std::string::npos) {
174
            if ((val_pos = base.find_first_not_of(key_value_separator, key_end)) ==
175
                std::string::npos) {
176
                break;
177
            }
178
            if ((val_end = base.find(element_separator, val_pos)) == std::string::npos) {
179
                val_end = base.size();
180
            }
181
            result->insert(std::make_pair(base.substr(key_pos, key_end - key_pos),
182
                                          base.substr(val_pos, val_end - val_pos)));
183
            key_pos = val_end;
184
            if (key_pos != std::string::npos) {
185
                ++key_pos;
186
            }
187
        }
188
189
        return Status::OK();
190
    }
191
192
private:
193
    // This is considerably faster than glibc's implementation.
194
    // In the case of overflow, the max/min value for the data type will be returned.
195
    // Assumes s represents a decimal number.
196
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
197
    template <typename T>
198
    static inline T string_to_int_internal(const char* __restrict s, int len, ParseResult* result);
199
200
    // This is considerably faster than glibc's implementation.
201
    // In the case of overflow, the max/min value for the data type will be returned.
202
    // Assumes s represents a decimal number.
203
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
204
    template <typename T>
205
    static inline T string_to_unsigned_int_internal(const char* __restrict s, int len,
206
                                                    ParseResult* result);
207
208
    // Convert a string s representing a number in given base into a decimal number.
209
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
210
    template <typename T>
211
    static inline T string_to_int_internal(const char* __restrict s, int64_t len, int base,
212
                                           ParseResult* result);
213
214
    // Converts an ascii string to an integer of type T assuming it cannot overflow
215
    // and the number is positive.
216
    // Leading whitespace is not allowed. Trailing whitespace will be skipped.
217
    template <typename T>
218
    static inline T string_to_int_no_overflow(const char* __restrict s, int len,
219
                                              ParseResult* result);
220
221
    // This is considerably faster than glibc's implementation (>100x why???)
222
    // No special case handling needs to be done for overflows, the floating point spec
223
    // already does it and will cap the values to -inf/inf
224
    // To avoid inaccurate conversions this function falls back to strtod for
225
    // scientific notation.
226
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
227
    // TODO: Investigate using intrinsics to speed up the slow strtod path.
228
    template <typename T>
229
    static inline T string_to_float_internal(const char* __restrict s, int len,
230
                                             ParseResult* result);
231
232
    // parses a string for 'true' or 'false', case insensitive
233
    // Return PARSE_FAILURE on leading whitespace. Trailing whitespace is allowed.
234
    static inline bool string_to_bool_internal(const char* __restrict s, int len,
235
                                               ParseResult* result);
236
237
    // Returns true if s only contains whitespace.
238
39.7k
    static inline bool is_all_whitespace(const char* __restrict s, int len) {
239
127k
        for (int i = 0; i < len; ++i) {
240
90.0k
            if (!LIKELY(is_whitespace(s[i]))) {
241
1.97k
                return false;
242
1.97k
            }
243
90.0k
        }
244
37.7k
        return true;
245
39.7k
    }
246
247
    // For strings like "3.0", "3.123", and "3.", can parse them as 3.
248
1.69k
    static inline bool is_float_suffix(const char* __restrict s, int len) {
249
1.69k
        return (s[0] == '.' && is_all_digit(s + 1, len - 1));
250
1.69k
    }
251
252
875
    static inline bool is_all_digit(const char* __restrict s, int len) {
253
1.89k
        for (int i = 0; i < len; ++i) {
254
1.03k
            if (!LIKELY(s[i] >= '0' && s[i] <= '9')) {
255
17
                return false;
256
17
            }
257
1.03k
        }
258
858
        return true;
259
875
    }
260
261
    // Returns the position of the first non-whitespace character in s.
262
88.8k
    static inline int skip_leading_whitespace(const char* __restrict s, int len) {
263
88.8k
        int i = 0;
264
255k
        while (i < len && is_whitespace(s[i])) {
265
166k
            ++i;
266
166k
        }
267
88.8k
        return i;
268
88.8k
    }
269
270
    // Our own definition of "isspace" that optimize on the ' ' branch.
271
3.98M
    static inline bool is_whitespace(const char& c) {
272
3.98M
        return LIKELY(c == ' ') ||
273
3.98M
               UNLIKELY(c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r');
274
3.98M
    }
275
276
}; // end of class StringParser
277
278
template <typename T>
279
580k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
580k
    if (UNLIKELY(len <= 0)) {
281
2.21k
        *result = PARSE_FAILURE;
282
2.21k
        return 0;
283
2.21k
    }
284
285
578k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
578k
    UnsignedT val = 0;
287
578k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
578k
    bool negative = false;
289
578k
    int i = 0;
290
578k
    switch (*s) {
291
111k
    case '-':
292
111k
        negative = true;
293
111k
        max_val += 1;
294
111k
        [[fallthrough]];
295
185k
    case '+':
296
185k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
185k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
578k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
578k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
396k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
396k
        return static_cast<T>(negative ? -val : val);
308
396k
    }
309
310
181k
    const T max_div_10 = max_val / 10;
311
181k
    const T max_mod_10 = max_val % 10;
312
313
181k
    int first = i;
314
1.70M
    for (; i < len; ++i) {
315
1.62M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
1.56M
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
1.56M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
46.3k
                *result = PARSE_OVERFLOW;
320
46.3k
                return negative ? -max_val : max_val;
321
46.3k
            }
322
1.52M
            val = val * 10 + digit;
323
1.52M
        } else {
324
59.5k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
59.5k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
44.9k
                *result = PARSE_FAILURE;
329
44.9k
                return 0;
330
44.9k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
14.6k
            *result = PARSE_SUCCESS;
333
14.6k
            return static_cast<T>(negative ? -val : val);
334
59.5k
        }
335
1.62M
    }
336
76.0k
    *result = PARSE_SUCCESS;
337
76.0k
    return static_cast<T>(negative ? -val : val);
338
181k
}
_ZN5doris12StringParser22string_to_int_internalIaEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
267k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
267k
    if (UNLIKELY(len <= 0)) {
281
206
        *result = PARSE_FAILURE;
282
206
        return 0;
283
206
    }
284
285
267k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
267k
    UnsignedT val = 0;
287
267k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
267k
    bool negative = false;
289
267k
    int i = 0;
290
267k
    switch (*s) {
291
28.9k
    case '-':
292
28.9k
        negative = true;
293
28.9k
        max_val += 1;
294
28.9k
        [[fallthrough]];
295
100k
    case '+':
296
100k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
100k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
267k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
267k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
201k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
201k
        return static_cast<T>(negative ? -val : val);
308
201k
    }
309
310
65.6k
    const T max_div_10 = max_val / 10;
311
65.6k
    const T max_mod_10 = max_val % 10;
312
313
65.6k
    int first = i;
314
159k
    for (; i < len; ++i) {
315
152k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
115k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
115k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
21.0k
                *result = PARSE_OVERFLOW;
320
21.0k
                return negative ? -max_val : max_val;
321
21.0k
            }
322
93.9k
            val = val * 10 + digit;
323
93.9k
        } else {
324
37.1k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
37.1k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
25.0k
                *result = PARSE_FAILURE;
329
25.0k
                return 0;
330
25.0k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
12.0k
            *result = PARSE_SUCCESS;
333
12.0k
            return static_cast<T>(negative ? -val : val);
334
37.1k
        }
335
152k
    }
336
7.48k
    *result = PARSE_SUCCESS;
337
7.48k
    return static_cast<T>(negative ? -val : val);
338
65.6k
}
_ZN5doris12StringParser22string_to_int_internalIlEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
90.5k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
90.5k
    if (UNLIKELY(len <= 0)) {
281
4
        *result = PARSE_FAILURE;
282
4
        return 0;
283
4
    }
284
285
90.5k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
90.5k
    UnsignedT val = 0;
287
90.5k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
90.5k
    bool negative = false;
289
90.5k
    int i = 0;
290
90.5k
    switch (*s) {
291
52.7k
    case '-':
292
52.7k
        negative = true;
293
52.7k
        max_val += 1;
294
52.7k
        [[fallthrough]];
295
53.3k
    case '+':
296
53.3k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
53.3k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
90.5k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
90.5k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
35.0k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
35.0k
        return static_cast<T>(negative ? -val : val);
308
35.0k
    }
309
310
55.5k
    const T max_div_10 = max_val / 10;
311
55.5k
    const T max_mod_10 = max_val % 10;
312
313
55.5k
    int first = i;
314
1.08M
    for (; i < len; ++i) {
315
1.03M
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
1.03M
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
1.03M
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
5.02k
                *result = PARSE_OVERFLOW;
320
5.02k
                return negative ? -max_val : max_val;
321
5.02k
            }
322
1.02M
            val = val * 10 + digit;
323
1.02M
        } else {
324
1.86k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
1.86k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
1.36k
                *result = PARSE_FAILURE;
329
1.36k
                return 0;
330
1.36k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
500
            *result = PARSE_SUCCESS;
333
500
            return static_cast<T>(negative ? -val : val);
334
1.86k
        }
335
1.03M
    }
336
48.6k
    *result = PARSE_SUCCESS;
337
48.6k
    return static_cast<T>(negative ? -val : val);
338
55.5k
}
_ZN5doris12StringParser22string_to_int_internalIsEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
79.0k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
79.0k
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
79.0k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
79.0k
    UnsignedT val = 0;
287
79.0k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
79.0k
    bool negative = false;
289
79.0k
    int i = 0;
290
79.0k
    switch (*s) {
291
13.7k
    case '-':
292
13.7k
        negative = true;
293
13.7k
        max_val += 1;
294
13.7k
        [[fallthrough]];
295
14.2k
    case '+':
296
14.2k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
14.2k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
79.0k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
79.0k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
53.6k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
53.6k
        return static_cast<T>(negative ? -val : val);
308
53.6k
    }
309
310
25.4k
    const T max_div_10 = max_val / 10;
311
25.4k
    const T max_mod_10 = max_val % 10;
312
313
25.4k
    int first = i;
314
131k
    for (; i < len; ++i) {
315
121k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
119k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
119k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
13.2k
                *result = PARSE_OVERFLOW;
320
13.2k
                return negative ? -max_val : max_val;
321
13.2k
            }
322
105k
            val = val * 10 + digit;
323
105k
        } else {
324
2.83k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
2.83k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
1.76k
                *result = PARSE_FAILURE;
329
1.76k
                return 0;
330
1.76k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
1.06k
            *result = PARSE_SUCCESS;
333
1.06k
            return static_cast<T>(negative ? -val : val);
334
2.83k
        }
335
121k
    }
336
9.42k
    *result = PARSE_SUCCESS;
337
9.42k
    return static_cast<T>(negative ? -val : val);
338
25.4k
}
_ZN5doris12StringParser22string_to_int_internalIiEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
71.5k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
71.5k
    if (UNLIKELY(len <= 0)) {
281
1.96k
        *result = PARSE_FAILURE;
282
1.96k
        return 0;
283
1.96k
    }
284
285
69.5k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
69.5k
    UnsignedT val = 0;
287
69.5k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
69.5k
    bool negative = false;
289
69.5k
    int i = 0;
290
69.5k
    switch (*s) {
291
10.9k
    case '-':
292
10.9k
        negative = true;
293
10.9k
        max_val += 1;
294
10.9k
        [[fallthrough]];
295
11.5k
    case '+':
296
11.5k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
11.5k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
69.5k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
69.5k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
55.4k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
55.4k
        return static_cast<T>(negative ? -val : val);
308
55.4k
    }
309
310
14.1k
    const T max_div_10 = max_val / 10;
311
14.1k
    const T max_mod_10 = max_val % 10;
312
313
14.1k
    int first = i;
314
134k
    for (; i < len; ++i) {
315
127k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
125k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
125k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
5.61k
                *result = PARSE_OVERFLOW;
320
5.61k
                return negative ? -max_val : max_val;
321
5.61k
            }
322
119k
            val = val * 10 + digit;
323
119k
        } else {
324
2.22k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
2.22k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
1.65k
                *result = PARSE_FAILURE;
329
1.65k
                return 0;
330
1.65k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
570
            *result = PARSE_SUCCESS;
333
570
            return static_cast<T>(negative ? -val : val);
334
2.22k
        }
335
127k
    }
336
6.26k
    *result = PARSE_SUCCESS;
337
6.26k
    return static_cast<T>(negative ? -val : val);
338
14.1k
}
_ZN5doris12StringParser22string_to_int_internalInEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
49.8k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
49.8k
    if (UNLIKELY(len <= 0)) {
281
36
        *result = PARSE_FAILURE;
282
36
        return 0;
283
36
    }
284
285
49.7k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
49.7k
    UnsignedT val = 0;
287
49.7k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
49.7k
    bool negative = false;
289
49.7k
    int i = 0;
290
49.7k
    switch (*s) {
291
3.95k
    case '-':
292
3.95k
        negative = true;
293
3.95k
        max_val += 1;
294
3.95k
        [[fallthrough]];
295
4.43k
    case '+':
296
4.43k
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
4.43k
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
49.7k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
49.7k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
44.8k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
44.8k
        return static_cast<T>(negative ? -val : val);
308
44.8k
    }
309
310
4.88k
    const T max_div_10 = max_val / 10;
311
4.88k
    const T max_mod_10 = max_val % 10;
312
313
4.88k
    int first = i;
314
177k
    for (; i < len; ++i) {
315
173k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
172k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
172k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
6
                *result = PARSE_OVERFLOW;
320
6
                return negative ? -max_val : max_val;
321
6
            }
322
172k
            val = val * 10 + digit;
323
172k
        } else {
324
938
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
938
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
522
                *result = PARSE_FAILURE;
329
522
                return 0;
330
522
            }
331
            // Returning here is slightly faster than breaking the loop.
332
416
            *result = PARSE_SUCCESS;
333
416
            return static_cast<T>(negative ? -val : val);
334
938
        }
335
173k
    }
336
3.94k
    *result = PARSE_SUCCESS;
337
3.94k
    return static_cast<T>(negative ? -val : val);
338
4.88k
}
_ZN5doris12StringParser22string_to_int_internalIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
19.9k
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
19.9k
    if (UNLIKELY(len <= 0)) {
281
4
        *result = PARSE_FAILURE;
282
4
        return 0;
283
4
    }
284
285
19.9k
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
19.9k
    UnsignedT val = 0;
287
19.9k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
19.9k
    bool negative = false;
289
19.9k
    int i = 0;
290
19.9k
    switch (*s) {
291
862
    case '-':
292
862
        negative = true;
293
862
        max_val += 1;
294
862
        [[fallthrough]];
295
862
    case '+':
296
862
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
862
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
19.9k
    }
303
304
    // This is the fast path where the string cannot overflow.
305
19.9k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
3.90k
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
3.90k
        return static_cast<T>(negative ? -val : val);
308
3.90k
    }
309
310
16.0k
    const T max_div_10 = max_val / 10;
311
16.0k
    const T max_mod_10 = max_val % 10;
312
313
16.0k
    int first = i;
314
18.2k
    for (; i < len; ++i) {
315
18.2k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
3.69k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
3.69k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
1.46k
                *result = PARSE_OVERFLOW;
320
1.46k
                return negative ? -max_val : max_val;
321
1.46k
            }
322
2.23k
            val = val * 10 + digit;
323
14.5k
        } else {
324
14.5k
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
14.5k
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
14.5k
                *result = PARSE_FAILURE;
329
14.5k
                return 0;
330
14.5k
            }
331
            // Returning here is slightly faster than breaking the loop.
332
1
            *result = PARSE_SUCCESS;
333
1
            return static_cast<T>(negative ? -val : val);
334
14.5k
        }
335
18.2k
    }
336
12
    *result = PARSE_SUCCESS;
337
12
    return static_cast<T>(negative ? -val : val);
338
16.0k
}
_ZN5doris12StringParser22string_to_int_internalIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
692
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
692
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
692
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
692
    UnsignedT val = 0;
287
692
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
692
    bool negative = false;
289
692
    int i = 0;
290
692
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
692
    }
303
304
    // This is the fast path where the string cannot overflow.
305
692
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
580
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
580
        return static_cast<T>(negative ? -val : val);
308
580
    }
309
310
112
    const T max_div_10 = max_val / 10;
311
112
    const T max_mod_10 = max_val % 10;
312
313
112
    int first = i;
314
1.23k
    for (; i < len; ++i) {
315
1.12k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
1.12k
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
1.12k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
1.12k
            val = val * 10 + digit;
323
1.12k
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
1.12k
    }
336
112
    *result = PARSE_SUCCESS;
337
112
    return static_cast<T>(negative ? -val : val);
338
112
}
_ZN5doris12StringParser22string_to_int_internalImEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
648
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
648
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
648
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
648
    UnsignedT val = 0;
287
648
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
648
    bool negative = false;
289
648
    int i = 0;
290
648
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
648
    }
303
304
    // This is the fast path where the string cannot overflow.
305
648
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
616
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
616
        return static_cast<T>(negative ? -val : val);
308
616
    }
309
310
32
    const T max_div_10 = max_val / 10;
311
32
    const T max_mod_10 = max_val % 10;
312
313
32
    int first = i;
314
672
    for (; i < len; ++i) {
315
640
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
640
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
640
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
640
            val = val * 10 + digit;
323
640
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
640
    }
336
32
    *result = PARSE_SUCCESS;
337
32
    return static_cast<T>(negative ? -val : val);
338
32
}
_ZN5doris12StringParser22string_to_int_internalIoEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
4
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
4
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
4
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
4
    UnsignedT val = 0;
287
4
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
4
    bool negative = false;
289
4
    int i = 0;
290
4
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
4
    }
303
304
    // This is the fast path where the string cannot overflow.
305
4
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
0
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
0
        return static_cast<T>(negative ? -val : val);
308
0
    }
309
310
4
    const T max_div_10 = max_val / 10;
311
4
    const T max_mod_10 = max_val % 10;
312
313
4
    int first = i;
314
84
    for (; i < len; ++i) {
315
80
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
80
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
80
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
80
            val = val * 10 + digit;
323
80
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
80
    }
336
4
    *result = PARSE_SUCCESS;
337
4
    return static_cast<T>(negative ? -val : val);
338
4
}
_ZN5doris12StringParser22string_to_int_internalIN4wide7integerILm256EiEEEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
4
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
4
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
4
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
4
    UnsignedT val = 0;
287
4
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
4
    bool negative = false;
289
4
    int i = 0;
290
4
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
4
    }
303
304
    // This is the fast path where the string cannot overflow.
305
4
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
4
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
4
        return static_cast<T>(negative ? -val : val);
308
4
    }
309
310
0
    const T max_div_10 = max_val / 10;
311
0
    const T max_mod_10 = max_val % 10;
312
313
0
    int first = i;
314
0
    for (; i < len; ++i) {
315
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
0
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
0
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
0
            val = val * 10 + digit;
323
0
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
0
    }
336
0
    *result = PARSE_SUCCESS;
337
0
    return static_cast<T>(negative ? -val : val);
338
0
}
_ZN5doris12StringParser22string_to_int_internalItEET_PKciPNS0_11ParseResultE
Line
Count
Source
279
596
T StringParser::string_to_int_internal(const char* __restrict s, int len, ParseResult* result) {
280
596
    if (UNLIKELY(len <= 0)) {
281
0
        *result = PARSE_FAILURE;
282
0
        return 0;
283
0
    }
284
285
596
    typedef typename std::make_unsigned<T>::type UnsignedT;
286
596
    UnsignedT val = 0;
287
596
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
288
596
    bool negative = false;
289
596
    int i = 0;
290
596
    switch (*s) {
291
0
    case '-':
292
0
        negative = true;
293
0
        max_val += 1;
294
0
        [[fallthrough]];
295
0
    case '+':
296
0
        ++i;
297
        // only one '+'/'-' char, so could return failure directly
298
0
        if (UNLIKELY(len == 1)) {
299
0
            *result = PARSE_FAILURE;
300
0
            return 0;
301
0
        }
302
596
    }
303
304
    // This is the fast path where the string cannot overflow.
305
596
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<T>())) {
306
532
        val = string_to_int_no_overflow<UnsignedT>(s + i, len - i, result);
307
532
        return static_cast<T>(negative ? -val : val);
308
532
    }
309
310
64
    const T max_div_10 = max_val / 10;
311
64
    const T max_mod_10 = max_val % 10;
312
313
64
    int first = i;
314
384
    for (; i < len; ++i) {
315
320
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
316
320
            T digit = s[i] - '0';
317
            // This is a tricky check to see if adding this digit will cause an overflow.
318
320
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
319
0
                *result = PARSE_OVERFLOW;
320
0
                return negative ? -max_val : max_val;
321
0
            }
322
320
            val = val * 10 + digit;
323
320
        } else {
324
0
            if ((UNLIKELY(i == first || (!is_all_whitespace(s + i, len - i) &&
325
0
                                         !is_float_suffix(s + i, len - i))))) {
326
                // Reject the string because either the first char was not a digit,
327
                // or the remaining chars are not all whitespace
328
0
                *result = PARSE_FAILURE;
329
0
                return 0;
330
0
            }
331
            // Returning here is slightly faster than breaking the loop.
332
0
            *result = PARSE_SUCCESS;
333
0
            return static_cast<T>(negative ? -val : val);
334
0
        }
335
320
    }
336
64
    *result = PARSE_SUCCESS;
337
64
    return static_cast<T>(negative ? -val : val);
338
64
}
339
340
template <typename T>
341
T StringParser::string_to_unsigned_int_internal(const char* __restrict s, int len,
342
2.86k
                                                ParseResult* result) {
343
2.86k
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
2.86k
    T val = 0;
349
2.86k
    T max_val = std::numeric_limits<T>::max();
350
2.86k
    int i = 0;
351
352
2.86k
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
2.86k
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
1.08k
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
1.08k
        return val;
357
1.08k
    }
358
359
1.78k
    const T max_div_10 = max_val / 10;
360
1.78k
    const T max_mod_10 = max_val % 10;
361
362
1.78k
    int first = i;
363
6.54k
    for (; i < len; ++i) {
364
6.49k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
4.99k
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
4.99k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
224
                *result = PARSE_OVERFLOW;
369
224
                return max_val;
370
224
            }
371
4.76k
            val = val * 10 + digit;
372
4.76k
        } else {
373
1.50k
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
1.13k
                *result = PARSE_FAILURE;
377
1.13k
                return 0;
378
1.13k
            }
379
            // Returning here is slightly faster than breaking the loop.
380
378
            *result = PARSE_SUCCESS;
381
378
            return val;
382
1.50k
        }
383
6.49k
    }
384
49
    *result = PARSE_SUCCESS;
385
49
    return val;
386
1.78k
}
_ZN5doris12StringParser31string_to_unsigned_int_internalImEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
869
                                                ParseResult* result) {
343
869
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
869
    T val = 0;
349
869
    T max_val = std::numeric_limits<T>::max();
350
869
    int i = 0;
351
352
869
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
869
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
644
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
644
        return val;
357
644
    }
358
359
225
    const T max_div_10 = max_val / 10;
360
225
    const T max_mod_10 = max_val % 10;
361
362
225
    int first = i;
363
2.26k
    for (; i < len; ++i) {
364
2.26k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
2.10k
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
2.10k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
2.04k
            val = val * 10 + digit;
372
2.04k
        } else {
373
162
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
120
                *result = PARSE_FAILURE;
377
120
                return 0;
378
120
            }
379
            // Returning here is slightly faster than breaking the loop.
380
42
            *result = PARSE_SUCCESS;
381
42
            return val;
382
162
        }
383
2.26k
    }
384
7
    *result = PARSE_SUCCESS;
385
7
    return val;
386
225
}
_ZN5doris12StringParser31string_to_unsigned_int_internalIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
665
                                                ParseResult* result) {
343
665
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
665
    T val = 0;
349
665
    T max_val = std::numeric_limits<T>::max();
350
665
    int i = 0;
351
352
665
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
665
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
16
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
16
        return val;
357
16
    }
358
359
649
    const T max_div_10 = max_val / 10;
360
649
    const T max_mod_10 = max_val % 10;
361
362
649
    int first = i;
363
1.20k
    for (; i < len; ++i) {
364
1.18k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
609
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
609
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
553
            val = val * 10 + digit;
372
572
        } else {
373
572
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
446
                *result = PARSE_FAILURE;
377
446
                return 0;
378
446
            }
379
            // Returning here is slightly faster than breaking the loop.
380
126
            *result = PARSE_SUCCESS;
381
126
            return val;
382
572
        }
383
1.18k
    }
384
21
    *result = PARSE_SUCCESS;
385
21
    return val;
386
649
}
_ZN5doris12StringParser31string_to_unsigned_int_internalItEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
665
                                                ParseResult* result) {
343
665
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
665
    T val = 0;
349
665
    T max_val = std::numeric_limits<T>::max();
350
665
    int i = 0;
351
352
665
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
665
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
31
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
31
        return val;
357
31
    }
358
359
634
    const T max_div_10 = max_val / 10;
360
634
    const T max_mod_10 = max_val % 10;
361
362
634
    int first = i;
363
1.47k
    for (; i < len; ++i) {
364
1.46k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
896
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
896
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
840
            val = val * 10 + digit;
372
840
        } else {
373
564
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
438
                *result = PARSE_FAILURE;
377
438
                return 0;
378
438
            }
379
            // Returning here is slightly faster than breaking the loop.
380
126
            *result = PARSE_SUCCESS;
381
126
            return val;
382
564
        }
383
1.46k
    }
384
14
    *result = PARSE_SUCCESS;
385
14
    return val;
386
634
}
_ZN5doris12StringParser31string_to_unsigned_int_internalIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
342
665
                                                ParseResult* result) {
343
665
    if (UNLIKELY(len <= 0)) {
344
0
        *result = PARSE_FAILURE;
345
0
        return 0;
346
0
    }
347
348
665
    T val = 0;
349
665
    T max_val = std::numeric_limits<T>::max();
350
665
    int i = 0;
351
352
665
    typedef typename std::make_signed<T>::type signedT;
353
    // This is the fast path where the string cannot overflow.
354
665
    if (LIKELY(len - i < vectorized::NumberTraits::max_ascii_len<signedT>())) {
355
392
        val = string_to_int_no_overflow<T>(s + i, len - i, result);
356
392
        return val;
357
392
    }
358
359
273
    const T max_div_10 = max_val / 10;
360
273
    const T max_mod_10 = max_val % 10;
361
362
273
    int first = i;
363
1.60k
    for (; i < len; ++i) {
364
1.59k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
365
1.38k
            T digit = s[i] - '0';
366
            // This is a tricky check to see if adding this digit will cause an overflow.
367
1.38k
            if (UNLIKELY(val > (max_div_10 - (digit > max_mod_10)))) {
368
56
                *result = PARSE_OVERFLOW;
369
56
                return max_val;
370
56
            }
371
1.33k
            val = val * 10 + digit;
372
1.33k
        } else {
373
210
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
374
                // Reject the string because either the first char was not a digit,
375
                // or the remaining chars are not all whitespace
376
126
                *result = PARSE_FAILURE;
377
126
                return 0;
378
126
            }
379
            // Returning here is slightly faster than breaking the loop.
380
84
            *result = PARSE_SUCCESS;
381
84
            return val;
382
210
        }
383
1.59k
    }
384
7
    *result = PARSE_SUCCESS;
385
7
    return val;
386
273
}
387
388
template <typename T>
389
T StringParser::string_to_int_internal(const char* __restrict s, int64_t len, int base,
390
53.6k
                                       ParseResult* result) {
391
53.6k
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
53.6k
    UnsignedT val = 0;
393
53.6k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
53.6k
    bool negative = false;
395
53.6k
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
53.6k
    int i = 0;
400
53.6k
    switch (*s) {
401
14.3k
    case '-':
402
14.3k
        negative = true;
403
14.3k
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
14.3k
        [[fallthrough]];
405
14.6k
    case '+':
406
14.6k
        i = 1;
407
53.6k
    }
408
409
53.6k
    const T max_div_base = max_val / base;
410
53.6k
    const T max_mod_base = max_val % base;
411
412
53.6k
    int first = i;
413
120k
    for (; i < len; ++i) {
414
118k
        T digit;
415
118k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
81.6k
            digit = s[i] - '0';
417
81.6k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
639
            digit = (s[i] - 'a' + 10);
419
36.4k
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
98
            digit = (s[i] - 'A' + 10);
421
36.3k
        } else {
422
36.3k
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
24.0k
                *result = PARSE_FAILURE;
426
24.0k
                return 0;
427
24.0k
            }
428
            // skip trailing whitespace.
429
12.2k
            break;
430
36.3k
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
82.4k
        if (digit >= base) {
434
392
            break;
435
392
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
82.0k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
14.8k
            *result = PARSE_OVERFLOW;
440
14.8k
            return static_cast<T>(negative ? -max_val : max_val);
441
14.8k
        }
442
67.2k
        val = val * base + digit;
443
67.2k
    }
444
14.7k
    *result = PARSE_SUCCESS;
445
14.7k
    return static_cast<T>(negative ? -val : val);
446
53.6k
}
_ZN5doris12StringParser22string_to_int_internalIaEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
51.0k
                                       ParseResult* result) {
391
51.0k
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
51.0k
    UnsignedT val = 0;
393
51.0k
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
51.0k
    bool negative = false;
395
51.0k
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
51.0k
    int i = 0;
400
51.0k
    switch (*s) {
401
13.7k
    case '-':
402
13.7k
        negative = true;
403
13.7k
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
13.7k
        [[fallthrough]];
405
13.8k
    case '+':
406
13.8k
        i = 1;
407
51.0k
    }
408
409
51.0k
    const T max_div_base = max_val / base;
410
51.0k
    const T max_mod_base = max_val % base;
411
412
51.0k
    int first = i;
413
108k
    for (; i < len; ++i) {
414
107k
        T digit;
415
107k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
72.1k
            digit = s[i] - '0';
417
72.1k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
539
            digit = (s[i] - 'a' + 10);
419
34.3k
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
98
            digit = (s[i] - 'A' + 10);
421
34.2k
        } else {
422
34.2k
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
22.8k
                *result = PARSE_FAILURE;
426
22.8k
                return 0;
427
22.8k
            }
428
            // skip trailing whitespace.
429
11.3k
            break;
430
34.2k
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
72.7k
        if (digit >= base) {
434
392
            break;
435
392
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
72.4k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
14.5k
            *result = PARSE_OVERFLOW;
440
14.5k
            return static_cast<T>(negative ? -max_val : max_val);
441
14.5k
        }
442
57.8k
        val = val * base + digit;
443
57.8k
    }
444
13.6k
    *result = PARSE_SUCCESS;
445
13.6k
    return static_cast<T>(negative ? -val : val);
446
51.0k
}
_ZN5doris12StringParser22string_to_int_internalIsEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
924
                                       ParseResult* result) {
391
924
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
924
    UnsignedT val = 0;
393
924
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
924
    bool negative = false;
395
924
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
924
    int i = 0;
400
924
    switch (*s) {
401
203
    case '-':
402
203
        negative = true;
403
203
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
203
        [[fallthrough]];
405
252
    case '+':
406
252
        i = 1;
407
924
    }
408
409
924
    const T max_div_base = max_val / base;
410
924
    const T max_mod_base = max_val % base;
411
412
924
    int first = i;
413
2.59k
    for (; i < len; ++i) {
414
2.54k
        T digit;
415
2.54k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
1.68k
            digit = s[i] - '0';
417
1.68k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
98
            digit = (s[i] - 'a' + 10);
419
756
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
756
        } else {
422
756
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
420
                *result = PARSE_FAILURE;
426
420
                return 0;
427
420
            }
428
            // skip trailing whitespace.
429
336
            break;
430
756
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
1.78k
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
1.78k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
112
            *result = PARSE_OVERFLOW;
440
112
            return static_cast<T>(negative ? -max_val : max_val);
441
112
        }
442
1.67k
        val = val * base + digit;
443
1.67k
    }
444
392
    *result = PARSE_SUCCESS;
445
392
    return static_cast<T>(negative ? -val : val);
446
924
}
_ZN5doris12StringParser22string_to_int_internalIiEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
833
                                       ParseResult* result) {
391
833
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
833
    UnsignedT val = 0;
393
833
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
833
    bool negative = false;
395
833
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
833
    int i = 0;
400
833
    switch (*s) {
401
154
    case '-':
402
154
        negative = true;
403
154
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
154
        [[fallthrough]];
405
252
    case '+':
406
252
        i = 1;
407
833
    }
408
409
833
    const T max_div_base = max_val / base;
410
833
    const T max_mod_base = max_val % base;
411
412
833
    int first = i;
413
3.55k
    for (; i < len; ++i) {
414
3.50k
        T digit;
415
3.50k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
2.83k
            digit = s[i] - '0';
417
2.83k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
0
            digit = (s[i] - 'a' + 10);
419
672
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
672
        } else {
422
672
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
378
                *result = PARSE_FAILURE;
426
378
                return 0;
427
378
            }
428
            // skip trailing whitespace.
429
294
            break;
430
672
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
2.83k
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
2.83k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
112
            *result = PARSE_OVERFLOW;
440
112
            return static_cast<T>(negative ? -max_val : max_val);
441
112
        }
442
2.72k
        val = val * base + digit;
443
2.72k
    }
444
343
    *result = PARSE_SUCCESS;
445
343
    return static_cast<T>(negative ? -val : val);
446
833
}
_ZN5doris12StringParser22string_to_int_internalIlEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
833
                                       ParseResult* result) {
391
833
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
833
    UnsignedT val = 0;
393
833
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
833
    bool negative = false;
395
833
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
833
    int i = 0;
400
833
    switch (*s) {
401
203
    case '-':
402
203
        negative = true;
403
203
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
203
        [[fallthrough]];
405
252
    case '+':
406
252
        i = 1;
407
833
    }
408
409
833
    const T max_div_base = max_val / base;
410
833
    const T max_mod_base = max_val % base;
411
412
833
    int first = i;
413
5.74k
    for (; i < len; ++i) {
414
5.69k
        T digit;
415
5.69k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
5.01k
            digit = s[i] - '0';
417
5.01k
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
0
            digit = (s[i] - 'a' + 10);
419
672
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
672
        } else {
422
672
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
378
                *result = PARSE_FAILURE;
426
378
                return 0;
427
378
            }
428
            // skip trailing whitespace.
429
294
            break;
430
672
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
5.01k
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
5.01k
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
112
            *result = PARSE_OVERFLOW;
440
112
            return static_cast<T>(negative ? -max_val : max_val);
441
112
        }
442
4.90k
        val = val * base + digit;
443
4.90k
    }
444
343
    *result = PARSE_SUCCESS;
445
343
    return static_cast<T>(negative ? -val : val);
446
833
}
_ZN5doris12StringParser22string_to_int_internalImEET_PKcliPNS0_11ParseResultE
Line
Count
Source
390
1
                                       ParseResult* result) {
391
1
    typedef typename std::make_unsigned<T>::type UnsignedT;
392
1
    UnsignedT val = 0;
393
1
    UnsignedT max_val = StringParser::numeric_limits<T>(false);
394
1
    bool negative = false;
395
1
    if (UNLIKELY(len <= 0)) {
396
0
        *result = PARSE_FAILURE;
397
0
        return 0;
398
0
    }
399
1
    int i = 0;
400
1
    switch (*s) {
401
0
    case '-':
402
0
        negative = true;
403
0
        max_val = StringParser::numeric_limits<T>(false) + 1;
404
0
        [[fallthrough]];
405
0
    case '+':
406
0
        i = 1;
407
1
    }
408
409
1
    const T max_div_base = max_val / base;
410
1
    const T max_mod_base = max_val % base;
411
412
1
    int first = i;
413
3
    for (; i < len; ++i) {
414
2
        T digit;
415
2
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
416
0
            digit = s[i] - '0';
417
2
        } else if (s[i] >= 'a' && s[i] <= 'z') {
418
2
            digit = (s[i] - 'a' + 10);
419
2
        } else if (s[i] >= 'A' && s[i] <= 'Z') {
420
0
            digit = (s[i] - 'A' + 10);
421
0
        } else {
422
0
            if ((UNLIKELY(i == first || !is_all_whitespace(s + i, len - i)))) {
423
                // Reject the string because either the first char was not an alpha/digit,
424
                // or the remaining chars are not all whitespace
425
0
                *result = PARSE_FAILURE;
426
0
                return 0;
427
0
            }
428
            // skip trailing whitespace.
429
0
            break;
430
0
        }
431
432
        // Bail, if we encounter a digit that is not available in base.
433
2
        if (digit >= base) {
434
0
            break;
435
0
        }
436
437
        // This is a tricky check to see if adding this digit will cause an overflow.
438
2
        if (UNLIKELY(val > (max_div_base - (digit > max_mod_base)))) {
439
0
            *result = PARSE_OVERFLOW;
440
0
            return static_cast<T>(negative ? -max_val : max_val);
441
0
        }
442
2
        val = val * base + digit;
443
2
    }
444
1
    *result = PARSE_SUCCESS;
445
1
    return static_cast<T>(negative ? -val : val);
446
1
}
447
448
template <typename T>
449
397k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
397k
    T val = 0;
451
397k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
397k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
390k
        val = s[0] - '0';
458
390k
    } else {
459
7.43k
        *result = PARSE_FAILURE;
460
7.43k
        return 0;
461
7.43k
    }
462
619k
    for (int i = 1; i < len; ++i) {
463
233k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
228k
            T digit = s[i] - '0';
465
228k
            val = val * 10 + digit;
466
228k
        } else {
467
4.28k
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
4.28k
                          !is_float_suffix(s + i, len - i)))) {
469
274
                *result = PARSE_FAILURE;
470
274
                return 0;
471
274
            }
472
4.01k
            *result = PARSE_SUCCESS;
473
4.01k
            return val;
474
4.28k
        }
475
233k
    }
476
385k
    *result = PARSE_SUCCESS;
477
385k
    return val;
478
390k
}
_ZN5doris12StringParser25string_to_int_no_overflowIhEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
205k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
205k
    T val = 0;
451
205k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
205k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
205k
        val = s[0] - '0';
458
205k
    } else {
459
514
        *result = PARSE_FAILURE;
460
514
        return 0;
461
514
    }
462
309k
    for (int i = 1; i < len; ++i) {
463
104k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
104k
            T digit = s[i] - '0';
465
104k
            val = val * 10 + digit;
466
104k
        } else {
467
120
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
120
                          !is_float_suffix(s + i, len - i)))) {
469
0
                *result = PARSE_FAILURE;
470
0
                return 0;
471
0
            }
472
120
            *result = PARSE_SUCCESS;
473
120
            return val;
474
120
        }
475
104k
    }
476
205k
    *result = PARSE_SUCCESS;
477
205k
    return val;
478
205k
}
_ZN5doris12StringParser25string_to_int_no_overflowImEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
36.2k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
36.2k
    T val = 0;
451
36.2k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
36.2k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
34.4k
        val = s[0] - '0';
458
34.4k
    } else {
459
1.79k
        *result = PARSE_FAILURE;
460
1.79k
        return 0;
461
1.79k
    }
462
77.0k
    for (int i = 1; i < len; ++i) {
463
43.7k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
42.5k
            T digit = s[i] - '0';
465
42.5k
            val = val * 10 + digit;
466
42.5k
        } else {
467
1.13k
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
1.13k
                          !is_float_suffix(s + i, len - i)))) {
469
66
                *result = PARSE_FAILURE;
470
66
                return 0;
471
66
            }
472
1.06k
            *result = PARSE_SUCCESS;
473
1.06k
            return val;
474
1.13k
        }
475
43.7k
    }
476
33.3k
    *result = PARSE_SUCCESS;
477
33.3k
    return val;
478
34.4k
}
_ZN5doris12StringParser25string_to_int_no_overflowItEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
54.1k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
54.1k
    T val = 0;
451
54.1k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
54.1k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
53.2k
        val = s[0] - '0';
458
53.2k
    } else {
459
903
        *result = PARSE_FAILURE;
460
903
        return 0;
461
903
    }
462
77.9k
    for (int i = 1; i < len; ++i) {
463
25.8k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
24.6k
            T digit = s[i] - '0';
465
24.6k
            val = val * 10 + digit;
466
24.6k
        } else {
467
1.23k
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
1.23k
                          !is_float_suffix(s + i, len - i)))) {
469
52
                *result = PARSE_FAILURE;
470
52
                return 0;
471
52
            }
472
1.18k
            *result = PARSE_SUCCESS;
473
1.18k
            return val;
474
1.23k
        }
475
25.8k
    }
476
52.0k
    *result = PARSE_SUCCESS;
477
52.0k
    return val;
478
53.2k
}
_ZN5doris12StringParser25string_to_int_no_overflowIjEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
56.4k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
56.4k
    T val = 0;
451
56.4k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
56.4k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
53.6k
        val = s[0] - '0';
458
53.6k
    } else {
459
2.81k
        *result = PARSE_FAILURE;
460
2.81k
        return 0;
461
2.81k
    }
462
92.0k
    for (int i = 1; i < len; ++i) {
463
39.4k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
38.4k
            T digit = s[i] - '0';
465
38.4k
            val = val * 10 + digit;
466
38.4k
        } else {
467
1.03k
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
1.03k
                          !is_float_suffix(s + i, len - i)))) {
469
122
                *result = PARSE_FAILURE;
470
122
                return 0;
471
122
            }
472
910
            *result = PARSE_SUCCESS;
473
910
            return val;
474
1.03k
        }
475
39.4k
    }
476
52.6k
    *result = PARSE_SUCCESS;
477
52.6k
    return val;
478
53.6k
}
_ZN5doris12StringParser25string_to_int_no_overflowIoEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
44.8k
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
44.8k
    T val = 0;
451
44.8k
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
44.8k
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
43.4k
        val = s[0] - '0';
458
43.4k
    } else {
459
1.40k
        *result = PARSE_FAILURE;
460
1.40k
        return 0;
461
1.40k
    }
462
62.2k
    for (int i = 1; i < len; ++i) {
463
19.5k
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
18.7k
            T digit = s[i] - '0';
465
18.7k
            val = val * 10 + digit;
466
18.7k
        } else {
467
762
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
762
                          !is_float_suffix(s + i, len - i)))) {
469
34
                *result = PARSE_FAILURE;
470
34
                return 0;
471
34
            }
472
728
            *result = PARSE_SUCCESS;
473
728
            return val;
474
762
        }
475
19.5k
    }
476
42.7k
    *result = PARSE_SUCCESS;
477
42.7k
    return val;
478
43.4k
}
_ZN5doris12StringParser25string_to_int_no_overflowIN4wide7integerILm256EjEEEET_PKciPNS0_11ParseResultE
Line
Count
Source
449
4
T StringParser::string_to_int_no_overflow(const char* __restrict s, int len, ParseResult* result) {
450
4
    T val = 0;
451
4
    if (UNLIKELY(len == 0)) {
452
0
        *result = PARSE_SUCCESS;
453
0
        return val;
454
0
    }
455
    // Factor out the first char for error handling speeds up the loop.
456
4
    if (LIKELY(s[0] >= '0' && s[0] <= '9')) {
457
4
        val = s[0] - '0';
458
4
    } else {
459
0
        *result = PARSE_FAILURE;
460
0
        return 0;
461
0
    }
462
4
    for (int i = 1; i < len; ++i) {
463
0
        if (LIKELY(s[i] >= '0' && s[i] <= '9')) {
464
0
            T digit = s[i] - '0';
465
0
            val = val * 10 + digit;
466
0
        } else {
467
0
            if ((UNLIKELY(!is_all_whitespace(s + i, len - i) &&
468
0
                          !is_float_suffix(s + i, len - i)))) {
469
0
                *result = PARSE_FAILURE;
470
0
                return 0;
471
0
            }
472
0
            *result = PARSE_SUCCESS;
473
0
            return val;
474
0
        }
475
0
    }
476
4
    *result = PARSE_SUCCESS;
477
4
    return val;
478
4
}
479
480
template <typename T>
481
137k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
482
137k
    int i = 0;
483
    // skip leading spaces
484
180k
    for (; i < len; ++i) {
485
180k
        if (!is_whitespace(s[i])) {
486
137k
            break;
487
137k
        }
488
180k
    }
489
490
    // skip back spaces
491
137k
    int j = len - 1;
492
178k
    for (; j >= i; j--) {
493
178k
        if (!is_whitespace(s[j])) {
494
137k
            break;
495
137k
        }
496
178k
    }
497
498
    // skip leading '+', from_chars can handle '-'
499
137k
    if (i < len && s[i] == '+') {
500
5.29k
        i++;
501
5.29k
    }
502
137k
    if (UNLIKELY(i > j)) {
503
3
        *result = PARSE_FAILURE;
504
3
        return 0;
505
3
    }
506
507
    // Use double here to not lose precision while accumulating the result
508
137k
    double val = 0;
509
137k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
510
511
137k
    if (res.ec == std::errc() && res.ptr == s + j + 1) {
512
133k
        if (abs(val) == std::numeric_limits<T>::infinity()) {
513
898
            auto contain_inf = false;
514
1.29k
            for (int k = i; k < j + 1; k++) {
515
1.29k
                if (s[k] == 'i' || s[k] == 'I') {
516
894
                    contain_inf = true;
517
894
                    break;
518
894
                }
519
1.29k
            }
520
521
898
            *result = contain_inf ? PARSE_SUCCESS : PARSE_OVERFLOW;
522
132k
        } else {
523
132k
            *result = PARSE_SUCCESS;
524
132k
        }
525
133k
        return val;
526
133k
    } else {
527
4.74k
        *result = PARSE_FAILURE;
528
4.74k
    }
529
4.74k
    return 0;
530
137k
}
_ZN5doris12StringParser24string_to_float_internalIdEET_PKciPNS0_11ParseResultE
Line
Count
Source
481
73.8k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
482
73.8k
    int i = 0;
483
    // skip leading spaces
484
95.0k
    for (; i < len; ++i) {
485
95.0k
        if (!is_whitespace(s[i])) {
486
73.8k
            break;
487
73.8k
        }
488
95.0k
    }
489
490
    // skip back spaces
491
73.8k
    int j = len - 1;
492
94.4k
    for (; j >= i; j--) {
493
94.4k
        if (!is_whitespace(s[j])) {
494
73.8k
            break;
495
73.8k
        }
496
94.4k
    }
497
498
    // skip leading '+', from_chars can handle '-'
499
73.8k
    if (i < len && s[i] == '+') {
500
2.64k
        i++;
501
2.64k
    }
502
73.8k
    if (UNLIKELY(i > j)) {
503
3
        *result = PARSE_FAILURE;
504
3
        return 0;
505
3
    }
506
507
    // Use double here to not lose precision while accumulating the result
508
73.8k
    double val = 0;
509
73.8k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
510
511
73.8k
    if (res.ec == std::errc() && res.ptr == s + j + 1) {
512
71.3k
        if (abs(val) == std::numeric_limits<T>::infinity()) {
513
449
            auto contain_inf = false;
514
656
            for (int k = i; k < j + 1; k++) {
515
654
                if (s[k] == 'i' || s[k] == 'I') {
516
447
                    contain_inf = true;
517
447
                    break;
518
447
                }
519
654
            }
520
521
449
            *result = contain_inf ? PARSE_SUCCESS : PARSE_OVERFLOW;
522
70.8k
        } else {
523
70.8k
            *result = PARSE_SUCCESS;
524
70.8k
        }
525
71.3k
        return val;
526
71.3k
    } else {
527
2.57k
        *result = PARSE_FAILURE;
528
2.57k
    }
529
2.57k
    return 0;
530
73.8k
}
_ZN5doris12StringParser24string_to_float_internalIfEET_PKciPNS0_11ParseResultE
Line
Count
Source
481
63.9k
T StringParser::string_to_float_internal(const char* __restrict s, int len, ParseResult* result) {
482
63.9k
    int i = 0;
483
    // skip leading spaces
484
85.1k
    for (; i < len; ++i) {
485
85.1k
        if (!is_whitespace(s[i])) {
486
63.9k
            break;
487
63.9k
        }
488
85.1k
    }
489
490
    // skip back spaces
491
63.9k
    int j = len - 1;
492
84.5k
    for (; j >= i; j--) {
493
84.5k
        if (!is_whitespace(s[j])) {
494
63.9k
            break;
495
63.9k
        }
496
84.5k
    }
497
498
    // skip leading '+', from_chars can handle '-'
499
63.9k
    if (i < len && s[i] == '+') {
500
2.64k
        i++;
501
2.64k
    }
502
63.9k
    if (UNLIKELY(i > j)) {
503
0
        *result = PARSE_FAILURE;
504
0
        return 0;
505
0
    }
506
507
    // Use double here to not lose precision while accumulating the result
508
63.9k
    double val = 0;
509
63.9k
    auto res = fast_float::from_chars(s + i, s + j + 1, val);
510
511
63.9k
    if (res.ec == std::errc() && res.ptr == s + j + 1) {
512
61.7k
        if (abs(val) == std::numeric_limits<T>::infinity()) {
513
449
            auto contain_inf = false;
514
638
            for (int k = i; k < j + 1; k++) {
515
636
                if (s[k] == 'i' || s[k] == 'I') {
516
447
                    contain_inf = true;
517
447
                    break;
518
447
                }
519
636
            }
520
521
449
            *result = contain_inf ? PARSE_SUCCESS : PARSE_OVERFLOW;
522
61.3k
        } else {
523
61.3k
            *result = PARSE_SUCCESS;
524
61.3k
        }
525
61.7k
        return val;
526
61.7k
    } else {
527
2.16k
        *result = PARSE_FAILURE;
528
2.16k
    }
529
2.16k
    return 0;
530
63.9k
}
531
532
inline bool StringParser::string_to_bool_internal(const char* __restrict s, int len,
533
9.37k
                                                  ParseResult* result) {
534
9.37k
    *result = PARSE_SUCCESS;
535
536
9.37k
    if (len >= 4 && (s[0] == 't' || s[0] == 'T')) {
537
3.77k
        bool match = (s[1] == 'r' || s[1] == 'R') && (s[2] == 'u' || s[2] == 'U') &&
538
3.77k
                     (s[3] == 'e' || s[3] == 'E');
539
3.77k
        if (match && LIKELY(is_all_whitespace(s + 4, len - 4))) {
540
3.66k
            return true;
541
3.66k
        }
542
5.59k
    } else if (len >= 5 && (s[0] == 'f' || s[0] == 'F')) {
543
3.83k
        bool match = (s[1] == 'a' || s[1] == 'A') && (s[2] == 'l' || s[2] == 'L') &&
544
3.83k
                     (s[3] == 's' || s[3] == 'S') && (s[4] == 'e' || s[4] == 'E');
545
3.83k
        if (match && LIKELY(is_all_whitespace(s + 5, len - 5))) {
546
3.71k
            return false;
547
3.71k
        }
548
3.83k
    }
549
550
1.99k
    *result = PARSE_FAILURE;
551
1.99k
    return false;
552
9.37k
}
553
554
template <PrimitiveType P, typename T, typename DecimalType>
555
T StringParser::string_to_decimal(const char* __restrict s, int len, int type_precision,
556
804k
                                  int type_scale, ParseResult* result) {
557
804k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
558
804k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
559
804k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
560
804k
                  "wide::Int256.");
561
    // Special cases:
562
    //   1) '' == Fail, an empty string fails to parse.
563
    //   2) '   #   ' == #, leading and trailing white space is ignored.
564
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
565
    //   4) '#.' == '#', a trailing dot is ignored.
566
567
    // Ignore leading and trailing spaces.
568
1.64M
    while (len > 0 && is_whitespace(*s)) {
569
835k
        ++s;
570
835k
        --len;
571
835k
    }
572
1.63M
    while (len > 0 && is_whitespace(s[len - 1])) {
573
833k
        --len;
574
833k
    }
575
576
804k
    bool is_negative = false;
577
804k
    if (len > 0) {
578
804k
        switch (*s) {
579
315k
        case '-':
580
315k
            is_negative = true;
581
315k
            [[fallthrough]];
582
454k
        case '+':
583
454k
            ++s;
584
454k
            --len;
585
804k
        }
586
804k
    }
587
588
    // Ignore leading zeros.
589
804k
    bool found_value = false;
590
1.87M
    while (len > 0 && UNLIKELY(*s == '0')) {
591
1.06M
        found_value = true;
592
1.06M
        ++s;
593
1.06M
        --len;
594
1.06M
    }
595
596
    // Ignore leading zeros even after a dot. This allows for differentiating between
597
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
598
    // overflow.
599
804k
    int scale = 0;
600
804k
    int found_dot = 0;
601
804k
    if (len > 0 && *s == '.') {
602
189k
        found_dot = 1;
603
189k
        ++s;
604
189k
        --len;
605
1.45M
        while (len > 0 && UNLIKELY(*s == '0')) {
606
1.26M
            found_value = true;
607
1.26M
            ++scale;
608
1.26M
            ++s;
609
1.26M
            --len;
610
1.26M
        }
611
189k
    }
612
613
804k
    int precision = 0;
614
804k
    int max_digit = type_precision - type_scale;
615
804k
    int cur_digit = 0;
616
804k
    bool found_exponent = false;
617
804k
    int8_t exponent = 0;
618
804k
    T value = 0;
619
804k
    bool has_round = false;
620
17.6M
    for (int i = 0; i < len; ++i) {
621
16.9M
        const char& c = s[i];
622
16.9M
        if (LIKELY('0' <= c && c <= '9')) {
623
16.3M
            found_value = true;
624
            // Ignore digits once the type's precision limit is reached. This avoids
625
            // overflowing the underlying storage while handling a string like
626
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
627
            // an exponent will be made later.
628
16.3M
            if (LIKELY(type_precision > precision) && !has_round) {
629
16.0M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
630
16.0M
                ++precision;
631
16.0M
                scale += found_dot;
632
16.0M
                cur_digit = precision - scale;
633
16.0M
            } else if (!found_dot && max_digit < (precision - scale)) {
634
438
                *result = StringParser::PARSE_OVERFLOW;
635
438
                value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
636
438
                                    : vectorized::max_decimal_value<DecimalType>(type_precision);
637
438
                return value;
638
225k
            } else if (found_dot && scale >= type_scale && !has_round) {
639
                // make rounding cases
640
225k
                if (c > '4') {
641
83.2k
                    value += 1;
642
83.2k
                }
643
225k
                has_round = true;
644
225k
                continue;
645
225k
            } else if (!found_dot) {
646
0
                ++cur_digit;
647
0
            }
648
16.0M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
649
16.0M
        } else if (c == '.' && LIKELY(!found_dot)) {
650
541k
            found_dot = 1;
651
541k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
652
138k
            found_exponent = true;
653
138k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
654
138k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
655
10
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
656
0
                    *result = StringParser::PARSE_UNDERFLOW;
657
0
                }
658
10
                return 0;
659
10
            }
660
138k
            break;
661
138k
        } else {
662
344
            if (value == 0) {
663
271
                *result = StringParser::PARSE_FAILURE;
664
271
                return 0;
665
271
            }
666
            // here to handle
667
73
            *result = StringParser::PARSE_SUCCESS;
668
73
            if (type_scale >= scale) {
669
71
                value *= get_scale_multiplier<T>(type_scale - scale);
670
                // here meet non-valid character, should return the value, keep going to meet
671
                // the E/e character because we make right user-given type_precision
672
                // not max number type_precision
673
71
                if (!is_numeric_ascii(c)) {
674
71
                    if (cur_digit > type_precision) {
675
0
                        *result = StringParser::PARSE_OVERFLOW;
676
0
                        value = is_negative
677
0
                                        ? vectorized::min_decimal_value<DecimalType>(type_precision)
678
0
                                        : vectorized::max_decimal_value<DecimalType>(
679
0
                                                  type_precision);
680
0
                        return value;
681
0
                    }
682
71
                    return is_negative ? T(-value) : T(value);
683
71
                }
684
71
            }
685
686
2
            return is_negative ? T(-value) : T(value);
687
73
        }
688
16.9M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
803k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
69.1k
        precision += exponent - scale;
695
696
69.1k
        value *= get_scale_multiplier<T>(exponent - scale);
697
69.1k
        scale = 0;
698
734k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
734k
        scale -= exponent;
703
734k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
803k
    if (scale > precision) {
707
76.9k
        precision = scale;
708
76.9k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
13.5k
    *result = StringParser::PARSE_SUCCESS;
713
790k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
11.4k
        *result = StringParser::PARSE_OVERFLOW;
715
11.4k
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
11.4k
            value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
718
11.4k
                                : vectorized::max_decimal_value<DecimalType>(type_precision);
719
11.4k
            return value;
720
11.4k
        }
721
792k
    } else if (UNLIKELY(scale > type_scale)) {
722
247k
        *result = StringParser::PARSE_UNDERFLOW;
723
247k
        int shift = scale - type_scale;
724
247k
        T divisor = get_scale_multiplier<T>(shift);
725
247k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
247k
        } else {
728
247k
            T remainder = value % divisor;
729
247k
            value /= divisor;
730
247k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
121k
                value += 1;
732
121k
            }
733
247k
        }
734
247k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
545k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
1
        *result = StringParser::PARSE_FAILURE;
737
1
    }
738
739
792k
    if (type_scale > scale) {
740
235k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
235k
    }
742
743
792k
    return is_negative ? T(-value) : T(value);
744
790k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE28EiNS_10vectorized7DecimalIiEEEET0_PKciiiPNS0_11ParseResultE
Line
Count
Source
556
199k
                                  int type_scale, ParseResult* result) {
557
199k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
558
199k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
559
199k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
560
199k
                  "wide::Int256.");
561
    // Special cases:
562
    //   1) '' == Fail, an empty string fails to parse.
563
    //   2) '   #   ' == #, leading and trailing white space is ignored.
564
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
565
    //   4) '#.' == '#', a trailing dot is ignored.
566
567
    // Ignore leading and trailing spaces.
568
460k
    while (len > 0 && is_whitespace(*s)) {
569
261k
        ++s;
570
261k
        --len;
571
261k
    }
572
460k
    while (len > 0 && is_whitespace(s[len - 1])) {
573
261k
        --len;
574
261k
    }
575
576
199k
    bool is_negative = false;
577
199k
    if (len > 0) {
578
199k
        switch (*s) {
579
97.5k
        case '-':
580
97.5k
            is_negative = true;
581
97.5k
            [[fallthrough]];
582
141k
        case '+':
583
141k
            ++s;
584
141k
            --len;
585
199k
        }
586
199k
    }
587
588
    // Ignore leading zeros.
589
199k
    bool found_value = false;
590
496k
    while (len > 0 && UNLIKELY(*s == '0')) {
591
296k
        found_value = true;
592
296k
        ++s;
593
296k
        --len;
594
296k
    }
595
596
    // Ignore leading zeros even after a dot. This allows for differentiating between
597
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
598
    // overflow.
599
199k
    int scale = 0;
600
199k
    int found_dot = 0;
601
199k
    if (len > 0 && *s == '.') {
602
55.9k
        found_dot = 1;
603
55.9k
        ++s;
604
55.9k
        --len;
605
160k
        while (len > 0 && UNLIKELY(*s == '0')) {
606
104k
            found_value = true;
607
104k
            ++scale;
608
104k
            ++s;
609
104k
            --len;
610
104k
        }
611
55.9k
    }
612
613
199k
    int precision = 0;
614
199k
    int max_digit = type_precision - type_scale;
615
199k
    int cur_digit = 0;
616
199k
    bool found_exponent = false;
617
199k
    int8_t exponent = 0;
618
199k
    T value = 0;
619
199k
    bool has_round = false;
620
1.55M
    for (int i = 0; i < len; ++i) {
621
1.39M
        const char& c = s[i];
622
1.39M
        if (LIKELY('0' <= c && c <= '9')) {
623
1.24M
            found_value = true;
624
            // Ignore digits once the type's precision limit is reached. This avoids
625
            // overflowing the underlying storage while handling a string like
626
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
627
            // an exponent will be made later.
628
1.24M
            if (LIKELY(type_precision > precision) && !has_round) {
629
1.17M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
630
1.17M
                ++precision;
631
1.17M
                scale += found_dot;
632
1.17M
                cur_digit = precision - scale;
633
1.17M
            } else if (!found_dot && max_digit < (precision - scale)) {
634
438
                *result = StringParser::PARSE_OVERFLOW;
635
438
                value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
636
438
                                    : vectorized::max_decimal_value<DecimalType>(type_precision);
637
438
                return value;
638
69.1k
            } else if (found_dot && scale >= type_scale && !has_round) {
639
                // make rounding cases
640
69.1k
                if (c > '4') {
641
25.6k
                    value += 1;
642
25.6k
                }
643
69.1k
                has_round = true;
644
69.1k
                continue;
645
69.1k
            } else if (!found_dot) {
646
0
                ++cur_digit;
647
0
            }
648
1.17M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
649
1.17M
        } else if (c == '.' && LIKELY(!found_dot)) {
650
110k
            found_dot = 1;
651
110k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
652
40.9k
            found_exponent = true;
653
40.9k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
654
40.9k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
655
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
656
0
                    *result = StringParser::PARSE_UNDERFLOW;
657
0
                }
658
0
                return 0;
659
0
            }
660
40.9k
            break;
661
40.9k
        } else {
662
92
            if (value == 0) {
663
66
                *result = StringParser::PARSE_FAILURE;
664
66
                return 0;
665
66
            }
666
            // here to handle
667
26
            *result = StringParser::PARSE_SUCCESS;
668
26
            if (type_scale >= scale) {
669
26
                value *= get_scale_multiplier<T>(type_scale - scale);
670
                // here meet non-valid character, should return the value, keep going to meet
671
                // the E/e character because we make right user-given type_precision
672
                // not max number type_precision
673
26
                if (!is_numeric_ascii(c)) {
674
26
                    if (cur_digit > type_precision) {
675
0
                        *result = StringParser::PARSE_OVERFLOW;
676
0
                        value = is_negative
677
0
                                        ? vectorized::min_decimal_value<DecimalType>(type_precision)
678
0
                                        : vectorized::max_decimal_value<DecimalType>(
679
0
                                                  type_precision);
680
0
                        return value;
681
0
                    }
682
26
                    return is_negative ? T(-value) : T(value);
683
26
                }
684
26
            }
685
686
0
            return is_negative ? T(-value) : T(value);
687
26
        }
688
1.39M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
198k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
0
        precision += exponent - scale;
695
696
0
        value *= get_scale_multiplier<T>(exponent - scale);
697
0
        scale = 0;
698
198k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
198k
        scale -= exponent;
703
198k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
198k
    if (scale > precision) {
707
19.5k
        precision = scale;
708
19.5k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
198k
    *result = StringParser::PARSE_SUCCESS;
713
198k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
736
        *result = StringParser::PARSE_OVERFLOW;
715
736
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
736
            value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
718
736
                                : vectorized::max_decimal_value<DecimalType>(type_precision);
719
736
            return value;
720
736
        }
721
198k
    } else if (UNLIKELY(scale > type_scale)) {
722
76.8k
        *result = StringParser::PARSE_UNDERFLOW;
723
76.8k
        int shift = scale - type_scale;
724
76.8k
        T divisor = get_scale_multiplier<T>(shift);
725
76.8k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
76.8k
        } else {
728
76.8k
            T remainder = value % divisor;
729
76.8k
            value /= divisor;
730
76.8k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
38.4k
                value += 1;
732
38.4k
            }
733
76.8k
        }
734
76.8k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
121k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
198k
    if (type_scale > scale) {
740
39.5k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
39.5k
    }
742
743
198k
    return is_negative ? T(-value) : T(value);
744
198k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE29ElNS_10vectorized7DecimalIlEEEET0_PKciiiPNS0_11ParseResultE
Line
Count
Source
556
206k
                                  int type_scale, ParseResult* result) {
557
206k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
558
206k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
559
206k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
560
206k
                  "wide::Int256.");
561
    // Special cases:
562
    //   1) '' == Fail, an empty string fails to parse.
563
    //   2) '   #   ' == #, leading and trailing white space is ignored.
564
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
565
    //   4) '#.' == '#', a trailing dot is ignored.
566
567
    // Ignore leading and trailing spaces.
568
426k
    while (len > 0 && is_whitespace(*s)) {
569
219k
        ++s;
570
219k
        --len;
571
219k
    }
572
425k
    while (len > 0 && is_whitespace(s[len - 1])) {
573
218k
        --len;
574
218k
    }
575
576
206k
    bool is_negative = false;
577
206k
    if (len > 0) {
578
206k
        switch (*s) {
579
81.2k
        case '-':
580
81.2k
            is_negative = true;
581
81.2k
            [[fallthrough]];
582
117k
        case '+':
583
117k
            ++s;
584
117k
            --len;
585
206k
        }
586
206k
    }
587
588
    // Ignore leading zeros.
589
206k
    bool found_value = false;
590
478k
    while (len > 0 && UNLIKELY(*s == '0')) {
591
272k
        found_value = true;
592
272k
        ++s;
593
272k
        --len;
594
272k
    }
595
596
    // Ignore leading zeros even after a dot. This allows for differentiating between
597
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
598
    // overflow.
599
206k
    int scale = 0;
600
206k
    int found_dot = 0;
601
206k
    if (len > 0 && *s == '.') {
602
53.2k
        found_dot = 1;
603
53.2k
        ++s;
604
53.2k
        --len;
605
263k
        while (len > 0 && UNLIKELY(*s == '0')) {
606
210k
            found_value = true;
607
210k
            ++scale;
608
210k
            ++s;
609
210k
            --len;
610
210k
        }
611
53.2k
    }
612
613
206k
    int precision = 0;
614
206k
    int max_digit = type_precision - type_scale;
615
206k
    int cur_digit = 0;
616
206k
    bool found_exponent = false;
617
206k
    int8_t exponent = 0;
618
206k
    T value = 0;
619
206k
    bool has_round = false;
620
2.88M
    for (int i = 0; i < len; ++i) {
621
2.70M
        const char& c = s[i];
622
2.70M
        if (LIKELY('0' <= c && c <= '9')) {
623
2.54M
            found_value = true;
624
            // Ignore digits once the type's precision limit is reached. This avoids
625
            // overflowing the underlying storage while handling a string like
626
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
627
            // an exponent will be made later.
628
2.54M
            if (LIKELY(type_precision > precision) && !has_round) {
629
2.48M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
630
2.48M
                ++precision;
631
2.48M
                scale += found_dot;
632
2.48M
                cur_digit = precision - scale;
633
2.48M
            } else if (!found_dot && max_digit < (precision - scale)) {
634
0
                *result = StringParser::PARSE_OVERFLOW;
635
0
                value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
636
0
                                    : vectorized::max_decimal_value<DecimalType>(type_precision);
637
0
                return value;
638
58.8k
            } else if (found_dot && scale >= type_scale && !has_round) {
639
                // make rounding cases
640
58.8k
                if (c > '4') {
641
21.7k
                    value += 1;
642
21.7k
                }
643
58.8k
                has_round = true;
644
58.8k
                continue;
645
58.8k
            } else if (!found_dot) {
646
0
                ++cur_digit;
647
0
            }
648
2.48M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
649
2.48M
        } else if (c == '.' && LIKELY(!found_dot)) {
650
137k
            found_dot = 1;
651
137k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
652
22.9k
            found_exponent = true;
653
22.9k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
654
22.9k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
655
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
656
0
                    *result = StringParser::PARSE_UNDERFLOW;
657
0
                }
658
0
                return 0;
659
0
            }
660
22.9k
            break;
661
22.9k
        } else {
662
91
            if (value == 0) {
663
68
                *result = StringParser::PARSE_FAILURE;
664
68
                return 0;
665
68
            }
666
            // here to handle
667
23
            *result = StringParser::PARSE_SUCCESS;
668
23
            if (type_scale >= scale) {
669
22
                value *= get_scale_multiplier<T>(type_scale - scale);
670
                // here meet non-valid character, should return the value, keep going to meet
671
                // the E/e character because we make right user-given type_precision
672
                // not max number type_precision
673
22
                if (!is_numeric_ascii(c)) {
674
22
                    if (cur_digit > type_precision) {
675
0
                        *result = StringParser::PARSE_OVERFLOW;
676
0
                        value = is_negative
677
0
                                        ? vectorized::min_decimal_value<DecimalType>(type_precision)
678
0
                                        : vectorized::max_decimal_value<DecimalType>(
679
0
                                                  type_precision);
680
0
                        return value;
681
0
                    }
682
22
                    return is_negative ? T(-value) : T(value);
683
22
                }
684
22
            }
685
686
1
            return is_negative ? T(-value) : T(value);
687
23
        }
688
2.70M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
206k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
1
        precision += exponent - scale;
695
696
1
        value *= get_scale_multiplier<T>(exponent - scale);
697
1
        scale = 0;
698
206k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
206k
        scale -= exponent;
703
206k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
206k
    if (scale > precision) {
707
20.6k
        precision = scale;
708
20.6k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
206k
    *result = StringParser::PARSE_SUCCESS;
713
206k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
10.7k
        *result = StringParser::PARSE_OVERFLOW;
715
10.7k
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
10.7k
            value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
718
10.7k
                                : vectorized::max_decimal_value<DecimalType>(type_precision);
719
10.7k
            return value;
720
10.7k
        }
721
195k
    } else if (UNLIKELY(scale > type_scale)) {
722
65.1k
        *result = StringParser::PARSE_UNDERFLOW;
723
65.1k
        int shift = scale - type_scale;
724
65.1k
        T divisor = get_scale_multiplier<T>(shift);
725
65.1k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
65.1k
        } else {
728
65.1k
            T remainder = value % divisor;
729
65.1k
            value /= divisor;
730
65.1k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
32.0k
                value += 1;
732
32.0k
            }
733
65.1k
        }
734
65.1k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
130k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
1
        *result = StringParser::PARSE_FAILURE;
737
1
    }
738
739
195k
    if (type_scale > scale) {
740
43.4k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
43.4k
    }
742
743
195k
    return is_negative ? T(-value) : T(value);
744
206k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE30EnNS_10vectorized12Decimal128V3EEET0_PKciiiPNS0_11ParseResultE
Line
Count
Source
556
174k
                                  int type_scale, ParseResult* result) {
557
174k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
558
174k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
559
174k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
560
174k
                  "wide::Int256.");
561
    // Special cases:
562
    //   1) '' == Fail, an empty string fails to parse.
563
    //   2) '   #   ' == #, leading and trailing white space is ignored.
564
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
565
    //   4) '#.' == '#', a trailing dot is ignored.
566
567
    // Ignore leading and trailing spaces.
568
352k
    while (len > 0 && is_whitespace(*s)) {
569
177k
        ++s;
570
177k
        --len;
571
177k
    }
572
351k
    while (len > 0 && is_whitespace(s[len - 1])) {
573
176k
        --len;
574
176k
    }
575
576
174k
    bool is_negative = false;
577
174k
    if (len > 0) {
578
174k
        switch (*s) {
579
67.1k
        case '-':
580
67.1k
            is_negative = true;
581
67.1k
            [[fallthrough]];
582
96.5k
        case '+':
583
96.5k
            ++s;
584
96.5k
            --len;
585
174k
        }
586
174k
    }
587
588
    // Ignore leading zeros.
589
174k
    bool found_value = false;
590
395k
    while (len > 0 && UNLIKELY(*s == '0')) {
591
220k
        found_value = true;
592
220k
        ++s;
593
220k
        --len;
594
220k
    }
595
596
    // Ignore leading zeros even after a dot. This allows for differentiating between
597
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
598
    // overflow.
599
174k
    int scale = 0;
600
174k
    int found_dot = 0;
601
174k
    if (len > 0 && *s == '.') {
602
43.7k
        found_dot = 1;
603
43.7k
        ++s;
604
43.7k
        --len;
605
363k
        while (len > 0 && UNLIKELY(*s == '0')) {
606
319k
            found_value = true;
607
319k
            ++scale;
608
319k
            ++s;
609
319k
            --len;
610
319k
        }
611
43.7k
    }
612
613
174k
    int precision = 0;
614
174k
    int max_digit = type_precision - type_scale;
615
174k
    int cur_digit = 0;
616
174k
    bool found_exponent = false;
617
174k
    int8_t exponent = 0;
618
174k
    T value = 0;
619
174k
    bool has_round = false;
620
4.38M
    for (int i = 0; i < len; ++i) {
621
4.21M
        const char& c = s[i];
622
4.21M
        if (LIKELY('0' <= c && c <= '9')) {
623
4.09M
            found_value = true;
624
            // Ignore digits once the type's precision limit is reached. This avoids
625
            // overflowing the underlying storage while handling a string like
626
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
627
            // an exponent will be made later.
628
4.09M
            if (LIKELY(type_precision > precision) && !has_round) {
629
4.04M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
630
4.04M
                ++precision;
631
4.04M
                scale += found_dot;
632
4.04M
                cur_digit = precision - scale;
633
4.04M
            } else if (!found_dot && max_digit < (precision - scale)) {
634
0
                *result = StringParser::PARSE_OVERFLOW;
635
0
                value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
636
0
                                    : vectorized::max_decimal_value<DecimalType>(type_precision);
637
0
                return value;
638
48.6k
            } else if (found_dot && scale >= type_scale && !has_round) {
639
                // make rounding cases
640
48.6k
                if (c > '4') {
641
17.9k
                    value += 1;
642
17.9k
                }
643
48.6k
                has_round = true;
644
48.6k
                continue;
645
48.6k
            } else if (!found_dot) {
646
0
                ++cur_digit;
647
0
            }
648
4.04M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
649
4.04M
        } else if (c == '.' && LIKELY(!found_dot)) {
650
118k
            found_dot = 1;
651
118k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
652
3.30k
            found_exponent = true;
653
3.30k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
654
3.30k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
655
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
656
0
                    *result = StringParser::PARSE_UNDERFLOW;
657
0
                }
658
0
                return 0;
659
0
            }
660
3.30k
            break;
661
3.30k
        } else {
662
65
            if (value == 0) {
663
53
                *result = StringParser::PARSE_FAILURE;
664
53
                return 0;
665
53
            }
666
            // here to handle
667
12
            *result = StringParser::PARSE_SUCCESS;
668
12
            if (type_scale >= scale) {
669
11
                value *= get_scale_multiplier<T>(type_scale - scale);
670
                // here meet non-valid character, should return the value, keep going to meet
671
                // the E/e character because we make right user-given type_precision
672
                // not max number type_precision
673
11
                if (!is_numeric_ascii(c)) {
674
11
                    if (cur_digit > type_precision) {
675
0
                        *result = StringParser::PARSE_OVERFLOW;
676
0
                        value = is_negative
677
0
                                        ? vectorized::min_decimal_value<DecimalType>(type_precision)
678
0
                                        : vectorized::max_decimal_value<DecimalType>(
679
0
                                                  type_precision);
680
0
                        return value;
681
0
                    }
682
11
                    return is_negative ? T(-value) : T(value);
683
11
                }
684
11
            }
685
686
1
            return is_negative ? T(-value) : T(value);
687
12
        }
688
4.21M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
174k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
0
        precision += exponent - scale;
695
696
0
        value *= get_scale_multiplier<T>(exponent - scale);
697
0
        scale = 0;
698
174k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
174k
        scale -= exponent;
703
174k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
174k
    if (scale > precision) {
707
20.7k
        precision = scale;
708
20.7k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
174k
    *result = StringParser::PARSE_SUCCESS;
713
174k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
2
        *result = StringParser::PARSE_OVERFLOW;
715
2
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
2
            value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
718
2
                                : vectorized::max_decimal_value<DecimalType>(type_precision);
719
2
            return value;
720
2
        }
721
174k
    } else if (UNLIKELY(scale > type_scale)) {
722
54.0k
        *result = StringParser::PARSE_UNDERFLOW;
723
54.0k
        int shift = scale - type_scale;
724
54.0k
        T divisor = get_scale_multiplier<T>(shift);
725
54.0k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
54.0k
        } else {
728
54.0k
            T remainder = value % divisor;
729
54.0k
            value /= divisor;
730
54.0k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
25.6k
                value += 1;
732
25.6k
            }
733
54.0k
        }
734
54.0k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
120k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
174k
    if (type_scale > scale) {
740
52.2k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
52.2k
    }
742
743
174k
    return is_negative ? T(-value) : T(value);
744
174k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE35EN4wide7integerILm256EiEENS_10vectorized7DecimalIS5_EEEET0_PKciiiPNS0_11ParseResultE
Line
Count
Source
556
210k
                                  int type_scale, ParseResult* result) {
557
210k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
558
210k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
559
210k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
560
210k
                  "wide::Int256.");
561
    // Special cases:
562
    //   1) '' == Fail, an empty string fails to parse.
563
    //   2) '   #   ' == #, leading and trailing white space is ignored.
564
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
565
    //   4) '#.' == '#', a trailing dot is ignored.
566
567
    // Ignore leading and trailing spaces.
568
387k
    while (len > 0 && is_whitespace(*s)) {
569
177k
        ++s;
570
177k
        --len;
571
177k
    }
572
386k
    while (len > 0 && is_whitespace(s[len - 1])) {
573
176k
        --len;
574
176k
    }
575
576
210k
    bool is_negative = false;
577
210k
    if (len > 0) {
578
210k
        switch (*s) {
579
62.7k
        case '-':
580
62.7k
            is_negative = true;
581
62.7k
            [[fallthrough]];
582
92.1k
        case '+':
583
92.1k
            ++s;
584
92.1k
            --len;
585
210k
        }
586
210k
    }
587
588
    // Ignore leading zeros.
589
210k
    bool found_value = false;
590
449k
    while (len > 0 && UNLIKELY(*s == '0')) {
591
239k
        found_value = true;
592
239k
        ++s;
593
239k
        --len;
594
239k
    }
595
596
    // Ignore leading zeros even after a dot. This allows for differentiating between
597
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
598
    // overflow.
599
210k
    int scale = 0;
600
210k
    int found_dot = 0;
601
210k
    if (len > 0 && *s == '.') {
602
34.4k
        found_dot = 1;
603
34.4k
        ++s;
604
34.4k
        --len;
605
657k
        while (len > 0 && UNLIKELY(*s == '0')) {
606
622k
            found_value = true;
607
622k
            ++scale;
608
622k
            ++s;
609
622k
            --len;
610
622k
        }
611
34.4k
    }
612
613
210k
    int precision = 0;
614
210k
    int max_digit = type_precision - type_scale;
615
210k
    int cur_digit = 0;
616
210k
    bool found_exponent = false;
617
210k
    int8_t exponent = 0;
618
210k
    T value = 0;
619
210k
    bool has_round = false;
620
8.56M
    for (int i = 0; i < len; ++i) {
621
8.42M
        const char& c = s[i];
622
8.42M
        if (LIKELY('0' <= c && c <= '9')) {
623
8.19M
            found_value = true;
624
            // Ignore digits once the type's precision limit is reached. This avoids
625
            // overflowing the underlying storage while handling a string like
626
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
627
            // an exponent will be made later.
628
8.19M
            if (LIKELY(type_precision > precision) && !has_round) {
629
8.14M
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
630
8.14M
                ++precision;
631
8.14M
                scale += found_dot;
632
8.14M
                cur_digit = precision - scale;
633
8.14M
            } else if (!found_dot && max_digit < (precision - scale)) {
634
0
                *result = StringParser::PARSE_OVERFLOW;
635
0
                value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
636
0
                                    : vectorized::max_decimal_value<DecimalType>(type_precision);
637
0
                return value;
638
48.6k
            } else if (found_dot && scale >= type_scale && !has_round) {
639
                // make rounding cases
640
48.6k
                if (c > '4') {
641
17.9k
                    value += 1;
642
17.9k
                }
643
48.6k
                has_round = true;
644
48.6k
                continue;
645
48.6k
            } else if (!found_dot) {
646
0
                ++cur_digit;
647
0
            }
648
8.14M
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
649
8.14M
        } else if (c == '.' && LIKELY(!found_dot)) {
650
164k
            found_dot = 1;
651
164k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
652
71.4k
            found_exponent = true;
653
71.4k
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
654
71.4k
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
655
10
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
656
0
                    *result = StringParser::PARSE_UNDERFLOW;
657
0
                }
658
10
                return 0;
659
10
            }
660
71.3k
            break;
661
71.4k
        } else {
662
80
            if (value == 0) {
663
74
                *result = StringParser::PARSE_FAILURE;
664
74
                return 0;
665
74
            }
666
            // here to handle
667
6
            *result = StringParser::PARSE_SUCCESS;
668
6
            if (type_scale >= scale) {
669
6
                value *= get_scale_multiplier<T>(type_scale - scale);
670
                // here meet non-valid character, should return the value, keep going to meet
671
                // the E/e character because we make right user-given type_precision
672
                // not max number type_precision
673
6
                if (!is_numeric_ascii(c)) {
674
6
                    if (cur_digit > type_precision) {
675
0
                        *result = StringParser::PARSE_OVERFLOW;
676
0
                        value = is_negative
677
0
                                        ? vectorized::min_decimal_value<DecimalType>(type_precision)
678
0
                                        : vectorized::max_decimal_value<DecimalType>(
679
0
                                                  type_precision);
680
0
                        return value;
681
0
                    }
682
6
                    return is_negative ? T(-value) : T(value);
683
6
                }
684
6
            }
685
686
0
            return is_negative ? T(-value) : T(value);
687
6
        }
688
8.42M
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
209k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
69.1k
        precision += exponent - scale;
695
696
69.1k
        value *= get_scale_multiplier<T>(exponent - scale);
697
69.1k
        scale = 0;
698
140k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
140k
        scale -= exponent;
703
140k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
209k
    if (scale > precision) {
707
15.3k
        precision = scale;
708
15.3k
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
209k
    *result = StringParser::PARSE_SUCCESS;
713
209k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
0
        *result = StringParser::PARSE_OVERFLOW;
715
0
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
0
            value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
718
0
                                : vectorized::max_decimal_value<DecimalType>(type_precision);
719
0
            return value;
720
0
        }
721
209k
    } else if (UNLIKELY(scale > type_scale)) {
722
51.2k
        *result = StringParser::PARSE_UNDERFLOW;
723
51.2k
        int shift = scale - type_scale;
724
51.2k
        T divisor = get_scale_multiplier<T>(shift);
725
51.2k
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
51.2k
        } else {
728
51.2k
            T remainder = value % divisor;
729
51.2k
            value /= divisor;
730
51.2k
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
25.6k
                value += 1;
732
25.6k
            }
733
51.2k
        }
734
51.2k
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
158k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
209k
    if (type_scale > scale) {
740
97.9k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
97.9k
    }
742
743
209k
    return is_negative ? T(-value) : T(value);
744
209k
}
_ZN5doris12StringParser17string_to_decimalILNS_13PrimitiveTypeE20EnNS_10vectorized7DecimalInEEEET0_PKciiiPNS0_11ParseResultE
Line
Count
Source
556
13.5k
                                  int type_scale, ParseResult* result) {
557
13.5k
    static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, int64_t> ||
558
13.5k
                          std::is_same_v<T, __int128> || std::is_same_v<T, wide::Int256>,
559
13.5k
                  "Cast string to decimal only support target type int32_t, int64_t, __int128 or "
560
13.5k
                  "wide::Int256.");
561
    // Special cases:
562
    //   1) '' == Fail, an empty string fails to parse.
563
    //   2) '   #   ' == #, leading and trailing white space is ignored.
564
    //   3) '.' == 0, a single dot parses as zero (for consistency with other types).
565
    //   4) '#.' == '#', a trailing dot is ignored.
566
567
    // Ignore leading and trailing spaces.
568
13.5k
    while (len > 0 && is_whitespace(*s)) {
569
0
        ++s;
570
0
        --len;
571
0
    }
572
13.5k
    while (len > 0 && is_whitespace(s[len - 1])) {
573
0
        --len;
574
0
    }
575
576
13.5k
    bool is_negative = false;
577
13.5k
    if (len > 0) {
578
13.5k
        switch (*s) {
579
6.68k
        case '-':
580
6.68k
            is_negative = true;
581
6.68k
            [[fallthrough]];
582
6.68k
        case '+':
583
6.68k
            ++s;
584
6.68k
            --len;
585
13.5k
        }
586
13.5k
    }
587
588
    // Ignore leading zeros.
589
13.5k
    bool found_value = false;
590
52.3k
    while (len > 0 && UNLIKELY(*s == '0')) {
591
38.8k
        found_value = true;
592
38.8k
        ++s;
593
38.8k
        --len;
594
38.8k
    }
595
596
    // Ignore leading zeros even after a dot. This allows for differentiating between
597
    // cases like 0.01e2, which would fit in a DECIMAL(1, 0), and 0.10e2, which would
598
    // overflow.
599
13.5k
    int scale = 0;
600
13.5k
    int found_dot = 0;
601
13.5k
    if (len > 0 && *s == '.') {
602
2.00k
        found_dot = 1;
603
2.00k
        ++s;
604
2.00k
        --len;
605
6.43k
        while (len > 0 && UNLIKELY(*s == '0')) {
606
4.42k
            found_value = true;
607
4.42k
            ++scale;
608
4.42k
            ++s;
609
4.42k
            --len;
610
4.42k
        }
611
2.00k
    }
612
613
13.5k
    int precision = 0;
614
13.5k
    int max_digit = type_precision - type_scale;
615
13.5k
    int cur_digit = 0;
616
13.5k
    bool found_exponent = false;
617
13.5k
    int8_t exponent = 0;
618
13.5k
    T value = 0;
619
13.5k
    bool has_round = false;
620
275k
    for (int i = 0; i < len; ++i) {
621
261k
        const char& c = s[i];
622
261k
        if (LIKELY('0' <= c && c <= '9')) {
623
250k
            found_value = true;
624
            // Ignore digits once the type's precision limit is reached. This avoids
625
            // overflowing the underlying storage while handling a string like
626
            // 10000000000e-10 into a DECIMAL(1, 0). Adjustments for ignored digits and
627
            // an exponent will be made later.
628
250k
            if (LIKELY(type_precision > precision) && !has_round) {
629
250k
                value = (value * 10) + (c - '0'); // Benchmarks are faster with parenthesis...
630
250k
                ++precision;
631
250k
                scale += found_dot;
632
250k
                cur_digit = precision - scale;
633
250k
            } else if (!found_dot && max_digit < (precision - scale)) {
634
0
                *result = StringParser::PARSE_OVERFLOW;
635
0
                value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
636
0
                                    : vectorized::max_decimal_value<DecimalType>(type_precision);
637
0
                return value;
638
8
            } else if (found_dot && scale >= type_scale && !has_round) {
639
                // make rounding cases
640
8
                if (c > '4') {
641
0
                    value += 1;
642
0
                }
643
8
                has_round = true;
644
8
                continue;
645
8
            } else if (!found_dot) {
646
0
                ++cur_digit;
647
0
            }
648
250k
            DCHECK(value >= 0); // For some reason //DCHECK_GE doesn't work with __int128.
649
250k
        } else if (c == '.' && LIKELY(!found_dot)) {
650
11.4k
            found_dot = 1;
651
11.4k
        } else if ((c == 'e' || c == 'E') && LIKELY(!found_exponent)) {
652
0
            found_exponent = true;
653
0
            exponent = string_to_int_internal<int8_t>(s + i + 1, len - i - 1, result);
654
0
            if (UNLIKELY(*result != StringParser::PARSE_SUCCESS)) {
655
0
                if (*result == StringParser::PARSE_OVERFLOW && exponent < 0) {
656
0
                    *result = StringParser::PARSE_UNDERFLOW;
657
0
                }
658
0
                return 0;
659
0
            }
660
0
            break;
661
16
        } else {
662
16
            if (value == 0) {
663
10
                *result = StringParser::PARSE_FAILURE;
664
10
                return 0;
665
10
            }
666
            // here to handle
667
6
            *result = StringParser::PARSE_SUCCESS;
668
6
            if (type_scale >= scale) {
669
6
                value *= get_scale_multiplier<T>(type_scale - scale);
670
                // here meet non-valid character, should return the value, keep going to meet
671
                // the E/e character because we make right user-given type_precision
672
                // not max number type_precision
673
6
                if (!is_numeric_ascii(c)) {
674
6
                    if (cur_digit > type_precision) {
675
0
                        *result = StringParser::PARSE_OVERFLOW;
676
0
                        value = is_negative
677
0
                                        ? vectorized::min_decimal_value<DecimalType>(type_precision)
678
0
                                        : vectorized::max_decimal_value<DecimalType>(
679
0
                                                  type_precision);
680
0
                        return value;
681
0
                    }
682
6
                    return is_negative ? T(-value) : T(value);
683
6
                }
684
6
            }
685
686
0
            return is_negative ? T(-value) : T(value);
687
6
        }
688
261k
    }
689
690
    // Find the number of truncated digits before adjusting the precision for an exponent.
691
13.5k
    if (exponent > scale) {
692
        // Ex: 0.1e3 (which at this point would have precision == 1 and scale == 1), the
693
        //     scale must be set to 0 and the value set to 100 which means a precision of 3.
694
0
        precision += exponent - scale;
695
696
0
        value *= get_scale_multiplier<T>(exponent - scale);
697
0
        scale = 0;
698
13.5k
    } else {
699
        // Ex: 100e-4, the scale must be set to 4 but no adjustment to the value is needed,
700
        //     the precision must also be set to 4 but that will be done below for the
701
        //     non-exponent case anyways.
702
13.5k
        scale -= exponent;
703
13.5k
    }
704
    // Ex: 0.001, at this point would have precision 1 and scale 3 since leading zeros
705
    //     were ignored during previous parsing.
706
13.5k
    if (scale > precision) {
707
675
        precision = scale;
708
675
    }
709
710
    // Microbenchmarks show that beyond this point, returning on parse failure is slower
711
    // than just letting the function run out.
712
13.5k
    *result = StringParser::PARSE_SUCCESS;
713
13.5k
    if (UNLIKELY(precision - scale > type_precision - type_scale)) {
714
9
        *result = StringParser::PARSE_OVERFLOW;
715
9
        if constexpr (TYPE_DECIMALV2 != P) {
716
            // decimalv3 overflow will return max min value for type precision
717
9
            value = is_negative ? vectorized::min_decimal_value<DecimalType>(type_precision)
718
9
                                : vectorized::max_decimal_value<DecimalType>(type_precision);
719
9
            return value;
720
9
        }
721
13.5k
    } else if (UNLIKELY(scale > type_scale)) {
722
17
        *result = StringParser::PARSE_UNDERFLOW;
723
17
        int shift = scale - type_scale;
724
17
        T divisor = get_scale_multiplier<T>(shift);
725
17
        if (UNLIKELY(divisor == std::numeric_limits<T>::max())) {
726
0
            value = 0;
727
17
        } else {
728
17
            T remainder = value % divisor;
729
17
            value /= divisor;
730
17
            if ((remainder > 0 ? T(remainder) : T(-remainder)) >= (divisor >> 1)) {
731
17
                value += 1;
732
17
            }
733
17
        }
734
17
        DCHECK(value >= 0); // //DCHECK_GE doesn't work with __int128.
735
13.5k
    } else if (UNLIKELY(!found_value && !found_dot)) {
736
0
        *result = StringParser::PARSE_FAILURE;
737
0
    }
738
739
13.5k
    if (type_scale > scale) {
740
1.95k
        value *= get_scale_multiplier<T>(type_scale - scale);
741
1.95k
    }
742
743
13.5k
    return is_negative ? T(-value) : T(value);
744
13.5k
}
745
746
} // end namespace doris