Skip to content

Commit

Permalink
Merge pull request #273 from tidyverse/f-meta
Browse files Browse the repository at this point in the history
feat: Record and replay functionality now includes the top-level function being called
  • Loading branch information
krlmlr authored Oct 16, 2024
2 parents a294979 + 6fe8f74 commit bc100d1
Show file tree
Hide file tree
Showing 92 changed files with 3,971 additions and 1 deletion.
2 changes: 2 additions & 0 deletions R/aaa-meta.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Overwritten in meta.R
meta_call_start <- function(...) {}
meta_call_end <- function(...) {}
meta_ext_register <- function(...) {}
meta_rel_register <- function(...) {}
meta_rel_register_df <- function(...) {}
Expand Down
23 changes: 22 additions & 1 deletion R/meta.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
call_stack <- collections::stack()
pre_code_cache <- collections::queue()
code_cache <- collections::queue()
ext_cache <- collections::dict()
macro_cache <- collections::dict()
df_cache <- collections::dict()
rel_cache <- collections::dict()

meta_call_start <- function(name) {
call_stack$push(name)
}

meta_call_end <- function() {
call_stack$pop()
}

meta_call_current <- function() {
if (call_stack$size() == 0) {
return(NULL)
}
call_stack$peek()
}

meta_clear <- function() {
pre_code_cache$clear()
code_cache$clear()
Expand Down Expand Up @@ -213,8 +229,13 @@ meta_rel_register <- function(rel, rel_expr) {
count <- rel_cache$size()
name <- sym(paste0("rel", count + 1))

# https://github.com/cynkra/constructive/issues/102
current_call <- meta_call_current()
if (!is.null(current_call)) {
# FIXME: This is probably too convoluted
meta_record(constructive::deparse_call(expr(!!current_call)))
}

# https://github.com/cynkra/constructive/issues/102
meta_record(constructive::deparse_call(expr(!!name <- !!rel_expr)))

obj <- list(rel = rel, name = name, df = df)
Expand Down
5 changes: 5 additions & 0 deletions R/relational.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
rel_try <- function(rel, ..., call = NULL) {
call_name <- as.character(sys.call(-1)[[1]])

if (!is.null(call$name)) {
meta_call_start(call$name)
withr::defer(meta_call_end())
}

# Avoid error when called via dplyr:::filter.data.frame() (in yamlet)
if (length(call_name) == 1 && !(call_name %in% stats$calls)) {
stats$calls <- c(stats$calls, call_name)
Expand Down
6 changes: 6 additions & 0 deletions R/tpch_raw_01.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by meta_replay_to_fun_file(), do not edit by hand
tpch_raw_01 <- function(con, experimental) {
df1 <- lineitem
"select"
rel1 <- duckdb$rel_from_df(con, df1, experimental = experimental)
"select"
rel2 <- duckdb$rel_project(
rel1,
list(
Expand Down Expand Up @@ -42,6 +44,7 @@ tpch_raw_01 <- function(con, experimental) {
}
)
)
"filter"
rel3 <- duckdb$rel_filter(
rel2,
list(
Expand All @@ -58,6 +61,7 @@ tpch_raw_01 <- function(con, experimental) {
)
)
)
"select"
rel4 <- duckdb$rel_project(
rel3,
list(
Expand Down Expand Up @@ -93,6 +97,7 @@ tpch_raw_01 <- function(con, experimental) {
}
)
)
"summarise"
rel5 <- duckdb$rel_aggregate(
rel4,
groups = list(duckdb$expr_reference("l_returnflag"), duckdb$expr_reference("l_linestatus")),
Expand Down Expand Up @@ -197,6 +202,7 @@ tpch_raw_01 <- function(con, experimental) {
}
)
)
"arrange"
rel6 <- duckdb$rel_order(
rel5,
list(duckdb$expr_reference("l_returnflag"), duckdb$expr_reference("l_linestatus"))
Expand Down
40 changes: 40 additions & 0 deletions R/tpch_raw_02.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by meta_replay_to_fun_file(), do not edit by hand
tpch_raw_02 <- function(con, experimental) {
df1 <- partsupp
"select"
rel1 <- duckdb$rel_from_df(con, df1, experimental = experimental)
"select"
rel2 <- duckdb$rel_project(
rel1,
list(
Expand All @@ -23,7 +25,9 @@ tpch_raw_02 <- function(con, experimental) {
)
)
df2 <- part
"select"
rel3 <- duckdb$rel_from_df(con, df2, experimental = experimental)
"select"
rel4 <- duckdb$rel_project(
rel3,
list(
Expand All @@ -49,6 +53,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"filter"
rel5 <- duckdb$rel_filter(
rel4,
list(
Expand Down Expand Up @@ -76,6 +81,7 @@ tpch_raw_02 <- function(con, experimental) {
)
)
)
"select"
rel6 <- duckdb$rel_project(
rel5,
list(
Expand All @@ -91,8 +97,11 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"inner_join"
rel7 <- duckdb$rel_set_alias(rel6, "lhs")
"inner_join"
rel8 <- duckdb$rel_set_alias(rel2, "rhs")
"inner_join"
rel9 <- duckdb$rel_join(
rel7,
rel8,
Expand All @@ -104,6 +113,7 @@ tpch_raw_02 <- function(con, experimental) {
),
"inner"
)
"inner_join"
rel10 <- duckdb$rel_project(
rel9,
list(
Expand Down Expand Up @@ -133,7 +143,9 @@ tpch_raw_02 <- function(con, experimental) {
)
)
df3 <- supplier
"select"
rel11 <- duckdb$rel_from_df(con, df3, experimental = experimental)
"select"
rel12 <- duckdb$rel_project(
rel11,
list(
Expand Down Expand Up @@ -174,8 +186,11 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"inner_join"
rel13 <- duckdb$rel_set_alias(rel10, "lhs")
"inner_join"
rel14 <- duckdb$rel_set_alias(rel12, "rhs")
"inner_join"
rel15 <- duckdb$rel_join(
rel13,
rel14,
Expand All @@ -187,6 +202,7 @@ tpch_raw_02 <- function(con, experimental) {
),
"inner"
)
"inner_join"
rel16 <- duckdb$rel_project(
rel15,
list(
Expand Down Expand Up @@ -245,6 +261,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"select"
rel17 <- duckdb$rel_project(
rel16,
list(
Expand Down Expand Up @@ -296,7 +313,9 @@ tpch_raw_02 <- function(con, experimental) {
)
)
df4 <- region
"filter"
rel18 <- duckdb$rel_from_df(con, df4, experimental = experimental)
"filter"
rel19 <- duckdb$rel_filter(
rel18,
list(
Expand All @@ -314,9 +333,13 @@ tpch_raw_02 <- function(con, experimental) {
)
)
df5 <- nation
"inner_join"
rel20 <- duckdb$rel_from_df(con, df5, experimental = experimental)
"inner_join"
rel21 <- duckdb$rel_set_alias(rel20, "lhs")
"inner_join"
rel22 <- duckdb$rel_set_alias(rel19, "rhs")
"inner_join"
rel23 <- duckdb$rel_join(
rel21,
rel22,
Expand All @@ -328,6 +351,7 @@ tpch_raw_02 <- function(con, experimental) {
),
"inner"
)
"inner_join"
rel24 <- duckdb$rel_project(
rel23,
list(
Expand Down Expand Up @@ -366,6 +390,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"select"
rel25 <- duckdb$rel_project(
rel24,
list(
Expand All @@ -381,8 +406,11 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"inner_join"
rel26 <- duckdb$rel_set_alias(rel17, "lhs")
"inner_join"
rel27 <- duckdb$rel_set_alias(rel25, "rhs")
"inner_join"
rel28 <- duckdb$rel_join(
rel26,
rel27,
Expand All @@ -394,6 +422,7 @@ tpch_raw_02 <- function(con, experimental) {
),
"inner"
)
"inner_join"
rel29 <- duckdb$rel_project(
rel28,
list(
Expand Down Expand Up @@ -452,6 +481,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"select"
rel30 <- duckdb$rel_project(
rel29,
list(
Expand Down Expand Up @@ -502,6 +532,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"summarise"
rel31 <- duckdb$rel_aggregate(
rel30,
groups = list(duckdb$expr_reference("p_partkey")),
Expand All @@ -513,8 +544,11 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"inner_join"
rel32 <- duckdb$rel_set_alias(rel30, "lhs")
"inner_join"
rel33 <- duckdb$rel_set_alias(rel31, "rhs")
"inner_join"
rel34 <- duckdb$rel_project(
rel32,
list(
Expand Down Expand Up @@ -565,6 +599,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"inner_join"
rel35 <- duckdb$rel_project(
rel33,
list(
Expand All @@ -580,6 +615,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"inner_join"
rel36 <- duckdb$rel_join(
rel34,
rel35,
Expand All @@ -595,6 +631,7 @@ tpch_raw_02 <- function(con, experimental) {
),
"inner"
)
"inner_join"
rel37 <- duckdb$rel_project(
rel36,
list(
Expand Down Expand Up @@ -651,6 +688,7 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"select"
rel38 <- duckdb$rel_project(
rel37,
list(
Expand Down Expand Up @@ -696,10 +734,12 @@ tpch_raw_02 <- function(con, experimental) {
}
)
)
"arrange"
rel39 <- duckdb$rel_order(
rel38,
list(duckdb$expr_reference("s_acctbal"), duckdb$expr_reference("n_name"), duckdb$expr_reference("s_name"), duckdb$expr_reference("p_partkey"))
)
"head"
rel40 <- duckdb$rel_limit(rel39, 100)
rel40
duckdb$rel_to_altrep(rel40)
Expand Down
Loading

0 comments on commit bc100d1

Please sign in to comment.