Skip to content

Plan shifts whose default picks from the column - #467

Merged
jbearak merged 3 commits into
mainfrom
claude/lag-pick-default
Oct 27, 2026
Merged

jbearak merged 3 commits into
mainfrom
claude/lag-pick-default

Conversation

@jbearak

@jbearak jbearak commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Summary

ADR 0088 plans lag() and lead() of a Stata numeric column with a literal default, alone and as operands, and ADR 0119 builds such operands from the columns' rows. A default that picks from the shifted column, as in x - lag(x, default = first(x)), kept the group loop. Over the 42,739 women in the first 100,000 rows of the India 2021 DHS births file, mutate(b, k = b3 - lag(b3, default = first(b3)), .by = caseid) took about 4.3 seconds, where a tibble takes about 0.81.

This PR lets the admissions take a default, tagged default or untagged after n, that calls dplyr's first(), last() or nth() with the shifted column as its first, untagged argument. The pick must pass ADR 0073's admission, and its closures join those R checks are not being debugged. The plans fill the positions a shift leaves with each group's value at the pick's position, or a missing value past either end, as the loop's cast of the pick does. ADR 0127 records the details.

Behavior

Values, attributes, ALTREP forms, warnings and errors are unchanged. The plan declines a pick of another column, whose cast would change the value's storage, a pick ADR 0073's plan declines, another function named first, last or nth where R finds it first, and a column named for one. As ADR 0097 requires of a pick, a group whose chunk has the position where the first group's chunk does not keeps the loop. A value that an old file wrote and the storage no longer takes keeps the loop, as for the shifted values.

As in ADR 0074 and ADR 0088, the plans do not prove what they skip, so a traced or replaced mask$evaluate(), rlang::eval_tidy(), .cast_to_dta() or other package helper of the loop no longer sees a planned expression's later groups.

Timing

Best of three runs in fresh R sessions, in seconds, with the builds interleaved. Before is main at 7ad6e9c.

Call Before After Tibble
mutate(b, k = b3 - lag(b3, default = first(b3)), .by = caseid) 4.265 0.010 0.811
mutate(b, k = lag(b3, default = first(b3)), .by = caseid) 3.961 0.025 0.772
mutate(b, k = lead(b11, default = last(b11)) - b11, .by = caseid) 4.352 0.009 0.779

Tests

  • test-summary-plan.R compares 8 mutate() calls and 6 operands with the group loop and counts the planned expressions. They cover each compact storage, labels, missing and tagged missing values, shifts as long as a group, a result a later plan reads, nth() from either end and the first group's missing position.
  • It checks that the plan declines picks of other columns, other calls, picks ADR 0073's plan declines, a global first(), a column named last and an old file's values, and that debugging first(), nth(), shift_c(), vec_check_size() or the package's cast and type methods keeps the loop.
  • Two tests build the column by rows, for 5 shapes over each column of the test data and for 60 random groupings, shifts and picks over byte, int, long and float storage with tagged missing values and values near each storage's range, and compare each with the loop's column.
  • The full suite passes: 170 files, 2,307 tests. The native manifest and repository guard tests pass.
  • Codex review ran two rounds. The first found only that the native test manifest was stale, which this PR refreshes, and the second is clean.

# Conflicts:
#	r-package/dtatools/NEWS.md
#	r-package/dtatools/tools/native-test-manifest.json
@coderabbitai

coderabbitai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@jbearak
jbearak merged commit cd976e8 into main Oct 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant