fix!: sort table names (#619)

https://github.com/lancedb/lance/issues/1385
This commit is contained in:
Bert
2023-11-01 10:50:09 -04:00
committed by GitHub
parent 7eec2b8f9a
commit 24111d543a
3 changed files with 22 additions and 5 deletions

View File

@@ -263,7 +263,7 @@ class LanceDBConnection(DBConnection):
return self._uri
def table_names(self) -> list[str]:
"""Get the names of all tables in the database.
"""Get the names of all tables in the database. The names are sorted.
Returns
-------
@@ -287,6 +287,7 @@ class LanceDBConnection(DBConnection):
for file_info in paths
if file_info.extension == "lance"
]
tables.sort()
return tables
def __len__(self) -> int:

View File

@@ -150,6 +150,21 @@ def test_ingest_iterator(tmp_path):
run_tests(PydanticSchema)
def test_table_names(tmp_path):
db = lancedb.connect(tmp_path)
data = pd.DataFrame(
{
"vector": [[3.1, 4.1], [5.9, 26.5]],
"item": ["foo", "bar"],
"price": [10.0, 20.0],
}
)
db.create_table("test2", data=data)
db.create_table("test1", data=data)
db.create_table("test3", data=data)
assert db.table_names() == ["test1", "test2", "test3"]
def test_create_mode(tmp_path):
db = lancedb.connect(tmp_path)
data = pd.DataFrame(

View File

@@ -161,7 +161,7 @@ impl Database {
///
/// * A [Vec<String>] with all table names.
pub async fn table_names(&self) -> Result<Vec<String>> {
let f = self
let mut f = self
.object_store
.read_dir(self.base_path.clone())
.await?
@@ -175,7 +175,8 @@ impl Database {
is_lance.unwrap_or(false)
})
.filter_map(|p| p.file_stem().and_then(|s| s.to_str().map(String::from)))
.collect();
.collect::<Vec<String>>();
f.sort();
Ok(f)
}
@@ -312,8 +313,8 @@ mod tests {
let db = Database::connect(uri).await.unwrap();
let tables = db.table_names().await.unwrap();
assert_eq!(tables.len(), 2);
assert!(tables.contains(&String::from("table1")));
assert!(tables.contains(&String::from("table2")));
assert!(tables[0].eq(&String::from("table1")));
assert!(tables[1].eq(&String::from("table2")));
}
#[tokio::test]