Skip to main content

parquet_index/cli/
mod.rs

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/// Parses a dot-separated Parquet column path, using `\` as an escape character
19/// for literal dots and backslashes.
20pub fn parse_column_path(path: &str) -> Result<Vec<String>, String> {
21    let mut parts = Vec::new();
22    let mut current = String::new();
23    let mut escaped = false;
24
25    for c in path.chars() {
26        if escaped {
27            match c {
28                '.' | '\\' => current.push(c),
29                _ => {
30                    return Err(format!(
31                        "Invalid escape sequence \\{c} in column path {}",
32                        path
33                    ));
34                }
35            }
36            escaped = false;
37            continue;
38        }
39
40        match c {
41            '\\' => escaped = true,
42            '.' => {
43                parts.push(current);
44                current = String::new();
45            }
46            _ => current.push(c),
47        }
48    }
49
50    if escaped {
51        return Err(format!(
52            "Column path {} ends with an incomplete escape sequence",
53            path
54        ));
55    }
56
57    parts.push(current);
58    Ok(parts)
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn parse_column_path_splits_unescaped_dots() {
67        assert_eq!(parse_column_path("a.b.c").unwrap(), vec!["a", "b", "c"]);
68    }
69
70    #[test]
71    fn parse_column_path_allows_escaped_dots() {
72        assert_eq!(parse_column_path(r"a\.b.c").unwrap(), vec!["a.b", "c"]);
73    }
74
75    #[test]
76    fn parse_column_path_allows_escaped_backslashes() {
77        assert_eq!(parse_column_path(r"a\\b.c").unwrap(), vec![r"a\b", "c"]);
78    }
79
80    #[test]
81    fn parse_column_path_invalid_escape_sequence() {
82        let err = parse_column_path(r"a.\b.c").unwrap_err();
83        assert!(err.contains("Invalid escape sequence"));
84    }
85
86    #[test]
87    fn parse_column_path_incomplete_escape_sequence() {
88        let err = parse_column_path(r"a.b.c\").unwrap_err();
89        assert!(err.contains("incomplete escape sequence"));
90    }
91}