Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: export DuckDB geometry as GeoParquet and GeoJSON (#11169)

Kiran Gadhavefeaf0b2464be
diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
index 6d4d4de28c..6fd7218335 100644
--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py
+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
@@ -40,6 +40,7 @@ from marimo._plugins.ui._impl.tables.geometry_export import (
ExportMetadata,
GeometryExportError,
get_export_metadata,
+ has_geometry_columns,
)
from marimo._plugins.ui._impl.tables.table_manager import (
FieldTypes,
@@ -394,7 +395,15 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
ValueError: If format is not supported.
"""
df = self._value
- manager = self._get_cached_table_manager(df, self._limit)
+ if args.format in ("parquet", "geojson"):
+ full_manager = get_table_manager(df)
+ manager = (
+ full_manager
+ if has_geometry_columns(full_manager)
+ else self._get_cached_table_manager(df, self._limit)
+ )
+ else:
+ manager = self._get_cached_table_manager(df, self._limit)
bound_filename = get_bound_name(self._id)
diff --git a/marimo/_plugins/ui/_impl/tables/geometry_export.py b/marimo/_plugins/ui/_impl/tables/geometry_export.py
index aa8c43b0a8..07fdf4cae1 100644
--- a/marimo/_plugins/ui/_impl/tables/geometry_export.py
+++ b/marimo/_plugins/ui/_impl/tables/geometry_export.py
@@ -133,6 +133,16 @@ class ExportMetadata:
formats: dict[str, ExportFormatEligibility] = field(default_factory=dict)
+@dataclass(frozen=True)
+class _ExportSource:
+ metadata: ExportMetadata
+ schema: pa.Schema | None = None
+ declarations: dict[str, ArrowGeometryDeclaration] = field(
+ default_factory=dict
+ )
+ rejection: GeometryExportError | None = None
+
+
def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
"""Read geometry declarations without materializing source rows.
@@ -142,17 +152,46 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
Args:
manager (TableManager[Any]): Manager for the export source.
"""
+ return _describe_export_source(manager).metadata
+
+
+def _missing_package_message(
+ manager: NarwhalsTableManager[Any, Any], package: str, label: str = ""
+) -> str:
+ if package == "pyarrow":
+ return (
+ "DuckDB geographic export requires pyarrow."
+ if manager.data.implementation.is_duckdb()
+ else "GeoParquet export requires pyarrow."
+ )
+ implementation = manager.data.implementation
+ source_label = (
+ "pandas"
+ if implementation.is_pandas()
+ else "DuckDB"
+ if implementation.is_duckdb()
+ else "Arrow"
+ )
+ return f"This {source_label} table needs {package} to export {label}."
+
+
+def _describe_export_source(manager: TableManager[Any]) -> _ExportSource:
+ """Keep parsed source declarations separate from the metadata response."""
if not isinstance(manager, NarwhalsTableManager):
- return ExportMetadata()
+ return _ExportSource(ExportMetadata())
info_by_name = _export_geometry_columns(manager)
if not info_by_name:
- return ExportMetadata()
+ return _ExportSource(ExportMetadata())
source = manager.data
primary: str | None = None
columns: list[GeometryExportColumn] = []
source_reason: str | None = None
+ source_missing_packages: list[str] = []
+ schema: pa.Schema | None = None
+ declarations: dict[str, ArrowGeometryDeclaration] = {}
+ first_rejection: GeometryExportError | None = None
if source.implementation.is_pandas():
native = source.to_native()
candidate = getattr(native, "_geometry_column_name", None)
@@ -168,12 +207,27 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
)
)
is_supported_source = DependencyManager.geopandas.has()
- elif source.implementation.is_pyarrow():
- native = source.to_native()
+ elif (
+ source.implementation.is_pyarrow() or source.implementation.is_duckdb()
+ ):
+ if source.implementation.is_duckdb():
+ if DependencyManager.pyarrow.has():
+ schema = _duckdb_geometry_schema(manager)
+ else:
+ source_reason = _missing_package_message(manager, "pyarrow")
+ source_missing_packages = ["pyarrow"]
+ else:
+ schema = source.to_native().schema
for name, info in info_by_name.items():
- declaration = _arrow_geometry_declaration(
- native.schema.field(name)
- )
+ if schema is None:
+ columns.append(
+ GeometryExportColumn(
+ name=name, encoding=info.encoding, crs=None
+ )
+ )
+ continue
+ declaration = _arrow_geometry_declaration(schema.field(name))
+ declarations[name] = declaration
columns.append(
GeometryExportColumn(
name=name,
@@ -188,8 +242,11 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
rejection = _arrow_geometry_rejection(
name, info.encoding, declaration
)
+ if source.implementation.is_duckdb() and info.encoding == "other":
+ rejection = _duckdb_geometry_rejection(name, info)
if source_reason is None and rejection is not None:
source_reason = rejection[1]
+ first_rejection = GeometryExportError(*rejection, column=name)
is_supported_source = DependencyManager.geopandas.has()
else:
columns = [
@@ -207,11 +264,9 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
if (
source.implementation.is_pandas()
or source.implementation.is_pyarrow()
+ or source.implementation.is_duckdb()
):
- source_label = (
- "pandas" if source.implementation.is_pandas() else "Arrow"
- )
- reason = f"This {source_label} table needs geopandas to export {label}."
+ reason = _missing_package_message(manager, "geopandas", label)
missing_packages = ["geopandas"]
else:
reason = (
@@ -242,7 +297,9 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
elif not DependencyManager.pyarrow.has():
formats["parquet"] = ExportFormatEligibility(
available=False,
- reason="GeoParquet export requires pyarrow.",
+ reason=_missing_package_message(
+ manager, "pyarrow", "GeoParquet"
+ ),
missing_packages=["pyarrow"],
)
else:
@@ -251,7 +308,9 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
if source_reason is not None:
formats = {
name: ExportFormatEligibility(
- available=False, reason=source_reason
+ available=False,
+ reason=source_reason,
+ missing_packages=source_missing_packages,
)
for name in ("parquet", "geojson")
}
@@ -261,14 +320,63 @@ def get_export_metadata(manager: TableManager[Any]) -> ExportMetadata:
if primary is not None
else (columns[0].name if len(columns) == 1 else None)
)
- return ExportMetadata(
- geometry_columns=columns,
- primary_geometry_column=primary,
- default_geometry_column=default,
- formats=formats,
+ return _ExportSource(
+ ExportMetadata(
+ geometry_columns=columns,
+ primary_geometry_column=primary,
+ default_geometry_column=default,
+ formats=formats,
+ ),
+ schema,
+ declarations,
+ first_rejection,
)
+def _duckdb_geometry_rejection(
+ name: str, info: GeometryColumnInfo
+) -> tuple[GeometryExportErrorCode, str]:
+ return (
+ "unsupported_representation",
+ (
+ f"Geometry column {name!r} uses {info.external_type}. "
+ "DuckDB geographic exports require GEOMETRY columns."
+ ),
+ )
+
+
+def _duckdb_geometry_schema(
+ manager: NarwhalsTableManager[Any, Any],
+) -> pa.Schema:
+ """Read geometry declarations from a zero-row relation's Arrow schema."""
+ import pyarrow as pa
+
+ relation = manager.data.to_native()
+ reader = relation.limit(0).arrow()
+ try:
+ schema = reader.schema
+ finally:
+ reader.close()
+ fields = []
+ for arrow_field in schema:
+ arrow_field = _arrow_storage_field(arrow_field)
+ info = manager._geometry_columns.get(arrow_field.name)
+ if info is not None and info.encoding == "wkb":
+ metadata = dict(arrow_field.metadata or {})
+ if b"ARROW:extension:name" not in metadata:
+ if info.external_type != "GEOMETRY":
+ raise GeometryExportError(
+ "invalid_metadata",
+ f"DuckDB did not preserve the CRS declaration for {arrow_field.name!r}.",
+ column=arrow_field.name,
+ )
+ metadata[b"ARROW:extension:name"] = b"geoarrow.wkb"
+ metadata[b"ARROW:extension:metadata"] = b"{}"
+ arrow_field = arrow_field.with_metadata(metadata)
+ fields.append(arrow_field)
+ return pa.schema(fields, metadata=schema.metadata)
+
+
def _arrow_geometry_declaration(
field: pa.Field[Any],
) -> ArrowGeometryDeclaration:
@@ -420,7 +528,9 @@ def _export_geometry_columns(
def _arrow_to_geodataframe(
- table: pa.Table, columns: list[GeometryExportColumn]
+ table: pa.Table,
+ columns: list[GeometryExportColumn],
+ declarations: dict[str, ArrowGeometryDeclaration] | None = None,
) -> Any:
import geopandas as gpd # type: ignore[import-not-found,import-untyped,unused-ignore]
import pandas as pd
@@ -432,8 +542,10 @@ def _arrow_to_geodataframe(
"Geographic exports require unique column names.",
)
for column in columns:
- declaration = _arrow_geometry_declaration(
- table.schema.field(column.name)
+ declaration = (
+ declarations[column.name]
+ if declarations is not None
+ else _arrow_geometry_declaration(table.schema.field(column.name))
)
rejection = _arrow_geometry_rejection(
column.name, column.encoding, declaration
@@ -477,58 +589,52 @@ def _arrow_to_geodataframe(
def _export_geodataframe(
- manager: NarwhalsTableManager[Any, Any], metadata: ExportMetadata
+ manager: NarwhalsTableManager[Any, Any],
+ metadata: ExportMetadata,
+ declarations: dict[str, ArrowGeometryDeclaration] | None = None,
) -> Any:
native = manager.as_frame().to_native()
if manager.data.implementation.is_pandas():
return native
if manager.data.implementation.is_pyarrow():
- return _arrow_to_geodataframe(native, metadata.geometry_columns)
+ return _arrow_to_geodataframe(
+ native, metadata.geometry_columns, declarations
+ )
raise GeometryExportError(
"unsupported_representation",
"Geographic export from this table type is not supported yet.",
)
-def serialize_geojson(
+def _prepare_geographic_export(
manager: TableManager[Any],
geometry_column: str | None,
- ensure_ascii: bool = True,
-) -> bytes:
- """Write selected geometry and ordinary JSON properties as RFC 7946.
-
- Args:
- manager (TableManager[Any]): Manager for the effective export rows.
- geometry_column (str | None): Explicit geometry, if chosen.
- ensure_ascii (bool, optional): Whether to escape non-ASCII text.
-
- Raises:
- GeometryExportError: If geometry or properties cannot be preserved.
- """
- if not has_geometry_columns(manager):
- raise GeometryExportError(
- "invalid_geometry"
- if geometry_column is not None
- else "geometry_required",
- "GeoJSON export requires a declared geometry column.",
- column=geometry_column,
- )
+ format_name: Literal["parquet", "geojson"],
+) -> tuple[NarwhalsTableManager[Any, Any], _ExportSource, str]:
+ """Validate a geographic request before materializing its source."""
+ label = "GeoParquet" if format_name == "parquet" else "GeoJSON"
if not isinstance(manager, NarwhalsTableManager) or not (
manager.data.implementation.is_pandas()
or manager.data.implementation.is_pyarrow()
+ or manager.data.implementation.is_duckdb()
):
raise GeometryExportError(
"unsupported_representation",
- "GeoJSON export from this table type is not supported yet.",
+ f"{label} export from this table type is not supported yet.",
column=geometry_column,
)
try:
- metadata = get_export_metadata(manager)
+ source = _describe_export_source(manager)
+ except GeometryExportError:
+ raise
except Exception as e:
raise GeometryExportError(
- "invalid_metadata", f"Could not read geometry metadata: {e}"
+ "conversion_failed"
+ if manager.data.implementation.is_duckdb()
+ else "invalid_metadata",
+ f"Could not read geometry metadata: {e}",
) from e
- names = {column.name for column in metadata.geometry_columns}
+ names = {column.name for column in source.metadata.geometry_columns}
if geometry_column is not None and geometry_column not in names:
raise GeometryExportError(
"invalid_geometry",
@@ -538,28 +644,130 @@ def serialize_geojson(
primary = (
geometry_column
if geometry_column is not None
- else metadata.default_geometry_column
+ else source.metadata.default_geometry_column
)
if primary is None:
raise GeometryExportError(
"geometry_required",
- "Choose a geometry column for GeoJSON export.",
+ f"Choose a {'primary ' if format_name == 'parquet' else ''}geometry column for {label} export.",
)
- if not DependencyManager.geopandas.has():
- source_label = (
- "pandas" if manager.data.implementation.is_pandas() else "Arrow"
+ if (
+ manager.data.implementation.is_duckdb()
+ and not DependencyManager.pyarrow.has()
+ ):
+ raise GeometryExportError(
+ "missing_packages",
+ _missing_package_message(manager, "pyarrow"),
+ missing_packages=["pyarrow"],
)
+ if not DependencyManager.geopandas.has():
raise GeometryExportError(
"missing_packages",
- f"This {source_label} table needs geopandas to export GeoJSON.",
+ _missing_package_message(manager, "geopandas", label),
missing_packages=["geopandas"],
)
- if not DependencyManager.shapely.has_at_version(
+ if format_name == "parquet":
+ if not DependencyManager.geopandas.has_at_version(
+ min_version=_MIN_GEOPANDAS_GEOPARQUET_VERSION, quiet=True
+ ):
+ raise GeometryExportError(
+ "unsupported_version", _GEOPANDAS_UPGRADE_MESSAGE
+ )
+ if not DependencyManager.pyarrow.has():
+ raise GeometryExportError(
+ "missing_packages",
+ _missing_package_message(manager, "pyarrow", "GeoParquet"),
+ missing_packages=["pyarrow"],
+ )
+ elif not DependencyManager.shapely.has_at_version(
min_version=_MIN_SHAPELY_GEOJSON_VERSION, quiet=True
):
raise GeometryExportError(
"unsupported_version", _SHAPELY_UPGRADE_MESSAGE
)
+ if source.rejection is not None:
+ raise source.rejection
+ if (
+ format_name == "geojson"
+ and next(
+ column
+ for column in source.metadata.geometry_columns
+ if column.name == primary
+ ).crs
+ is None
+ ):
+ raise GeometryExportError(
+ "missing_crs",
+ f"Geometry column {primary!r} needs a CRS for GeoJSON export. Declare its source CRS in Python before exporting.",
+ column=primary,
+ )
+ if manager.data.implementation.is_duckdb():
+ assert source.schema is not None
+ manager = _duckdb_to_arrow_manager(manager, source.schema)
+ return manager, source, primary
+
+
+def _duckdb_to_arrow_manager(
+ manager: NarwhalsTableManager[Any, Any],
+ schema: pa.Schema,
+) -> NarwhalsTableManager[Any, Any]:
+ """Convert native geometry to portable WKB while retaining declarations."""
+ try:
+ relation = manager.data.to_native()
+ expressions = []
+ for name in relation.columns:
+ quoted = quote_sql_identifier(name, dialect="duckdb")
+ expressions.append(
+ f"ST_AsWKB({quoted})::BLOB AS {quoted}"
+ if name in manager._geometry_columns
+ else quoted
+ )
+ table = relation.project(", ".join(expressions)).to_arrow_table()
+ for name in manager._geometry_columns:
+ index = table.schema.get_field_index(name)
+ field = table.schema.field(index).with_metadata(
+ schema.field(name).metadata
+ )
+ table = table.set_column(index, field, table.column(index))
+ return NarwhalsTableManager.from_dataframe(table)
+ except GeometryExportError:
+ raise
+ except Exception as e:
+ raise GeometryExportError(
+ "conversion_failed",
+ f"Could not convert DuckDB geometry to WKB: {e}",
+ ) from e
+
+
+def serialize_geojson(
+ manager: TableManager[Any],
+ geometry_column: str | None,
+ ensure_ascii: bool = True,
+) -> bytes:
+ """Write selected geometry and ordinary JSON properties as RFC 7946.
+
+ Args:
+ manager (TableManager[Any]): Manager for the effective export rows.
+ geometry_column (str | None): Explicit geometry, if chosen.
+ ensure_ascii (bool, optional): Whether to escape non-ASCII text.
+
+ Raises:
+ GeometryExportError: If geometry or properties cannot be preserved.
+ """
+ if not has_geometry_columns(manager):
+ raise GeometryExportError(
+ "invalid_geometry"
+ if geometry_column is not None
+ else "geometry_required",
+ "GeoJSON export requires a declared geometry column.",
+ column=geometry_column,
+ )
+ prepared, source, primary = _prepare_geographic_export(
+ manager, geometry_column, "geojson"
+ )
+ manager = prepared
+ metadata = source.metadata
+ names = {column.name for column in metadata.geometry_columns}
import geopandas as gpd # type: ignore[import-not-found,import-untyped,unused-ignore]
from shapely.ops import orient # type: ignore[import-untyped]
@@ -570,7 +778,7 @@ def serialize_geojson(
_trivial_range_index,
)
- native = _export_geodataframe(manager, metadata)
+ native = _export_geodataframe(prepared, metadata, source.declarations)
if not native.columns.is_unique:
raise GeometryExportError(
"invalid_metadata", "GeoJSON export requires unique column names."
@@ -898,73 +1106,26 @@ def serialize_geoparquet(
)
return None
- if not isinstance(manager, NarwhalsTableManager) or not (
- manager.data.implementation.is_pandas()
- or manager.data.implementation.is_pyarrow()
+ if (
+ isinstance(manager, NarwhalsTableManager)
+ and manager.data.implementation.is_pandas()
+ and not manager.as_frame().to_native().columns.is_unique
):
raise GeometryExportError(
- "unsupported_representation",
- "GeoParquet export from this table type is not supported yet.",
- column=geometry_column,
+ "invalid_metadata",
+ "GeoParquet export requires unique column names.",
)
- if manager.data.implementation.is_pandas():
- if not manager.as_frame().to_native().columns.is_unique:
- raise GeometryExportError(
- "invalid_metadata",
- "GeoParquet export requires unique column names.",
- )
-
- try:
- metadata = get_export_metadata(manager)
- except Exception as e:
- raise GeometryExportError(
- "invalid_metadata", f"Could not read geometry metadata: {e}"
- ) from e
-
- names = {column.name for column in metadata.geometry_columns}
- if geometry_column is not None and geometry_column not in names:
- raise GeometryExportError(
- "invalid_geometry",
- f"{geometry_column!r} is not a geometry column in this table.",
- column=geometry_column,
- )
- primary = (
- geometry_column
- if geometry_column is not None
- else metadata.default_geometry_column
+ prepared, source, primary = _prepare_geographic_export(
+ manager, geometry_column, "parquet"
)
- if primary is None:
- raise GeometryExportError(
- "geometry_required",
- "Choose a primary geometry column for GeoParquet export.",
- )
-
- if not DependencyManager.geopandas.has():
- source_label = (
- "pandas" if manager.data.implementation.is_pandas() else "Arrow"
- )
- raise GeometryExportError(
- "missing_packages",
- f"This {source_label} table needs geopandas to export GeoParquet.",
- missing_packages=["geopandas"],
- )
- if not DependencyManager.geopandas.has_at_version(
- min_version=_MIN_GEOPANDAS_GEOPARQUET_VERSION, quiet=True
- ):
- raise GeometryExportError(
- "unsupported_version", _GEOPANDAS_UPGRADE_MESSAGE
- )
- if not DependencyManager.pyarrow.has():
- raise GeometryExportError(
- "missing_packages",
- "GeoParquet export requires pyarrow.",
- missing_packages=["pyarrow"],
- )
+ manager = prepared
+ metadata = source.metadata
+ names = {column.name for column in metadata.geometry_columns}
import geopandas as gpd # type: ignore[import-not-found,import-untyped,unused-ignore]
- native = _export_geodataframe(manager, metadata)
+ native = _export_geodataframe(prepared, metadata, source.declarations)
if not native.columns.is_unique:
raise GeometryExportError(
"invalid_metadata",
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
index 839ad9210c..06f743f9d6 100644
--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py
+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
@@ -6,6 +6,7 @@ import functools
import io
import json
import math
+import uuid
from enum import Enum
from functools import cached_property
from typing import TYPE_CHECKING, Any, Literal, cast
@@ -35,6 +36,7 @@ from marimo._plugins.ui._impl.tables.table_manager import (
TableCoordinate,
TableManager,
)
+from marimo._sql.sql_quoting import quote_sql_identifier
from marimo._utils.narwhals_utils import (
can_narwhalify,
dataframe_to_csv,
@@ -60,6 +62,51 @@ POSITIVE_INF = str(float("inf"))
NEGATIVE_INF = str(float("-inf"))
+def _duckdb_snapshot_needs_text(dtype: Any) -> bool:
+ if dtype.id in {"time with time zone", "bit", "uhugeint"}:
+ return True
+ if dtype.id in {"list", "struct", "map", "array"}:
+ return any(
+ _duckdb_snapshot_needs_text(child)
+ for name, child in dtype.children
+ if name != "size"
+ )
+ return False
+
+
+def _snapshot_duckdb_relation(relation: Any) -> tuple[Any, int]:
+ """Bind one portable copy back to the source connection's native types."""
+ expressions = []
+ restored = []
+ for name, dtype in zip(relation.columns, relation.types, strict=True):
+ quoted = quote_sql_identifier(name, dialect="duckdb")
+ is_geometry = str(dtype).startswith("GEOMETRY")
+ if is_geometry:
+ expressions.append(f"ST_AsWKB({quoted})::BLOB AS {quoted}")
+ elif _duckdb_snapshot_needs_text(dtype):
+ # Arrow cannot round-trip these native values without data loss.
+ expressions.append(f"{quoted}::VARCHAR AS {quoted}")
+ else:
+ expressions.append(quoted)
+ value = f"ST_GeomFromWKB({quoted})" if is_geometry else quoted
+ restored.append(f"{value}::{dtype} AS {quoted}")
+ table = relation.project(", ".join(expressions)).to_arrow_table()
+ suffix = uuid.uuid4().hex
+ data_name = f"__marimo_selection_data_{suffix}"
+ view = f"__marimo_selection_source_{suffix}"
+ # DuckDB captures the Arrow object through its Python replacement scan.
+ # A unique binding avoids shadowing a table in the user's connection.
+ globals()[data_name] = table
+ try:
+ snapshot = relation.query(
+ view, f"SELECT {', '.join(restored)} FROM {data_name}"
+ )
+ finally:
+ globals().pop(data_name)
+ relation.query(view, f"DROP VIEW IF EXISTS {view}")
+ return snapshot, table.num_rows
+
+
class NarwhalsTableManager(
TableManager[nw.DataFrame[IntoDataFrameT] | nw.LazyFrame[IntoLazyFrameT]]
):
@@ -170,10 +217,68 @@ class NarwhalsTableManager(
self.data.filter(nw.col(INDEX_COLUMN_NAME).is_in(indices))
)
+ if self.data.implementation.is_duckdb() and self._geometry_columns:
+ relation: Any = self.data.to_native()
+ if min(indices) < 0:
+ raise IndexError("Row index out of bounds")
+ index_name = "__marimo_export_row"
+ occupied = {name.casefold() for name in relation.columns}
+ while index_name.casefold() in occupied:
+ index_name += "_"
+ quoted_index = quote_sql_identifier(index_name, dialect="duckdb")
+ columns = ", ".join(
+ quote_sql_identifier(name, dialect="duckdb")
+ for name in relation.columns
+ )
+ indexed = relation.project(
+ f"{columns}, row_number() OVER () - 1 AS {quoted_index}"
+ )
+ selected_columns = ", ".join(
+ f"source.{quote_sql_identifier(name, dialect='duckdb')}"
+ for name in relation.columns
+ )
+ indexed, row_count = _snapshot_duckdb_relation(indexed)
+ if max(indices) >= row_count:
+ raise IndexError("Row index out of bounds")
+ view = f"__marimo_selection_indexes_{uuid.uuid4().hex}"
+ try:
+ requested = relation.query(
+ view,
+ f"SELECT * FROM unnest([{', '.join(str(int(i)) for i in indices)}]) "
+ "WITH ORDINALITY AS selected(row_index, ordinal)",
+ )
+ finally:
+ relation.query(view, f"DROP VIEW IF EXISTS {view}")
+ selected = (
+ indexed.set_alias("source")
+ .join(
+ requested.set_alias("selected"),
+ f"source.{quoted_index} = selected.row_index",
+ )
+ .order("selected.ordinal")
+ .project(selected_columns)
+ )
+ return self.with_new_data(
+ nw.from_native(selected, pass_through=False)
+ )
+
df = self.as_frame()
return self.with_new_data(df[indices])
def select_columns(self, columns: list[str]) -> TableManager[Any]:
+ if self.data.implementation.is_duckdb() and self._geometry_columns:
+ relation: Any = self.data.to_native()
+ return self.with_new_data(
+ nw.from_native(
+ relation.project(
+ ", ".join(
+ quote_sql_identifier(name, dialect="duckdb")
+ for name in columns
+ )
+ ),
+ pass_through=False,
+ )
+ )
return self.with_new_data(self.data.select(columns))
def select_cells(self, cells: list[TableCoordinate]) -> list[TableCell]:
@@ -202,6 +307,10 @@ class NarwhalsTableManager(
]
def drop_columns(self, columns: list[str]) -> TableManager[Any]:
+ if self.data.implementation.is_duckdb() and self._geometry_columns:
+ return self.select_columns(
+ [name for name in self.data.columns if name not in columns]
+ )
return self.with_new_data(self.data.drop(columns, strict=False))
def get_row_headers(self) -> FieldTypes:
diff --git a/tests/_plugins/ui/_impl/conftest.py b/tests/_plugins/ui/_impl/conftest.py
new file mode 100644
index 0000000000..9631afdcf9
--- /dev/null
+++ b/tests/_plugins/ui/_impl/conftest.py
@@ -0,0 +1,38 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Any
+
+import pytest
+
+from tests._plugins.ui._impl.tables import geometry_fixtures
+
+if TYPE_CHECKING:
+ from collections.abc import Iterator
+
+
+def duckdb_export_conn() -> Iterator[Any]:
+ conn = geometry_fixtures.duckdb_export_connection()
+ try:
+ yield conn
+ finally:
+ conn.close()
+
+
+def duckdb_crs_conn() -> Iterator[Any]:
+ conn = geometry_fixtures.duckdb_crs_geometry_connection()
+ try:
+ yield conn
+ finally:
+ conn.close()
+
+
+def duckdb_spatial_conn() -> Iterator[Any]:
+ conn = geometry_fixtures.duckdb_spatial_connection()
+ try:
+ yield conn
+ finally:
+ conn.close()
diff --git a/tests/_plugins/ui/_impl/tables/geometry_fixtures.py b/tests/_plugins/ui/_impl/tables/geometry_fixtures.py
index 2a80e21b97..30ab2f472b 100644
--- a/tests/_plugins/ui/_impl/tables/geometry_fixtures.py
+++ b/tests/_plugins/ui/_impl/tables/geometry_fixtures.py
@@ -392,3 +392,31 @@ def ordinary_data() -> dict[str, list[Any]]:
datetime.datetime(2024, 1, 3),
],
}
+
+
+def duckdb_export_connection() -> duckdb_mod.DuckDBPyConnection:
+ """Connection with core geometry support, or an older spatial extension."""
+ import duckdb
+
+ conn = duckdb.connect()
+ try:
+ _ = conn.sql("SELECT NULL::GEOMETRY").types
+ except duckdb.Error:
+ conn.close()
+ return duckdb_spatial_connection()
+ return conn
+
+
+def duckdb_export_relation(
+ conn: duckdb_mod.DuckDBPyConnection, *, known_crs: bool = True
+) -> duckdb_mod.DuckDBPyRelation:
+ """Two geometry columns, a null row, and nullable ordinary values."""
+ crs = "('OGC:CRS84')" if known_crs else ""
+ return conn.sql(
+ f"SELECT id, geom::GEOMETRY{crs} AS geom, "
+ "alternate::GEOMETRY AS alternate, label, big::BIGINT AS big FROM "
+ "(VALUES (1, 'POINT (10 0)', 'POINT (20.1234567890123 5)', 'first', 9007199254740993), "
+ "(2, NULL, NULL, 'second', NULL), "
+ "(3, 'POINT (30 1)', 'POINT (40 2)', NULL, 7)) "
+ "AS data(id, geom, alternate, label, big)"
+ )
diff --git a/tests/_plugins/ui/_impl/tables/test_geometry.py b/tests/_plugins/ui/_impl/tables/test_geometry.py
index 447f901963..03c0d38301 100644
--- a/tests/_plugins/ui/_impl/tables/test_geometry.py
+++ b/tests/_plugins/ui/_impl/tables/test_geometry.py
@@ -972,3 +972,203 @@ assert downloaded[1]["geom"] is None
)
except subprocess.CalledProcessError as e:
raise AssertionError(e.stderr) from e
+
+
[email protected]("duckdb", "pyarrow")
[email protected]("indices", [[2, 0, 2], [1, 1], [], [2, 1, 0]])
+def test_duckdb_geometry_selection_preserves_order_and_duplicates(
+ indices: list[int],
+) -> None:
+ conn = geo.duckdb_export_connection()
+ try:
+ source = conn.sql(
+ 'SELECT i AS id, \'POINT (1 2)\'::GEOMETRY AS "g""eom", 1 AS "__marimo_export_row" FROM range(3) t(i)'
+ )
+ manager = get_table_manager(source).select_rows(indices)
+ assert manager.data.implementation.is_duckdb()
+ assert manager.as_frame().get_column("id").to_list() == indices
+ assert 'g"eom' in manager.get_column_names()
+ assert manager.data.to_native().types[1] == source.types[1]
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
[email protected]("indices", [[-1], [3], [0, 3], [2, -1]])
+def test_duckdb_geometry_selection_rejects_out_of_bounds(
+ indices: list[int],
+) -> None:
+ conn = geo.duckdb_export_connection()
+ try:
+ with pytest.raises(IndexError, match="out of bounds"):
+ get_table_manager(
+ conn.sql(
+ "SELECT i AS id, NULL::GEOMETRY AS geom FROM range(3) t(i)"
+ )
+ ).select_rows(indices)
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
+ "name", ["__MARIMO_EXPORT_ROW", "__Marimo_Export_Row"]
+)
+def test_duckdb_selection_avoids_case_insensitive_index_collision(
+ name: str,
+) -> None:
+ conn = geo.duckdb_export_connection()
+ try:
+ source = conn.sql(
+ f'SELECT i AS id, NULL::GEOMETRY AS geom, 99 AS "{name}", 98 AS "__MARIMO_EXPORT_ROW_" FROM range(3) t(i)'
+ )
+ manager = get_table_manager(source).select_rows([2, 0, 2])
+ rows = manager.data.to_native().fetchall()
+ assert [row[0] for row in rows] == [2, 0, 2]
+ assert [row[2:] for row in rows] == [(99, 98)] * 3
+ assert manager.data.to_native().types == source.types
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
+def test_duckdb_selection_preserves_nonportable_arrow_values() -> None:
+ conn = geo.duckdb_export_connection()
+ try:
+ source = conn.sql(
+ "SELECT NULL::GEOMETRY AS geom, '01010'::BIT AS bits, '12:34:56+05:30'::TIMETZ AS clock, 340282366920938463463374607431768211455::UHUGEINT AS large, ['01010'::BIT, NULL] AS bit_list, {'clock': '12:34:56+05:30'::TIMETZ} AS nested_clock, ['12:34:56+05:30'::TIMETZ, NULL]::TIMETZ[2] AS clock_array, MAP {'clock': '12:34:56+05:30'::TIMETZ} AS clock_map"
+ )
+ selected = get_table_manager(source).select_rows([0, 0])
+ native = selected.data.to_native()
+ assert native.types == source.types
+ assert native.fetchall() == source.fetchall() * 2
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
+def test_duckdb_selection_evaluates_volatile_source_once() -> None:
+ conn = geo.duckdb_export_connection()
+ calls = []
+
+ def include(value: int) -> bool:
+ calls.append(value)
+ return len(calls) <= 3
+
+ try:
+ conn.create_function("include_row", include, side_effects=True)
+ source = conn.sql(
+ "SELECT i AS id, 'POINT (1 2)'::GEOMETRY AS geom FROM range(3) t(i) WHERE include_row(i)"
+ )
+ manager = get_table_manager(source).select_rows([2, 0, 2])
+ assert len(calls) == 3
+ for _ in range(2):
+ assert [row[0] for row in manager.data.to_native().fetchall()] == [
+ 2,
+ 0,
+ 2,
+ ]
+ assert len(calls) == 3
+ assert manager.data.to_native().types == source.types
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
+def test_duckdb_snapshot_lifetime_follows_derived_native_relation() -> None:
+ import gc
+
+ conn = geo.duckdb_export_connection()
+ try:
+ source = get_table_manager(
+ conn.sql(
+ "SELECT i AS id, NULL::GEOMETRY AS geom FROM range(3) t(i)"
+ )
+ )
+ before = conn.sql(
+ "SELECT table_name FROM duckdb_tables() WHERE temporary"
+ ).fetchall()
+ selected = source.select_rows([2, 0, 2])
+ derived = selected.select_columns(["id", "geom"])
+ native = derived.data.to_native()
+ descendant = native.project("id, geom")
+ del selected, derived
+ del native
+ gc.collect()
+ assert [row[0] for row in descendant.fetchall()] == [2, 0, 2]
+ assert (
+ conn.sql(
+ "SELECT table_name FROM duckdb_tables() WHERE temporary"
+ ).fetchall()
+ == before
+ )
+ del descendant
+ gc.collect()
+ assert (
+ conn.sql(
+ "SELECT table_name FROM duckdb_tables() WHERE temporary"
+ ).fetchall()
+ == before
+ )
+ assert (
+ conn.sql(
+ "SELECT view_name FROM duckdb_views() WHERE NOT internal AND view_name LIKE '__marimo_selection_source_%'"
+ ).fetchall()
+ == []
+ )
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
+def test_duckdb_invalid_selection_releases_snapshot() -> None:
+ import gc
+
+ conn = geo.duckdb_export_connection()
+ try:
+ source = get_table_manager(conn.sql("SELECT NULL::GEOMETRY AS geom"))
+ with pytest.raises(IndexError):
+ source.select_rows([1])
+ gc.collect()
+ assert (
+ conn.sql(
+ "SELECT table_name FROM duckdb_tables() WHERE temporary AND table_name LIKE '__marimo_selection_%'"
+ ).fetchall()
+ == []
+ )
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_selection_snapshot_preserves_crs_after_source_changes() -> (
+ None
+):
+ from marimo._plugins.ui._impl.tables.geometry_export import (
+ get_export_metadata,
+ )
+
+ conn = geo.duckdb_crs_geometry_connection()
+ try:
+ conn.execute(
+ "CREATE TABLE locations AS SELECT i AS id, 'POINT (1 2)'::GEOMETRY('OGC:CRS84') AS geom FROM range(3) t(i)"
+ )
+ original = get_table_manager(conn.table("locations"))
+ metadata = get_export_metadata(original)
+ selected = original.select_rows([2, 0, 2])
+ conn.execute("DELETE FROM locations")
+ assert (
+ selected.data.to_native().types == original.data.to_native().types
+ )
+ assert (
+ get_export_metadata(selected).geometry_columns
+ == metadata.geometry_columns
+ )
+ assert [row[0] for row in selected.data.to_native().fetchall()] == [
+ 2,
+ 0,
+ 2,
+ ]
+ assert conn.table("locations").count("*").fetchall() == [(0,)]
+ finally:
+ conn.close()
diff --git a/tests/_plugins/ui/_impl/test_export_metadata.py b/tests/_plugins/ui/_impl/test_export_metadata.py
index 4ba671036d..54cbdde1a7 100644
--- a/tests/_plugins/ui/_impl/test_export_metadata.py
+++ b/tests/_plugins/ui/_impl/test_export_metadata.py
@@ -330,3 +330,125 @@ def test_widget_passes_geometry_request_to_export(
)
assert response.url == "data:test"
assert export.call_args.kwargs["geometry_column"] == "geom_b"
+
+
[email protected]("duckdb", "pyarrow", "geopandas")
[email protected]("empty", [False, True])
+def test_duckdb_export_metadata(
+ duckdb_crs_conn: Any, widget: Any, empty: bool
+) -> None:
+ from pyproj import CRS
+
+ conn = duckdb_crs_conn
+ source = fixtures.duckdb_export_relation(conn)
+ if empty:
+ source = source.limit(0)
+ subject = widget(source)
+ with patch.object(
+ NarwhalsTableManager,
+ "as_frame",
+ side_effect=AssertionError("must not collect"),
+ ):
+ result = subject._get_export_metadata(EmptyArgs())
+ assert [c.name for c in result.geometry_columns] == [
+ "geom",
+ "alternate",
+ ]
+ assert CRS.from_user_input(
+ result.geometry_columns[0].crs
+ ) == CRS.from_user_input("OGC:CRS84")
+ assert result.geometry_columns[1].crs is None
+ assert result.default_geometry_column is None
+ assert all(f.available for f in result.formats.values())
+
+
[email protected]("duckdb", "pyarrow")
+def test_duckdb_schema_does_not_execute_source_rows() -> None:
+ import duckdb
+
+ from marimo._plugins.ui._impl.tables.geometry_export import (
+ get_export_metadata,
+ )
+ from marimo._plugins.ui._impl.tables.utils import get_table_manager
+
+ conn = fixtures.duckdb_crs_geometry_connection()
+ calls = []
+
+ def count(value: int) -> int:
+ calls.append(value)
+ return value
+
+ try:
+ conn.create_function(
+ "count_rows",
+ count,
+ [duckdb.sqltypes.BIGINT],
+ duckdb.sqltypes.BIGINT,
+ side_effects=True,
+ )
+ source = conn.sql(
+ "SELECT count_rows(i) AS id, NULL::GEOMETRY('OGC:CRS84') AS geom FROM range(100) t(i)"
+ )
+ manager = get_table_manager(source)
+ result = get_export_metadata(manager)
+ assert result.geometry_columns[0].crs is not None
+ assert calls == []
+ finally:
+ conn.close()
+
+
[email protected]("duckdb", "pyarrow")
[email protected]("missing", ["geopandas", "pyarrow"])
+def test_duckdb_missing_packages(
+ duckdb_export_conn: Any, widget: Any, missing: str
+) -> None:
+
+ conn = duckdb_export_conn
+ subject = widget(conn.sql("SELECT NULL::GEOMETRY AS geom"))
+ with patch.object(
+ getattr(DependencyManager, missing), "has", return_value=False
+ ):
+ result = subject._get_export_metadata(EmptyArgs())
+ assert all(
+ f.missing_packages == [missing] for f in result.formats.values()
+ )
+ assert result.geometry_columns[0].crs is None
+
+
[email protected]("duckdb", "pyarrow", "geopandas")
+def test_duckdb_fixed_layout_disables_both_formats(
+ duckdb_spatial_conn: Any, widget: Any
+) -> None:
+
+ conn = duckdb_spatial_conn
+ subject = widget(
+ conn.sql(
+ "SELECT ST_Point(1, 2) AS geom, {'x': 1.0, 'y': 2.0}::POINT_2D AS point"
+ )
+ )
+ result = subject._get_export_metadata(EmptyArgs())
+ assert [c.encoding for c in result.geometry_columns] == [
+ "wkb",
+ "other",
+ ]
+ assert all(
+ not f.available and "POINT_2D" in (f.reason or "")
+ for f in result.formats.values()
+ )
+
+
[email protected]("duckdb", "pyarrow")
+def test_duckdb_binary_is_not_geometry(widget: Any) -> None:
+ import duckdb
+
+ conn = duckdb.connect()
+ try:
+ result = widget(
+ conn.sql(
+ "SELECT 'POINT (1 2)'::VARCHAR AS geometry, 'abc'::BLOB AS blob"
+ )
+ )._get_export_metadata(EmptyArgs())
+ assert result.geometry_columns == []
+ assert result.formats == {}
+ finally:
+ conn.close()
diff --git a/tests/_plugins/ui/_impl/test_geojson_export.py b/tests/_plugins/ui/_impl/test_geojson_export.py
index b04c02cd59..8e5caa1bb0 100644
--- a/tests/_plugins/ui/_impl/test_geojson_export.py
+++ b/tests/_plugins/ui/_impl/test_geojson_export.py
@@ -1230,3 +1230,312 @@ def test_dataframe_transformed_value_and_obsolete_choice() -> None:
feature["properties"]["name"]
for feature in _artifact(subject)["features"]
] == ["null"]
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("empty", [False, True])
+def test_duckdb_geojson_properties_and_secondary_wkt(
+ duckdb_crs_conn: Any, widget: Any, empty: bool
+) -> None:
+
+ conn = duckdb_crs_conn
+ source = fixtures.duckdb_export_relation(conn)
+ if empty:
+ source = source.limit(0)
+ before = source.to_arrow_table()
+ subject = widget(source)
+ ordinary = subject._download_as(DownloadAsArgs(format="json"))
+ expected = json.loads(from_data_uri(ordinary.url)[1])
+ for row in expected:
+ del row["geom"]
+ document = _artifact(subject, "geom")
+ assert "crs" not in document
+ assert [f["properties"] for f in document["features"]] == expected
+ if not empty:
+ assert document["features"][0]["geometry"]["coordinates"] == [
+ 10,
+ 0,
+ ]
+ assert document["features"][1]["geometry"] is None
+ assert (
+ "20.1234567890123"
+ in document["features"][0]["properties"]["alternate"]
+ )
+ assert source.to_arrow_table().equals(before, check_metadata=True)
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_geojson_projected_geometry_choice(
+ duckdb_spatial_conn: Any, widget: Any
+) -> None:
+
+ conn = duckdb_spatial_conn
+ try:
+ source = conn.sql(
+ "SELECT 'POINT (1113194.9079327357 0)'::GEOMETRY('EPSG:3857') AS projected, 'POINT (20 5)'::GEOMETRY('OGC:CRS84') AS geographic"
+ )
+ except Exception as e:
+ pytest.skip(f"CRS-parameterized geometry unavailable: {e}")
+ subject = widget(source)
+ document = _artifact(subject, "projected")
+ assert document["features"][0]["geometry"]["coordinates"] == pytest.approx(
+ [10, 0]
+ )
+ assert (
+ document["features"][0]["properties"]["geographic"] == "POINT (20 5)"
+ )
+ document = _artifact(subject, "geographic")
+ assert document["features"][0]["geometry"]["coordinates"] == [20, 5]
+ assert (
+ "1113194.9079327357"
+ in document["features"][0]["properties"]["projected"]
+ )
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_geojson_missing_crs_and_alternate_choice(
+ duckdb_crs_conn: Any, widget: Any
+) -> None:
+
+ conn = duckdb_crs_conn
+ subject = widget(fixtures.duckdb_export_relation(conn))
+ response = subject._download_geojson(
+ DownloadGeoJSONArgs(format="geojson", geometry_column="alternate")
+ )
+ assert response.code == "missing_crs"
+ assert response.column == "alternate"
+ assert response.url == ""
+ assert "Declare its source CRS" in response.error
+ assert len(_artifact(subject, "geom")["features"]) == 3
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_geojson_effective_rows(
+ duckdb_crs_conn: Any,
+) -> None:
+
+ conn = duckdb_crs_conn
+ source = fixtures.duckdb_export_relation(conn)
+ table = ui.table(source)
+ table._update(["0", "2"])
+ assert [
+ f["properties"]["id"] for f in _artifact(table, "geom")["features"]
+ ] == [1, 3]
+ frame = ui.dataframe(source, limit=1)
+ assert len(_artifact(frame, "geom")["features"]) == 3
+ frame._update(
+ {"transforms": [{"type": "select_columns", "column_ids": ["geom"]}]}
+ )
+ assert [f["properties"] for f in _artifact(frame)["features"]] == [
+ {},
+ {},
+ {},
+ ]
+ searched = ui.table(source)
+ searched._searched_manager = searched._manager.search("first")
+ assert [
+ f["properties"]["id"] for f in _artifact(searched, "geom")["features"]
+ ] == [1]
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+ "value", ["NULL", "'POINT EMPTY'", "'POINT Z (1 2 3)'"]
+)
+def test_duckdb_geojson_geometry_only(
+ duckdb_crs_conn: Any, widget: Any, value: str
+) -> None:
+
+ conn = duckdb_crs_conn
+ subject = widget(
+ conn.sql(f"SELECT {value}::GEOMETRY('OGC:CRS84') AS geom")
+ )
+ feature = _artifact(subject)["features"][0]
+ assert feature["properties"] == {}
+ if value == "NULL":
+ assert feature["geometry"] is None
+ elif "EMPTY" in value:
+ assert (
+ feature["geometry"] is None
+ or feature["geometry"]["coordinates"] == []
+ )
+ else:
+ assert feature["geometry"]["coordinates"] == [1, 2, 3]
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("wkt", ["POINT M (1 2 3)", "POINT ZM (1 2 3 4)"])
+def test_duckdb_geojson_measured_geometry_rejects(
+ duckdb_crs_conn: Any, widget: Any, wkt: str
+) -> None:
+
+ conn = duckdb_crs_conn
+ response = widget(
+ conn.sql(f"SELECT '{wkt}'::GEOMETRY('OGC:CRS84') AS geom")
+ )._download_geojson(DownloadGeoJSONArgs(format="geojson"))
+ assert response.code == "unsupported_representation"
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("value", ["'NaN'::DOUBLE", "'Infinity'::DOUBLE"])
+def test_duckdb_geojson_nonfinite_properties_follow_json_policy(
+ duckdb_crs_conn: Any, widget: Any, value: str
+) -> None:
+
+ conn = duckdb_crs_conn
+ subject = widget(
+ conn.sql(
+ f"SELECT 'POINT (1 2)'::GEOMETRY('OGC:CRS84') AS geom, {value} AS value"
+ )
+ )
+ response = subject._download_geojson(DownloadGeoJSONArgs(format="geojson"))
+ if "NaN" in value:
+ assert response.code == "conversion_failed"
+ assert response.url == ""
+ else:
+ ordinary = subject._download_as(DownloadAsArgs(format="json"))
+ expected = json.loads(from_data_uri(ordinary.url)[1])[0]
+ del expected["geom"]
+ assert _artifact(subject)["features"][0]["properties"] == expected
+
+
[email protected]("duckdb", "pyarrow")
[email protected]("missing", ["geopandas", "pyarrow"])
+def test_duckdb_geojson_missing_packages(
+ duckdb_crs_conn: Any, widget: Any, missing: str
+) -> None:
+
+ conn = duckdb_crs_conn
+ subject = widget(conn.sql("SELECT NULL::GEOMETRY('OGC:CRS84') AS geom"))
+ with patch.object(
+ getattr(DependencyManager, missing), "has", return_value=False
+ ):
+ response = subject._download_geojson(
+ DownloadGeoJSONArgs(format="geojson")
+ )
+ assert response.code == "missing_packages"
+ assert response.missing_packages == [missing]
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+ "kind", ["POINT_2D", "LINESTRING_2D", "POLYGON_2D", "BOX_2D"]
+)
+def test_duckdb_geojson_fixed_layout_rejects(
+ duckdb_spatial_conn: Any, widget: Any, kind: str
+) -> None:
+
+ conn = duckdb_spatial_conn
+ response = widget(
+ conn.sql(f"SELECT NULL::{kind} AS geom")
+ )._download_geojson(DownloadGeoJSONArgs(format="geojson"))
+ assert response.code == "unsupported_representation"
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+ ("payload", "code"),
+ [
+ (b"[]", "invalid_metadata"),
+ (b'{"crs":"123","crs_type":"srid"}', "missing_crs"),
+ ],
+)
+def test_duckdb_geojson_declaration_rejects(
+ duckdb_crs_conn: Any, widget: Any, payload: bytes, code: str
+) -> None:
+ import pyarrow as pa
+
+ conn = duckdb_crs_conn
+ subject = widget(
+ conn.sql("SELECT 'POINT (1 2)'::GEOMETRY('OGC:CRS84') AS geom")
+ )
+ schema = pa.schema(
+ [
+ pa.field(
+ "geom",
+ pa.binary(),
+ metadata={
+ b"ARROW:extension:name": b"geoarrow.wkb",
+ b"ARROW:extension:metadata": payload,
+ },
+ )
+ ]
+ )
+ with patch(
+ "marimo._plugins.ui._impl.tables.geometry_export._duckdb_geometry_schema",
+ return_value=schema,
+ ):
+ response = subject._download_geojson(
+ DownloadGeoJSONArgs(format="geojson")
+ )
+ assert response.code == code
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_unknown_crs_geojson_rejects_on_older_versions(
+ duckdb_export_conn: Any,
+ widget: Any,
+) -> None:
+
+ conn = duckdb_export_conn
+ response = widget(
+ conn.sql("SELECT 'POINT (1 2)'::GEOMETRY AS geom")
+ )._download_geojson(DownloadGeoJSONArgs(format="geojson"))
+ assert response.code == "missing_crs"
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_closed_connection_is_structured_failure() -> None:
+ from marimo._plugins.ui._impl.tables.geometry_export import (
+ GeometryExportError,
+ has_geometry_columns,
+ serialize_geojson,
+ )
+ from marimo._plugins.ui._impl.tables.utils import get_table_manager
+
+ conn = fixtures.duckdb_crs_geometry_connection()
+ manager = get_table_manager(
+ conn.sql("SELECT NULL::GEOMETRY('OGC:CRS84') AS geom")
+ )
+ assert has_geometry_columns(manager)
+ conn.close()
+ with pytest.raises(GeometryExportError) as error:
+ serialize_geojson(manager, None)
+ assert error.value.code == "conversion_failed"
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_registered_wkb_preserves_crs(widget: Any) -> None:
+ import pyarrow as pa
+
+ class GeometryType(pa.ExtensionType):
+ def __init__(self, metadata: bytes = b"{}") -> None:
+ self.metadata = metadata
+ super().__init__(pa.binary(), "geoarrow.wkb")
+
+ def __arrow_ext_serialize__(self) -> bytes:
+ return self.metadata
+
+ @classmethod
+ def __arrow_ext_deserialize__(
+ cls, storage: Any, metadata: bytes
+ ) -> Any:
+ return cls(metadata)
+
+ conn = fixtures.duckdb_crs_geometry_connection()
+ pa.register_extension_type(GeometryType()) # type: ignore[arg-type]
+ try:
+ document = _artifact(
+ widget(
+ conn.sql("SELECT 'POINT (1 2)'::GEOMETRY('OGC:CRS84') AS geom")
+ )
+ )
+ assert document["features"][0]["geometry"]["coordinates"] == [1, 2]
+ finally:
+ pa.unregister_extension_type("geoarrow.wkb")
+ conn.close()
diff --git a/tests/_plugins/ui/_impl/test_geoparquet_export.py b/tests/_plugins/ui/_impl/test_geoparquet_export.py
index fbfbaead20..ca47e59450 100644
--- a/tests/_plugins/ui/_impl/test_geoparquet_export.py
+++ b/tests/_plugins/ui/_impl/test_geoparquet_export.py
@@ -1210,3 +1210,299 @@ def test_pandas_duplicate_columns_precede_dependency_errors() -> None:
"invalid_metadata",
"GeoParquet export requires unique column names.",
)
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("empty", [False, True])
[email protected]("choice", ["geom", "alternate"])
+def test_duckdb_geoparquet_keeps_native_geometry(
+ duckdb_crs_conn: Any, widget: Any, empty: bool, choice: str
+) -> None:
+ import geopandas as gpd
+ import pyarrow.parquet as pq
+ from pyproj import CRS
+
+ conn = duckdb_crs_conn
+ source = fixtures.duckdb_export_relation(conn)
+ if empty:
+ source = source.limit(0)
+ before = source.to_arrow_table()
+ artifact, _ = _artifact(widget(source), choice)
+ result = pq.read_table(io.BytesIO(artifact))
+ geo = json.loads(result.schema.metadata[b"geo"])
+ assert geo["version"] == "1.0.0"
+ assert geo["primary_column"] == choice
+ assert geo["columns"]["alternate"]["crs"] is None
+ assert CRS.from_json_dict(
+ geo["columns"]["geom"]["crs"]
+ ) == CRS.from_user_input("OGC:CRS84")
+ assert result.column_names == before.column_names
+ assert result["big"].to_pylist() == before["big"].to_pylist()
+ assert result["geom"].to_pylist() == before["geom"].to_pylist()
+ assert source.to_arrow_table().equals(before, check_metadata=True)
+ restored = gpd.read_parquet(io.BytesIO(artifact))
+ assert len(restored) == (0 if empty else 3)
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_selected_rows_preserve_native_declarations(
+ duckdb_export_conn: Any,
+) -> None:
+ import pyarrow.parquet as pq
+
+ from marimo._plugins.ui._impl.tables.geometry_export import (
+ serialize_geoparquet,
+ )
+ from marimo._plugins.ui._impl.tables.utils import get_table_manager
+
+ conn = duckdb_export_conn
+ source = fixtures.duckdb_export_relation(conn, known_crs=False)
+ selected = get_table_manager(source).select_rows([2, 0])
+ assert selected.data.implementation.is_duckdb()
+ artifact = serialize_geoparquet(selected, "geom")
+ assert artifact is not None
+ result = pq.read_table(io.BytesIO(artifact))
+ assert result["id"].to_pylist() == [3, 1]
+ assert (
+ json.loads(result.schema.metadata[b"geo"])["columns"]["geom"]["crs"]
+ is None
+ )
+ subject = ui.table(source)
+ subject._update(["0", "2"])
+ result = pq.read_table(io.BytesIO(_artifact(subject, "geom")[0]))
+ assert result["id"].to_pylist() == [1, 3]
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_dataframe_exports_full_transformed_rows(
+ duckdb_export_conn: Any,
+) -> None:
+ import pyarrow.parquet as pq
+
+ conn = duckdb_export_conn
+ subject = ui.dataframe(
+ fixtures.duckdb_export_relation(conn, known_crs=False), limit=1
+ )
+ result = pq.read_table(io.BytesIO(_artifact(subject, "geom")[0]))
+ assert result["id"].to_pylist() == [1, 2, 3]
+ subject._update(
+ {
+ "transforms": [
+ {"type": "select_columns", "column_ids": ["id", "geom"]}
+ ]
+ }
+ )
+ result = pq.read_table(io.BytesIO(_artifact(subject)[0]))
+ assert result.column_names == ["id", "geom"]
+ assert result.num_rows == 3
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_geoparquet_quotes_identifiers(
+ duckdb_export_conn: Any, widget: Any
+) -> None:
+ import pyarrow.parquet as pq
+
+ conn = duckdb_export_conn
+ source = conn.sql(
+ 'SELECT \'POINT (1 2)\'::GEOMETRY AS "g""eom", DATE \'2026-10-08\' AS "date", 1.25::DECIMAL(8,2) AS amount'
+ )
+ artifact, _ = _artifact(widget(source))
+ result = pq.read_table(io.BytesIO(artifact))
+ assert result.column_names == ['g"eom', "date", "amount"]
+ assert str(result["amount"][0].as_py()) == "1.25"
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+ "kind", ["POINT_2D", "LINESTRING_2D", "POLYGON_2D", "BOX_2D"]
+)
+def test_duckdb_fixed_layout_rejects_without_artifact(
+ duckdb_spatial_conn: Any, widget: Any, kind: str
+) -> None:
+
+ conn = duckdb_spatial_conn
+ subject = widget(conn.sql(f"SELECT NULL::{kind} AS geom"))
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert response.code == "unsupported_representation"
+ assert response.url == ""
+ assert kind in response.error
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("wkt", ["POINT M (1 2 3)", "POINT ZM (1 2 3 4)"])
+def test_duckdb_measured_geometry_rejects(
+ duckdb_export_conn: Any, widget: Any, wkt: str
+) -> None:
+
+ conn = duckdb_export_conn
+ response = widget(
+ conn.sql(f"SELECT '{wkt}'::GEOMETRY AS geom")
+ )._download_as(DownloadAsArgs(format="parquet"))
+ assert response.code == "unsupported_representation"
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_projected_and_geographic_crs(
+ duckdb_spatial_conn: Any, widget: Any
+) -> None:
+ import pyarrow.parquet as pq
+ from pyproj import CRS
+
+ conn = duckdb_spatial_conn
+ try:
+ source = conn.sql(
+ "SELECT 'POINT (1113194.9079327357 0)'::GEOMETRY('EPSG:3857') AS projected, 'POINT (20 5)'::GEOMETRY('OGC:CRS84') AS geographic"
+ )
+ except Exception as e:
+ pytest.skip(f"CRS-parameterized geometry unavailable: {e}")
+ result = pq.read_table(
+ io.BytesIO(_artifact(widget(source), "projected")[0])
+ )
+ geo = json.loads(result.schema.metadata[b"geo"])
+ assert CRS.from_json_dict(
+ geo["columns"]["projected"]["crs"]
+ ) == CRS.from_epsg(3857)
+ assert CRS.from_json_dict(
+ geo["columns"]["geographic"]["crs"]
+ ) == CRS.from_user_input("OGC:CRS84")
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+ "value", ["NULL", "'POINT EMPTY'", "'POINT Z (1 2 3)'"]
+)
+def test_duckdb_geometry_only_roundtrip(
+ duckdb_export_conn: Any, widget: Any, value: str
+) -> None:
+ import pyarrow.parquet as pq
+ from shapely import from_wkb
+
+ conn = duckdb_export_conn
+ artifact, _ = _artifact(
+ widget(conn.sql(f"SELECT {value}::GEOMETRY AS geom"))
+ )
+ result = pq.read_table(io.BytesIO(artifact))
+ assert result.num_rows == 1
+ geometry = from_wkb(result["geom"][0].as_py())
+ if value == "NULL":
+ assert geometry is None
+ elif "EMPTY" in value:
+ assert geometry.is_empty
+ else:
+ assert geometry.has_z
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+def test_duckdb_conversion_failure_is_atomic(
+ duckdb_export_conn: Any, widget: Any
+) -> None:
+
+ conn = duckdb_export_conn
+ subject = widget(conn.sql("SELECT 'POINT (1 2)'::GEOMETRY AS geom"))
+ with patch(
+ "marimo._plugins.ui._impl.tables.geometry_export._duckdb_geometry_schema",
+ side_effect=RuntimeError("conversion unavailable"),
+ ):
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert response.code == "conversion_failed"
+ assert response.url == ""
+
+
[email protected]("duckdb", "pyarrow")
[email protected]("missing", ["geopandas", "pyarrow"])
+def test_duckdb_geoparquet_missing_package_is_atomic(
+ duckdb_export_conn: Any, widget: Any, missing: str
+) -> None:
+
+ conn = duckdb_export_conn
+ subject = widget(conn.sql("SELECT NULL::GEOMETRY AS geom"))
+ with patch.object(
+ getattr(DependencyManager, missing), "has", return_value=False
+ ):
+ response = subject._download_as(DownloadAsArgs(format="parquet"))
+ assert response.code == "missing_packages"
+ assert response.missing_packages == [missing]
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
+ ("choice", "code"),
+ [(None, "geometry_required"), ("missing", "invalid_geometry")],
+)
+def test_duckdb_geoparquet_choice_is_validated(
+ duckdb_export_conn: Any, widget: Any, choice: str | None, code: str
+) -> None:
+
+ conn = duckdb_export_conn
+ subject = widget(fixtures.duckdb_export_relation(conn, known_crs=False))
+ response = subject._download_as(
+ DownloadAsArgs(format="parquet", geometry_column=choice)
+ )
+ assert response.code == code
+ assert response.url == ""
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("format_name", ["parquet", "geojson"])
+def test_duckdb_export_parses_source_declarations_once(
+ duckdb_crs_conn: Any,
+ format_name: str,
+) -> None:
+ from marimo._plugins.ui._impl.tables import geometry_export as export
+ from marimo._plugins.ui._impl.tables.utils import get_table_manager
+
+ conn = duckdb_crs_conn
+ source = get_table_manager(fixtures.duckdb_export_relation(conn))
+ with (
+ patch.object(
+ export,
+ "_duckdb_geometry_schema",
+ wraps=export._duckdb_geometry_schema,
+ ) as schema,
+ patch.object(
+ export,
+ "_arrow_geometry_declaration",
+ wraps=export._arrow_geometry_declaration,
+ ) as declaration,
+ ):
+ if format_name == "parquet":
+ export.serialize_geoparquet(source, "geom")
+ else:
+ export.serialize_geojson(source, "geom")
+ assert schema.call_count == 1
+ assert declaration.call_count == 2
+
+
[email protected]("duckdb", "geopandas", "pyarrow")
[email protected]("format_name", ["parquet", "geojson"])
[email protected]("failure", ["invalid_geometry", "missing_packages"])
+def test_duckdb_preflight_rejects_before_collection(
+ duckdb_export_conn: Any, format_name: str, failure: str
+) -> None:
+ from marimo._plugins.ui._impl.tables import geometry_export as export
+ from marimo._plugins.ui._impl.tables.utils import get_table_manager
+
+ manager = get_table_manager(
+ fixtures.duckdb_export_relation(duckdb_export_conn, known_crs=False)
+ )
+ writer = (
+ export.serialize_geoparquet
+ if format_name == "parquet"
+ else export.serialize_geojson
+ )
+ choice = "absent" if failure == "invalid_geometry" else "geom"
+ with (
+ patch.object(export, "_duckdb_to_arrow_manager") as collect,
+ patch.object(
+ DependencyManager.geopandas,
+ "has",
+ return_value=failure != "missing_packages",
+ ),
+ pytest.raises(export.GeometryExportError) as error,
+ ):
+ writer(manager, choice)
+ assert error.value.code == failure
+ collect.assert_not_called()