-
Notifications
You must be signed in to change notification settings - Fork 128
feat: implement DuckDB filesystem integration for Vortex file handling #6198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iceboundrock
wants to merge
12
commits into
vortex-data:develop
Choose a base branch
from
Lychee-Technology:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+744
−509
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c408ffa
feat: implement DuckDB filesystem integration for Vortex file handling
ruoshili 4fffe00
refactor(tests): update context setup for DuckDB glob tests
ruoshili 94e617b
Merge branch 'vortex-data:develop' into develop
iceboundrock a131078
refactor: address PR comments.
ruoshili 7e51cf2
Merge branch 'vortex-data:develop' into develop
iceboundrock c5b1e85
Merge branch 'vortex-data:develop' into develop
iceboundrock 93e17a3
Merge branch 'vortex-data:develop' into develop
iceboundrock 4a07f61
refactor: remove unused dependencies and improve error handling in Du…
ruoshili 3778b8f
Update vortex-duckdb/cpp/error.cpp
iceboundrock 7b5d893
Merge branch 'vortex-data:develop' into develop
iceboundrock 061124b
refactor: rename string list to URI list and simplify glob impl in sc…
ruoshili a43872a
Merge branch 'vortex-data:develop' into develop
iceboundrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #include "duckdb_vx.h" | ||
| #include "duckdb_vx/error.hpp" | ||
|
|
||
| #include <duckdb/common/exception.hpp> | ||
| #include <duckdb/common/file_system.hpp> | ||
| #include <duckdb/common/helper.hpp> | ||
| #include <duckdb/main/client_context.hpp> | ||
|
|
||
| #include <cstring> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| using namespace duckdb; | ||
|
|
||
| struct FileHandleWrapper { | ||
| explicit FileHandleWrapper(unique_ptr<FileHandle> handle_p) : handle(std::move(handle_p)) { | ||
| } | ||
|
|
||
| unique_ptr<FileHandle> handle; | ||
| }; | ||
|
|
||
| using vortex::HandleException; | ||
| using vortex::SetError; | ||
|
|
||
| extern "C" duckdb_vx_file_handle duckdb_vx_fs_open(duckdb_vx_client_context ctx, const char *path, | ||
| duckdb_vx_error *error_out) { | ||
| if (!ctx || !path) { | ||
| SetError(error_out, "Invalid filesystem open arguments"); | ||
| return nullptr; | ||
| } | ||
|
|
||
| try { | ||
| auto *client_context = reinterpret_cast<ClientContext *>(ctx); | ||
| auto &fs = FileSystem::GetFileSystem(*client_context); | ||
| auto handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_READ | FileFlags::FILE_FLAGS_PARALLEL_ACCESS); | ||
| return reinterpret_cast<duckdb_vx_file_handle>(new FileHandleWrapper(std::move(handle))); | ||
| } catch (...) { | ||
| HandleException(std::current_exception(), error_out); | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| extern "C" duckdb_vx_file_handle duckdb_vx_fs_create(duckdb_vx_client_context ctx, const char *path, | ||
| duckdb_vx_error *error_out) { | ||
| if (!ctx || !path) { | ||
| SetError(error_out, "Invalid filesystem create arguments"); | ||
| return nullptr; | ||
| } | ||
|
|
||
| try { | ||
| auto *client_context = reinterpret_cast<ClientContext *>(ctx); | ||
| auto &fs = FileSystem::GetFileSystem(*client_context); | ||
| auto handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE | | ||
| FileFlags::FILE_FLAGS_PARALLEL_ACCESS); | ||
| handle->Truncate(0); | ||
| return reinterpret_cast<duckdb_vx_file_handle>(new FileHandleWrapper(std::move(handle))); | ||
| } catch (...) { | ||
| HandleException(std::current_exception(), error_out); | ||
| return nullptr; | ||
| } | ||
| } | ||
|
|
||
| extern "C" void duckdb_vx_fs_close(duckdb_vx_file_handle *handle) { | ||
| if (!handle || !*handle) { | ||
| return; | ||
| } | ||
| auto wrapper = reinterpret_cast<FileHandleWrapper *>(*handle); | ||
| delete wrapper; | ||
| *handle = nullptr; | ||
| } | ||
|
|
||
| extern "C" duckdb_state duckdb_vx_fs_get_size(duckdb_vx_file_handle handle, idx_t *size_out, | ||
| duckdb_vx_error *error_out) { | ||
| if (!handle || !size_out) { | ||
| SetError(error_out, "Invalid arguments to fs_get_size"); | ||
| return DuckDBError; | ||
| } | ||
|
|
||
| try { | ||
| auto *wrapper = reinterpret_cast<FileHandleWrapper *>(handle); | ||
| *size_out = wrapper->handle->GetFileSize(); | ||
| return DuckDBSuccess; | ||
| } catch (...) { | ||
| return HandleException(std::current_exception(), error_out); | ||
| } | ||
| } | ||
|
|
||
| extern "C" duckdb_state duckdb_vx_fs_read(duckdb_vx_file_handle handle, idx_t offset, idx_t len, uint8_t *buffer, | ||
| idx_t *out_len, duckdb_vx_error *error_out) { | ||
| if (!handle || !buffer || !out_len) { | ||
| SetError(error_out, "Invalid arguments to fs_read"); | ||
| return DuckDBError; | ||
| } | ||
|
|
||
| try { | ||
| auto *wrapper = reinterpret_cast<FileHandleWrapper *>(handle); | ||
| wrapper->handle->Read(buffer, len, offset); | ||
| *out_len = len; | ||
| return DuckDBSuccess; | ||
| } catch (...) { | ||
| return HandleException(std::current_exception(), error_out); | ||
| } | ||
| } | ||
|
|
||
| extern "C" duckdb_state duckdb_vx_fs_write(duckdb_vx_file_handle handle, idx_t offset, idx_t len, | ||
| const uint8_t *buffer, idx_t *out_len, | ||
| duckdb_vx_error *error_out) { | ||
| if (!handle || !buffer || !out_len) { | ||
| SetError(error_out, "Invalid arguments to fs_write"); | ||
| return DuckDBError; | ||
| } | ||
|
|
||
| try { | ||
| auto *wrapper = reinterpret_cast<FileHandleWrapper *>(handle); | ||
| wrapper->handle->Write(QueryContext(), const_cast<uint8_t *>(buffer), len, offset); | ||
| *out_len = len; | ||
| return DuckDBSuccess; | ||
| } catch (...) { | ||
| return HandleException(std::current_exception(), error_out); | ||
| } | ||
| } | ||
|
|
||
| extern "C" duckdb_vx_uri_list duckdb_vx_fs_glob(duckdb_vx_client_context ctx, const char *pattern, | ||
| duckdb_vx_error *error_out) { | ||
| duckdb_vx_uri_list result{nullptr, 0}; | ||
|
|
||
| if (!ctx || !pattern) { | ||
| SetError(error_out, "Invalid arguments to fs_glob"); | ||
| return result; | ||
| } | ||
|
|
||
| try { | ||
| auto *client_context = reinterpret_cast<ClientContext *>(ctx); | ||
| auto &fs = FileSystem::GetFileSystem(*client_context); | ||
| auto matches = fs.Glob(pattern); | ||
|
|
||
| if (matches.empty()) { | ||
| return result; | ||
| } | ||
|
|
||
| result.count = matches.size(); | ||
| result.entries = static_cast<const char **>(duckdb_malloc(sizeof(char *) * matches.size())); | ||
| for (size_t i = 0; i < matches.size(); i++) { | ||
| const auto &entry = matches[i].path; | ||
| auto *owned = static_cast<char *>(duckdb_malloc(entry.size() + 1)); | ||
| std::memcpy(owned, entry.data(), entry.size()); | ||
| owned[entry.size()] = '\0'; | ||
| result.entries[i] = owned; | ||
| } | ||
|
|
||
| return result; | ||
| } catch (...) { | ||
| HandleException(std::current_exception(), error_out); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| extern "C" void duckdb_vx_uri_list_free(duckdb_vx_uri_list *list) { | ||
| if (!list || !list->entries) { | ||
| return; | ||
| } | ||
| for (size_t i = 0; i < list->count; i++) { | ||
| duckdb_free(const_cast<char *>(list->entries[i])); | ||
| } | ||
| duckdb_free(list->entries); | ||
| list->entries = nullptr; | ||
| list->count = 0; | ||
| } | ||
|
|
||
| extern "C" duckdb_state duckdb_vx_fs_sync(duckdb_vx_file_handle handle, duckdb_vx_error *error_out) { | ||
| if (!handle) { | ||
| SetError(error_out, "Invalid arguments to fs_sync"); | ||
| return DuckDBError; | ||
| } | ||
|
|
||
| try { | ||
| auto *wrapper = reinterpret_cast<FileHandleWrapper *>(handle); | ||
| wrapper->handle->Sync(); | ||
| return DuckDBSuccess; | ||
| } catch (...) { | ||
| return HandleException(std::current_exception(), error_out); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "duckdb.h" | ||
| #include "duckdb_vx/client_context.h" | ||
| #include "duckdb_vx/error.h" | ||
|
|
||
| #ifdef __cplusplus /* If compiled as C++, use C ABI */ | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef struct duckdb_vx_file_handle_ *duckdb_vx_file_handle; | ||
|
|
||
| typedef struct { | ||
| const char **entries; | ||
| size_t count; | ||
| } duckdb_vx_uri_list; | ||
|
|
||
| // Open a file using DuckDB's filesystem (supports httpfs, s3, etc.). | ||
| duckdb_vx_file_handle duckdb_vx_fs_open(duckdb_vx_client_context ctx, const char *path, | ||
| duckdb_vx_error *error_out); | ||
|
|
||
| // Close a previously opened file handle. | ||
| void duckdb_vx_fs_close(duckdb_vx_file_handle *handle); | ||
|
|
||
| // Get the size of an opened file. | ||
| duckdb_state duckdb_vx_fs_get_size(duckdb_vx_file_handle handle, idx_t *size_out, | ||
| duckdb_vx_error *error_out); | ||
|
|
||
| // Read up to len bytes at the given offset into buffer. Returns bytes read via out_len. | ||
| duckdb_state duckdb_vx_fs_read(duckdb_vx_file_handle handle, idx_t offset, idx_t len, uint8_t *buffer, | ||
| idx_t *out_len, duckdb_vx_error *error_out); | ||
|
|
||
| // Expand a glob using DuckDB's filesystem. | ||
| duckdb_vx_uri_list duckdb_vx_fs_glob(duckdb_vx_client_context ctx, const char *pattern, | ||
| duckdb_vx_error *error_out); | ||
|
|
||
| // Free a string list allocated by duckdb_vx_fs_glob. | ||
| void duckdb_vx_uri_list_free(duckdb_vx_uri_list *list); | ||
|
|
||
| // Create/truncate a file for writing using DuckDB's filesystem. | ||
| duckdb_vx_file_handle duckdb_vx_fs_create(duckdb_vx_client_context ctx, const char *path, | ||
| duckdb_vx_error *error_out); | ||
|
|
||
| // Write len bytes at the given offset from buffer. | ||
| duckdb_state duckdb_vx_fs_write(duckdb_vx_file_handle handle, idx_t offset, idx_t len, const uint8_t *buffer, | ||
| idx_t *out_len, duckdb_vx_error *error_out); | ||
|
|
||
| // Flush pending writes to storage. | ||
| duckdb_state duckdb_vx_fs_sync(duckdb_vx_file_handle handle, duckdb_vx_error *error_out); | ||
|
|
||
| #ifdef __cplusplus /* End C ABI */ | ||
| } | ||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.