Skip to content

Commit 4fc49c9

Browse files
committed
Add query result helpers and guard call sites
1 parent c80b915 commit 4fc49c9

6 files changed

Lines changed: 149 additions & 112 deletions

File tree

src/functions/ducklake_add_data_files.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ struct DuckLakeFileProcessor {
168168
};
169169

170170
void DuckLakeFileProcessor::ReadParquetFullMetadata(const string &glob, vector<DuckLakeDataFile> &written_files) {
171-
auto result = transaction.Query(StringUtil::Format(R"(
171+
auto result = result_or_throw(
172+
transaction.Query(StringUtil::Format(R"(
172173
SELECT
173174
list_transform(parquet_file_metadata, lambda x: struct_pack(
174175
file_name := x.file_name,
@@ -198,10 +199,8 @@ SELECT
198199
)) AS parquet_schema
199200
FROM parquet_full_metadata(%s)
200201
)",
201-
SQLString(glob)));
202-
if (result->HasError()) {
203-
result->GetErrorObject().Throw("Failed to add data files to DuckLake: ");
204-
}
202+
SQLString(glob))),
203+
"Failed to add data files to DuckLake: ");
205204

206205
for (auto &row : *result) {
207206
auto &chunk = row.GetChunk();

src/functions/ducklake_flush_inlined_data.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ static void FlushInlinedFileDeletions(ClientContext &context, DuckLakeCatalog &c
429429
}
430430

431431
// Query the inlined deletions with file paths and existing delete file info
432-
auto deletions_result = transaction.Query(snapshot, StringUtil::Format(R"(
432+
auto deletions_result = result_or_throw(
433+
transaction.Query(snapshot, StringUtil::Format(R"(
433434
SELECT del.file_id, data.path, data.path_is_relative, del.row_id, del.begin_snapshot,
434435
existing_del.delete_file_id, existing_del.path as del_path, existing_del.path_is_relative as del_path_is_relative,
435436
existing_del.begin_snapshot as del_begin_snapshot, existing_del.encryption_key as del_encryption_key,
@@ -442,10 +443,8 @@ LEFT JOIN (
442443
AND ({SNAPSHOT_ID} < end_snapshot OR end_snapshot IS NULL)
443444
) existing_del ON del.file_id = existing_del.data_file_id
444445
)",
445-
inlined_table_name, table_id.index));
446-
if (deletions_result->HasError()) {
447-
deletions_result->GetErrorObject().Throw("Failed to query inlined file deletions for flush: ");
448-
}
446+
inlined_table_name, table_id.index)),
447+
"Failed to query inlined file deletions for flush: ");
449448

450449
unordered_map<idx_t, FileDeleteInfo> files_to_flush;
451450

src/include/storage/ducklake_transaction.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
#include "common/ducklake_data_file.hpp"
1313
#include "common/ducklake_snapshot.hpp"
1414
#include "duckdb/common/case_insensitive_map.hpp"
15+
#include "duckdb/common/exception.hpp"
1516
#include "duckdb/common/types/value_map.hpp"
1617
#include "duckdb/main/connection.hpp"
18+
#include "duckdb/main/query_result.hpp"
1719
#include "duckdb/transaction/transaction.hpp"
20+
#include <utility>
1821
#include "storage/ducklake_catalog_set.hpp"
1922
#include "storage/ducklake_inlined_data.hpp"
2023
#include "storage/ducklake_metadata_manager.hpp"
@@ -41,6 +44,36 @@ struct DuckLakeCommitState;
4144
class DuckLakeFieldId;
4245
class LocalTableChangeIterationHelper;
4346

