mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-03 00:41:56 -06:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89cf961a2f |
+65
-4
@@ -1,7 +1,7 @@
|
||||
//! Conditional formatting evaluation: what extra style, data bar, colour scale or icon a cell
|
||||
//! gets.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use gridcraft_core::{CellRef, RangeRef, Value, compare};
|
||||
use gridcraft_model::{CfOperator, CfRule, CfValueKind, Color, CondFormat, PatternType, Sheet, Style, Underline, Workbook, default_icon_thresholds};
|
||||
@@ -70,15 +70,31 @@ 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 {
|
||||
let r = match sh.used_range().and_then(|u| r.intersection(&u)) {
|
||||
if budget == 0 {
|
||||
break;
|
||||
}
|
||||
let r = match used.and_then(|u| r.intersection(&u)) {
|
||||
Some(x) => x,
|
||||
None => continue,
|
||||
};
|
||||
for c in r.iter().take(2_000_000) {
|
||||
// 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;
|
||||
#[cfg(test)]
|
||||
SCANNED.with(|n| n.set(n.get() + 1));
|
||||
let v = sh.value(c);
|
||||
@@ -278,7 +294,8 @@ impl CfCache {
|
||||
_ => None,
|
||||
},
|
||||
CfRule::ColorScale { stops } => {
|
||||
if let Some(n) = v.as_f64() {
|
||||
// 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)) {
|
||||
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])))
|
||||
@@ -560,6 +577,50 @@ 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();
|
||||
|
||||
@@ -42,15 +42,6 @@ pub(super) fn table_resize(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Most cells (rows × columns, blanks included) Get Data imports. The rows are built densely
|
||||
/// before they are written, so a few sparse records or one far-away CSV cell must not expand
|
||||
/// into a rectangle of billions of slots.
|
||||
const MAX_IMPORT_CELLS: u64 = 10_000_000;
|
||||
|
||||
fn too_many_cells() -> EngineError {
|
||||
EngineError::Other(format!("The data is too large to import ({MAX_IMPORT_CELLS} cells at most)."))
|
||||
}
|
||||
|
||||
pub(super) fn from_csv(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
let bytes = if let Some(t) = str_param(p, "text") {
|
||||
t.as_bytes().to_vec()
|
||||
@@ -66,22 +57,15 @@ pub(super) fn from_csv(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
if text.trim_start().starts_with('[') {
|
||||
let arr: Vec<Json> = serde_json::from_str(&text).map_err(|e| bad("data.getData", e.to_string()))?;
|
||||
let mut keys: Vec<String> = Vec::new();
|
||||
let mut seen: std::collections::HashSet<&str> = std::collections::HashSet::new();
|
||||
for o in &arr {
|
||||
if let Some(m) = o.as_object() {
|
||||
for k in m.keys() {
|
||||
if seen.insert(k.as_str()) {
|
||||
if !keys.contains(k) {
|
||||
keys.push(k.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if keys.len() > gridcraft_core::MAX_COLS as usize {
|
||||
return Err(EngineError::Other("The data has more fields than a sheet has columns.".into()));
|
||||
}
|
||||
if (arr.len() as u64 + 1).saturating_mul(keys.len().max(1) as u64) > MAX_IMPORT_CELLS {
|
||||
return Err(too_many_cells());
|
||||
}
|
||||
let mut out = vec![keys.iter().map(|k| json!(k)).collect::<Vec<_>>()];
|
||||
for o in &arr {
|
||||
out.push(keys.iter().map(|k| o.get(k).cloned().unwrap_or(Json::Null)).collect());
|
||||
@@ -95,9 +79,6 @@ pub(super) fn from_csv(s: &mut Session, p: &Json) -> Result<Json> {
|
||||
let wb = gridcraft_xlsx::read_csv(&bytes, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
|
||||
let sh = wb.sheet(0).ok_or(EngineError::NoDocument)?;
|
||||
let Some(u) = sh.used_range() else { return Err(EngineError::Other("The file is empty.".into())) };
|
||||
if u.count() > MAX_IMPORT_CELLS {
|
||||
return Err(too_many_cells());
|
||||
}
|
||||
(u.start.row..=u.end.row)
|
||||
.map(|r| {
|
||||
(u.start.col..=u.end.col)
|
||||
@@ -148,35 +129,3 @@ pub(super) fn queries(s: &mut Session, _: &Json) -> Result<Json> {
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn s() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.new_workbook();
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sparse_imports_are_refused_before_their_rows_are_built() {
|
||||
let mut s = s();
|
||||
let sheets = s.doc().unwrap().wb.sheets.len();
|
||||
// 5,000 records with one field each: 5,001 × 5,000 slots once every record gets every field.
|
||||
let records: Vec<_> = (0..5_000).map(|i| json!({ format!("k{i}"): i })).collect();
|
||||
let text = serde_json::to_string(&records).unwrap();
|
||||
let err = s.execute("data.getData", json!({"text": text})).expect_err("too many cells");
|
||||
assert!(err.to_string().contains("too large"), "{err}");
|
||||
// A CSV whose last line reaches 16,001 columns, 1,000 lines down.
|
||||
let text = format!("1{}{}x", "\n".repeat(999), ",".repeat(16_000));
|
||||
let err = s.execute("data.fromTextCsv", json!({"text": text})).expect_err("too many cells");
|
||||
assert!(err.to_string().contains("too large"), "{err}");
|
||||
assert_eq!(s.doc().unwrap().wb.sheets.len(), sheets);
|
||||
// Small imports still work.
|
||||
let out = s.execute("data.getData", json!({"text": r#"[{"a": 1}, {"b": 2}]"#})).unwrap();
|
||||
assert_eq!((out["rows"].as_u64(), out["cols"].as_u64()), (Some(3), Some(2)), "{out}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user