Skip to content

feat(rust/sedona-raster): array-level column overrides for metadata setters - #1198

Open
james-willis wants to merge 1 commit into
jw/copy-into-override-semanticsfrom
james/db-99-array-level-override
Open

feat(rust/sedona-raster): array-level column overrides for metadata setters#1198
james-willis wants to merge 1 commit into
jw/copy-into-override-semanticsfrom
james/db-99-array-level-override

Conversation

@james-willis

Copy link
Copy Markdown
Contributor

Closes DB-99. Stacked on #1158 — review that first; this diff shows only its own commit.

The problem

A raster is an Arrow StructArray with five columns:

[0] crs           Utf8View
[1] transform     List<Float64>  (6 per row)
[2] spatial_dims  List<Utf8View>
[3] spatial_shape List<Int64>
[4] bands         ← every pixel byte in the batch

RS_SetCRS, RS_SetSRID and RS_SetGeoReference change column 0 or 1. Column 4 is untouched by definition. But the three setters were split across two very different ways of producing that output:

how cost
swap_crs_column replace one column, carry the rest over as the same Arc correct, but private to rs_setsrid.rs and hardcoded to CRS
copy_raster_from walk every band, re-emit every band field into a fresh builder rebuilds all band metadata to change one number

RS_SetGeoReference used the builder. Pixel buffers were shared by refcount, but the bands came out identical only if the rebuild was faithful. #1192 is what happens when it isn't — that same rebuild silently dropped every band name.

The change

Generalize the first into a shared free function and move all three setters onto it.

pub struct RasterColumnOverrides {
    pub crs: Override<ArrayRef>,
    pub transform: Override<ArrayRef>,
}

pub fn with_column_overrides(
    raster: &StructArray,
    overrides: RasterColumnOverrides,
    input_nulls: Option<&NullBuffer>,
) -> Result<StructArray, RasterError>;
  • Override<T>, not OptionRS_SetSRID(raster, 0) must clear the CRS while keeping the raster, which is distinct from leaving the column alone. Option can't say both; that's exactly why swap_crs_column was bespoke.
  • Absorbs the null merge duplicated inline at both rs_setsrid.rs call sites, including the length-1 broadcast for scalar arguments. A null argument nulls the raster row — a separate axis from Clearing a column.
  • A free function taking &StructArray, not a method on RasterStructArray. This is pure column surgery; routing it through try_new would parse and validate all five columns and add a failure mode swap_crs_column never had.
  • RS_SetGeoReference drops copy_raster_from, building a List<Float64> transform column directly. Its bands are now provably untouched.

Two override vocabularies, deliberately

The ticket asked for one shared RasterOverrides. That can't work: RasterOverrides is a scalar — one value per call — while broadcast_string_view passes an N-row CRS array straight through when it's already the right length. RS_SetSRID(raster_col, srid_col) assigns a different CRS per row, and folding it onto a scalar struct would regress that to one CRS for the whole call.

So RasterOverrides stays the builder's language (a scalar applied while constructing a row), and the array path gets column-shaped overrides. They share the Override<T> trinary from #1158, which is the vocabulary that actually matters.

Also worth noting: the ticket assigns "extend RasterOverrides with a crs field and switch it to Override<T>" to DB-98, but #1158 only converted BandOverrides. That prerequisite is moot under this design — the array path doesn't use RasterOverrides at all.

Tests

with_column_overrides_shares_untouched_columns asserts Arc::ptr_eq on every carried-over column — the only assertion that distinguishes a shared column from an equal-looking rebuild. with_column_overrides_clear_and_null_merge pins Clear (nulls the column, keeps the raster) against input_nulls (nulls the raster).

I tried the same ptr_eq assertion end-to-end through ScalarUdfTester and removed it: the harness re-materializes the raster argument through DataFusion's extension-type plumbing, so it measures the harness rather than the contract.

sedona-raster            187 passed; 0 failed
sedona-raster-functions  257 passed; 0 failed
sedona-raster-zarr        66 passed; 0 failed
sedonadb                  10 passed; 0 failed

fmt and clippy clean; sedona-raster-gdal builds.

…etters

Closes DB-99.

`RS_SetCRS`, `RS_SetSRID` and `RS_SetGeoReference` change only top-level
metadata — one of five columns on the raster `StructArray`. The bands column,
which holds every pixel byte, is untouched by definition.

Two ways to produce that output existed, and the setters were split across
both:

* `swap_crs_column` (private to `rs_setsrid.rs`, hardcoded to the CRS column)
  replaced one column and carried the rest over as the same `Arc`. Correct,
  but unreachable from anywhere else.
* `RasterBuilder::copy_raster_from` walked every band and re-emitted every
  band field into a fresh builder. `RS_SetGeoReference` used this. The pixel
  buffers were shared by refcount, but all the band metadata was rebuilt to
  change one number — and the bands came out identical only if the rebuild
  was faithful. DB-506 is what happens when it isn't: that rebuild silently
  dropped band names.

Generalize the first into a shared `with_column_overrides` and move all three
setters onto it.

* `RasterColumnOverrides { crs, transform }` uses the `Override<T>` trinary
  from #1158. `Keep` leaves a column in place sharing its `Arc`, `Clear` nulls
  it, `Set(a)` installs a per-row array. `Override` rather than `Option`
  because `RS_SetSRID(raster, 0)` must *clear* the CRS while keeping the
  raster — distinct from leaving the column alone, which `Option` cannot
  express. That is why `swap_crs_column` was bespoke.
* The helper also absorbs the input-null merge that was duplicated inline at
  both `rs_setsrid.rs` call sites, including the length-1 broadcast for scalar
  arguments. A null *argument* nulls the whole raster row; that is a separate
  axis from `Clear`ing a column.
* It is a free function taking `&StructArray`, not a method on
  `RasterStructArray`, so it does not require parsing and validating every
  column for what is pure column surgery — and so introduces no failure mode
  `swap_crs_column` did not have.
* `RS_SetGeoReference` builds a `List<Float64>` transform column directly and
  drops `copy_raster_from`. Its bands are now provably untouched.

Note this keeps two override vocabularies on purpose. `RasterOverrides` stays
the *builder's* language — a scalar applied while a row is constructed. The
array path replaces whole columns and takes per-row values already in an
array. Forcing the setters onto the scalar struct would regress `RS_SetSRID`
from per-row CRS (`broadcast_string_view` passes an N-row array straight
through) to a single CRS per call. They share `Override<T>`, which is the
vocabulary that matters.

`with_column_overrides_shares_untouched_columns` asserts `Arc::ptr_eq` on
every carried-over column, which is the only assertion that distinguishes a
shared column from an equal-looking rebuild.
@james-willis
james-willis force-pushed the james/db-99-array-level-override branch from 0040157 to b170d19 Compare August 28, 2026 18:01
@github-actions
github-actions Bot requested a review from zhangfengcdt August 28, 2026 18:42
@james-willis
james-willis marked this pull request as ready for review August 28, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant