be/src/exec/sink/writer/vhive_utils.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 | | |
18 | | #include "exec/sink/writer/vhive_utils.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <regex> |
22 | | #include <sstream> |
23 | | |
24 | | namespace doris { |
25 | | |
26 | | const std::regex VHiveUtils::PATH_CHAR_TO_ESCAPE("[\\x00-\\x1F\"#%'*/:=?\\\\\\x7F\\{\\[\\]\\^]"); |
27 | | |
28 | | std::string VHiveUtils::make_partition_name(const std::vector<THiveColumn>& columns, |
29 | | const std::vector<int>& partition_columns_input_index, |
30 | 1.29k | const std::vector<std::string>& values) { |
31 | 1.29k | std::stringstream partition_name_stream; |
32 | | |
33 | 4.94k | for (size_t i = 0; i < partition_columns_input_index.size(); i++) { |
34 | 3.64k | if (i > 0) { |
35 | 2.34k | partition_name_stream << '/'; |
36 | 2.34k | } |
37 | 3.64k | std::string column = columns[partition_columns_input_index[i]].name; |
38 | 3.64k | std::string value = values[i]; |
39 | 3.64k | std::transform(column.begin(), column.end(), column.begin(), |
40 | 25.9k | [&](char c) { return std::tolower(c); }); |
41 | 3.64k | partition_name_stream << escape_path_name(column) << '=' << escape_path_name(value); |
42 | 3.64k | } |
43 | | |
44 | 1.29k | return partition_name_stream.str(); |
45 | 1.29k | } |
46 | | |
47 | 14.2k | std::string VHiveUtils::escape_path_name(const std::string& path) { |
48 | 14.2k | if (path.empty()) { |
49 | 4 | return "__HIVE_DEFAULT_PARTITION__"; |
50 | 4 | } |
51 | | |
52 | 14.2k | std::smatch match; |
53 | 14.2k | if (!std::regex_search(path, match, PATH_CHAR_TO_ESCAPE)) { |
54 | 13.9k | return path; |
55 | 13.9k | } |
56 | | |
57 | 346 | std::stringstream ss; |
58 | 346 | size_t from_index = 0; |
59 | 346 | auto begin = path.begin(); |
60 | 346 | auto end = path.end(); |
61 | 2.51k | while (std::regex_search(begin + from_index, end, match, PATH_CHAR_TO_ESCAPE)) { |
62 | 2.17k | size_t escape_at_index = match.position() + from_index; |
63 | 2.17k | if (escape_at_index > from_index) { |
64 | 1.63k | ss << path.substr(from_index, escape_at_index - from_index); |
65 | 1.63k | } |
66 | 2.17k | char c = path[escape_at_index]; |
67 | 2.17k | ss << '%' << std::hex << std::uppercase << static_cast<int>(c >> 4) |
68 | 2.17k | << static_cast<int>(c & 0xF); |
69 | 2.17k | from_index = escape_at_index + 1; |
70 | 2.17k | } |
71 | 346 | if (from_index < path.length()) { |
72 | 298 | ss << path.substr(from_index); |
73 | 298 | } |
74 | 346 | return ss.str(); |
75 | 14.2k | } |
76 | | } // namespace doris |