diff --git a/python/lancedb/db.py b/python/lancedb/db.py index f03e9614..4d5f4bb8 100644 --- a/python/lancedb/db.py +++ b/python/lancedb/db.py @@ -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: diff --git a/python/tests/test_db.py b/python/tests/test_db.py index f7e600dc..a1f4f9c6 100644 --- a/python/tests/test_db.py +++ b/python/tests/test_db.py @@ -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( diff --git a/rust/vectordb/src/database.rs b/rust/vectordb/src/database.rs index dbffe12c..f8c97af4 100644 --- a/rust/vectordb/src/database.rs +++ b/rust/vectordb/src/database.rs @@ -161,7 +161,7 @@ impl Database { /// /// * A [Vec] with all table names. pub async fn table_names(&self) -> Result> { - 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::>(); + 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]