Skip to content

Commit

Permalink
bug(python): fix path handling in windows
Browse files Browse the repository at this point in the history
Use pathlib for local paths so that pathlib
can handle the correct separator on windows.

Closes #703
  • Loading branch information
changhiskhan committed Dec 20, 2023
1 parent b34497c commit 51c4bfa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions python/lancedb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pyarrow import fs

from .table import LanceTable, Table
from .util import fs_from_uri, get_uri_location, get_uri_scheme
from .util import fs_from_uri, get_uri_location, get_uri_scheme, join_uri

if TYPE_CHECKING:
from .common import DATA, URI
Expand Down Expand Up @@ -373,7 +373,7 @@ def drop_table(self, name: str, ignore_missing: bool = False):
"""
try:
filesystem, path = fs_from_uri(self.uri)
table_path = os.path.join(path, name + ".lance")
table_path = join_uri(path, name + ".lance")
filesystem.delete_dir(table_path)
except FileNotFoundError:
if not ignore_missing:
Expand Down
6 changes: 3 additions & 3 deletions python/lancedb/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from .embeddings import EmbeddingFunctionConfig, EmbeddingFunctionRegistry
from .pydantic import LanceModel, model_to_dict
from .query import LanceQueryBuilder, Query
from .util import fs_from_uri, safe_import_pandas, value_to_sql
from .util import fs_from_uri, safe_import_pandas, value_to_sql, join_uri
from .utils.events import register_event

if TYPE_CHECKING:
Expand Down Expand Up @@ -551,7 +551,7 @@ def to_arrow(self) -> pa.Table:

@property
def _dataset_uri(self) -> str:
return os.path.join(self._conn.uri, f"{self.name}.lance")
return join_uri(self._conn.uri, f"{self.name}.lance")

def create_index(
self,
Expand Down Expand Up @@ -597,7 +597,7 @@ def create_fts_index(self, field_names: Union[str, List[str]]):
register_event("create_fts_index")

def _get_fts_index_path(self):
return os.path.join(self._dataset_uri, "_indices", "tantivy")
return join_uri(self._dataset_uri, "_indices", "tantivy")

@cached_property
def _dataset(self) -> LanceDataset:
Expand Down
15 changes: 14 additions & 1 deletion python/lancedb/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import os
from datetime import date, datetime
from functools import singledispatch
from typing import Tuple
import pathlib
from typing import Tuple, Union
from urllib.parse import urlparse

import numpy as np
Expand Down Expand Up @@ -84,6 +85,18 @@ def fs_from_uri(uri: str) -> Tuple[pa_fs.FileSystem, str]:
return pa_fs.FileSystem.from_uri(uri)


def join_uri(base: Union[str, pathlib.Path], *parts: str) -> str:
"""
Join a URI with multiple parts, handling extra environment variables.
"""
if isinstance(base, pathlib.Path):
return base.joinpath(*parts)
base = str(base)
if get_uri_scheme(base) == "file":
return str(pathlib.Path(base, *parts))
return os.path.join(base, *parts)


def safe_import_pandas():
try:
import pandas as pd
Expand Down

0 comments on commit 51c4bfa

Please sign in to comment.