16
16
#include " duckdb/main/query_result.hpp"
17
17
#include " duckdb/main/relation/limit_relation.hpp"
18
18
19
+ #include " fmt/format.h"
20
+
19
21
#ifdef TRUE
20
22
#undef TRUE
21
23
#endif
@@ -100,16 +102,22 @@ AltrepRelationWrapper *AltrepRelationWrapper::Get(SEXP x) {
100
102
return GetFromExternalPtr<AltrepRelationWrapper>(x);
101
103
}
102
104
103
- AltrepRelationWrapper::AltrepRelationWrapper (rel_extptr_t rel_, bool allow_materialization_, size_t n_rows_, size_t n_cells_)
104
- : allow_materialization(allow_materialization_), n_rows(n_rows_), n_cells(n_cells_), rel_eptr(rel_), rel(rel_->rel) {
105
+ AltrepRelationWrapper::AltrepRelationWrapper (rel_extptr_t rel_, bool allow_materialization_, size_t n_rows_,
106
+ size_t n_cells_)
107
+ : allow_materialization(allow_materialization_), n_rows(n_rows_), n_cells(n_cells_), rel_eptr(rel_),
108
+ rel(rel_->rel) {
105
109
}
106
110
107
111
bool AltrepRelationWrapper::HasQueryResult () const {
108
- return (bool )res ;
112
+ return (bool )mat_result ;
109
113
}
110
114
111
115
MaterializedQueryResult *AltrepRelationWrapper::GetQueryResult () {
112
- if (!res) {
116
+ if (!mat_error.empty ()) {
117
+ cpp11::stop (mat_error);
118
+ }
119
+
120
+ if (!mat_result) {
113
121
if (!allow_materialization || n_cells == 0 ) {
114
122
cpp11::stop (" Materialization is disabled, use collect() or as_tibble() to materialize." );
115
123
}
@@ -121,7 +129,8 @@ MaterializedQueryResult *AltrepRelationWrapper::GetQueryResult() {
121
129
}
122
130
123
131
auto materialize_message = Rf_GetOption (RStrings::get ().materialize_message_sym , R_BaseEnv);
124
- if (Rf_isLogical (materialize_message) && Rf_length (materialize_message) == 1 && LOGICAL_ELT (materialize_message, 0 ) == true ) {
132
+ if (Rf_isLogical (materialize_message) && Rf_length (materialize_message) == 1 &&
133
+ LOGICAL_ELT (materialize_message, 0 ) == true ) {
125
134
// Legacy
126
135
Rprintf (" duckplyr: materializing\n " );
127
136
}
@@ -135,12 +144,16 @@ MaterializedQueryResult *AltrepRelationWrapper::GetQueryResult() {
135
144
duckdb_httplib::detail::scope_exit reset_max_expression_depth (
136
145
[&]() { rel->context ->GetContext ()->config .max_expression_depth = old_depth; });
137
146
138
- res = Materialize ();
147
+ Materialize ();
148
+
149
+ if (!mat_error.empty ()) {
150
+ cpp11::stop (mat_error);
151
+ }
139
152
140
153
// FIXME: Use std::experimental::scope_exit
141
154
if (rel->context ->GetContext ()->config .max_expression_depth != old_depth * 2 ) {
142
- Rprintf (" Internal error: max_expression_depth was changed from %" PRIu64 " to %" PRIu64 " \n " ,
143
- old_depth * 2 , rel->context ->GetContext ()->config .max_expression_depth );
155
+ Rprintf (" Internal error: max_expression_depth was changed from %" PRIu64 " to %" PRIu64 " \n " , old_depth * 2 ,
156
+ rel->context ->GetContext ()->config .max_expression_depth );
144
157
}
145
158
rel->context ->GetContext ()->config .max_expression_depth = old_depth;
146
159
reset_max_expression_depth.release ();
@@ -151,11 +164,11 @@ MaterializedQueryResult *AltrepRelationWrapper::GetQueryResult() {
151
164
152
165
signal_handler.Disable ();
153
166
}
154
- D_ASSERT (res );
155
- return (MaterializedQueryResult *)res .get ();
167
+ D_ASSERT (mat_result );
168
+ return (MaterializedQueryResult *)mat_result .get ();
156
169
}
157
170
158
- duckdb::unique_ptr<QueryResult> AltrepRelationWrapper::Materialize () {
171
+ void AltrepRelationWrapper::Materialize () {
159
172
// Init with max value
160
173
size_t max_rows = MAX_SIZE_T;
161
174
@@ -178,20 +191,22 @@ duckdb::unique_ptr<QueryResult> AltrepRelationWrapper::Materialize() {
178
191
auto local_res = local_rel->Execute ();
179
192
180
193
if (local_res->HasError ()) {
181
- cpp11::stop (" Error evaluating duckdb query: %s" , local_res->GetError ().c_str ());
194
+ mat_error = duckdb_fmt::format (" Error evaluating duckdb query: {}" , local_res->GetError ().c_str ());
195
+ return ;
182
196
}
183
197
D_ASSERT (local_res->type == QueryResultType::MATERIALIZED_RESULT);
184
198
185
199
if (max_rows < MAX_SIZE_T) {
186
- auto mat_res = (MaterializedQueryResult *)local_res.get ();
187
- if (mat_res->RowCount () > max_rows) {
188
- cpp11::stop (" Materialization would result in more than %" PRIu64
189
- " rows. Use collect() or as_tibble() to materialize." ,
190
- max_rows);
200
+ auto local_mat_res = (MaterializedQueryResult *)local_res.get ();
201
+ if (local_mat_res->RowCount () > max_rows) {
202
+ mat_error = duckdb_fmt::format (
203
+ " Materialization would result in more than {} rows. Use collect() or as_tibble() to materialize." ,
204
+ max_rows);
205
+ return ;
191
206
}
192
207
}
193
208
194
- return std::move (local_res);
209
+ mat_result = std::move (local_res);
195
210
}
196
211
197
212
struct AltrepRownamesWrapper {
@@ -403,8 +418,8 @@ size_t DoubleToSize(double d) {
403
418
404
419
// FIXME: Remove allow_materialization
405
420
auto allow_materialization = true ;
406
- auto relation_wrapper = make_shared_ptr<AltrepRelationWrapper>(rel, allow_materialization, DoubleToSize (n_rows),
407
- DoubleToSize (n_cells));
421
+ auto relation_wrapper =
422
+ make_shared_ptr<AltrepRelationWrapper>(rel, allow_materialization, DoubleToSize (n_rows), DoubleToSize (n_cells));
408
423
409
424
cpp11::writable::list data_frame;
410
425
data_frame.reserve (ncols);
@@ -479,7 +494,7 @@ size_t DoubleToSize(double d) {
479
494
480
495
auto wrapper = GetFromExternalPtr<AltrepRownamesWrapper>(row_names);
481
496
if (!allow_materialized) {
482
- if (wrapper->rel ->res .get ()) {
497
+ if (wrapper->rel ->mat_result .get ()) {
483
498
// We return NULL here even for strict = true
484
499
// because this is expected from df_is_materialized()
485
500
return R_NilValue;
0 commit comments