-
Notifications
You must be signed in to change notification settings - Fork 28
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
feat: data frame with lazy relation AltrepDataFrameRelation
#960
base: main
Are you sure you want to change the base?
Changes from 10 commits
6fa7109
c59db7e
65e4a64
d3a250e
45ae5e6
7e3bdb8
ca1ac17
af84bd4
eddf816
889af85
32a0eb8
5a89610
2a0c344
4d8b847
dc1e31e
950f568
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,27 @@ | ||
#include "altrepdataframe_relation.hpp" | ||
|
||
namespace duckdb { | ||
|
||
AltrepDataFrameRelation::AltrepDataFrameRelation(shared_ptr<Relation> parent) | ||
// TODO: which RelationType should be used? | ||
: Relation(parent->context, RelationType::AGGREGATE_RELATION), parent(std::move(parent)) { | ||
TryBindRelation(columns); | ||
} | ||
|
||
const vector<ColumnDefinition> &AltrepDataFrameRelation::Columns() { | ||
return columns; | ||
} | ||
|
||
string AltrepDataFrameRelation::ToString(idx_t depth) { | ||
return parent->ToString(depth); | ||
} | ||
|
||
bool AltrepDataFrameRelation::IsReadOnly() { | ||
return parent->IsReadOnly(); | ||
} | ||
|
||
unique_ptr<QueryNode> AltrepDataFrameRelation::GetQueryNode() { | ||
return parent->GetQueryNode(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include "duckdb/main/relation.hpp" | ||
|
||
namespace duckdb { | ||
|
||
class AltrepDataFrameRelation final : public Relation { | ||
public: | ||
AltrepDataFrameRelation(shared_ptr<Relation> parent); | ||
|
||
shared_ptr<Relation> parent; | ||
vector<ColumnDefinition> columns; | ||
public: | ||
unique_ptr<QueryNode> GetQueryNode() override; | ||
|
||
const vector<ColumnDefinition> &Columns() override; | ||
string ToString(idx_t depth) override; | ||
bool IsReadOnly() override; | ||
}; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#include "rapi.hpp" | ||
#include "signal.hpp" | ||
#include "typesr.hpp" | ||
#include "reltoaltrep.hpp" | ||
#include "altrepdataframe_relation.hpp" | ||
|
||
#include "R_ext/Random.h" | ||
|
||
|
@@ -141,6 +143,15 @@ using namespace cpp11; | |
return res; | ||
} | ||
|
||
SEXP rapi_rel_from_any_df(duckdb::conn_eptr_t con, SEXP df, bool allow_materialized) { | ||
auto rel = rapi_rel_from_altrep_df(df, false, allow_materialized); | ||
if (rel != R_NilValue) { | ||
return rel; | ||
} | ||
|
||
return rapi_rel_from_df(con, df, false); | ||
} | ||
|
||
[[cpp11::register]] SEXP rapi_rel_filter(duckdb::rel_extptr_t rel, list exprs) { | ||
duckdb::unique_ptr<ParsedExpression> filter_expr; | ||
if (exprs.size() == 0) { // nop | ||
|
@@ -162,6 +173,30 @@ using namespace cpp11; | |
return make_external_prot<RelationWrapper>("duckdb_relation", prot, res); | ||
} | ||
|
||
[[cpp11::register]] SEXP rapi_rel_filter2(data_frame df, duckdb::conn_eptr_t con, list exprs) { | ||
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 move these to a separate 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 still valid. Can you please follow up? |
||
duckdb::unique_ptr<ParsedExpression> filter_expr; | ||
if (exprs.size() == 0) { // nop | ||
stop("expected filter expressions"); | ||
} else if (exprs.size() == 1) { | ||
filter_expr = ((expr_extptr_t)exprs[0])->Copy(); | ||
} else { | ||
vector<duckdb::unique_ptr<ParsedExpression>> filters; | ||
for (expr_extptr_t expr : exprs) { | ||
filters.push_back(expr->Copy()); | ||
} | ||
filter_expr = make_uniq<ConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(filters)); | ||
} | ||
duckdb::rel_extptr_t rel = cpp11::as_cpp<cpp11::decay_t<duckdb::rel_extptr_t>>(rapi_rel_from_any_df(con, df, true)); | ||
|
||
auto filter = make_shared_ptr<FilterRelation>(rel->rel, std::move(filter_expr)); | ||
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 should be wrapped in an ALTREP data frame at this stage. |
||
|
||
auto res = make_shared_ptr<AltrepDataFrameRelation>(filter); | ||
Antonov548 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
cpp11::writable::list prot = {rel}; | ||
|
||
return make_external_prot<RelationWrapper>("duckdb_relation", prot, res); | ||
} | ||
|
||
[[cpp11::register]] SEXP rapi_rel_project(duckdb::rel_extptr_t rel, list exprs) { | ||
if (exprs.size() == 0) { | ||
warning("rel_project without projection expressions has no effect"); | ||
|
@@ -183,6 +218,30 @@ using namespace cpp11; | |
return make_external_prot<RelationWrapper>("duckdb_relation", prot, res); | ||
} | ||
|
||
[[cpp11::register]] SEXP rapi_rel_project2(data_frame df, duckdb::conn_eptr_t con, list exprs) { | ||
if (exprs.size() == 0) { | ||
stop("expected projection expressions"); | ||
} | ||
vector<duckdb::unique_ptr<ParsedExpression>> projections; | ||
vector<string> aliases; | ||
|
||
for (expr_extptr_t expr : exprs) { | ||
auto dexpr = expr->Copy(); | ||
aliases.push_back(dexpr->GetName()); | ||
projections.push_back(std::move(dexpr)); | ||
} | ||
|
||
duckdb::rel_extptr_t rel = cpp11::as_cpp<cpp11::decay_t<duckdb::rel_extptr_t>>(rapi_rel_from_any_df(con, df, true)); | ||
|
||
auto projection = make_shared_ptr<ProjectionRelation>(rel->rel, std::move(projections), std::move(aliases)); | ||
|
||
auto res = make_shared_ptr<AltrepDataFrameRelation>(projection); | ||
|
||
cpp11::writable::list prot = {rel}; | ||
|
||
return make_external_prot<RelationWrapper>("duckdb_relation", prot, res); | ||
} | ||
|
||
[[cpp11::register]] SEXP rapi_rel_aggregate(duckdb::rel_extptr_t rel, list groups, list aggregates) { | ||
vector<duckdb::unique_ptr<ParsedExpression>> res_groups, res_aggregates; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
drv <- duckdb::duckdb() | ||
con <- DBI::dbConnect(drv) | ||
df1 <- tibble::tibble(a = 1) | ||
|
||
"mutate" | ||
#> [1] "mutate" | ||
rel1 <- duckdb:::rel_from_df(con, df1) | ||
"mutate" | ||
#> [1] "mutate" | ||
rel2 <- duckdb:::rel_project( | ||
rel1, | ||
list( | ||
{ | ||
tmp_expr <- duckdb:::expr_reference("a") | ||
duckdb:::expr_set_alias(tmp_expr, "a") | ||
tmp_expr | ||
}, | ||
{ | ||
tmp_expr <- duckdb:::expr_constant(2) | ||
duckdb:::expr_set_alias(tmp_expr, "b") | ||
tmp_expr | ||
} | ||
) | ||
) | ||
"filter" | ||
#> [1] "filter" | ||
rel3 <- duckdb:::rel_filter( | ||
rel2, | ||
list( | ||
duckdb:::expr_comparison( | ||
"==", | ||
list( | ||
duckdb:::expr_reference("b"), | ||
duckdb:::expr_constant(2) | ||
) | ||
) | ||
) | ||
) | ||
rel2 | ||
rel3 | ||
duckdb:::rel_to_altrep(rel2) | ||
rel2 | ||
rel3 |
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.
Why this change? Can this still be
parent->...()
?