Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor c233917ca5 calc: cap the text one & over arrays may build in all 2026-11-02 20:29:53 -05:00
2 changed files with 51 additions and 66 deletions
+47 -1
View File
@@ -135,10 +135,41 @@ pub(super) fn single(v: Value) -> Option<Value> {
}
}
/// Text bytes that one `&` over arrays may produce in all. Each piece is capped on its own, but a
/// compact formula can broadcast a long text across millions of cells
/// (`SEQUENCE(4000,4000)&REPT("x",30000)` is 480 GB): past this the whole result is `#NUM!`, the
/// error for a result too large to hold, before the rest is built.
pub(super) const MAX_CONCAT_ARRAY_BYTES: usize = 512 * 1024 * 1024;
pub fn binary_values(op: BinOp, x: &Value, y: &Value, sys: gridcraft_core::DateSystem) -> Value {
if op == BinOp::Concat && (matches!(x, Value::Array(_)) || matches!(y, Value::Array(_))) {
return concat_arrays(x, y, sys, MAX_CONCAT_ARRAY_BYTES);
}
zip2(x, y, |a, b| binary_scalar(op, a, b, sys))
}
/// `x & y` with at least one array operand, giving `#NUM!` once the texts built so far pass `budget`
/// bytes.
fn concat_arrays(x: &Value, y: &Value, sys: gridcraft_core::DateSystem, budget: usize) -> Value {
let used = std::cell::Cell::new(0usize);
let over = std::cell::Cell::new(false);
let r = zip2(x, y, |a, b| {
if over.get() {
return Value::Empty;
}
let v = binary_scalar(BinOp::Concat, a, b, sys);
if let Value::Text(t) = &v {
used.set(used.get().saturating_add(t.len()));
if used.get() > budget {
over.set(true);
return Value::Empty;
}
}
v
});
if over.get() { Value::Error(CellError::Num) } else { r }
}
fn binary_scalar(op: BinOp, a: &Value, b: &Value, sys: gridcraft_core::DateSystem) -> Value {
if let Value::Error(e) = a {
return Value::Error(*e);
@@ -187,7 +218,22 @@ fn binary_scalar(op: BinOp, a: &Value, b: &Value, sys: gridcraft_core::DateSyste
#[cfg(test)]
mod tests {
use super::{MAX_CELLS, broadcast_len};
use super::{Array, BinOp, CellError, MAX_CELLS, Value, broadcast_len, concat_arrays};
#[test]
fn concatenating_arrays_stops_at_the_byte_budget() {
let sys = gridcraft_core::DateSystem::default();
let col = Value::from(Array::column((0..100).map(|i| Value::number(i as f64)).collect()));
let long = Value::text("x".repeat(100));
// 100 cells of 101 or 102 bytes each: about 10 KB in all.
let r = concat_arrays(&col, &long, sys, 20_000);
assert!(matches!(&r, Value::Array(a) if a.data.len() == 100), "{r:?}");
assert_eq!(concat_arrays(&col, &long, sys, 5_000), Value::Error(CellError::Num));
assert_eq!(concat_arrays(&long, &col, sys, 5_000), Value::Error(CellError::Num));
// Through the operator, a small result is unaffected.
let r = super::binary_values(BinOp::Concat, &col, &Value::text("a"), sys);
assert!(matches!(&r, Value::Array(a) if a.data.first() == Some(&Value::text("0a"))), "{r:?}");
}
#[test]
fn broadcast_len_caps_at_max_cells() {
+4 -65
View File
@@ -1,7 +1,7 @@
//! Conditional formatting evaluation: what extra style, data bar, colour scale or icon a cell
//! gets.
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use gridcraft_core::{CellRef, RangeRef, Value, compare};
use gridcraft_model::{CfOperator, CfRule, CfValueKind, Color, CondFormat, PatternType, Sheet, Style, Underline, Workbook, default_icon_thresholds};
@@ -70,31 +70,15 @@ thread_local! {
static SCANNED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
/// Most cells one rule's statistics visit, across all of its ranges together: a rule repeating
/// the same range many times must not multiply the scan.
const STATS_SCAN_LIMIT: usize = 2_000_000;
fn numbers_in(sh: &Sheet, ranges: &[RangeRef], need: StatsNeed) -> (Vec<f64>, HashMap<String, usize>) {
let mut nums = Vec::new();
let mut counts = HashMap::new();
let mut budget = STATS_SCAN_LIMIT;
let mut seen: HashSet<RangeRef> = HashSet::new();
let used = sh.used_range();
for r in ranges {
if budget == 0 {
break;
}
let r = match used.and_then(|u| r.intersection(&u)) {
let r = match sh.used_range().and_then(|u| r.intersection(&u)) {
Some(x) => x,
None => continue,
};
// The same range listed twice counts its cells once.
if !seen.insert(r) {
continue;
}
let cells = r.iter().take(budget);
for c in cells {
budget -= 1;
for c in r.iter().take(2_000_000) {
#[cfg(test)]
SCANNED.with(|n| n.set(n.get() + 1));
let v = sh.value(c);
@@ -294,8 +278,7 @@ impl CfCache {
_ => None,
},
CfRule::ColorScale { stops } => {
// Only two- and three-colour scales paint: evaluate no thresholds for others.
if let Some(n) = v.as_f64().filter(|_| matches!(stops.len(), 2 | 3)) {
if let Some(n) = v.as_f64() {
let pts: Vec<(f64, [u8; 3])> = stops
.iter()
.map(|(k, col)| (threshold(wb, sheet, c, st, k), col.resolve(&wb.theme).unwrap_or([255, 255, 255])))
@@ -577,50 +560,6 @@ mod tests {
assert_eq!(SCANNED.with(|n| n.get()), 10_000);
}
/// A rule listing the same range many times scans it once, and never more than the shared
/// limit however many ranges it holds.
#[test]
fn repeated_ranges_share_one_scan_budget() {
let mut s = crate::Session::new();
s.new_workbook();
s.execute("range.setValues", json!({"range": "A1", "values": [[1], [2], [3]]})).unwrap();
s.execute("range.setValues", json!({"range": "XFD1048576", "values": [[4]]})).unwrap();
let d = s.doc().unwrap();
let sh = d.wb.sheet(0).unwrap();
let dense = RangeRef::parse("A1:XFD1048576").unwrap();
let small = RangeRef::parse("A1:A3").unwrap();
SCANNED.with(|n| n.set(0));
let st = RuleStats::build(sh, &vec![small; 1000], StatsNeed::Numbers);
assert_eq!(st.sorted, vec![1.0, 2.0, 3.0]);
assert_eq!(SCANNED.with(|n| n.get()), 3);
let shifted: Vec<RangeRef> = (0..50).map(|i| RangeRef::new(CellRef::new(i, 0), dense.end)).collect();
SCANNED.with(|n| n.set(0));
RuleStats::build(sh, &shifted, StatsNeed::Numbers);
assert!(SCANNED.with(|n| n.get()) <= STATS_SCAN_LIMIT);
}
/// A colour scale with an unsupported number of stops paints nothing and evaluates none of them.
#[test]
fn colour_scales_with_other_stop_counts_paint_nothing() {
let mut s = crate::Session::new();
s.new_workbook();
s.execute("range.setValues", json!({"range": "A1", "values": [[1], [5]]})).unwrap();
let mut wb: Workbook = (*s.doc().unwrap().wb).clone();
let stop = (CfValueKind::Formula("=1".into()), Color::default());
let rule = |n: usize| CondFormat {
ranges: vec![RangeRef::parse("A1:A2").unwrap()],
rule: CfRule::ColorScale { stops: vec![stop.clone(); n] },
priority: 1,
stop_if_true: false,
};
wb.sheet_mut(0).unwrap().cond_formats.push(rule(4));
let mut cache = CfCache::new();
assert_eq!(cache.look(&wb, 0, CellRef::parse("A1").unwrap()).and_then(|l| l.scale_fill), None);
wb.sheet_mut(0).unwrap().cond_formats = vec![rule(2)];
let mut cache = CfCache::new();
assert!(cache.look(&wb, 0, CellRef::parse("A1").unwrap()).and_then(|l| l.scale_fill).is_some());
}
#[test]
fn cell_value_operator_names() {
let mut s = crate::Session::new();