-
Notifications
You must be signed in to change notification settings - Fork 201
Add query result helpers and guard call sites #1061
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 |
|---|---|---|
|
|
@@ -12,9 +12,12 @@ | |
| #include "common/ducklake_data_file.hpp" | ||
| #include "common/ducklake_snapshot.hpp" | ||
| #include "duckdb/common/case_insensitive_map.hpp" | ||
| #include "duckdb/common/exception.hpp" | ||
| #include "duckdb/common/types/value_map.hpp" | ||
| #include "duckdb/main/connection.hpp" | ||
| #include "duckdb/main/query_result.hpp" | ||
| #include "duckdb/transaction/transaction.hpp" | ||
| #include <utility> | ||
| #include "storage/ducklake_catalog_set.hpp" | ||
| #include "storage/ducklake_inlined_data.hpp" | ||
| #include "storage/ducklake_metadata_manager.hpp" | ||
|
|
@@ -41,6 +44,36 @@ struct DuckLakeCommitState; | |
| class DuckLakeFieldId; | ||
| class LocalTableChangeIterationHelper; | ||
|
|
||
| inline unique_ptr<QueryResult> result_or_throw(unique_ptr<QueryResult> result, const string &err) { | ||
|
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. For these functions should we do |
||
| if (result->HasError()) { | ||
| result->GetErrorObject().Throw(err); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| inline void execute_or_throw(unique_ptr<QueryResult> result, const string &err) { | ||
| (void)result_or_throw(std::move(result), err); | ||
|
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. We need the cast?
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. actually do we even need this function? we can just ignore the result of the og function at call no? |
||
| } | ||
|
|
||
| template <class T> | ||
| T query_scalar_or_throw(unique_ptr<QueryResult> result, const string &err) { | ||
| auto checked_result = result_or_throw(std::move(result), err); | ||
| auto chunk = checked_result->Fetch(); | ||
| if (!chunk || chunk->size() == 0) { | ||
| throw InvalidInputException("Expected scalar result"); | ||
| } | ||
| return chunk->GetValue(0, 0).template GetValue<T>(); | ||
| } | ||
|
Comment on lines
+60
to
+66
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. nit: auto checked_result = result_or_throw(std::move(result), err);
for (auto &row : *checked_result) {
return row.template GetValue<T>(0);
}
throw InvalidInputException("Expected scalar result"); |
||
|
|
||
| template <class T> | ||
| T query_scalar_or_zero(unique_ptr<QueryResult> result, const string &err) { | ||
| auto checked_result = result_or_throw(std::move(result), err); | ||
| for (auto &row : *checked_result) { | ||
| return row.template GetValue<T>(0); | ||
| } | ||
| return T {}; | ||
| } | ||
|
Comment on lines
+47
to
+75
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. They don't follow name convention for methods
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. maybe we can also drop them to one function? e.g., template <class T>
T query_scalar(unique_ptr<QueryResult> result, const string &err, T default_value = T {}) {
auto checked_result = result_or_throw(std::move(result), err);
for (auto &row : *checked_result) {
return row.template GetValue<T>(0);
}
return default_value;
} |
||
|
|
||
| struct FlushedInlinedTableInfo { | ||
| DuckLakeInlinedTableInfo inlined_table; | ||
| idx_t flush_snapshot_id; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,15 +61,13 @@ string DuckLakeInitializer::GetAttachOptions() { | |
| void DuckLakeInitializer::Initialize() { | ||
| auto &transaction = DuckLakeTransaction::Get(context, catalog); | ||
| // attach the metadata database | ||
| auto result = transaction.Query("ATTACH OR REPLACE {METADATA_PATH} AS {METADATA_CATALOG_NAME_IDENTIFIER}" + | ||
| GetAttachOptions()); | ||
| if (result->HasError()) { | ||
| auto &error_obj = result->GetErrorObject(); | ||
| error_obj.Throw("Failed to attach DuckLake MetaData \"" + catalog.MetadataDatabaseName() + "\" at path + \"" + | ||
| catalog.MetadataPath() + "\""); | ||
| } | ||
| execute_or_throw( | ||
| transaction.Query("ATTACH OR REPLACE {METADATA_PATH} AS {METADATA_CATALOG_NAME_IDENTIFIER}" + | ||
| GetAttachOptions()), | ||
| "Failed to attach DuckLake MetaData \"" + catalog.MetadataDatabaseName() + "\" at path + \"" + | ||
| catalog.MetadataPath() + "\""); | ||
| // explicitly load all secrets - work-around to secret initialization bug | ||
| transaction.Query("FROM duckdb_secrets()"); | ||
| execute_or_throw(transaction.Query("FROM duckdb_secrets()"), "Failed to load DuckDB secrets"); | ||
|
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. I'm not sure if that's what we want to do, as errors being discarded before would allow for initialization still |
||
|
|
||
| bool has_explicit_schema = !options.metadata_schema.empty(); | ||
| if (options.metadata_schema.empty()) { | ||
|
|
@@ -94,7 +92,7 @@ void DuckLakeInitializer::Initialize() { | |
| // directly query a known ducklake metadata table to avoid scanning all attached catalogs via duckdb_tables() | ||
| // this prevents a corrupted ducklake catalog from blocking initialization of unrelated ducklake databases | ||
| // FIXME: verify that all ducklake tables are in the correct format | ||
| result = transaction.Query("SELECT NULL FROM {METADATA_CATALOG}.ducklake_metadata LIMIT 1"); | ||
| auto result = transaction.Query("SELECT NULL FROM {METADATA_CATALOG}.ducklake_metadata LIMIT 1"); | ||
| if (result->HasError()) { | ||
| auto &error_obj = result->GetErrorObject(); | ||
| if (error_obj.Type() == ExceptionType::CATALOG) { | ||
|
|
||
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.
Do we need this?