From 433959787dbf84863f080458f183cb184c5e4e23 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Sat, 28 Nov 2026 19:37:15 -0300 Subject: [PATCH] fix(symbology): the "not the whole layer" notice asks a GeoPackage for its rows, not its kept count (#2898) On a layer read in place, layer.symbology compared the features read with SourceLayer::exact_count, which for a GeoPackage is the count GDAL keeps in gpkg_ogr_contents since #2859; an INSERT OR REPLACE leaves it high, so the notice was said with the whole layer in view (and missed when a low count equalled the features read). A source whose count may be wrong now answers FeatureSource::holds_more_than: a GeoPackage with a kept count runs SELECT EXISTS(SELECT 1 FROM t LIMIT 1 OFFSET n), which steps over at most the n features read. Every other source goes by its exact count as before. Closes #2898 Co-Authored-By: Claude Opus 5.5 --- crates/data/src/source/gpkg.rs | 16 +++++++++ crates/data/src/source/mod.rs | 10 ++++++ crates/engine/src/cmd/layer.rs | 2 +- crates/engine/src/tests.rs | 60 ++++++++++++++++++++++++++++++++++ crates/map/src/source.rs | 9 +++++ 5 files changed, 96 insertions(+), 1 deletion(-) diff --git a/crates/data/src/source/gpkg.rs b/crates/data/src/source/gpkg.rs index 3e1c3b0b1d..b8a77305d0 100644 --- a/crates/data/src/source/gpkg.rs +++ b/crates/data/src/source/gpkg.rs @@ -531,6 +531,22 @@ impl FeatureSource for GpkgSource { true } + /// Asked only of a count GDAL keeps (`count_kept`), which may be past the rows or short of + /// them: whether the table has a row past the first `n`. SQLite steps over `n` rows to answer, + /// without counting the rest. A file that can't answer is taken to hold more, so what depends + /// on it says it may not have every feature rather than going by a count that may be wrong. + fn holds_more_than(&self, n: u64) -> Option { + if !self.count_kept { + return None; + } + let sql = format!("SELECT EXISTS(SELECT 1 FROM {} LIMIT 1 OFFSET ?1)", quote(&self.table)); + let asked = |offset: i64| { + let guard = self.conn.lock().ok()?; + guard.as_ref()?.query_row(&sql, [offset], |r| r.get(0)).ok() + }; + Some(i64::try_from(n).ok().and_then(asked).unwrap_or(true)) + } + /// Its own system, in which it gives its features as the file holds them. fn coord_crs(&self) -> crate::raster::Crs { self.reproject.crs() diff --git a/crates/data/src/source/mod.rs b/crates/data/src/source/mod.rs index a0aafa97f9..3e338ca9d3 100644 --- a/crates/data/src/source/mod.rs +++ b/crates/data/src/source/mod.rs @@ -629,6 +629,12 @@ pub trait FeatureSource: Send + Sync { fn field_range(&self, _field: &str) -> Option<(f64, f64)> { None } + /// Whether the source holds more features than the `n` read, asked of it when its count + /// ([`SourceInfo::count`]) may be wrong and it can tell without reading them all (a + /// GeoPackage's kept count, #2898); `None` when its count is taken as it is, or it can't tell. + fn holds_more_than(&self, _n: u64) -> Option { + None + } /// Whether the source's notes ([`SourceInfo::notes`]) already count the features it keeps /// with a place that can't be given in longitude/latitude, so a whole read doesn't count them /// again. @@ -902,6 +908,10 @@ impl FeatureSource for Fetching { self.0.field_range(field) } + fn holds_more_than(&self, n: u64) -> Option { + self.0.holds_more_than(n) + } + fn says_unplaced_at_open(&self) -> bool { self.0.says_unplaced_at_open() } diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index c3ff12fdf6..ac2c38eaaf 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -8324,7 +8324,7 @@ fn symbology(s: &mut Session, p: &Value) -> CmdResult { }; let order = if rest.by_number { "by number" } else { "in text order" }; // A view that holds the whole source says nothing. - let part = l.source.as_ref().is_some_and(|src| src.exact_count() != Some(l.dataset.features.len() as u64)); + let part = l.source.as_ref().is_some_and(|src| !src.holds_only(l.dataset.features.len() as u64)); let from_view = from_view.filter(|_| part).map(|then| (l.dataset.kept_features(&l.visible_rows()).count(), then)); let mut dropped = None; let out = with(s, p, |l| { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 0cf477dd2f..ba15d70d29 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -51299,6 +51299,66 @@ fn hand_edits_and_the_notice_on_a_geoparquet_read_in_place_follow_the_view() { let _ = std::fs::remove_dir_all(dir); } +/// On a GeoPackage read in place, the notice of a view that isn't the whole layer follows the rows +/// the file holds, not the count GDAL keeps in `gpkg_ogr_contents`, which an `INSERT OR REPLACE` +/// leaves high (#2898): 40 points, whose kept count says 40, 50 or 10, with the whole layer in +/// view or a quarter of it. +#[test] +fn the_notice_of_a_part_of_a_geopackage_follows_its_rows_not_its_kept_count() { + use rustgis_data::{Coord, Dataset, Field, FieldType, Geometry, GeometryType, Value as V}; + // Ten points on each of the parallels 0, 20, 40 and 60. + let mut ds = Dataset::new("pts", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]); + for i in 0..40 { + ds.push(Some(Geometry::Point(Coord::new(f64::from(i % 10), f64::from(i / 10 * 20)))), vec![V::Int(i64::from(i))]); + } + let dir = std::env::temp_dir().join(format!("rustgis-kept-count-notice-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let whole = rustgis_geom::Extent::new(-1.0, -5.0, 10.0, 65.0); + let part = rustgis_geom::Extent::new(-1.0, -3.0, 10.0, 3.0); + // The kept count, the view, the features it reads and whether the notice is said. + let mut wrong = Vec::new(); + for (kept, view, read, said) in [ + (40, whole, 40, false), + (40, part, 10, true), + (50, whole, 40, false), + (50, part, 10, true), + (10, whole, 40, false), + (10, part, 10, true), + ] { + let path = dir.join(format!("pts-{kept}-{read}.gpkg")); + rustgis_data::io::write_path(&ds, &path).unwrap(); + // The count and the two triggers GDAL keeps it with. + rusqlite::Connection::open(&path) + .unwrap() + .execute_batch(&format!( + "CREATE TABLE IF NOT EXISTS gpkg_ogr_contents (table_name TEXT NOT NULL PRIMARY KEY, feature_count INTEGER DEFAULT NULL); + INSERT OR REPLACE INTO gpkg_ogr_contents VALUES ('pts', {kept}); + CREATE TRIGGER IF NOT EXISTS \"trigger_insert_feature_count_pts\" AFTER INSERT ON \"pts\" BEGIN UPDATE gpkg_ogr_contents + SET feature_count = feature_count + 1 WHERE lower(table_name) = lower('pts'); END; + CREATE TRIGGER IF NOT EXISTS \"trigger_delete_feature_count_pts\" AFTER DELETE ON \"pts\" BEGIN UPDATE gpkg_ogr_contents + SET feature_count = feature_count - 1 WHERE lower(table_name) = lower('pts'); END;" + )) + .unwrap(); + let mut s = Session::new(Project::default()); + let id = + s.run("layer.addData", &json!({"path": path.display().to_string(), "stream": true})).unwrap()["layer"].as_u64().unwrap() as u32; + let l = s.map().layer(id).unwrap(); + assert!(l.is_streamed() && l.source.as_ref().and_then(|src| src.exact_count()) == Some(kept), "{kept}"); + let v = view_of(&mut s, view); + crate::render::render_png(s.map_mut(), &v).unwrap(); + assert_eq!(s.map().layer(id).unwrap().dataset.features.len(), read, "{kept}"); + let mark = s.notice_mark(); + s.run("layer.symbology", &json!({"layer": id, "type": "graduated", "field": "n", "classes": 3})).unwrap(); + let notices: Vec<_> = + s.notices_since(mark).iter().filter(|n| n.text.contains("of the view read so far")).map(|n| n.text.clone()).collect(); + if notices.len() != usize::from(said) { + wrong.push(format!("kept {kept}, {read} read: {notices:?}")); + } + } + let _ = std::fs::remove_dir_all(dir); + assert!(wrong.is_empty(), "{wrong:#?}"); +} + /// Raster-side projection failures retain good multipart coverage and reach both command results. #[test] fn raster_projection_failures_are_reported_for_zonal_statistics_and_clipping() { diff --git a/crates/map/src/source.rs b/crates/map/src/source.rs index 6f9d2588e7..4ea696bfff 100644 --- a/crates/map/src/source.rs +++ b/crates/map/src/source.rs @@ -1298,6 +1298,15 @@ impl SourceLayer { self.info().filter(|_| !database)?.count } + /// Whether the `n` features read from it are all its source holds: asked of the source when + /// its count may be wrong ([`FeatureSource::holds_more_than`], #2898), else by its exact count. + pub fn holds_only(&self, n: u64) -> bool { + match self.handle.as_ref().and_then(|h| h.holds_more_than(n)) { + Some(more) => !more, + None => self.exact_count() == Some(n), + } + } + /// Whether its source tests its features against a box in its own system /// ([`FeatureSource::reads_own_box`]); no until it is open. pub fn reads_own_box(&self) -> bool {