Skip to main content

parquet_index/cli/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5/// Parses a dot-separated Parquet column path, using `\` as an escape character
6/// for literal dots and backslashes.
7pub fn parse_column_path(path: &str) -> Result<Vec<String>, String> {
8    let mut parts = Vec::new();
9    let mut current = String::new();
10    let mut escaped = false;
11
12    for c in path.chars() {
13        if escaped {
14            match c {
15                '.' | '\\' => current.push(c),
16                _ => {
17                    return Err(format!(
18                        "Invalid escape sequence \\{c} in column path {path}"
19                    ));
20                }
21            }
22            escaped = false;
23            continue;
24        }
25
26        match c {
27            '\\' => escaped = true,
28            '.' => {
29                parts.push(current);
30                current = String::new();
31            }
32            _ => current.push(c),
33        }
34    }
35
36    if escaped {
37        return Err(format!(
38            "Column path {path} ends with an incomplete escape sequence"
39        ));
40    }
41
42    parts.push(current);
43    Ok(parts)
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn parse_column_path_splits_unescaped_dots() {
52        assert_eq!(parse_column_path("a.b.c").unwrap(), vec!["a", "b", "c"]);
53    }
54
55    #[test]
56    fn parse_column_path_allows_escaped_dots() {
57        assert_eq!(parse_column_path(r"a\.b.c").unwrap(), vec!["a.b", "c"]);
58    }
59
60    #[test]
61    fn parse_column_path_allows_escaped_backslashes() {
62        assert_eq!(parse_column_path(r"a\\b.c").unwrap(), vec![r"a\b", "c"]);
63    }
64
65    #[test]
66    fn parse_column_path_invalid_escape_sequence() {
67        let err = parse_column_path(r"a.\b.c").unwrap_err();
68        assert!(err.contains("Invalid escape sequence"));
69    }
70
71    #[test]
72    fn parse_column_path_incomplete_escape_sequence() {
73        let err = parse_column_path(r"a.b.c\").unwrap_err();
74        assert!(err.contains("incomplete escape sequence"));
75    }
76}