Repository navigation
Build a grouped mutate() of arithmetic from the columns' rows - #456
Merged
Merged
Conversation
Arithmetic of columns, numbers and summaries of columns in a grouped mutate() now evaluates every group at once from the columns' rows, when only the first group has read each column, and builds the column the combine would build (ADR 0118).
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
# Conflicts: # r-package/dtatools/tools/native-test-manifest.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ADR 0065's plan of arithmetic in a grouped
mutate()evaluated the tree for each group after the first from the columns' chunks, and returned a result vector for each group, which the steps after the loop combined. #455 leaves a column that only the first group has read unsplit, but this plan asked for the chunks, so it split the column anyway. With many small groups, the splits, the groups' results and the combine took most of the time. Over the 42,739 women in the first 100,000 rows of the India 2021 DHS births file,mutate(b, m = (b2 - b4) / 12, .by = caseid)took about 0.24 seconds andmutate(b, k = b3 - min(b3), .by = caseid)about 0.13. A tibble takes about 0.07 for each.This PR adds
C_dtatools_arith_plan_rows(). When a tree holds only numbers,(, columns,mean(),min(),max(),sum()ormedian()of a column, and the four operators, and only the first group has read each of its columns, the call evaluates the tree for every group at once from the whole columns:.dta_computed()types a small result: the minimum storage the package's method passes, the first storage from it that holds every value, and each value as that storage reads it back.dtatools_planned_summary_rows()then builds the column the combine would build from every group's values and storages..arith_planned()takes the first group's result asheadand tries this call first in amutate()..arith_plan_columns()takesunsplit = TRUEto return the whole columns after the same admission checks. Any other tree, a column already split, or a decline goes to the plan of the chunks as before.Behavior
Values, attributes, ALTREP forms, warnings and errors are unchanged. ADR 0118 records the decision. The columns must be compact Stata numerics that the native gather takes, the rows must be bare and cover each row once, and every group must be shorter than the size gate, under which each group's arithmetic reaches the native call. The R code checks the same closures for a debugger as the plan of the chunks does.
Timing
Best of three runs in fresh R sessions, in seconds, with the builds interleaved, over the first 100,000 rows of the India 2021 DHS births file. Before is #455's head.
mutate(b, m = (b2 - b4) / 12, .by = caseid)mutate(b, k = b3 - min(b3), .by = caseid)mutate(b, across(c(b2, b3, bord), ~ .x - mean(.x)), .by = caseid)GC time fell from about 0.08, 0.012 and 0.37 seconds to none in each call.
Tests
x - mean(x),(x - median(x)) * 2andx * 1e6, and each operator on every pair of columns, 360 cases. Each must give the same value, warnings and result form with the plans as without them. 287 build their column from the rows. The other 73 read the double column, which is not compact. A case that builds its column splits no column.min()andmax()of a column declared double whose second group has no value. Those summaries are Inf and -Inf, which the plan of the chunks reads back as missing, so the second group's results must be missing.lag()plan has split,lag()in the tree, rowwise and ungrouped data,summarise()andfilter(). It also checks that a column an earlier expression built, and a column the result replaces, build from the rows.t = identity(i + 1L), whose groups the loop still combines, and expects two calls where it expected three. The other wrapsweight - min(weight)inidentity(), so that its first groupedmutate()in a fresh session still reaches the combine.