Repository navigation
feat(data): open a Parquet file without a geometry column as a standalone table (#3539, part 1) #3550
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
Merged
elpidioassinaturas
merged 4 commits into
opengeos:main
from
elpidioassinaturas:feat/parquet-table-3539
Dec 2, 2026
+400
−4
Merged
feat(data): open a Parquet file without a geometry column as a standalone table (#3539, part 1) #3550
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7e0118e
feat(data): open a Parquet file without a geometry column as a standa…
elpidiofilho 5284ef9
fix(data): keep a Parquet table's file, read it ahead, and bound its …
elpidiofilho 9b517c1
fix(data): stop a Parquet table's read when it is cancelled
elpidiofilho 23d5e69
fix(engine): ask nothing about linking a Parquet table
elpidiofilho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| //! A Parquet file with no geometry column, read as a standalone table (#3539): its top-level | ||
| //! columns with the types the GeoParquet reader gives attribute columns, and every row, within the | ||
| //! limits of a GeoParquet's row groups. | ||
|
|
||
| use super::{Budget, ColumnValues, FileReader, column_kind, err, field_type, max_decoded_bytes, read_values, whole_group}; | ||
| use crate::source::{Batch, FeatureSource, MAX_READ_BYTES, Request, SourceInfo, clamp_limit}; | ||
| use crate::{DataError, Feature, Field, GeometryType, Result}; | ||
| use rustgis_geom::Extent; | ||
| use std::collections::HashSet; | ||
| use std::sync::Arc; | ||
|
|
||
| /// The format a [`ParquetTable`]'s [`SourceInfo`] gives, which tells it from a GeoParquet. | ||
| pub const TABLE_FORMAT: &str = "Parquet"; | ||
|
|
||
| /// A Parquet file with no geometry column, whose rows have no place. | ||
| pub struct ParquetTable { | ||
| /// The local file, observed again at each read. | ||
| path: Option<std::path::PathBuf>, | ||
| info: SourceInfo, | ||
| reader: Arc<dyn FileReader>, | ||
| /// Leaf column index of each field, in `info.fields` order. | ||
| field_cols: Vec<usize>, | ||
| /// Most bytes the rows read may hold, across all row groups: [`MAX_READ_BYTES`], lowered by tests. | ||
| read_bytes: u64, | ||
| } | ||
|
|
||
| impl ParquetTable { | ||
| pub(super) fn new(reader: Arc<dyn FileReader>, name: &str, path: Option<&std::path::Path>) -> Self { | ||
| let md = reader.metadata(); | ||
| let schema = md.file_metadata().schema_descr(); | ||
| let (mut fields, mut field_cols) = (Vec::new(), Vec::new()); | ||
| for i in 0..schema.num_columns() { | ||
| let c = schema.column(i); | ||
| if c.path().parts().len() != 1 || c.max_rep_level() != 0 { | ||
| continue; | ||
| } | ||
| if let Some(ty) = field_type(&c) { | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| fields.push(Field::new(c.name().to_string(), ty)); | ||
| field_cols.push(i); | ||
| } | ||
| } | ||
| let read: HashSet<&str> = fields.iter().map(|f| f.name.as_str()).collect(); | ||
| let left_out: Vec<(String, String)> = schema | ||
| .root_schema() | ||
| .get_fields() | ||
| .iter() | ||
| .filter(|t| !read.contains(t.name())) | ||
| .map(|t| (t.name().to_string(), column_kind(t).to_string())) | ||
| .collect(); | ||
| let mut info = SourceInfo { | ||
| name: name.to_string(), | ||
| format: TABLE_FORMAT, | ||
| geometry_type: GeometryType::None, | ||
| fields, | ||
| extent: Extent::EMPTY, | ||
| count: u64::try_from(md.file_metadata().num_rows()).ok(), | ||
| crs: String::new(), | ||
| indexed: false, | ||
| notes: Default::default(), | ||
| zm_dropped: false, | ||
| }; | ||
| info.note_columns( | ||
| &left_out, | ||
| ["of a type RustGIS has no field for was left out", "of types RustGIS has no field for were left out"], | ||
| ); | ||
| ParquetTable { path: path.map(std::path::Path::to_path_buf), info, reader, field_cols, read_bytes: MAX_READ_BYTES } | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| impl FeatureSource for ParquetTable { | ||
| fn info(&self) -> &SourceInfo { | ||
| &self.info | ||
| } | ||
|
|
||
| /// The first rows up to the request's limit, in the file's order; a read by place meets none. | ||
| /// A table whose rows would hold more than [`MAX_READ_BYTES`] is refused as it passes them. | ||
| fn read(&self, req: &Request) -> Result<Batch> { | ||
| if let Some(path) = &self.path { | ||
| crate::provenance::path(path); | ||
| } | ||
| let mut batch = Batch { complete: true, ..Batch::default() }; | ||
| if req.extent.is_some() || req.own.is_some() { | ||
| return Ok(batch); | ||
| } | ||
| let limit = clamp_limit(req.limit); | ||
| let md = self.reader.metadata(); | ||
| let (mut base, mut held) = (0u64, 0u64); | ||
| for g in 0..md.num_row_groups() { | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| crate::progress::check()?; | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| let rows = whole_group(g, md.row_group(g).num_rows(), super::MAX_GROUP_ROWS)?; | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| batch.matched = batch.matched.saturating_add(rows as u64); | ||
| let wanted = rows.min(limit.saturating_sub(batch.features.len())); | ||
| if wanted > 0 { | ||
| let take: Vec<bool> = (0..rows).map(|r| r < wanted).collect(); | ||
| let rg = self.reader.get_row_group(g).map_err(err)?; | ||
| let budget = Budget::new(max_decoded_bytes(), g); | ||
| let attrs: Vec<ColumnValues> = | ||
| self.field_cols.iter().map(|&c| read_values(rg.as_ref(), c, &take, &budget)).collect::<Result<_>>()?; | ||
| if attrs.iter().any(|a| a.values.len() != wanted) { | ||
| return Err(DataError::Format(format!("Parquet: row group {g} has columns shorter than its rows"))); | ||
| } | ||
| for k in 0..wanted { | ||
| let attrs = attrs | ||
| .iter() | ||
| .enumerate() | ||
| .map(|(field, col)| col.value(k, field, &mut batch.unread_dates, &mut batch.rounded_dates)) | ||
| .collect(); | ||
| let feature = Feature { id: base.saturating_add(k as u64 + 1), geometry: None, attrs }; | ||
| held = held.saturating_add(crate::source::memory::feature_bytes(&feature) as u64); | ||
| if held > self.read_bytes { | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| return Err(DataError::Format(format!( | ||
| "{}: the table holds more than the {} MiB read at once: add a file with fewer rows or columns", | ||
| self.info.name, | ||
| self.read_bytes >> 20 | ||
| ))); | ||
| } | ||
| batch.features.push(feature); | ||
| } | ||
| } | ||
| base = base.saturating_add(rows as u64); | ||
| } | ||
| batch.complete = batch.features.len() as u64 == batch.matched; | ||
| Ok(batch) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::{Dataset, FieldType, Value}; | ||
| use parquet::file::reader::SerializedFileReader; | ||
|
|
||
| /// A table whose rows hold more than the budget is refused, saying what to do; one within it is | ||
| /// read whole. | ||
| #[test] | ||
| fn a_table_past_the_read_budget_is_refused() { | ||
| let mut ds = Dataset::new("t", GeometryType::None, vec![Field::new("note", FieldType::Text)]); | ||
| for i in 0..100u64 { | ||
| ds.features.push(Feature { id: i + 1, geometry: None, attrs: vec![Value::Text("x".repeat(1000))] }); | ||
| } | ||
| let bytes = bytes::Bytes::from(super::super::write(&ds).unwrap()); | ||
| let open = |read_bytes| { | ||
| let reader = SerializedFileReader::new(bytes.clone()).unwrap(); | ||
| ParquetTable { read_bytes, ..ParquetTable::new(Arc::new(reader), "t", None) } | ||
| }; | ||
| let held: u64 = ds.features.iter().map(|f| crate::source::memory::feature_bytes(f) as u64).sum(); | ||
| let read = open(held).read(&Request::all()).unwrap(); | ||
| assert_eq!((read.features.len(), read.complete), (100, true)); | ||
| let error = open(held - 1).read(&Request::all()).unwrap_err().to_string(); | ||
| assert!(error.contains("t: the table holds more than") && error.contains("fewer rows or columns"), "{error}"); | ||
| } | ||
|
|
||
| /// A read whose run was cancelled stops instead of reading the rows. | ||
| #[test] | ||
| fn a_cancelled_read_stops() { | ||
| let mut ds = Dataset::new("t", GeometryType::None, vec![Field::new("n", FieldType::Integer)]); | ||
| ds.features.push(Feature { id: 1, geometry: None, attrs: vec![Value::Int(7)] }); | ||
| let reader = SerializedFileReader::new(bytes::Bytes::from(super::super::write(&ds).unwrap())).unwrap(); | ||
| let table = ParquetTable::new(Arc::new(reader), "t", None); | ||
| let probe = Arc::new(crate::progress::tests::Counted::default()); | ||
| probe.cancel.store(true, std::sync::atomic::Ordering::Relaxed); | ||
| let _g = crate::progress::install(Some(probe)); | ||
| assert!(matches!(table.read(&Request::all()), Err(DataError::Cancelled))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.