Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions python/sedonadb/python/sedonadb/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def read_parquet(
table_paths: Union[str, Path, Iterable[str]],
options: Optional[Dict[str, Any]] = None,
geometry_columns: Optional[Union[str, Dict[str, Any]]] = None,
validate: bool = False,
) -> DataFrame:
"""Create a [DataFrame][sedonadb.dataframe.DataFrame] from one or more Parquet files

Expand Down Expand Up @@ -176,9 +177,18 @@ def read_parquet(


Safety:
- Columns specified here are not validated against the provided options
(e.g., WKB encoding checks); inconsistent data may cause undefined
behavior.
- Columns specified here can optionally be validated according to the
`validate` option (e.g., WKB encoding checks). If validation is not
enabled, inconsistent data may cause undefined behavior.
validate:
When set to `True`, geometry column contents are validated against
their metadata. Metadata can come from the source Parquet file or
the user-provided `geometry_columns` option.
Only supported properties are validated; unsupported properties are
ignored. If validation fails, execution stops with an error.

Currently the only property that is validated is the WKB of input geometry
columns.


Examples:
Expand All @@ -200,7 +210,7 @@ def read_parquet(
return DataFrame(
self._impl,
self._impl.read_parquet(
[str(path) for path in table_paths], options, geometry_columns
[str(path) for path in table_paths], options, geometry_columns, validate
),
self.options,
)
Expand Down
2 changes: 2 additions & 0 deletions python/sedonadb/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl InternalContext {
table_paths: Vec<String>,
options: HashMap<String, PyObject>,
geometry_columns: Option<String>,
validate: bool,
) -> Result<InternalDataFrame, PySedonaError> {
// Convert Python options to strings, filtering out None values
let rust_options: HashMap<String, String> = options
Expand Down Expand Up @@ -108,6 +109,7 @@ impl InternalContext {
PySedonaError::SedonaPython(format!("Invalid geometry_columns JSON: {e}"))
})?;
}
geo_options = geo_options.with_validate(validate);

let df = wait_for_future(
py,
Expand Down
77 changes: 77 additions & 0 deletions python/sedonadb/tests/io/test_parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import geopandas
import geopandas.testing
import pyarrow as pa
import pyarrow.parquet as pq
import pytest
import sedonadb
import shapely
Expand Down Expand Up @@ -412,3 +413,79 @@ def test_write_geoparquet_geography(con, geoarrow_data):

table_roundtrip = con.read_parquet(tmp_parquet).to_arrow_table()
assert table_roundtrip == table


def test_read_parquet_validate_wkb_single_valid_row(con, tmp_path):
valid_wkb = bytes.fromhex("0101000000000000000000F03F0000000000000040")

table = pa.table({"id": [1], "geom": [valid_wkb]})
path = tmp_path / "single_valid_wkb.parquet"
pq.write_table(table, path)

geometry_columns = json.dumps({"geom": {"encoding": "WKB"}})

tab = con.read_parquet(
path, geometry_columns=geometry_columns, validate=False
).to_arrow_table()
assert tab["geom"].type.extension_name == "geoarrow.wkb"
assert len(tab) == 1

tab = con.read_parquet(
path, geometry_columns=geometry_columns, validate=True
).to_arrow_table()
assert tab["geom"].type.extension_name == "geoarrow.wkb"
assert len(tab) == 1


def test_read_parquet_validate_wkb_single_invalid_row(con, tmp_path):
invalid_wkb = b"\x01"

table = pa.table({"id": [1], "geom": [invalid_wkb]})
path = tmp_path / "single_invalid_wkb.parquet"
pq.write_table(table, path)

geometry_columns = json.dumps({"geom": {"encoding": "WKB"}})

tab = con.read_parquet(
path, geometry_columns=geometry_columns, validate=False
).to_arrow_table()
assert tab["geom"].type.extension_name == "geoarrow.wkb"
assert len(tab) == 1

with pytest.raises(
sedonadb._lib.SedonaError,
match=r"WKB validation failed",
):
con.read_parquet(
path, geometry_columns=geometry_columns, validate=True
).to_arrow_table()


def test_read_parquet_validate_wkb_partial_invalid_rows(con, tmp_path):
valid_wkb = bytes.fromhex("0101000000000000000000F03F0000000000000040")
invalid_wkb = b"\x01"

table = pa.table(
{
"id": [1, 2, 3],
"geom": [valid_wkb, invalid_wkb, valid_wkb],
}
)
path = tmp_path / "partial_invalid_wkb.parquet"
pq.write_table(table, path)

geometry_columns = json.dumps({"geom": {"encoding": "WKB"}})

tab = con.read_parquet(
path, geometry_columns=geometry_columns, validate=False
).to_arrow_table()
assert tab["geom"].type.extension_name == "geoarrow.wkb"
assert len(tab) == 3

with pytest.raises(
sedonadb._lib.SedonaError,
match=r"WKB validation failed",
):
con.read_parquet(
path, geometry_columns=geometry_columns, validate=True
).to_arrow_table()
1 change: 1 addition & 0 deletions rust/sedona-geoparquet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ sedona-schema = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
wkb = { workspace = true }
Loading