-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add try_*_get()
for each vector type, and fail more elegantly in Vector
element extraction
#503
Open
DavisVaughan
wants to merge
4
commits into
main
Choose a base branch
from
feature/try-elt-get
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1264,3 +1264,86 @@ pub fn plain_binding_force_with_rollback(binding: &Binding) -> anyhow::Result<RO | |||||
_ => Err(anyhow!("Unexpected binding type")), | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(test)] | ||||||
mod tests { | ||||||
use regex::Regex; | ||||||
|
||||||
use crate::test::r_test; | ||||||
use crate::variables::variable::WorkspaceVariableDisplayValue; | ||||||
|
||||||
fn get_display_value(code: &str) -> String { | ||||||
WorkspaceVariableDisplayValue::from(harp::parse_eval_base(code).unwrap().sexp).display_value | ||||||
} | ||||||
|
||||||
fn expect_display_value(code: &str, expected: &str) { | ||||||
let display = get_display_value(code); | ||||||
assert_eq!(display, expected.to_string()); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_simple_display_values() { | ||||||
r_test(|| { | ||||||
expect_display_value("1", "1"); | ||||||
expect_display_value("1L", "1"); | ||||||
expect_display_value("'a'", "\"a\""); | ||||||
expect_display_value("NULL", "NULL"); | ||||||
expect_display_value("TRUE", "TRUE"); | ||||||
expect_display_value("FALSE", "FALSE"); | ||||||
expect_display_value("1i", "0+1i"); | ||||||
}) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_data_frame_display_value() { | ||||||
r_test(|| { | ||||||
expect_display_value("datasets::mtcars", "[32 rows x 11 columns] <data.frame>"); | ||||||
expect_display_value("matrix(1:4, ncol=2)", "[[1 2], [3 4]]"); | ||||||
}) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_list_display_value() { | ||||||
r_test(|| { | ||||||
expect_display_value("list(x=1:4)", "[x = 1 2 3 4]"); | ||||||
expect_display_value("list(1:4)", "[1 2 3 4]"); | ||||||
}) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_functions_display_value() { | ||||||
r_test(|| { | ||||||
expect_display_value("function() NULL", "function () "); | ||||||
expect_display_value("function(a) NULL", "function (a) "); | ||||||
expect_display_value("function(a, b) NULL", "function (a, b) "); | ||||||
expect_display_value("function(a = 1, b) NULL", "function (a = 1, b) "); | ||||||
}) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_altrep_is_not_materialized() { | ||||||
r_test(|| { | ||||||
// Usage of `INTEGER_ELT()` combined with an ALTREP compact integer sequence | ||||||
// should allow us to display this no matter what | ||||||
let display = get_display_value("1:1e10"); | ||||||
assert!(Regex::new(r"^1 2 3.*").unwrap().is_match(display.as_str())); | ||||||
|
||||||
// With ALTREP deferred string names used below, we use `STRING_ELT()` as we | ||||||
// should to extract the values, but even that causes a full materialization | ||||||
// of the STRSXP vector inside the ALTREP `Elt` method for deferred strings, | ||||||
// which throws an OOM error when trying to look at elements of `names(x)`. | ||||||
// We catch this, log an error, and return `NA` as the element value since | ||||||
// we can't determine what it is. | ||||||
let success = Regex::new(r#"^"1" "2" "3""#).unwrap(); | ||||||
let failure = Regex::new(r#"^NA NA NA"#).unwrap(); | ||||||
|
||||||
// Small, should always pass with `success` | ||||||
let display = get_display_value("local({x = 1:1e3; names(x) = x; names(x)})"); | ||||||
assert!(success.is_match(display.as_str()) || failure.is_match(display.as_str())); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// Extremely large, would only work if you have >32gb of RAM | ||||||
let display = get_display_value("local({x = 1:1e10; names(x) = x; names(x)})"); | ||||||
assert!(success.is_match(display.as_str()) || failure.is_match(display.as_str())); | ||||||
}) | ||||||
} | ||||||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!