prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  /search and /filter are currently broken
  Any search or filter is currently failing.
  
  https://datasets-server.huggingface.co/search?dataset=gsarti%2Fflores_101&config=afr&split=devtest&offset=0&length=100&query=a
  https://datasets-server.huggingface.co/filter?dataset=gsarti%2Fflores_101&config=afr&split=devtest&offset=0&length=100&where=id%3E%3D409+and+id%3C511
  
  Logs:
  
  ```
  FileNotFoundError: [Errno 2] No such file or directory: '/tmp/duckdb-index'
  ```
  
  when doing `disk_stat = os.statvfs(path)` here: 
  
  https://github.com/huggingface/dataset-viewer/blob/a489c0b6ad4e5a78b2670679abbfab93f9be5742/libs/libapi/src/libapi/duckdb.py#L78
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `libs/libapi/src/libapi/duckdb.py`
  Content:
  ```
  1 # SPDX-License-Identifier: Apache-2.0
  2 # Copyright 2023 The HuggingFace Authors.
  3 
  4 import errno
  5 import json
  6 import logging
  7 import os
  8 import re
  9 from hashlib import sha1
  10 from typing import Optional
  11 
  12 import anyio
  13 from anyio import Path
  14 from libcommon.constants import DUCKDB_INDEX_DOWNLOADS_SUBDIRECTORY, SPLIT_DUCKDB_INDEX_KIND
  15 from libcommon.parquet_utils import extract_split_name_from_parquet_url
  16 from libcommon.prometheus import StepProfiler
  17 from libcommon.simple_cache import CacheEntry
  18 from libcommon.storage import StrPath, init_dir
  19 from libcommon.storage_client import StorageClient
  20 from libcommon.utils import download_file_from_hub
  21 
  22 from libapi.exceptions import DownloadIndexError
  23 from libapi.utils import get_cache_entry_from_step
  24 
  25 REPO_TYPE = "dataset"
  26 HUB_DOWNLOAD_CACHE_FOLDER = "cache"
  27 
  28 
  29 async def get_index_file_location_and_download_if_missing(
  30     duckdb_index_file_directory: StrPath,
  31     dataset: str,
  32     revision: str,
  33     config: str,
  34     split: str,
  35     filename: str,
  36     size_bytes: int,
  37     url: str,
  38     target_revision: str,
  39     hf_token: Optional[str],
  40 ) -> str:
  41     with StepProfiler(method="get_index_file_location_and_download_if_missing", step="all"):
  42         index_folder = get_download_folder(duckdb_index_file_directory, size_bytes, dataset, config, split, revision)
  43         # For directories like "partial-train" for the file
  44         # at "en/partial-train/0000.parquet" in the C4 dataset.
  45         # Note that "-" is forbidden for split names, so it doesn't create directory names collisions.
  46         split_directory = extract_split_name_from_parquet_url(url)
  47         repo_file_location = f"{config}/{split_directory}/{filename}"
  48         index_file_location = f"{index_folder}/{repo_file_location}"
  49         index_path = Path(index_file_location)
  50         if not await index_path.is_file():
  51             with StepProfiler(method="get_index_file_location_and_download_if_missing", step="download index file"):
  52                 cache_folder = f"{duckdb_index_file_directory}/{HUB_DOWNLOAD_CACHE_FOLDER}"
  53                 await anyio.to_thread.run_sync(
  54                     download_index_file,
  55                     cache_folder,
  56                     index_folder,
  57                     target_revision,
  58                     dataset,
  59                     repo_file_location,
  60                     hf_token,
  61                 )
  62         # Update its modification time
  63         await index_path.touch()
  64         return index_file_location
  65 
  66 
  67 def get_download_folder(
  68     root_directory: StrPath, size_bytes: int, dataset: str, revision: str, config: str, split: str
  69 ) -> str:
  70     check_available_disk_space(root_directory, size_bytes)
  71     payload = (dataset, config, split, revision)
  72     hash_suffix = sha1(json.dumps(payload, sort_keys=True).encode(), usedforsecurity=False).hexdigest()[:8]
  73     subdirectory = "".join([c if re.match(r"[\w-]", c) else "-" for c in f"{dataset}-{hash_suffix}"])
  74     return f"{root_directory}/{DUCKDB_INDEX_DOWNLOADS_SUBDIRECTORY}/{subdirectory}"
  75 
  76 
  77 def check_available_disk_space(path: StrPath, required_space: int) -> None:
  78     disk_stat = os.statvfs(path)
  79     # Calculate free space in bytes
  80     free_space = disk_stat.f_bavail * disk_stat.f_frsize
  81     logging.debug(f"{free_space} available space, needed {required_space}")
  82     if free_space < required_space:
  83         raise DownloadIndexError(
  84             "Cannot perform the search due to a lack of disk space on the server. Please report the issue."
  85         )
  86 
  87 
  88 def download_index_file(
  89     cache_folder: str,
  90     index_folder: str,
  91     target_revision: str,
  92     dataset: str,
  93     repo_file_location: str,
  94     hf_token: Optional[str] = None,
  95 ) -> None:
  96     logging.info(f"init_dir {index_folder}")
  97     try:
  98         init_dir(index_folder)
  99         download_file_from_hub(
  100             repo_type=REPO_TYPE,
  101             revision=target_revision,
  102             repo_id=dataset,
  103             filename=repo_file_location,
  104             local_dir=index_folder,
  105             hf_token=hf_token,
  106             cache_dir=cache_folder,
  107         )
  108     except OSError as err:
  109         if err.errno == errno.ENOSPC:
  110             raise DownloadIndexError(
  111                 "Cannot perform the operation due to a lack of disk space on the server. Please report the issue.", err
  112             )
  113 
  114 
  115 def get_cache_entry_from_duckdb_index_job(
  116     dataset: str,
  117     config: str,
  118     split: str,
  119     hf_endpoint: str,
  120     hf_token: Optional[str],
  121     hf_timeout_seconds: Optional[float],
  122     blocked_datasets: list[str],
  123     storage_clients: Optional[list[StorageClient]] = None,
  124 ) -> CacheEntry:
  125     return get_cache_entry_from_step(
  126         processing_step_name=SPLIT_DUCKDB_INDEX_KIND,
  127         dataset=dataset,
  128         config=config,
  129         split=split,
  130         hf_endpoint=hf_endpoint,
  131         hf_token=hf_token,
  132         hf_timeout_seconds=hf_timeout_seconds,
  133         blocked_datasets=blocked_datasets,
  134         storage_clients=storage_clients,
  135     )
  136
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/libs/libapi/src/libapi/duckdb.py b/libs/libapi/src/libapi/duckdb.py
  --- a/libs/libapi/src/libapi/duckdb.py
  +++ b/libs/libapi/src/libapi/duckdb.py
  @@ -75,7 +75,12 @@
   
   
   def check_available_disk_space(path: StrPath, required_space: int) -> None:
  -    disk_stat = os.statvfs(path)
  +    try:
  +        disk_stat = os.statvfs(path)
  +    except FileNotFoundError:
  +        # The path does not exist, we create it and
  +        init_dir(path)
  +        disk_stat = os.statvfs(path)
       # Calculate free space in bytes
       free_space = disk_stat.f_bavail * disk_stat.f_frsize
       logging.debug(f"{free_space} available space, needed {required_space}")
  
verification_info: {"golden_diff": "diff --git a/libs/libapi/src/libapi/duckdb.py b/libs/libapi/src/libapi/duckdb.py\n--- a/libs/libapi/src/libapi/duckdb.py\n+++ b/libs/libapi/src/libapi/duckdb.py\n@@ -75,7 +75,12 @@\n \n \n def check_available_disk_space(path: StrPath, required_space: int) -> None:\n-    disk_stat = os.statvfs(path)\n+    try:\n+        disk_stat = os.statvfs(path)\n+    except FileNotFoundError:\n+        # The path does not exist, we create it and\n+        init_dir(path)\n+        disk_stat = os.statvfs(path)\n     # Calculate free space in bytes\n     free_space = disk_stat.f_bavail * disk_stat.f_frsize\n     logging.debug(f\"{free_space} available space, needed {required_space}\")\n", "issue": "/search and /filter are currently broken\nAny search or filter is currently failing.\r\n\r\nhttps://datasets-server.huggingface.co/search?dataset=gsarti%2Fflores_101&config=afr&split=devtest&offset=0&length=100&query=a\r\nhttps://datasets-server.huggingface.co/filter?dataset=gsarti%2Fflores_101&config=afr&split=devtest&offset=0&length=100&where=id%3E%3D409+and+id%3C511\r\n\r\nLogs:\r\n\r\n```\r\nFileNotFoundError: [Errno 2] No such file or directory: '/tmp/duckdb-index'\r\n```\r\n\r\nwhen doing `disk_stat = os.statvfs(path)` here: \r\n\r\nhttps://github.com/huggingface/dataset-viewer/blob/a489c0b6ad4e5a78b2670679abbfab93f9be5742/libs/libapi/src/libapi/duckdb.py#L78\n", "before_files": [{"content": "# SPDX-License-Identifier: Apache-2.0\n# Copyright 2023 The HuggingFace Authors.\n\nimport errno\nimport json\nimport logging\nimport os\nimport re\nfrom hashlib import sha1\nfrom typing import Optional\n\nimport anyio\nfrom anyio import Path\nfrom libcommon.constants import DUCKDB_INDEX_DOWNLOADS_SUBDIRECTORY, SPLIT_DUCKDB_INDEX_KIND\nfrom libcommon.parquet_utils import extract_split_name_from_parquet_url\nfrom libcommon.prometheus import StepProfiler\nfrom libcommon.simple_cache import CacheEntry\nfrom libcommon.storage import StrPath, init_dir\nfrom libcommon.storage_client import StorageClient\nfrom libcommon.utils import download_file_from_hub\n\nfrom libapi.exceptions import DownloadIndexError\nfrom libapi.utils import get_cache_entry_from_step\n\nREPO_TYPE = \"dataset\"\nHUB_DOWNLOAD_CACHE_FOLDER = \"cache\"\n\n\nasync def get_index_file_location_and_download_if_missing(\n    duckdb_index_file_directory: StrPath,\n    dataset: str,\n    revision: str,\n    config: str,\n    split: str,\n    filename: str,\n    size_bytes: int,\n    url: str,\n    target_revision: str,\n    hf_token: Optional[str],\n) -> str:\n    with StepProfiler(method=\"get_index_file_location_and_download_if_missing\", step=\"all\"):\n        index_folder = get_download_folder(duckdb_index_file_directory, size_bytes, dataset, config, split, revision)\n        # For directories like \"partial-train\" for the file\n        # at \"en/partial-train/0000.parquet\" in the C4 dataset.\n        # Note that \"-\" is forbidden for split names, so it doesn't create directory names collisions.\n        split_directory = extract_split_name_from_parquet_url(url)\n        repo_file_location = f\"{config}/{split_directory}/{filename}\"\n        index_file_location = f\"{index_folder}/{repo_file_location}\"\n        index_path = Path(index_file_location)\n        if not await index_path.is_file():\n            with StepProfiler(method=\"get_index_file_location_and_download_if_missing\", step=\"download index file\"):\n                cache_folder = f\"{duckdb_index_file_directory}/{HUB_DOWNLOAD_CACHE_FOLDER}\"\n                await anyio.to_thread.run_sync(\n                    download_index_file,\n                    cache_folder,\n                    index_folder,\n                    target_revision,\n                    dataset,\n                    repo_file_location,\n                    hf_token,\n                )\n        # Update its modification time\n        await index_path.touch()\n        return index_file_location\n\n\ndef get_download_folder(\n    root_directory: StrPath, size_bytes: int, dataset: str, revision: str, config: str, split: str\n) -> str:\n    check_available_disk_space(root_directory, size_bytes)\n    payload = (dataset, config, split, revision)\n    hash_suffix = sha1(json.dumps(payload, sort_keys=True).encode(), usedforsecurity=False).hexdigest()[:8]\n    subdirectory = \"\".join([c if re.match(r\"[\\w-]\", c) else \"-\" for c in f\"{dataset}-{hash_suffix}\"])\n    return f\"{root_directory}/{DUCKDB_INDEX_DOWNLOADS_SUBDIRECTORY}/{subdirectory}\"\n\n\ndef check_available_disk_space(path: StrPath, required_space: int) -> None:\n    disk_stat = os.statvfs(path)\n    # Calculate free space in bytes\n    free_space = disk_stat.f_bavail * disk_stat.f_frsize\n    logging.debug(f\"{free_space} available space, needed {required_space}\")\n    if free_space < required_space:\n        raise DownloadIndexError(\n            \"Cannot perform the search due to a lack of disk space on the server. Please report the issue.\"\n        )\n\n\ndef download_index_file(\n    cache_folder: str,\n    index_folder: str,\n    target_revision: str,\n    dataset: str,\n    repo_file_location: str,\n    hf_token: Optional[str] = None,\n) -> None:\n    logging.info(f\"init_dir {index_folder}\")\n    try:\n        init_dir(index_folder)\n        download_file_from_hub(\n            repo_type=REPO_TYPE,\n            revision=target_revision,\n            repo_id=dataset,\n            filename=repo_file_location,\n            local_dir=index_folder,\n            hf_token=hf_token,\n            cache_dir=cache_folder,\n        )\n    except OSError as err:\n        if err.errno == errno.ENOSPC:\n            raise DownloadIndexError(\n                \"Cannot perform the operation due to a lack of disk space on the server. Please report the issue.\", err\n            )\n\n\ndef get_cache_entry_from_duckdb_index_job(\n    dataset: str,\n    config: str,\n    split: str,\n    hf_endpoint: str,\n    hf_token: Optional[str],\n    hf_timeout_seconds: Optional[float],\n    blocked_datasets: list[str],\n    storage_clients: Optional[list[StorageClient]] = None,\n) -> CacheEntry:\n    return get_cache_entry_from_step(\n        processing_step_name=SPLIT_DUCKDB_INDEX_KIND,\n        dataset=dataset,\n        config=config,\n        split=split,\n        hf_endpoint=hf_endpoint,\n        hf_token=hf_token,\n        hf_timeout_seconds=hf_timeout_seconds,\n        blocked_datasets=blocked_datasets,\n        storage_clients=storage_clients,\n    )\n", "path": "libs/libapi/src/libapi/duckdb.py"}], "after_files": [{"content": "# SPDX-License-Identifier: Apache-2.0\n# Copyright 2023 The HuggingFace Authors.\n\nimport errno\nimport json\nimport logging\nimport os\nimport re\nfrom hashlib import sha1\nfrom typing import Optional\n\nimport anyio\nfrom anyio import Path\nfrom libcommon.constants import DUCKDB_INDEX_DOWNLOADS_SUBDIRECTORY, SPLIT_DUCKDB_INDEX_KIND\nfrom libcommon.parquet_utils import extract_split_name_from_parquet_url\nfrom libcommon.prometheus import StepProfiler\nfrom libcommon.simple_cache import CacheEntry\nfrom libcommon.storage import StrPath, init_dir\nfrom libcommon.storage_client import StorageClient\nfrom libcommon.utils import download_file_from_hub\n\nfrom libapi.exceptions import DownloadIndexError\nfrom libapi.utils import get_cache_entry_from_step\n\nREPO_TYPE = \"dataset\"\nHUB_DOWNLOAD_CACHE_FOLDER = \"cache\"\n\n\nasync def get_index_file_location_and_download_if_missing(\n    duckdb_index_file_directory: StrPath,\n    dataset: str,\n    revision: str,\n    config: str,\n    split: str,\n    filename: str,\n    size_bytes: int,\n    url: str,\n    target_revision: str,\n    hf_token: Optional[str],\n) -> str:\n    with StepProfiler(method=\"get_index_file_location_and_download_if_missing\", step=\"all\"):\n        index_folder = get_download_folder(duckdb_index_file_directory, size_bytes, dataset, config, split, revision)\n        # For directories like \"partial-train\" for the file\n        # at \"en/partial-train/0000.parquet\" in the C4 dataset.\n        # Note that \"-\" is forbidden for split names, so it doesn't create directory names collisions.\n        split_directory = extract_split_name_from_parquet_url(url)\n        repo_file_location = f\"{config}/{split_directory}/{filename}\"\n        index_file_location = f\"{index_folder}/{repo_file_location}\"\n        index_path = Path(index_file_location)\n        if not await index_path.is_file():\n            with StepProfiler(method=\"get_index_file_location_and_download_if_missing\", step=\"download index file\"):\n                cache_folder = f\"{duckdb_index_file_directory}/{HUB_DOWNLOAD_CACHE_FOLDER}\"\n                await anyio.to_thread.run_sync(\n                    download_index_file,\n                    cache_folder,\n                    index_folder,\n                    target_revision,\n                    dataset,\n                    repo_file_location,\n                    hf_token,\n                )\n        # Update its modification time\n        await index_path.touch()\n        return index_file_location\n\n\ndef get_download_folder(\n    root_directory: StrPath, size_bytes: int, dataset: str, revision: str, config: str, split: str\n) -> str:\n    check_available_disk_space(root_directory, size_bytes)\n    payload = (dataset, config, split, revision)\n    hash_suffix = sha1(json.dumps(payload, sort_keys=True).encode(), usedforsecurity=False).hexdigest()[:8]\n    subdirectory = \"\".join([c if re.match(r\"[\\w-]\", c) else \"-\" for c in f\"{dataset}-{hash_suffix}\"])\n    return f\"{root_directory}/{DUCKDB_INDEX_DOWNLOADS_SUBDIRECTORY}/{subdirectory}\"\n\n\ndef check_available_disk_space(path: StrPath, required_space: int) -> None:\n    try:\n        disk_stat = os.statvfs(path)\n    except FileNotFoundError:\n        # The path does not exist, we create it and\n        init_dir(path)\n        disk_stat = os.statvfs(path)\n    # Calculate free space in bytes\n    free_space = disk_stat.f_bavail * disk_stat.f_frsize\n    logging.debug(f\"{free_space} available space, needed {required_space}\")\n    if free_space < required_space:\n        raise DownloadIndexError(\n            \"Cannot perform the search due to a lack of disk space on the server. Please report the issue.\"\n        )\n\n\ndef download_index_file(\n    cache_folder: str,\n    index_folder: str,\n    target_revision: str,\n    dataset: str,\n    repo_file_location: str,\n    hf_token: Optional[str] = None,\n) -> None:\n    logging.info(f\"init_dir {index_folder}\")\n    try:\n        init_dir(index_folder)\n        download_file_from_hub(\n            repo_type=REPO_TYPE,\n            revision=target_revision,\n            repo_id=dataset,\n            filename=repo_file_location,\n            local_dir=index_folder,\n            hf_token=hf_token,\n            cache_dir=cache_folder,\n        )\n    except OSError as err:\n        if err.errno == errno.ENOSPC:\n            raise DownloadIndexError(\n                \"Cannot perform the operation due to a lack of disk space on the server. Please report the issue.\", err\n            )\n\n\ndef get_cache_entry_from_duckdb_index_job(\n    dataset: str,\n    config: str,\n    split: str,\n    hf_endpoint: str,\n    hf_token: Optional[str],\n    hf_timeout_seconds: Optional[float],\n    blocked_datasets: list[str],\n    storage_clients: Optional[list[StorageClient]] = None,\n) -> CacheEntry:\n    return get_cache_entry_from_step(\n        processing_step_name=SPLIT_DUCKDB_INDEX_KIND,\n        dataset=dataset,\n        config=config,\n        split=split,\n        hf_endpoint=hf_endpoint,\n        hf_token=hf_token,\n        hf_timeout_seconds=hf_timeout_seconds,\n        blocked_datasets=blocked_datasets,\n        storage_clients=storage_clients,\n    )\n", "path": "libs/libapi/src/libapi/duckdb.py"}]}