mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-03 00:41:56 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b1d623549 |
Generated
+62
-51
@@ -195,6 +195,15 @@ version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
|
||||
|
||||
[[package]]
|
||||
name = "arbitrary"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed"
|
||||
dependencies = [
|
||||
"derive_arbitrary",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arboard"
|
||||
version = "3.7.0"
|
||||
@@ -211,7 +220,7 @@ dependencies = [
|
||||
"objc2-foundation 0.3.2",
|
||||
"parking_lot",
|
||||
"percent-encoding",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
"wl-clipboard-rs",
|
||||
"x11rb",
|
||||
]
|
||||
@@ -954,7 +963,7 @@ dependencies = [
|
||||
"objc2 0.6.5",
|
||||
"objc2-app-kit 0.3.2",
|
||||
"objc2-foundation 0.3.2",
|
||||
"skrifa 0.44.0",
|
||||
"skrifa",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
@@ -983,7 +992,7 @@ dependencies = [
|
||||
"verifysign",
|
||||
"winreg",
|
||||
"winsafe",
|
||||
"zip",
|
||||
"zip 9.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1121,6 +1130,17 @@ version = "0.5.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e"
|
||||
|
||||
[[package]]
|
||||
name = "derive_arbitrary"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b034bd7d5f032402a2479444dcc6f74e36a03f31854d41680fb240ef682a1ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dify"
|
||||
version = "0.8.0"
|
||||
@@ -1431,7 +1451,7 @@ dependencies = [
|
||||
"parking_lot",
|
||||
"profiling",
|
||||
"self_cell",
|
||||
"skrifa 0.44.0",
|
||||
"skrifa",
|
||||
"smallvec",
|
||||
"unicode-bidi",
|
||||
"unicode-general-category",
|
||||
@@ -1458,7 +1478,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1637,9 +1657,9 @@ checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
|
||||
|
||||
[[package]]
|
||||
name = "font-types"
|
||||
version = "0.12.7"
|
||||
version = "0.12.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "835ae9d781464a79026cde387f9cbc650c9a4909d7cf7e658b127e3c35d2f35d"
|
||||
checksum = "6c92d9081c7ab3bd83ef1eecdfad090f3d966e3ce74106779664114cfe1a6274"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
@@ -1850,7 +1870,7 @@ dependencies = [
|
||||
"hashbrown 0.17.1",
|
||||
"log",
|
||||
"peniko",
|
||||
"skrifa 0.44.0",
|
||||
"skrifa",
|
||||
"smallvec",
|
||||
"vello_common",
|
||||
]
|
||||
@@ -2004,9 +2024,9 @@ dependencies = [
|
||||
"gridcraft-engine",
|
||||
"gridcraft-mcp",
|
||||
"log",
|
||||
"quick-xml 0.42.0",
|
||||
"quick-xml 0.41.0",
|
||||
"serde_json",
|
||||
"zip",
|
||||
"zip 3.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2040,7 +2060,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"web-time",
|
||||
"zip",
|
||||
"zip 3.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2120,7 +2140,7 @@ dependencies = [
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"skrifa 0.48.0",
|
||||
"skrifa",
|
||||
"sys-locale",
|
||||
"toml",
|
||||
"web-sys",
|
||||
@@ -2155,11 +2175,11 @@ dependencies = [
|
||||
"gridcraft-functions",
|
||||
"gridcraft-math",
|
||||
"gridcraft-model",
|
||||
"quick-xml 0.42.0",
|
||||
"quick-xml 0.41.0",
|
||||
"sha1 0.11.0",
|
||||
"sha2 0.11.1",
|
||||
"thiserror 2.0.21",
|
||||
"zip",
|
||||
"zip 3.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2210,7 +2230,7 @@ checksum = "c03d949a14aa089bbb282f7dd76a498a7f684428e4257202efc119ec010376f9"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"bytemuck",
|
||||
"read-fonts 0.41.0",
|
||||
"read-fonts",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
@@ -2880,9 +2900,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.35"
|
||||
version = "0.4.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
|
||||
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
|
||||
|
||||
[[package]]
|
||||
name = "lru-slab"
|
||||
@@ -3487,7 +3507,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3943,7 +3963,7 @@ dependencies = [
|
||||
"once_cell",
|
||||
"socket2",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4016,9 +4036,9 @@ checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08"
|
||||
|
||||
[[package]]
|
||||
name = "ratex-font"
|
||||
version = "0.1.15"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4373503a1068c9f7d07862583bbd897ac4d2bdf89d42ac8c65870c33135bf6f"
|
||||
checksum = "6a7b73531dabe934a3e725ed5c90d18dacf43778b7a208381390943a9a8c24db"
|
||||
dependencies = [
|
||||
"phf 0.11.3",
|
||||
"ratex-types",
|
||||
@@ -4067,9 +4087,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ratex-types"
|
||||
version = "0.1.15"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae81208192f9898cbd2424aa42fd4472bd79c44a52092673b101cdca72c89b7c"
|
||||
checksum = "0cf0be3d050525f0f7d37af55a443d7648c222ccc617639950e3de1f8e6f9900"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
@@ -4123,19 +4143,6 @@ dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "read-fonts"
|
||||
version = "0.45.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4edc8db0d972de88e53a085b1f01fb3fbdae7bb0cb7a0ce2b2f8285a412fe3ef"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"foldhash 0.2.0",
|
||||
"font-types",
|
||||
"once_cell",
|
||||
"yoke",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.4.1"
|
||||
@@ -4341,7 +4348,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.12.1",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4400,7 +4407,7 @@ dependencies = [
|
||||
"security-framework",
|
||||
"security-framework-sys",
|
||||
"webpki-root-certs",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4875,17 +4882,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "819ab7d62b1d3e72d9d9dea5650bac30424f9111364bb94928dbf5ecad1baa68"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"read-fonts 0.41.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "skrifa"
|
||||
version = "0.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5213b4825bac3587046330052d61bfac7bd3bd0d203c758d7c2ef726fcea034"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"read-fonts 0.45.1",
|
||||
"read-fonts",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5161,7 +5158,7 @@ dependencies = [
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix 1.1.5",
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6154,7 +6151,7 @@ version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
"windows-sys 0.59.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6854,6 +6851,20 @@ dependencies = [
|
||||
"syn 3.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "3.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12598812502ed0105f607f941c386f43d441e00148fce9dec3ca5ffb0bde9308"
|
||||
dependencies = [
|
||||
"arbitrary",
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"indexmap",
|
||||
"memchr",
|
||||
"zopfli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "9.1.0"
|
||||
|
||||
+5
-5
@@ -40,8 +40,8 @@ chrono = { version = "0.4.45", default-features = false, features = ["std", "clo
|
||||
web-time = "1.1.0"
|
||||
rayon = "1"
|
||||
regex = { version = "1", default-features = false, features = ["std", "unicode", "perf"] }
|
||||
quick-xml = "0.42"
|
||||
zip = { version = "9", default-features = false, features = ["deflate"] }
|
||||
quick-xml = "0.41"
|
||||
zip = { version = "3", default-features = false, features = ["deflate"] }
|
||||
sha2 = { version = "0.11", default-features = false }
|
||||
# Decrypting password-protected workbooks ([MS-OFFCRYPTO]): AES and SHA-1, pure Rust (wasm-safe).
|
||||
aes = { version = "0.9", default-features = false }
|
||||
@@ -52,8 +52,8 @@ image = { version = "0.25", default-features = false, features = ["png", "jpeg"]
|
||||
# young (0.1.x), and the gate recorded in docs/decisions.md vetted this release.
|
||||
ratex-parser = "=0.1.14"
|
||||
ratex-layout = "=0.1.14"
|
||||
ratex-types = "=0.1.15"
|
||||
ratex-font = "=0.1.15"
|
||||
ratex-types = "=0.1.14"
|
||||
ratex-font = "=0.1.14"
|
||||
|
||||
# ui
|
||||
egui = "0.37"
|
||||
@@ -66,7 +66,7 @@ arboard = { version = "3.6", default-features = false }
|
||||
sys-locale = "0.3"
|
||||
# Font parser egui's epaint uses; the UI validates system font files with it before handing them
|
||||
# to egui, which panics on a file it can't parse. Same version as epaint's, so no second copy.
|
||||
skrifa = { version = "0.48", default-features = false, features = ["std"] }
|
||||
skrifa = { version = "0.44", default-features = false, features = ["std"] }
|
||||
# Real font bytes for tests (Ubuntu Light, compiled into epaint).
|
||||
epaint_default_fonts = "0.37"
|
||||
# One native window per workbook: the window list, Window menu titles and the immediate-viewport
|
||||
|
||||
@@ -70,27 +70,42 @@ fn cell_criterion(v: &Value) -> Option<Criterion> {
|
||||
/// when the label is unknown, criterion).
|
||||
type CriteriaRow = (usize, Vec<(usize, Option<usize>, Criterion)>);
|
||||
|
||||
/// Most condition tests one selection may make (records times conditions): every record can be
|
||||
/// tested against every criteria row when none matches, so a modest database and criteria table
|
||||
/// multiply into billions of tests. Past it the function is `#NUM!`, as lifted lookups are; 5e8
|
||||
/// is a few seconds of matching.
|
||||
const SELECTION_WORK_BUDGET: usize = 500_000_000;
|
||||
|
||||
/// Rows (1-based within the database, excluding the header) of matching records.
|
||||
fn matching_records(db: &Array, crit: &Array, ctx: &mut dyn Ctx) -> Vec<usize> {
|
||||
fn matching_records(db: &Array, crit: &Array, ctx: &mut dyn Ctx) -> R<Vec<usize>> {
|
||||
// The database column under each criteria label, found once per label rather than per cell.
|
||||
let columns: Vec<Option<usize>> = (0..crit.cols)
|
||||
.map(|c| match crit.get(0, c) {
|
||||
Some(Value::Text(t)) => find_column(db, t),
|
||||
Some(Value::Number(n)) => find_column(db, &gridcraft_core::number_to_text(*n)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
// Per criteria row: its index and a list of (criteria column, database column or None when
|
||||
// the label is unknown, criterion).
|
||||
let mut rows: Vec<CriteriaRow> = Vec::new();
|
||||
let mut tests = 0usize;
|
||||
for r in 1..crit.rows {
|
||||
let mut conds = Vec::new();
|
||||
for c in 0..crit.cols {
|
||||
let Some(cell) = crit.get(r, c) else { continue };
|
||||
let Some(cr) = cell_criterion(cell) else { continue };
|
||||
let col = match crit.get(0, c) {
|
||||
Some(Value::Text(t)) => find_column(db, t),
|
||||
Some(Value::Number(n)) => find_column(db, &gridcraft_core::number_to_text(*n)),
|
||||
_ => None,
|
||||
};
|
||||
conds.push((c, col, cr));
|
||||
conds.push((c, columns.get(c).copied().flatten(), cr));
|
||||
}
|
||||
tests = tests.saturating_add(conds.len().max(1));
|
||||
rows.push((r, conds));
|
||||
}
|
||||
// Computed criteria (a formula evaluated per record) count like any other test.
|
||||
if db.rows.saturating_sub(1).saturating_mul(tests) > SELECTION_WORK_BUDGET {
|
||||
return Err(CellError::Num);
|
||||
}
|
||||
let all_match = rows.is_empty();
|
||||
(1..db.rows)
|
||||
Ok((1..db.rows)
|
||||
.filter(|&rec| {
|
||||
all_match
|
||||
|| rows.iter().any(|(r, conds)| {
|
||||
@@ -102,7 +117,7 @@ fn matching_records(db: &Array, crit: &Array, ctx: &mut dyn Ctx) -> Vec<usize> {
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
.collect())
|
||||
}
|
||||
|
||||
struct Selection<'a> {
|
||||
@@ -123,7 +138,7 @@ fn select<'a>(a: &'a [Arg], ctx: &mut dyn Ctx) -> R<Selection<'a>> {
|
||||
let db = as_array(dbv);
|
||||
let crit = as_array(critv);
|
||||
let field = field_index(&db, a)?;
|
||||
let records = matching_records(&db, &crit, ctx);
|
||||
let records = matching_records(&db, &crit, ctx)?;
|
||||
Ok(Selection { db, field, records })
|
||||
}
|
||||
|
||||
@@ -352,6 +367,24 @@ mod tests {
|
||||
is_err(d("DSUM", n(9.0)), CellError::Value);
|
||||
}
|
||||
|
||||
/// Security: records times criteria conditions past the selection budget is `#NUM!` before
|
||||
/// any record is tested; a nonmatching criteria table used to test every record against
|
||||
/// every criteria row.
|
||||
#[test]
|
||||
fn selection_work_is_bounded() {
|
||||
use gridcraft_core::Array;
|
||||
let column = |label: &str, len: usize, v: Value| {
|
||||
let mut data = vec![tv(label)];
|
||||
data.extend(std::iter::repeat_n(v, len));
|
||||
Value::from(Array::column(data))
|
||||
};
|
||||
let db = column("Tree", 100_000, tv("Apple"));
|
||||
let wide = column("Tree", 5_001, tv("=Plum"));
|
||||
is_err(ev("DCOUNTA", vec![rf(db.clone()), t("Tree"), rf(wide)]), CellError::Num);
|
||||
let narrow = column("Tree", 2, tv("=Apple"));
|
||||
close(ev("DCOUNTA", vec![rf(db), t("Tree"), rf(narrow)]), 100_000.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_criteria() {
|
||||
let c = |cells: Vec<Value>| arr(vec![vec![tv("Tree"), tv("Height")], cells]);
|
||||
|
||||
@@ -416,13 +416,17 @@ fn workday_core(a: &[Arg], c: &mut dyn Ctx, mask: [bool; 7], hol: Option<&Arg>)
|
||||
if days == 0 {
|
||||
return num_val(start as f64);
|
||||
}
|
||||
// The holidays on working days, still sorted (`holidays` sorts them), so each step counts
|
||||
// the ones it passed with two binary searches rather than a scan of every holiday: a run of
|
||||
// consecutive holidays takes one step per holiday.
|
||||
let hs: Vec<i64> = hs.into_iter().filter(|&h| !mask[weekday0(sys, h)]).collect();
|
||||
let dir = days.signum();
|
||||
let mut cur = start;
|
||||
let mut rem = days.abs();
|
||||
while rem > 0 {
|
||||
let next = advance(sys, cur, rem, dir, &mask, per_week);
|
||||
let (lo, hi) = if dir > 0 { (cur + 1, next) } else { (next, cur - 1) };
|
||||
rem = hs.iter().filter(|&&h| h >= lo && h <= hi && !mask[weekday0(sys, h)]).count() as i64;
|
||||
rem = hs.partition_point(|&h| h <= hi).saturating_sub(hs.partition_point(|&h| h < lo)) as i64;
|
||||
cur = next;
|
||||
if !(0..=MAX_SERIAL as i64).contains(&cur) {
|
||||
return Err(CellError::Num);
|
||||
@@ -755,6 +759,23 @@ mod tests {
|
||||
is_err(ev("TIMEVALUE", vec![t("x")]), CellError::Value);
|
||||
}
|
||||
|
||||
/// Security: WORKDAY counts the holidays each step passes by binary search, so a long run of
|
||||
/// consecutive holidays is linear work, not one scan of every holiday per holiday.
|
||||
#[test]
|
||||
fn workday_over_many_consecutive_holidays() {
|
||||
let start = d(2000.0, 1.0, 3.0);
|
||||
let hol: Vec<f64> = (1..=200_000).map(|i| start + f64::from(i)).collect();
|
||||
let out = ev("WORKDAY.INTL", vec![n(start), n(1.0), n(11.0), av(col(&hol))]);
|
||||
// Sundays are the only weekend (11): 200,000 days of holidays are skipped, landing on the
|
||||
// first working day after them.
|
||||
let after = start + 200_001.0;
|
||||
let sunday = ev("WEEKDAY", vec![n(after), n(2.0)]) == nv(7.0);
|
||||
close(out, if sunday { after + 1.0 } else { after });
|
||||
// Holidays on a weekend don't push the result out.
|
||||
let fri = d(2026.0, 10.0, 9.0);
|
||||
close(ev("WORKDAY", vec![n(fri), n(1.0), av(col(&[fri + 1.0, fri + 3.0]))]), fri + 4.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn day_counts() {
|
||||
close(ev("DAYS", vec![n(d(2021.0, 3.0, 15.0)), n(d(2021.0, 2.0, 1.0))]), 42.0);
|
||||
|
||||
@@ -178,6 +178,10 @@ fn sort(args: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
|
||||
return Err(CellError::Value);
|
||||
}
|
||||
let orders: Vec<i64> = (0..idx.len()).map(|i| *ord.get(i).or(ord.first()).unwrap_or(&1)).collect();
|
||||
// A column repeated in sort_index can't break a tie its first use left, so only first uses
|
||||
// stay: the keys copied per row are then at most the row itself, however long sort_index is.
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let (idx, orders): (Vec<i64>, Vec<i64>) = idx.into_iter().zip(orders).filter(|(i, _)| seen.insert(*i)).unzip();
|
||||
let mut lines: Vec<(Vec<Value>, Vec<Value>)> =
|
||||
rows_of(&a).into_iter().map(|r| (idx.iter().map(|&i| r.get(i as usize - 1).cloned().unwrap_or(Value::Empty)).collect(), r)).collect();
|
||||
lines.sort_by(|x, y| cmp_keys(&x.0, &y.0, &orders));
|
||||
@@ -834,9 +838,16 @@ fn pivotby(args: &[Arg], ctx: &mut dyn Ctx) -> R<Value> {
|
||||
let ckeys: Vec<Vec<Value>> = p.rows.iter().map(|&r| key_row(&cf, r)).collect();
|
||||
let rk = distinct_keys(&rkeys, if row_sort.unsigned_abs() as usize <= rf.cols { row_sort } else { 0 });
|
||||
let ck = distinct_keys(&ckeys, if col_sort.unsigned_abs() as usize <= cf.cols { col_sort } else { 0 });
|
||||
// Source rows of every row key, column key and pair, built in one pass, so a total reads
|
||||
// its rows instead of rescanning the whole source once per total.
|
||||
let mut cell: HashMap<(String, String), Vec<usize>> = HashMap::new();
|
||||
let mut by_row: HashMap<String, Vec<usize>> = HashMap::new();
|
||||
let mut by_col: HashMap<String, Vec<usize>> = HashMap::new();
|
||||
for ((r, c), &i) in rkeys.iter().zip(ckeys.iter()).zip(p.rows.iter()) {
|
||||
cell.entry((row_key(r), row_key(c))).or_default().push(i);
|
||||
let (r, c) = (row_key(r), row_key(c));
|
||||
by_row.entry(r.clone()).or_default().push(i);
|
||||
by_col.entry(c.clone()).or_default().push(i);
|
||||
cell.entry((r, c)).or_default().push(i);
|
||||
}
|
||||
let mut agg = |rows: &[usize], c: usize| -> Value {
|
||||
if rows.is_empty() {
|
||||
@@ -846,14 +857,12 @@ fn pivotby(args: &[Arg], ctx: &mut dyn Ctx) -> R<Value> {
|
||||
aggregate_with(f, &vs, ctx)
|
||||
};
|
||||
let rows_for = |r: Option<&Vec<Value>>, c: Option<&Vec<Value>>| -> Vec<usize> {
|
||||
let rkey = r.map(|k| row_key(k));
|
||||
let ckey = c.map(|k| row_key(k));
|
||||
p.rows
|
||||
.iter()
|
||||
.zip(rkeys.iter().zip(ckeys.iter()))
|
||||
.filter(|(_, (a, b))| rkey.as_ref().is_none_or(|k| *k == row_key(a)) && ckey.as_ref().is_none_or(|k| *k == row_key(b)))
|
||||
.map(|(&i, _)| i)
|
||||
.collect()
|
||||
match (r, c) {
|
||||
(Some(r), Some(c)) => cell.get(&(row_key(r), row_key(c))).cloned().unwrap_or_default(),
|
||||
(Some(r), None) => by_row.get(&row_key(r)).cloned().unwrap_or_default(),
|
||||
(None, Some(c)) => by_col.get(&row_key(c)).cloned().unwrap_or_default(),
|
||||
(None, None) => p.rows.clone(),
|
||||
}
|
||||
};
|
||||
let mut cols: Vec<Option<&Vec<Value>>> = ck.iter().map(Some).collect();
|
||||
if col_total > 0 {
|
||||
@@ -903,10 +912,7 @@ fn pivotby(args: &[Arg], ctx: &mut dyn Ctx) -> R<Value> {
|
||||
}
|
||||
};
|
||||
for c in &cols {
|
||||
let rows = match (r, c) {
|
||||
(Some(rk), Some(ck)) => cell.get(&(row_key(rk), row_key(ck))).cloned().unwrap_or_default(),
|
||||
_ => rows_for(r, *c),
|
||||
};
|
||||
let rows = rows_for(r, *c);
|
||||
for vc in 0..vals.cols {
|
||||
line.push(agg(&rows, vc));
|
||||
}
|
||||
@@ -1110,6 +1116,21 @@ mod tests {
|
||||
assert_eq!(rows_of(&r), vec![vec![nv(0.0), nv(5.0)], vec![nv(1.0), nv(2.0)], vec![nv(1.0), nv(1.0)]]);
|
||||
}
|
||||
|
||||
/// Security: a column repeated in sort_index keys the sort once. The keys copied per row stay
|
||||
/// within the row, however long sort_index is, and a repeat (even with the other order) can't
|
||||
/// change the result, since its first use already ordered the rows.
|
||||
#[test]
|
||||
fn sort_drops_repeated_sort_indices() {
|
||||
let m = || av(arr(vec![vec![nv(1.0), nv(2.0)], vec![nv(1.0), nv(1.0)], vec![nv(0.0), nv(5.0)]]));
|
||||
let want = vec![vec![nv(0.0), nv(5.0)], vec![nv(1.0), nv(2.0)], vec![nv(1.0), nv(1.0)]];
|
||||
let r = ev("SORT", vec![m(), av(row(&[1.0, 1.0, 2.0, 1.0])), av(row(&[1.0, -1.0, -1.0, 1.0]))]);
|
||||
assert_eq!(rows_of(&r), want);
|
||||
let idx = vec![1.0; 1_000_000];
|
||||
let tall = av(gridcraft_core::Array::filled(2_000, 1, nv(1.0)).into());
|
||||
let r = ev("SORT", vec![tall, av(row(&idx))]);
|
||||
assert_eq!(rows_of(&r).len(), 2_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sortby() {
|
||||
let r = ev("SORTBY", vec![av(col(&[1.0, 2.0, 3.0])), av(col(&[30.0, 10.0, 20.0]))]);
|
||||
@@ -1281,6 +1302,16 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// Security: PIVOTBY totals read their source rows from indexes built in the grouping pass,
|
||||
/// rather than rescanning the source once per total, so many distinct keys stay linear.
|
||||
#[test]
|
||||
fn pivotby_totals_with_many_groups() {
|
||||
let keys: Vec<f64> = (0..20_000).map(f64::from).collect();
|
||||
let r = rows_of(&ev("PIVOTBY", vec![av(col(&keys)), av(col(&vec![1.0; 20_000])), av(col(&vec![2.0; 20_000])), t("SUM")]));
|
||||
assert_eq!(r.get(1), Some(&vec![nv(0.0), nv(2.0), nv(2.0)]));
|
||||
assert_eq!(r.last(), Some(&vec![tv("Total"), nv(40_000.0), nv(40_000.0)]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pivotby_size_check_counts_column_header_rows() {
|
||||
// One row, n column fields and n value columns: n header rows + value-name row + body row + total row,
|
||||
|
||||
Reference in New Issue
Block a user