Coverage Report

Created: 2026-07-27 15:00

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