Repository navigation
fix(map): retain source positions in row-number queries #3465
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2009,7 +2009,27 @@ impl Layer { | |
| /// Applies the definition query in the supplied drawing context. | ||
| pub fn query_rows_with(&self, dataset: &Dataset, env: &rustgis_data::query::Env) -> Option<Vec<bool>> { | ||
| let query = rustgis_data::rows::Query::compile(self.definition_query.as_deref(), dataset).ok()?; | ||
| query.is_filtered().then(|| (0..dataset.features.len()).map(|row| query.matches(dataset, row, env)).collect()) | ||
| if !query.is_filtered() { | ||
| return None; | ||
| } | ||
| // A different dataset passed in by a whole-layer consumer has its own dense order. | ||
| if dataset.windowed | ||
| && let Some(source) = self.source.as_ref().filter(|_| std::ptr::eq(dataset, &self.dataset)) | ||
| && self.definition_query.as_deref().is_some_and(rustgis_data::query::reads_row_number) | ||
| { | ||
| // Hide unnumbered rows while the new query is pending or refused. | ||
| let Some(order) = source.window().and(source.row_order) else { | ||
| return Some(vec![false; dataset.features.len()]); | ||
| }; | ||
| if let rustgis_data::source::WindowRowOrder::IdsStartingAt(origin) = order { | ||
| let matches = dataset.features.rows().enumerate().map(|(row, feature)| { | ||
| let index = feature.id().checked_sub(origin).and_then(|id| usize::try_from(id).ok()); | ||
| index.and_then(|index| index.checked_sub(row)).is_some_and(|offset| query.matches_at(dataset, row, env, offset)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Low confidence (about 25%), defensive. If the ordering proof is ever wrong, or ids are not increasing in window order, Category: bug
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current providers establish the bounds before returning this variant. FlatGeobuf, Shapefile and Parquet require the exact prefix I found no input through these providers that reaches the failed subtraction/conversion. This is a proposed defensive check for a future broken provider contract, rather than a demonstrated silent-loss path in the current change; no code change is made for it. |
||
| }); | ||
| return Some(matches.collect()); | ||
| } | ||
| } | ||
| Some((0..dataset.features.len()).map(|row| query.matches(dataset, row, env)).collect()) | ||
| } | ||
|
|
||
| /// The rows whose shape's box, in the layer's own coordinates ([`Dataset::coord_crs`]), may | ||
|
|
@@ -5256,19 +5276,18 @@ mod tests { | |
| assert_eq!((shown(&l).len(), l.dataset.features.len()), (2, 2)); | ||
| let src = l.source.as_ref().unwrap(); | ||
| assert_eq!((src.sampled(), src.sampled_matches()), (Some((2, 4)), true)); | ||
| // A row number is a row's place in the view: the view is read as without a query. The | ||
| // words in a text are only text. | ||
| // The sample cannot prove the position of rows whose predecessors it omitted. | ||
| l.definition_query = Some("@row_number = 1".into()); | ||
| l.sync_source(&view, true); | ||
| let src = l.source.as_ref().unwrap(); | ||
| assert_eq!((src.sampled(), src.sampled_matches()), (Some((2, 5)), false)); | ||
| assert!(l.source.as_ref().unwrap().read_error().unwrap().contains("Use Load All Features")); | ||
| // The words in a string literal are only text. | ||
| l.definition_query = Some(format!("n = {left_out} or 'x' = '@row_number'")); | ||
| l.sync_source(&view, true); | ||
| assert_eq!(shown(&l), [Value::Int(left_out)]); | ||
| // However it's quoted or nested, a row number is found in the parsed query. | ||
| l.definition_query = Some(format!("!n! = {left_out} and (@row_number >= 1 or 'it''s' = '')")); | ||
| l.sync_source(&view, true); | ||
| assert!(!l.source.as_ref().unwrap().sampled_matches()); | ||
| assert!(l.source.as_ref().unwrap().read_error().unwrap().contains("Use Load All Features")); | ||
| // One view on two series pages: a sampled-out match must arrive on each page, | ||
| // and the map view afterwards must evaluate the same query with null page variables. | ||
| l.definition_query = Some("n = @page_number - 1".into()); | ||
|
|
@@ -5290,6 +5309,94 @@ mod tests { | |
| assert_eq!(shown(&l), sampled); | ||
| } | ||
|
|
||
| /// A spatial window must not call its first row the first row of the whole layer. | ||
| #[test] | ||
| fn a_spatial_window_does_not_renumber_a_definition_query() { | ||
| let mut ds = Dataset::new("sites", GeometryType::Point, vec![]); | ||
| for x in [-20.0, 20.0] { | ||
| ds.push(Some(Geometry::Point(Coord::new(x, 0.0))), vec![]); | ||
| } | ||
| let bytes = std::sync::Arc::new(rustgis_data::source::fgb::write(&ds).unwrap()); | ||
| let source = rustgis_data::source::open_shared("sites.fgb", bytes.clone(), None).unwrap(); | ||
| let reader = source::SourceLayer::from_bytes("sites.fgb", bytes, None, source); | ||
| let mut layer = Layer::new_streamed(1, reader); | ||
| let whole = layer.load_all().unwrap(); | ||
| let first = whole.features.first().unwrap(); | ||
| let other = whole.features.last().unwrap(); | ||
| let view = other.geometry.as_ref().unwrap().extent().padded(0.0, 0.1); | ||
| layer.sync_source(&view, true); | ||
| assert_eq!(layer.dataset.features.len(), 1); | ||
| assert_eq!(layer.dataset.features[0].id, other.id); | ||
| layer.selection.push(0); | ||
| layer.definition_query = Some("@row_number = 1".into()); | ||
| assert_eq!(layer.visible_rows(), [false], "old window rows must not be renumbered before the read"); | ||
| layer.sync_source(&view, true); | ||
| assert_eq!(layer.query_rows(&whole).unwrap(), [true, false]); | ||
| assert_eq!(layer.dataset.features[0].id, other.id); | ||
| assert_eq!(layer.selection, [0]); | ||
| assert!(layer.visible_rows().iter().all(|visible| !visible)); | ||
| let why = layer.source.as_ref().unwrap().read_error().unwrap(); | ||
| assert!(why.contains("@row_number") && why.contains("Use Load All Features"), "{why}"); | ||
|
|
||
| let prefix = first.geometry.as_ref().unwrap().extent().padded(0.0, 0.1); | ||
| layer.sync_source(&prefix, true); | ||
| assert_eq!(layer.visible_rows(), [true]); | ||
| assert_eq!(layer.dataset.features[0].id, first.id); | ||
| assert!(layer.source.as_ref().unwrap().read_error().is_none()); | ||
|
|
||
| // Even a correctly numbered prefix can omit the feature the query asks for. | ||
| layer.definition_query = Some("@row_number = 2".into()); | ||
| let source = layer.source.as_mut().unwrap(); | ||
| source.limit = 1; | ||
| source.reset(); | ||
| layer.sync_source(&Extent::WORLD_LONLAT, true); | ||
| let source = layer.source.as_ref().unwrap(); | ||
| assert_eq!(source.sampled(), Some((1, 2))); | ||
| assert_eq!(layer.dataset.features[0].id, first.id); | ||
| assert_eq!(layer.query_rows(&whole).unwrap(), [false, true]); | ||
| assert_eq!(layer.visible_rows(), [false]); | ||
| assert!(source.read_error().is_some_and(|why| why.contains("@row_number") && why.contains("Use Load All Features"))); | ||
|
|
||
| let source = layer.source.as_mut().unwrap(); | ||
| source.limit = 2; | ||
| source.reset(); | ||
| layer.sync_source(&Extent::WORLD_LONLAT, true); | ||
| assert_eq!(layer.visible_rows(), [false, true]); | ||
| assert!(layer.source.as_ref().unwrap().read_error().is_none()); | ||
| } | ||
|
|
||
| /// The GeoJSON index knows that the geometry-less row belongs before a spatial match. | ||
| #[cfg(not(target_arch = "wasm32"))] | ||
| #[test] | ||
| fn a_geojson_window_uses_the_whole_layer_position() { | ||
| // Remove the fixture on a failing assertion too, without adding a test dependency. | ||
| struct Remove(std::path::PathBuf); | ||
| impl Drop for Remove { | ||
| fn drop(&mut self) { | ||
| let _ = std::fs::remove_file(&self.0); | ||
| } | ||
| } | ||
| let mut ds = Dataset::new("sites", GeometryType::Point, vec![]); | ||
| ds.push(None, vec![]); | ||
| ds.push(Some(Geometry::Point(Coord::new(20.0, 0.0))), vec![]); | ||
| let path = std::env::temp_dir().join(format!("rustgis-map-row-number-{}.geojson", std::process::id())); | ||
| let _remove = Remove(path.clone()); | ||
| std::fs::write(&path, rustgis_data::io::to_geojson(&ds).to_string()).unwrap(); | ||
| let source = rustgis_data::source::geojson::GeoJsonSource::open_path(&path, "sites").unwrap(); | ||
| let reader = source::SourceLayer::from_path(path.to_string_lossy(), None, Box::new(source)); | ||
| let mut layer = Layer::new_streamed(1, reader); | ||
| let view = Extent::new(19.0, -1.0, 21.0, 1.0); | ||
| layer.sync_source(&view, true); | ||
| assert_eq!(layer.dataset.features.len(), 1); | ||
|
|
||
| layer.definition_query = Some("@ROW_NUMBER = 2".into()); | ||
| layer.sync_source(&view, true); | ||
| assert_eq!(layer.visible_rows(), [true]); | ||
| assert_eq!(layer.query_rows(&layer.load_all().unwrap()).unwrap(), [false, true]); | ||
| assert!(layer.source.as_ref().unwrap().read_error().is_none()); | ||
| std::fs::remove_file(path).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn geometry_only_lod_does_not_charge_for_unused_extents() { | ||
| let l = long_line(100); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Twin check: this default makes every source without an override refuse
@row_numberon a window. The overrides cover fgb, geojson, gpkg_mem, lidar, parquet and shp.GpkgSource(crates/data/src/source/gpkg.rs, desktop GeoPackage, read in place viareads_own_box) is not overridden, so on desktop every window of a GeoPackage now refuses@row_numberuntil Load All Features. The PR body doesn't mention this, and it is a common format. Refusing is the right default (no wrong numbers), but the PR should say GeoPackage refuses, and say whether a proof is possible for it (for example rowid order on an unchanged file, as geojson does withFileVersion). Postgis, MySQL, SQL Server and ArcGIS refusing is reasonable and could be stated the same way. I checked the otherFeatureSourceimpls listed by grep;Fetchingdelegates correctly.Category: quality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the desktop GeoPackage and database/service limitations to the PR description. These sources intentionally keep the conservative default and require Load All Features. GeoPackage's
order_by()provides stable key/rowid ordering, but those values can have gaps, differ from the feature key, and include rows whose geometry cannot be read. File freshness alone does not establish dense feature positions. This change therefore does not infer positions from those keys.