47+
inline unique_ptr<QueryResult> result_or_throw(unique_ptr<QueryResult> result, const string &err) {
48+
if (result->HasError()) {
49+
result->GetErrorObject().Throw(err);
50+
}
51+
return result;
52+
}
53+
54+
inline void execute_or_throw(unique_ptr<QueryResult> result, const string &err) {
55+
(void)result_or_throw(std::move(result), err);
56+
}
57+
58+
template <class T>
59+
T query_scalar_or_throw(unique_ptr<QueryResult> result, const string &err) {
60+
auto checked_result = result_or_throw(std::move(result), err);
61+
auto chunk = checked_result->Fetch();
62+
if (!chunk || chunk->size() == 0) {
63+
throw InvalidInputException("Expected scalar result");
64+
}
65+
return chunk->GetValue(0, 0).template GetValue<T>();
66+
}
67+
68+
template <class T>
69+
T query_scalar_or_zero(unique_ptr<QueryResult> result, const string &err) {
70+
auto checked_result = result_or_throw(std::move(result), err);
71+
for (auto &row : *checked_result) {
72+
return row.template GetValue<T>(0);
73+
}
74+
return T {};
75+
}
76+
4477
struct FlushedInlinedTableInfo {
4578
DuckLakeInlinedTableInfo inlined_table;
4679
idx_t flush_snapshot_id;

src/storage/ducklake_initializer.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ string DuckLakeInitializer::GetAttachOptions() {
6161
void DuckLakeInitializer::Initialize() {
6262
auto &transaction = DuckLakeTransaction::Get(context, catalog);
6363
// attach the metadata database
64-
auto result = transaction.Query("ATTACH OR REPLACE {METADATA_PATH} AS {METADATA_CATALOG_NAME_IDENTIFIER}" +
65-
GetAttachOptions());
66-
if (result->HasError()) {
67-
auto &error_obj = result->GetErrorObject();
68-
error_obj.Throw("Failed to attach DuckLake MetaData \"" + catalog.MetadataDatabaseName() + "\" at path + \"" +
69-
catalog.MetadataPath() + "\"");
70-
}
64+
execute_or_throw(
65+
transaction.Query("ATTACH OR REPLACE {METADATA_PATH} AS {METADATA_CATALOG_NAME_IDENTIFIER}" +
66+
GetAttachOptions()),
67+
"Failed to attach DuckLake MetaData \"" + catalog.MetadataDatabaseName() + "\" at path + \"" +
68+
catalog.MetadataPath() + "\"");
7169
// explicitly load all secrets - work-around to secret initialization bug
72-
transaction.Query("FROM duckdb_secrets()");
70+
execute_or_throw(transaction.Query("FROM duckdb_secrets()"), "Failed to load DuckDB secrets");
7371

7472
bool has_explicit_schema = !options.metadata_schema.empty();
7573
if (options.metadata_schema.empty()) {
@@ -94,7 +92,7 @@ void DuckLakeInitializer::Initialize() {
9492
// directly query a known ducklake metadata table to avoid scanning all attached catalogs via duckdb_tables()
9593
// this prevents a corrupted ducklake catalog from blocking initialization of unrelated ducklake databases
9694
// FIXME: verify that all ducklake tables are in the correct format
97-
result = transaction.Query("SELECT NULL FROM {METADATA_CATALOG}.ducklake_metadata LIMIT 1");
95+
auto result = transaction.Query("SELECT NULL FROM {METADATA_CATALOG}.ducklake_metadata LIMIT 1");
9896
if (result->HasError()) {
9997
auto &error_obj = result->GetErrorObject();
10098
if (error_obj.Type() == ExceptionType::CATALOG) {

src/storage/ducklake_inlined_data_reader.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,26 @@ bool DuckLakeInlinedDataReader::TryInitializeScan(ClientContext &context, Global
105105
virtual_columns.push_back(InlinedVirtualColumn::NONE);
106106
}
107107
}
108-
unique_ptr<QueryResult> query_result;
108+
unique_ptr<QueryResult> scan_result;
109109
switch (read_info.scan_type) {
110110
case DuckLakeScanType::SCAN_TABLE:
111-
query_result = metadata_manager.ReadInlinedData(read_info.snapshot, table_name, columns_to_read);
111+
scan_result = metadata_manager.ReadInlinedData(read_info.snapshot, table_name, columns_to_read);
112112
break;
113113
case DuckLakeScanType::SCAN_INSERTIONS:
114-
query_result = metadata_manager.ReadInlinedDataInsertions(*read_info.start_snapshot, read_info.snapshot,
115-
table_name, columns_to_read);
114+
scan_result = metadata_manager.ReadInlinedDataInsertions(*read_info.start_snapshot, read_info.snapshot,
115+
table_name, columns_to_read);
116116
break;
117117
case DuckLakeScanType::SCAN_DELETIONS:
118-
query_result = metadata_manager.ReadInlinedDataDeletions(*read_info.start_snapshot, read_info.snapshot,
119-
table_name, columns_to_read);
118+
scan_result = metadata_manager.ReadInlinedDataDeletions(*read_info.start_snapshot, read_info.snapshot,
119+
table_name, columns_to_read);
120120
break;
121121
case DuckLakeScanType::SCAN_FOR_FLUSH:
122-
query_result = metadata_manager.ReadAllInlinedDataForFlush(read_info.snapshot, table_name, columns_to_read);
122+
scan_result = metadata_manager.ReadAllInlinedDataForFlush(read_info.snapshot, table_name, columns_to_read);
123123
break;
124124
default:
125125
throw InternalException("Unknown DuckLake scan type");
126126
}
127+
auto query_result = result_or_throw(std::move(scan_result), "Failed to read inlined data from DuckLake: ");
127128
data = metadata_manager.TransformInlinedData(*query_result, expected_types);
128129
if (!virtual_columns.empty()) {
129130
auto scan_types = data->data->Types();

0 commit comments

Comments
 (0)