Skip to content

Commit

Permalink
feat: issue apache#9224 substitute tlide in table path (apache#9259)
Browse files Browse the repository at this point in the history
* substitute tilde in file path

* change test

* disable test on windows

* fix typo
  • Loading branch information
Lordworms authored Feb 28, 2024
1 parent cf69d38 commit f68864b
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion datafusion-cli/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use datafusion::datasource::listing::{
use datafusion::datasource::TableProvider;
use datafusion::error::Result;
use datafusion::execution::context::SessionState;
use dirs::home_dir;
use parking_lot::RwLock;
use std::any::Any;
use std::collections::HashMap;
Expand Down Expand Up @@ -160,7 +161,8 @@ impl SchemaProvider for DynamicFileSchemaProvider {
.ok_or_else(|| plan_datafusion_err!("locking error"))?
.read()
.clone();
let table_url = ListingTableUrl::parse(name)?;
let optimized_name = substitute_tilde(name.to_owned());
let table_url = ListingTableUrl::parse(optimized_name.as_str())?;
let url: &Url = table_url.as_ref();

// If the store is already registered for this URL then `get_store`
Expand Down Expand Up @@ -199,6 +201,16 @@ impl SchemaProvider for DynamicFileSchemaProvider {
self.inner.table_exist(name)
}
}
fn substitute_tilde(cur: String) -> String {
if let Some(usr_dir_path) = home_dir() {
if let Some(usr_dir) = usr_dir_path.to_str() {
if cur.starts_with('~') && !usr_dir.is_empty() {
return cur.replacen('~', usr_dir, 1);
}
}
}
cur
}

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -303,4 +315,42 @@ mod tests {

assert!(schema.table(location).await.is_err());
}
#[cfg(not(target_os = "windows"))]
#[test]
fn test_substitute_tilde() {
use std::env;
use std::path::MAIN_SEPARATOR;
let original_home = home_dir();
let test_home_path = if cfg!(windows) {
"C:\\Users\\user"
} else {
"/home/user"
};
env::set_var(
if cfg!(windows) { "USERPROFILE" } else { "HOME" },
test_home_path,
);
let input =
"~/Code/arrow-datafusion/benchmarks/data/tpch_sf1/part/part-0.parquet";
let expected = format!(
"{}{}Code{}arrow-datafusion{}benchmarks{}data{}tpch_sf1{}part{}part-0.parquet",
test_home_path,
MAIN_SEPARATOR,
MAIN_SEPARATOR,
MAIN_SEPARATOR,
MAIN_SEPARATOR,
MAIN_SEPARATOR,
MAIN_SEPARATOR,
MAIN_SEPARATOR
);
let actual = substitute_tilde(input.to_string());
assert_eq!(actual, expected);
match original_home {
Some(home_path) => env::set_var(
if cfg!(windows) { "USERPROFILE" } else { "HOME" },
home_path.to_str().unwrap(),
),
None => env::remove_var(if cfg!(windows) { "USERPROFILE" } else { "HOME" }),
}
}
}

0 comments on commit f68864b

Please sign in to comment.