Skip to content
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

Refactor helper function issue #6702 (Refactored duplicated logic in setDT and [,:=]) #6752

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
ef314b9
Setup Docker inside the dev container
nipun-gupta-3108 Jan 20, 2025
0be4b62
Trigger CI
nipun-gupta-3108 Jan 21, 2025
7bddf44
Add GitHub Actions CI/CD workflow
nipun-gupta-3108 Jan 21, 2025
4e9bd73
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
97e1517
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
068474e
Merge branch 'master' into refactor-helper-function-issue-6702
nipun-gupta-3108 Jan 22, 2025
b23d277
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
581694a
Merge branch 'refactor-helper-function-issue-6702' of https://github.…
nipun-gupta-3108 Jan 22, 2025
76eb5bd
Reverted .devcontainer/devcontainer.json to match master
nipun-gupta-3108 Jan 22, 2025
e12f726
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
cecfb0b
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
3592327
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
52f9d89
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 22, 2025
9ee7835
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 23, 2025
2e9835e
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 23, 2025
f554637
Refactored duplicated logic in setDT and [,:=] to use a helper function
nipun-gupta-3108 Jan 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/ci-cd.yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this change

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: R Project CI/CD

on:
push:
branches:
- main
- setup-github-actions
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up R
uses: r-lib/actions/setup-r@v2
with:
r-version: devel

- name: Install dependencies
run: |
install.packages("devtools")
devtools::install_deps(dependencies = TRUE)

- name: Run tests
run: |
R CMD check --as-cran
11 changes: 10 additions & 1 deletion .github/workflows/performance-tests.yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this change

Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: Anirban166/[email protected]
- name: Checkout code
uses: actions/checkout@v4
with:
ref: refactor-helper-function-issue-6702
- run: |
echo "Current branch:"
git branch
echo "Listing all branches:"
git branch -a
- uses: Anirban166/[email protected]
7 changes: 5 additions & 2 deletions .github/workflows/test-coverage.yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this change

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches: [master]
pull_request:
branches:
- master
- refactor-helper-function-issue-6702 # Ensure this branch exists

name: test-coverage.yaml

Expand All @@ -21,7 +24,7 @@ jobs:
- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true
r-version: '4.3' # TODO(r-lib/covr#567): Go back to using r-devel
r-version: '4.3' # TODO: Go back to using r-devel

- uses: r-lib/actions/setup-r-dependencies@v2
with:
Expand Down Expand Up @@ -51,4 +54,4 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
path: ${{ runner.temp }}/package
61 changes: 28 additions & 33 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ methods::setPackageName("data.table",.global)
is.data.table = function(x) inherits(x, "data.table")
is.ff = function(x) inherits(x, "ff") # define this in data.table so that we don't have to require(ff), but if user is using ff we'd like it to work

# Helper function to process assignment operations in lists or environments.
# Used internally for efficient recursive assignments in data.table.

process_assignment <- function(name, x, parent_env) {
k = eval(name[[2L]], parent_env, parent_env)
if (is.list(k)) {
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent_env, parent_env)
if (is.character(j)) {
if (length(j) != 1L)
stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
j = match(j, names(k))
if (is.na(j))
stopf("Item '%s' not found in names of input list", origj)
}
.Call(Csetlistelt, k, as.integer(j), x)
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
assign(as.character(name[[3L]]), x, k, inherits = FALSE)
}
}

