Skip to content

ARROW-12575: [R] Use unary negative kernel#10196

Closed
thisisnic wants to merge 7 commits into
apache:masterfrom
thisisnic:ARROW-12575-negative
Closed

ARROW-12575: [R] Use unary negative kernel#10196
thisisnic wants to merge 7 commits into
apache:masterfrom
thisisnic:ARROW-12575-negative

Conversation

@thisisnic

Copy link
Copy Markdown
Member

No description provided.

@github-actions

Copy link
Copy Markdown

@thisisnic thisisnic force-pushed the ARROW-12575-negative branch from 3d861e7 to 09da203 Compare May 3, 2021 09:54
Comment thread r/R/expression.R Outdated
# Make it be 0 - arg
# TODO(ARROW-11950): do this in C++ compute
args <- list(0L, args[[1]])
return(build_array_expression("negate", args[[1]]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Question: do we want to use "negate" or "negate_checked" here? https://github.com/apache/arrow/blob/master/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc#L514-L524

We have tended to use the "checked" versions of the other arithmetic functions, but we haven't thought too hard about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the edge case which negate_checked watches for is probably not necessary here. @edponce

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Perhaps for the sake of consistency, using negate_checked makes sense, even if not strictly necessary?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alright, note that negate_checked does not support unsigned arguments:

call_function("negate_checked", args=list(Array$create(c(1,2), type=uint8())))
#> Error: NotImplemented: Function negate_checked has no kernel matching input types (array[uint8])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just noticed that we should update the negate_checked_doc to state explicitly that it does not support unsigned arguments. We do show this in the function signature table https://github.com/apache/arrow/blob/master/docs/source/cpp/compute.rst#arithmetic-functions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would think that using the checked versions for language bindings is more desirable as you want errors to be triggered ASAP. The unchecked versions should be used knowing the wrap around behavior may occur. I think of it as unchecked = safe math and checked = true math

@ianmcook

ianmcook commented May 4, 2021

Copy link
Copy Markdown
Member

@thisisnic could you add some tests with multiple unary negative operators stacked up. Maybe test with odd & even numbers of them, and parens also, e.g.

---2
-(-(-2))
--2
-(-(2))

@nealrichardson

Copy link
Copy Markdown
Member

@thisisnic could you add some tests with multiple unary negative operators stacked up. Maybe test with odd & even numbers of them, and parens also, e.g.

---2
-(-(-2))
--2
-(-(2))

Wouldn't this just be testing R's parser?

@edponce

edponce commented May 4, 2021

Copy link
Copy Markdown
Contributor

The multiple negations and parenthesis are resolved on R side as only the resolved value is passed to the C++ backend. If the expression was passed as a string then it would make sense to check these cases.

When we did the C++ tests, we checked for nested negations and they passed (although these tests were not included in the final PR).

@ianmcook

ianmcook commented May 4, 2021

Copy link
Copy Markdown
Member

Wouldn't this just be testing R's parser?

In theory yes? I tend to have this philosophy re tests: https://twitter.com/gshotwell/status/1389563734679605251?s=20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants