zarr backend: non-ASCII variable names are percent-encoded in store keys

Aperta
#337 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
python, rust
Ambito
backend, data

Direzione di ricerca

Inizia da src/storage/zarr/common.rs, in particolare da create_arrays e dalle relative chiamate a ArrayBuilder, quindi esamina le conversioni dei percorsi descritte per zarrs_object_store e object_store. Riproduci il problema con il modello non ASCII fornito e confronta le chiavi raw dello store con dimension_names. Il lavoro è completato quando zarr e xarray leggono i nomi originali delle variabili e delle coordinate senza declassare la coordinata, mentre i nomi ASCII e quelli contenenti percentuali continuano a fare round-trip.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When sampling into a zarr_store, array keys are written percent-encoded while the
dimension_names metadata is not. Any model with a non-ASCII variable or coordinate name
therefore reads back under a different name than the model declared, and its dimension
coordinate is demoted to a data variable. The arrow backend is unaffected.

Reproduction

import pymc as pm, nutpie, tempfile
from nutpie import zarr_store

with pm.Model(coords={"abieluvõrdsus_outp": ["a", "b", "c"]}) as m:
    z = pm.Normal("abieluvõrdsus", dims="abieluvõrdsus_outp")
    pm.Normal("obs", z.sum(), 1.0, observed=1.0)

c = nutpie.compile_pymc_model(m, backend="numba")
arrow = nutpie.sample(c, draws=20, tune=20, chains=2, progress_bar=False)
zarr = nutpie.sample(c, draws=20, tune=20, chains=2, progress_bar=False,
                     zarr_store=zarr_store.LocalStore(tempfile.mkdtemp()))

print(sorted(map(str, arrow["posterior"].dataset.data_vars)))
print(sorted(map(str, zarr["posterior"].dataset.data_vars)))
['abieluvõrdsus']
['abieluv%C3%B5rdsus', 'abieluv%C3%B5rdsus_outp']

Two things went wrong. The variable is renamed, and the coordinate array — which should be a
dimension coordinate — is now a data variable, because its name no longer equals its dimension.

On disk the store contains /posterior/abieluv%C3%B5rdsus and
/posterior/abieluv%C3%B5rdsus_outp, while the arrays' dimension_names metadata still reads
abieluvõrdsus_outp. So the encoding is applied to the store key but not to the metadata that
has to agree with it.

Scope of the encoding: only non-ASCII bytes are affected — spaces, hyphens and parentheses come
through untouched (Euroopa Liit_outp is fine). A literal % is escaped as %25, so the
transformation is invertible with urllib.parse.unquote.

Impact

Downstream, pm.sample_posterior_predictive transposes every posterior data variable to
(chain, draw, ...), and the demoted coordinate has neither:

ValueError: Dimensions {'draw', 'chain'} do not exist.
Expected one or more of ('abieluvõrdsus_outp',)

The quieter failure is the one that worries me more: a non-ASCII variable name with no
coordinate sharing its name is silently renamed, with nothing raised at all. Every model in a
language with non-ASCII column names is affected — this surfaced across a set of Estonian and
Romanian survey models, where it broke four of six.

Where the encoding comes from

nuts-rs builds keys as plain UTF-8 strings — create_arrays in src/storage/zarr/common.rs
does format!("{}/{}", group_path, name) and hands that to ArrayBuilder::build. The encoding
is applied one layer down, in the object-store adapter:

  • zarrs_object_store 0.6.2, src/lib.rs:50 —
    fn key_to_path(key: &StoreKey) -> Path { Path::from(key.as_str()) }, called on every store
    operation.
  • object_store 0.13.2, src/path/parts.rs:106 — PathPart::from(&str) runs
    percent_encode(other, INVALID), which is what escapes the non-ASCII bytes.

dimension_names escapes this because it is JSON content inside the array metadata payload,
never a store key, so it never passes through key_to_path.

That also explains the asymmetry. object_store::path::Path decodes again via parts(), so a
Rust reader going through the same adapter round-trips consistently — but zarr-python reads the
raw keys and sees the encoded form. A zarr key is UTF-8 by spec, so pairing a writer that
applies object-store path semantics with a reader that doesn't is the actual defect; whether
that's best fixed in zarrs_object_store or by nuts-rs avoiding that adapter's encoding for
zarr keys is your call.

One caveat on the above: the zarrs crate itself wasn't available locally to inspect, so I
can't rule out that it normalizes a key before handing it to key_to_path. Everything else
here was read from the vendored sources.

Workaround

For anyone hitting this before it's fixed — decode the keys after reading and re-promote the
arrays whose name matches a dimension:

from urllib.parse import unquote

mangled = {n: unquote(str(n)) for n in ds.variables if "%" in str(n)}
mangled = {k: v for k, v in mangled.items() if v != k}
ds = ds.rename(mangled).set_coords([v for v in mangled.values() if v in ds.dims])

Versions: nutpie 0.16.10, pymc 6.0.1, zarr 3.1.5, xarray 2026.2.0, numba backend, Linux.
Happy to test a fix against the real models that surfaced it.

Lingua principale
Python
Stelle
214
Fork
30
Merge medio
3h 50m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pymc-devs/nutpie

Tutte le issue di pymc-devs/nutpie

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.