#NCOL = function(x) {
# # copied from base, but additionally covers data.table via is.list()
# # because NCOL in base explicitly tests using is.data.frame()
Expand Down Expand Up @@ -1214,22 +1234,9 @@ replace_dot_alias = function(e) {
setalloccol(x, n, verbose=verbose) # always assigns to calling scope; i.e. this scope
if (is.name(name)) {
assign(as.character(name),x,parent.frame(),inherits=TRUE)
} else if (.is_simple_extraction(name)) { # TODO(#6702): use a helper here as the code is very similar to setDT().
k = eval(name[[2L]], parent.frame(), parent.frame())
if (is.list(k)) {
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent.frame(), parent.frame())
if (is.character(j)) {
if (length(j)!=1L) stopf("Cannot assign to an under-allocated recursively indexed list -- L[[i]][,:=] syntax is only valid when i is length 1, but its length is %d", length(j))
j = match(j, names(k))
if (is.na(j)) internal_error("item '%s' not found in names of list", origj) # nocov
}
.Call(Csetlistelt,k,as.integer(j), x)
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
assign(as.character(name[[3L]]), x, k, inherits=FALSE)
} else if (isS4(k)) {
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
}
} # TO DO: else if env$<- or list$<-
} else if (name %iscall% c('$', '[[') && is.name(name[[2L]])) {
process_assignment(name, x, parent.frame())
}
}
}
}
Expand Down Expand Up @@ -2962,25 +2969,13 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) {
if (is.name(name)) {
name = as.character(name)
assign(name, x, parent.frame(), inherits=TRUE)
} else if (.is_simple_extraction(name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please be aware that the underlying code has changed since what was permalinked in #6702. In particular note this .is_simple_extraction() helper function which was not present then. Please adapt.

# common case is call from 'lapply()'
k = eval(name[[2L]], parent.frame(), parent.frame())
if (is.list(k)) {
origj = j = if (name[[1L]] == "$") as.character(name[[3L]]) else eval(name[[3L]], parent.frame(), parent.frame())
if (length(j) == 1L) {
if (is.character(j)) {
j = match(j, names(k))
if (is.na(j))
stopf("Item '%s' not found in names of input list", origj)
}
}
.Call(Csetlistelt, k, as.integer(j), x)
} else if (is.environment(k) && exists(as.character(name[[3L]]), k)) {
assign(as.character(name[[3L]]), x, k, inherits=FALSE)
} else if (isS4(k)) {
} else if (name %iscall% c('$', '[[') && is.name(name[[2L]])) {
process_assignment(name, x, parent.frame())
}
else if (isS4(k)) {
.Call(CsetS4elt, k, as.character(name[[3L]]), x)
}
} else if (name %iscall% "get") { # #6725
else if (name %iscall% "get") { # #6725
# edit 'get(nm, env)' call to be 'assign(nm, x, envir=env)'
name = match.call(get, name)
name[[1L]] = quote(assign)
Expand Down
6 changes: 6 additions & 0 deletions R/fmelt.R
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is irrelevant

Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl
value.name = "value", ..., na.rm = FALSE, variable.factor = TRUE, value.factor = FALSE,
verbose = getOption("datatable.verbose")) {
if (!is.data.table(data)) stopf("'data' must be a data.table")

# Validate id.vars
if (any(is.na(id.vars) | !nzchar(id.vars))) {
stopf("One or more values in 'id.vars' is invalid.")
}

if (missing(id.vars)) id.vars=NULL
if (missing(measure.vars)) measure.vars = NULL
measure.sub = substitute(measure.vars)
Expand Down
2 changes: 1 addition & 1 deletion inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -3295,7 +3295,7 @@ test(1034, as.data.table(x<-as.character(sample(letters, 5))), data.table(V1=x))
test(1035.291, melt(dt, measure.vars=NA_integer_, id.vars=NULL), error="One or more values in 'measure.vars'")
test(1035.30, melt(dt, id.vars=NA_integer_), error="One or more values in 'id.vars'")
test(1035.31, melt(dt, measure.vars=NA_character_), error="One or more values in 'measure.vars'")
test(1035.32, melt(dt, id.vars=NA_character_), error="One or more values in 'id.vars'")
test(1035.32, melt(dt, id.vars=NA_character_), error="One or more values in 'id.vars' is invalid.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this change


if (test_R.utils) {
# dup names in variable used to generate malformed factor error and/or segfault, #1754; was test 1570
Expand Down
Loading