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

Limit the number of bins for histograms to avoid crashes #655

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions crates/ark/src/data_explorer/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,29 @@ mod tests {
);
})
}

#[test]
fn test_limit_bins() {
// Regression test for https://github.com/posit-dev/positron/issues/5744
r_task(|| {
let column = harp::parse_eval_global("rep(c(1:10, 5e7), 10)").unwrap();

let hist = profile_histogram(
column.sexp,
&ColumnHistogramParams {
method: ColumnHistogramParamsMethod::FreedmanDiaconis,
// If num_bins wasn't set to 10, that would generate almost 200k bins
num_bins: 10,
quantiles: None,
},
&default_options(),
)
.unwrap();

assert_match!(hist, ColumnHistogram { bin_edges, bin_counts, .. } => {
assert_eq!(bin_edges.len(), 11);
assert_eq!(bin_counts.len(), 10);
});
})
}
}
5 changes: 5 additions & 0 deletions crates/ark/src/modules/positron/r_data_explorer.R
Original file line number Diff line number Diff line change
Expand Up @@ -519,5 +519,10 @@ histogram_num_bins <- function(x, method, fixed_num_bins) {
}
}

# Honor the maximum amount of bins requested by the front-end.
if (!is.null(fixed_num_bins) && num_bins > fixed_num_bins) {
num_bins <- fixed_num_bins
}

as.integer(num_bins)
}
Loading