Skip to content

feat(#1458): SparkAdapter Codec Protocol#1472

Merged
dimitri-yatsenko merged 2 commits into
masterfrom
feat/1458-renderable-protocol
Jul 1, 2026
Merged

feat(#1458): SparkAdapter Codec Protocol#1472
dimitri-yatsenko merged 2 commits into
masterfrom
feat/1458-renderable-protocol

Conversation

@dimitri-yatsenko

Copy link
Copy Markdown
Member

Summary

Implements T3.2 of the 2.3 release plan, against the spec in datajoint-docs#188.

A runtime-checkable Protocol that codecs opt into by implementing `render_spark(self, decoded, *, key=None) -> Any`. Consumers (e.g., a Databricks silver-layer publish pipeline) detect support via `isinstance(codec, Renderable)`.

Closes #1458. Supersedes #1457. Slated for DataJoint 2.3.

What's added

Component File
`Renderable` Protocol declaration `src/datajoint/rendering.py` (new, ~85 lines including docstrings)
Top-level re-export as `dj.Renderable` `src/datajoint/init.py`
9 unit tests `tests/unit/test_rendering.py` (new)

The Protocol surface is intentionally tiny — a single method. The bulk of `rendering.py` is docstring (module-level rationale + class docstring documenting allowed return-value shapes with worked codec examples).

Tests

  • 9/9 pass on the new file.
  • Coverage: opt-in detection, non-opt-in rejection, top-level re-export, `@runtime_checkable` guarantee, built-in `<blob@>` and `<hash@>` codecs are NOT Renderable (per spec contract), invocation pass-through, `key` kwarg acceptance, subclass opt-in behavior.

What's NOT in this PR (out of scope per spec)

  • Specific renderable codec implementations. `<float_array@>`, `<image_2d@>`, `<labeled_struct@>`, `<timeseries@>`, etc. ship downstream as plugins. They register via existing codec auto-registration and opt in by implementing `render_spark()`.
  • Silver-layer publish pipeline. Lives in `datajoint-databricks`. This PR provides the eligibility-check contract; the pipeline consumes it.
  • No `decode_spark` (reverse direction). Delta consumers query rendered columns directly via Spark SQL.
  • No `BINARY` fallback. Codecs either implement `Renderable` or remain non-eligible — no automatic blob → bytes-passthrough.

Sequencing

Independent of the provenance trinity (T2.2.a–c, in flight) — no code overlap. Lands in parallel.

Once this PR merges, the matching docs spec PR (datajoint-docs#188) flips from draft to ready for review.

Test plan

  • 9/9 new unit tests pass
  • CI green (lint, test matrix, unit-tests)
  • `isinstance(codec, dj.Renderable)` returns True for a plugin codec that adds the method (smoke-test against a sample plugin once one exists)

Implements T3.2 of the 2.3 release plan against the spec in
datajoint-docs#188.

A runtime-checkable Protocol that codecs opt into by implementing
``render_spark(self, decoded, *, key=None) -> Any``. Consumers (e.g.,
a Databricks silver-layer publish pipeline) detect support via
``isinstance(codec, Renderable)``.

What's added:

- src/datajoint/rendering.py (new, ~85 lines including docstrings):
  Single @runtime_checkable Protocol declaration. Module-level
  docstring explains the design rationale (Protocol vs. abstract method
  on Codec); class docstring documents allowed return-value shapes
  (primitives / lists / dicts mapping to Spark ArrayType / StructType /
  MapType), with worked codec examples.

- src/datajoint/__init__.py: ``dj.Renderable`` exported at the top
  level alongside the existing Codec API exports.

- tests/unit/test_rendering.py (new, 9 tests): detection of opt-in vs
  non-opt-in classes, top-level re-export, @runtime_checkable guarantee,
  built-in <blob@> and <hash@> codecs are not Renderable (per spec
  contract), invocation pass-through, key kwarg acceptance, subclass
  opt-in behavior.

What's NOT in this PR (out of scope per spec):

- Specific renderable codec implementations. Codecs like <float_array@>,
  <image_2d@>, <labeled_struct@>, <timeseries@> ship downstream as
  plugins. They register via the existing codec auto-registration and
  opt in by implementing render_spark().
- Silver-layer publish pipeline (lives in datajoint-databricks).
- No decode_spark (reverse direction).
- No BINARY fallback — codecs either implement Renderable or remain
  non-eligible.

All 9 unit tests pass. No regressions expected — this is purely additive
(a new module + one top-level re-export + tests).

Slated for DataJoint 2.3.
Renderable conflicts with the broader notion of graphically renderable
field types and is too generic for an interface targeted specifically at
Spark / Lakehouse Sync. Rename for clarity:

- Class: Renderable → SparkAdapter (parallels StorageAdapter)
- Method: render_spark → to_spark (matches pandas/Arrow conventions like
  to_pandas, to_arrow, __dataframe__)
- Module: datajoint.rendering → datajoint.spark
- Tests: tests/unit/test_rendering.py → tests/unit/test_spark.py
- Top-level export: dj.Renderable → dj.SparkAdapter
@dimitri-yatsenko dimitri-yatsenko changed the title feat(#1458): Renderable Codec Protocol feat(#1458): SparkAdapter Codec Protocol Jun 26, 2026

@ttngu207 ttngu207 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, approving.

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified ae8cdf1 end-to-end. Small, well-scoped PR — Thinh's LGTM holds.

Verified ✅

  • Protocol shape. @runtime_checkable Protocol with a single method to_spark(self, decoded, *, key=None) in src/datajoint/spark.py. Structural opt-in; no inheritance forced on existing codecs. Right choice for an eligibility contract.
  • Signature consistency. key: dict | None = None keyword-only, matching Codec.encode / Codec.decode conventions. Return type Any with shape constraints deferred to the docstring.
  • Built-in exemption. BlobCodec and HashCodec explicitly NOT SparkAdapter — enforced by test_blob_codec_is_not_spark_adapter and test_hash_codec_is_not_spark_adapter. Prevents accidental eligibility of opaque-payload codecs.
  • Top-level export. dj.SparkAdapter is SparkAdapter via src/datajoint/__init__.py. Consistent with dj.StorageAdapter, dj.Codec patterns.
  • 9 tests, all substantive. Opt-in detection, non-opt-in rejection, top-level re-export, @runtime_checkable guard, blob/hash non-eligibility, invocation pass-through, key kwarg, subclass opt-in. Covers the Protocol's public contract.

Observations

  1. PR description is stale after the rename. Still references Renderable, render_spark, rendering.py, test_rendering.py. Code renamed to SparkAdapter / to_spark / spark.py / test_spark.py in ae8cdf1. Description text should follow.
  2. Docstring links to datajoint-docs/src/reference/specs/spark-adapter.md (lines 21–23 of spark.py). That spec is in-flight as datajoint-docs#188 per the PR body — worth confirming the link resolves once #188 lands, and/or adding "(forthcoming)" until it does.
  3. @runtime_checkable structural check verifies method existence, not signature. A plugin codec with def to_spark(self): (missing decoded) would still pass isinstance(..., SparkAdapter) and fail at call time. Standard Python limitation; a docstring line warning plugin authors to match the signature exactly would help.
  4. key=None explicit invocation not tested. All test calls pass key={"some_pk": 1} or no key kwarg via positional. A codec.to_spark(x, key=None) case would round out the signature check. Minor.

Bottom line

Approving. Cleanly scoped Protocol declaration with sensible naming (SparkAdapter parallels StorageAdapter; to_spark parallels to_pandas/to_arrow). Ready to merge once the stale PR-description text is refreshed.

@dimitri-yatsenko dimitri-yatsenko merged commit ec77b47 into master Jul 1, 2026
7 checks passed
@dimitri-yatsenko dimitri-yatsenko deleted the feat/1458-renderable-protocol branch July 1, 2026 17:05
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.

Add Renderable Protocol for codec-driven Spark-native rendering

3 participants