Skip to content

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

Closed
nipun-gupta-3108 wants to merge 16 commits intoRdatatable:masterfrom
nipun-gupta-3108:refactor-helper-function-issue-6702
Closed

Refactor helper function issue #6702 (Refactored duplicated logic in setDT and [,:=])#6752
nipun-gupta-3108 wants to merge 16 commits intoRdatatable:masterfrom
nipun-gupta-3108:refactor-helper-function-issue-6702

Conversation

@nipun-gupta-3108
Copy link

Hi MichaelChirico,

I’ve been working on refactoring the duplicated logic in the setDT function and the [,:=] operation as discussed in issue #6702.

Here’s what I’ve done so far:

  1. I identified the common logic that handles reassignments for list and environment objects.
  2. I extracted this logic into a helper function named process_assignment.

Here’s the helper function I’ve created:

process_assignment <- function(name, x, parent_frame) {
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)) {
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)
}
}

I then replaced the original duplicated blocks with calls to this helper function in both places. For example:

In the setDT function:

} else if (name %iscall% c('$', '[[') && is.name(name[[2L]])) {
process_assignment(name, x, parent.frame())
}

Questions I’d Like Feedback On:

  1. Does this approach align with what you had in mind for the refactoring?
  2. Are there any specific edge cases or performance concerns I should consider?
  3. Would you suggest any additional steps for improving or testing this change?

I’d appreciate your guidance on any further improvements or next steps.

Looking forward to your feedback!

Best regards,
Nipun Gupta

@@ -1,9 +1,10 @@
{
"build": { "dockerfile": "r-devel-gcc/Dockerfile", "context": ".." },
"build": { "dockerfile": "Dockerfile", "context": "../.." },
Copy link
Member

Choose a reason for hiding this comment

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

it looks like you've still got some edits from earlier #6747 included here -- please revert, for example:

git checkout master -- .devcontainer/devcontainer.json

@nipun-gupta-3108
Copy link
Author

Hi MichaelChirico,

I'm encountering a test failure in test-coverage.yaml related to the pull request. The test is failing with the following error:

Error in test.data.table(): Failed in 14.0s elapsed (14.6s cpu) after test 1035.32 before the next test() call in /home/runner/work/_temp/package/data.table/tests/tests.Rraw

This issue is related to test 1035.32. Could you please help me understand why this test is failing?

Thank you for your assistance!

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

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

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 (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.

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

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

@MichaelChirico
Copy link
Member

It looks like #6702 is working on the same thing. Please feel free to add your contributions there as that PR is much closer to complete.

Sorry for not flagging earlier to reduce duplicate effort.

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.

2 participants