diff --git a/R/meta.R b/R/meta.R index 4689f1706..74b6ade63 100644 --- a/R/meta.R +++ b/R/meta.R @@ -235,13 +235,19 @@ meta_rel_register <- function(rel, rel_expr) { meta_record(constructive::deparse_call(expr(!!current_call))) } - # https://github.com/cynkra/constructive/issues/102 - meta_record(constructive::deparse_call(expr(!!name <- !!rel_expr))) + # Will be NULL if the computation fails + if (is.null(rel)) { + # Multi-expressions not there yet: + # https://github.com/cynkra/constructive/issues/102 + meta_record(constructive::deparse_call(expr(try(!!name <- !!rel_expr)))) + } else { + meta_record(constructive::deparse_call(expr(!!name <- !!rel_expr))) - obj <- list(rel = rel, name = name, df = df) - hash <- deparse(rel) + obj <- list(rel = rel, name = name) + hash <- deparse(rel) - rel_cache$set(hash, obj) + rel_cache$set(hash, obj) + } invisible() } diff --git a/R/relational-duckdb.R b/R/relational-duckdb.R index d8bc8ba9b..c5733743c 100644 --- a/R/relational-duckdb.R +++ b/R/relational-duckdb.R @@ -192,12 +192,15 @@ rel_to_df.duckdb_relation <- function(rel, ...) { #' @export rel_filter.duckdb_relation <- function(rel, exprs, ...) { duckdb_exprs <- to_duckdb_exprs(exprs) - out <- duckdb$rel_filter(rel, duckdb_exprs) + + out <- NULL - meta_rel_register(out, expr(duckdb$rel_filter( + withr::defer(meta_rel_register(out, expr(duckdb$rel_filter( !!meta_rel_get(rel)$name, list(!!!to_duckdb_exprs_meta(exprs)) - ))) + )))) + + out <- duckdb$rel_filter(rel, duckdb_exprs) out } @@ -206,12 +209,14 @@ rel_filter.duckdb_relation <- function(rel, exprs, ...) { rel_project.duckdb_relation <- function(rel, exprs, ...) { duckdb_exprs <- to_duckdb_exprs(exprs) - out <- duckdb$rel_project(rel, duckdb_exprs) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_project( + withr::defer(meta_rel_register(out, expr(duckdb$rel_project( !!meta_rel_get(rel)$name, list(!!!to_duckdb_exprs_meta(exprs)) - ))) + )))) + + out <- duckdb$rel_project(rel, duckdb_exprs) out } @@ -221,18 +226,20 @@ rel_aggregate.duckdb_relation <- function(rel, groups, aggregates, ...) { duckdb_groups <- to_duckdb_exprs(groups) duckdb_aggregates <- to_duckdb_exprs(aggregates) + out <- NULL + + withr::defer(meta_rel_register(out, expr(duckdb$rel_aggregate( + !!meta_rel_get(rel)$name, + groups = list(!!!to_duckdb_exprs_meta(groups)), + aggregates = list(!!!to_duckdb_exprs_meta(aggregates)) + )))) + out <- duckdb$rel_aggregate( rel, groups = duckdb_groups, aggregates = duckdb_aggregates ) - meta_rel_register(out, expr(duckdb$rel_aggregate( - !!meta_rel_get(rel)$name, - groups = list(!!!to_duckdb_exprs_meta(groups)), - aggregates = list(!!!to_duckdb_exprs_meta(aggregates)) - ))) - out } @@ -241,12 +248,14 @@ rel_order.duckdb_relation <- function(rel, orders, ascending = NULL, ...) { duckdb_orders <- to_duckdb_exprs(orders) - out <- duckdb$rel_order(rel, duckdb_orders, ascending) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_order( + withr::defer(meta_rel_register(out, expr(duckdb$rel_order( !!meta_rel_get(rel)$name, list(!!!to_duckdb_exprs_meta(orders)) - ))) + )))) + + out <- duckdb$rel_order(rel, duckdb_orders, ascending) out } @@ -258,26 +267,30 @@ rel_join.duckdb_relation <- function(left, right, conds, join, join_ref_type, .. join <- "outer" } + out <- NULL + if (join_ref_type == "regular") { # Compatibility with older duckdb versions - out <- duckdb$rel_join(left, right, duckdb_conds, join) - meta_rel_register(out, expr(duckdb$rel_join( + withr::defer(meta_rel_register(out, expr(duckdb$rel_join( !!meta_rel_get(left)$name, !!meta_rel_get(right)$name, list(!!!to_duckdb_exprs_meta(conds)), !!join - ))) + )))) + + out <- duckdb$rel_join(left, right, duckdb_conds, join) } else { - out <- duckdb$rel_join(left, right, duckdb_conds, join, join_ref_type) - meta_rel_register(out, expr(duckdb$rel_join( + withr::defer(meta_rel_register(out, expr(duckdb$rel_join( !!meta_rel_get(left)$name, !!meta_rel_get(right)$name, list(!!!to_duckdb_exprs_meta(conds)), !!join, !!join_ref_type - ))) + )))) + + out <- duckdb$rel_join(left, right, duckdb_conds, join, join_ref_type) } out @@ -285,71 +298,83 @@ rel_join.duckdb_relation <- function(left, right, conds, join, join_ref_type, .. #' @export rel_limit.duckdb_relation <- function(rel, n, ...) { - out <- duckdb$rel_limit(rel, n) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_limit( + withr::defer(meta_rel_register(out, expr(duckdb$rel_limit( !!meta_rel_get(rel)$name, !!n - ))) + )))) + + out <- duckdb$rel_limit(rel, n) out } #' @export rel_distinct.duckdb_relation <- function(rel, ...) { - out <- duckdb$rel_distinct(rel) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_distinct( + withr::defer(meta_rel_register(out, expr(duckdb$rel_distinct( !!meta_rel_get(rel)$name - ))) + )))) + + out <- duckdb$rel_distinct(rel) out } #' @export rel_set_intersect.duckdb_relation <- function(rel_a, rel_b, ...) { - out <- duckdb$rel_set_intersect(rel_a, rel_b) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_set_intersect( + withr::defer(meta_rel_register(out, expr(duckdb$rel_set_intersect( !!meta_rel_get(rel_a)$name, !!meta_rel_get(rel_b)$name - ))) + )))) + + out <- duckdb$rel_set_intersect(rel_a, rel_b) out } #' @export rel_set_diff.duckdb_relation <- function(rel_a, rel_b, ...) { - out <- duckdb$rel_set_diff(rel_a, rel_b) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_set_diff( + withr::defer(meta_rel_register(out, expr(duckdb$rel_set_diff( !!meta_rel_get(rel_a)$name, !!meta_rel_get(rel_b)$name - ))) + )))) + + out <- duckdb$rel_set_diff(rel_a, rel_b) out } #' @export rel_set_symdiff.duckdb_relation <- function(rel_a, rel_b, ...) { - out <- duckdb$rel_set_symdiff(rel_a, rel_b) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_set_symdiff( + withr::defer(meta_rel_register(out, expr(duckdb$rel_set_symdiff( !!meta_rel_get(rel_a)$name, !!meta_rel_get(rel_b)$name - ))) + )))) + + out <- duckdb$rel_set_symdiff(rel_a, rel_b) out } #' @export rel_union_all.duckdb_relation <- function(rel_a, rel_b, ...) { - out <- duckdb$rel_union_all(rel_a, rel_b) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_union_all( + withr::defer(meta_rel_register(out, expr(duckdb$rel_union_all( !!meta_rel_get(rel_a)$name, !!meta_rel_get(rel_b)$name - ))) + )))) + + out <- duckdb$rel_union_all(rel_a, rel_b) out } @@ -365,12 +390,14 @@ rel_alias.duckdb_relation <- function(rel, ...) { #' @export rel_set_alias.duckdb_relation <- function(rel, alias, ...) { - out <- duckdb$rel_set_alias(rel, alias) + out <- NULL - meta_rel_register(out, expr(duckdb$rel_set_alias( + withr::defer(meta_rel_register(out, expr(duckdb$rel_set_alias( !!meta_rel_get(rel)$name, !!alias - ))) + )))) + + out <- duckdb$rel_set_alias(rel, alias) out }