diff --git a/crates/data/src/export.rs b/crates/data/src/export.rs index 83a7912ddc..8033930711 100644 --- a/crates/data/src/export.rs +++ b/crates/data/src/export.rs @@ -7,8 +7,8 @@ use std::{borrow::Cow, path::Path}; use crate::io::{CsvWriter, GeoJsonWriter, GpkgTable, GpkgWritten, PROJECTED_FORMATS, check_projected, project_each, write_atomic_with}; -use crate::query::{self, Expr}; use crate::raster::Crs; +use crate::rows::Query; use crate::source::FeatureSource; use crate::{DataError, Dataset, Feature, Field, GeometryType, Result, Value}; @@ -81,10 +81,9 @@ pub struct QueryReport { /// Reads a stored query against the export schema. Blank queries are absent; unreadable queries /// leave all rows visible, as when drawing the layer, and carry the reason for the export notice. -fn export_query(text: Option<&str>, ds: &Dataset) -> (Option, QueryReport) { - let Some(text) = text.filter(|text| !text.trim().is_empty()) else { return (None, QueryReport::default()) }; - match query::parse_fields(text, ds) { - Ok(expr) => (Some(expr), QueryReport::default()), +fn export_query(text: Option<&str>, ds: &Dataset) -> (Option, QueryReport) { + match Query::compile(text, ds) { + Ok(query) => (query.is_filtered().then_some(query), QueryReport::default()), Err(error) => (None, QueryReport { filtered: 0, parse_error: Some(error) }), } } @@ -138,7 +137,7 @@ fn write_chunks_to( crs: Crs, gpkg: &GpkgTable, bytes: usize, - query: Option<&Expr>, + query: Option<&Query>, ) -> Result<(Exported, Option)> { let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("").to_ascii_lowercase(); write_chunks_as(src, layer, path, crs, gpkg, &ext, (bytes, query)) @@ -168,7 +167,7 @@ pub fn write_source_as_filtered( /// Filters export rows in source order, preserving their positions for `@row_number`. A whole /// read or an in-memory layer counts its own progress; a chunk's source already counts the read. -fn retain_matches(ds: &mut Cow<'_, Dataset>, expr: &Expr, offset: usize, count_progress: bool) -> Result { +fn retain_matches(ds: &mut Cow<'_, Dataset>, query: &Query, offset: usize, count_progress: bool) -> Result { let len = ds.features.len(); if count_progress { crate::progress::begin(len); @@ -178,7 +177,7 @@ fn retain_matches(ds: &mut Cow<'_, Dataset>, expr: &Expr, offset: usize, count_p let mut filtered = 0; for row in 0..len { crate::progress::step(usize::from(count_progress))?; - let matches = query::eval_in_chunk(expr, ds, row, offset); + let matches = query.matches_in_chunk(ds, row, offset); keep.push(matches); filtered += usize::from(!matches); } @@ -220,7 +219,7 @@ fn write_chunks_as( crs: Crs, gpkg: &GpkgTable, ext: &str, - (bytes, query): (usize, Option<&Expr>), + (bytes, query): (usize, Option<&Query>), ) -> Result<(Exported, Option)> { if !streams(ext) { return Err(DataError::Unsupported(format!(".{ext} written a chunk at a time"))); diff --git a/crates/data/src/export/tests.rs b/crates/data/src/export/tests.rs index 690ca8e8d9..20ffb15248 100644 --- a/crates/data/src/export/tests.rs +++ b/crates/data/src/export/tests.rs @@ -250,7 +250,7 @@ fn streamed_export_counts_query_omissions_across_chunks() { let ds = squares(); let src = Runs::new(&ds, 3); let layer = Dataset::new(ds.name.clone(), ds.geometry_type, ds.fields.clone()); - let query = query::parse_fields("@row_number BETWEEN 5 AND 7", &layer).unwrap(); + let query = Query::compile(Some("@row_number BETWEEN 5 AND 7"), &layer).unwrap(); let path = d.join("export.geojson"); let (exported, _) = write_chunks_to(&src, &layer, &path, Crs::LonLat, &GpkgTable::default(), CHUNK_BYTES, Some(&query)).unwrap(); assert_eq!((exported.written, exported.query.filtered, exported.left_out), (3, ds.features.len() - 3, 0)); diff --git a/crates/data/src/rows.rs b/crates/data/src/rows.rs index 672ad18504..1253daf9b4 100644 --- a/crates/data/src/rows.rs +++ b/crates/data/src/rows.rs @@ -23,8 +23,14 @@ impl Query { } /// Match an existing row using the expression language's normal null and variable rules. - /// A chunked consumer must supply source-wide row numbering through its traversal adapter. + /// For source-wide row numbering in chunks, use [`Self::matches_in_chunk`]. pub fn matches(&self, dataset: &Dataset, row: usize, env: &query::Env) -> bool { self.expr.as_ref().is_none_or(|expr| query::matches(expr, dataset, row, env)) } + + /// Match a chunk row with the default expression environment. `offset` counts source rows + /// preceding this chunk, including rows earlier chunks excluded; callers own that count. + pub fn matches_in_chunk(&self, dataset: &Dataset, row: usize, offset: usize) -> bool { + self.expr.as_ref().is_none_or(|expr| query::eval_in_chunk(expr, dataset, row, offset)) + } }