Coverage Report

Created: 2026-04-14 03:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/parse_util.cpp
Line
Count
Source
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/parse-util.cc
19
// and modified by Doris
20
21
#include "util/parse_util.h"
22
23
#include "util/string_parser.hpp"
24
25
namespace doris {
26
int64_t ParseUtil::parse_mem_spec(const std::string& mem_spec_str, int64_t parent_limit,
27
112k
                                  int64_t physical_mem, bool* is_percent) {
28
112k
    if (mem_spec_str.empty()) {
29
0
        return 0;
30
0
    }
31
32
    // Assume last character indicates unit or percent.
33
112k
    auto number_str_len = mem_spec_str.size() - 1;
34
112k
    *is_percent = false;
35
112k
    int64_t multiplier = -1;
36
37
    // Look for accepted suffix character.
38
112k
    switch (*mem_spec_str.rbegin()) {
39
1
    case 't':
40
2
    case 'T':
41
        // Terabytes.
42
2
        multiplier = 1024L * 1024L * 1024L * 1024L;
43
2
        break;
44
1
    case 'g':
45
4
    case 'G':
46
        // Gigabytes.
47
4
        multiplier = 1024L * 1024L * 1024L;
48
4
        break;
49
1
    case 'm':
50
5
    case 'M':
51
        // Megabytes.
52
5
        multiplier = 1024L * 1024L;
53
5
        break;
54
1
    case 'k':
55
2
    case 'K':
56
        // Kilobytes
57
2
        multiplier = 1024L;
58
2
        break;
59
10
    case 'b':
60
11
    case 'B':
61
11
        break;
62
112k
    case '%':
63
112k
        *is_percent = true;
64
112k
        break;
65
8
    default:
66
        // No unit was given. Default to bytes.
67
8
        number_str_len = (int)mem_spec_str.size();
68
8
        break;
69
112k
    }
70
71
112k
    StringParser::ParseResult result;
72
112k
    int64_t bytes = -1;
73
74
112k
    if (multiplier != -1 || *is_percent) {
75
        // Parse float - MB or GB or percent
76
112k
        double limit_val =
77
112k
                StringParser::string_to_float<double>(mem_spec_str.data(), number_str_len, &result);
78
79
112k
        if (result != StringParser::PARSE_SUCCESS) {
80
3
            return -1;
81
3
        }
82
83
112k
        if (multiplier != -1) {
84
13
            bytes = int64_t((double)multiplier * limit_val);
85
112k
        } else if (*is_percent) {
86
112k
            if (parent_limit == -1) {
87
112k
                bytes = int64_t(limit_val / 100.0 * (double)physical_mem);
88
112k
            } else {
89
43
                bytes = int64_t(limit_val / 100.0 * (double)parent_limit);
90
43
            }
91
112k
        }
92
112k
    } else {
93
        // Parse int - bytes
94
19
        int64_t limit_val =
95
19
                StringParser::string_to_int<int64_t>(mem_spec_str.data(), number_str_len, &result);
96
97
19
        if (result != StringParser::PARSE_SUCCESS) {
98
9
            return -1;
99
9
        }
100
101
10
        auto limit_val_double =
102
10
                StringParser::string_to_float<double>(mem_spec_str.data(), number_str_len, &result);
103
10
        if (result == StringParser::PARSE_SUCCESS && limit_val_double != (double)limit_val) {
104
1
            return -1; // mem_spec_str is double.
105
1
        }
106
107
9
        bytes = limit_val;
108
9
    }
109
110
    // Accept -1 as indicator for infinite memory that we report by a 0 return value.
111
112k
    if (bytes == -1) {
112
0
        return 0;
113
0
    }
114
112k
    return bytes;
115
112k
}
116
117
} // namespace doris