-
Notifications
You must be signed in to change notification settings - Fork 182
Add ducklake_static_backup #429
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #include "functions/ducklake_table_functions.hpp" | ||
| #include "duckdb/common/types/uuid.hpp" | ||
| #include "duckdb/main/attached_database.hpp" | ||
| #include "duckdb/main/database_manager.hpp" | ||
| #include "storage/ducklake_catalog.hpp" | ||
| #include "storage/ducklake_transaction.hpp" | ||
|
|
||
| namespace duckdb { | ||
|
|
||
| struct BackupBindData : public TableFunctionData { | ||
|
|
||
| explicit BackupBindData(Catalog &catalog) : catalog(catalog) { | ||
| } | ||
|
|
||
| Catalog &catalog; | ||
| string backup_location; | ||
| }; | ||
|
|
||
| static unique_ptr<FunctionData> DuckLakeStaticBackupBind(ClientContext &context, TableFunctionBindInput &input, | ||
| vector<LogicalType> &return_types, vector<string> &names) { | ||
| auto &catalog = BaseMetadataFunction::GetCatalog(context, input.inputs[0]); | ||
| auto result = make_uniq<BackupBindData>(catalog); | ||
|
|
||
| auto &ducklake_catalog = reinterpret_cast<DuckLakeCatalog &>(catalog); | ||
| string backup_location = ducklake_catalog.GetStaticBackup(); | ||
|
|
||
| if (backup_location.empty()) { | ||
| throw InvalidInputException("static_backup not specified as attach option"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this strictly necessary? Is it possible to just accept a second parameter to the function and use that if it's not defined on the catalog? |
||
| } | ||
|
|
||
| result->backup_location = backup_location; | ||
|
|
||
| return_types.emplace_back(LogicalType::VARCHAR); | ||
| names.emplace_back("errors"); | ||
|
|
||
| return std::move(result); | ||
| } | ||
|
|
||
| struct DuckLakeBackupData : public GlobalTableFunctionState { | ||
| DuckLakeBackupData() : offset(0), executed(false) { | ||
| } | ||
|
|
||
| idx_t offset; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused |
||
| bool executed; | ||
| }; | ||
|
|
||
| unique_ptr<GlobalTableFunctionState> DuckLakeStaticBackupInit(ClientContext &context, TableFunctionInitInput &input) { | ||
| auto result = make_uniq<DuckLakeBackupData>(); | ||
| return std::move(result); | ||
| } | ||
|
|
||
| void DuckLakeStaticBackupExecute(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { | ||
| auto &data = data_p.bind_data->Cast<BackupBindData>(); | ||
| auto &state = data_p.global_state->Cast<DuckLakeBackupData>(); | ||
|
|
||
| if (!state.executed) { | ||
| auto &transaction = DuckLakeTransaction::Get(context, data.catalog); | ||
|
|
||
| auto tmp_uuid = "ducklake_backup_file." + UUID::ToString(UUID::GenerateRandomUUID()); | ||
|
|
||
| auto &fs = FileSystem::GetFileSystem(context); | ||
|
|
||
| if (fs.FileExists(tmp_uuid) || fs.FileExists(tmp_uuid + ".wal")) { | ||
| throw BinderException( | ||
| "Temporary file \"%s\" is already in use, please cleanup files in the form \"ducklake_backup_file.*\"", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is generated, we can just regenerate if this is the case, no? |
||
| tmp_uuid); | ||
| } | ||
|
|
||
| auto result = transaction.Query( | ||
| string("") + "ATTACH IF NOT EXISTS '" + tmp_uuid + | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use: StringUtil::Format(R"(
ATTACH IF NOT EXISTS '%s' AS {METADATA_CATALOG_NAME_IDENTIFIER_BACKUP} (STORAGE_VERSION 'v1.4.0');
COPY FROM DATABASE {METADATA_CATALOG_NAME_IDENTIFIER} TO {METADATA_CATALOG_NAME_IDENTIFIER_BACKUP};
DETACH {METADATA_CATALOG_NAME_IDENTIFIER_BACKUP};
COPY (SELECT content FROM read_blob('%s')) TO '%s' (FORMAT BLOB);
COPY (SELECT content FROM read_blob('%s.wal')) TO '%s.wal' (FORMAT BLOB);
)", ...); |
||
| "' AS {METADATA_CATALOG_NAME_IDENTIFIER_BACKUP} (STORAGE_VERSION 'v1.4.0');" + | ||
| "COPY FROM DATABASE {METADATA_CATALOG_NAME_IDENTIFIER} TO {METADATA_CATALOG_NAME_IDENTIFIER_BACKUP};" + | ||
| "DETACH {METADATA_CATALOG_NAME_IDENTIFIER_BACKUP};" + "COPY (SELECT content FROM read_blob('" + tmp_uuid + | ||
| "')) TO '" + data.backup_location + "' (FORMAT BLOB);" + "COPY (SELECT content FROM read_blob('" + | ||
| tmp_uuid + ".wal')) TO '" + data.backup_location + ".wal' (FORMAT BLOB);" + ""); | ||
|
|
||
| fs.TryRemoveFile(tmp_uuid); | ||
| fs.TryRemoveFile(tmp_uuid + ".wal"); | ||
|
|
||
| if (result->HasError()) { | ||
| auto &error_obj = result->GetErrorObject(); | ||
| error_obj.Throw("Failed to attach temp backup"); | ||
| } | ||
| state.executed = true; | ||
| } | ||
| idx_t count = 0; | ||
| output.SetCardinality(count); | ||
| } | ||
|
|
||
| DuckLakeStaticBackupFunction::DuckLakeStaticBackupFunction() | ||
| : TableFunction("ducklake_static_backup", {LogicalType::VARCHAR}, DuckLakeStaticBackupExecute, | ||
| DuckLakeStaticBackupBind, DuckLakeStaticBackupInit) { | ||
| } | ||
|
|
||
| } // namespace duckdb | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please wrap these in an anonymous namespace to avoid name collisions