Add agtype <-> jsonb bidirectional casts (#350) by gregfelice · Pull Request #2361 · apache/age
Register explicit casts between agtype and jsonb, enabling:
SELECT properties(n)::jsonb FROM cypher(...) AS (n agtype);
SELECT '{"key": "value"}'::jsonb::agtype;
-- Use jsonb operators on graph data:
SELECT (props::jsonb)->>'name' FROM cypher(...) AS (props agtype);
Implementation uses SQL language functions that go through proven
text-intermediate paths:
agtype -> jsonb: agtype_to_json() -> json::jsonb
jsonb -> agtype: jsonb::text -> text::agtype
This approach is safe because agtype extends jsonb's binary format
with types (AGTV_INTEGER, AGTV_FLOAT, AGTV_VERTEX, AGTV_EDGE,
AGTV_PATH) that jsonb does not recognize, making direct binary
conversion unreliable. The text roundtrip handles all value types
correctly including graph types (vertex/edge properties are
extracted as JSON objects).
All 31 regression tests pass.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…asts - Fix comment in agtype_coercions.sql: document json-intermediate path (agtype_to_json -> json::jsonb) instead of incorrect "text intermediate" - Add agtype_jsonb_cast regression test covering: string/null/array/object agtype->jsonb, all jsonb scalar types->agtype, roundtrips, vertex/edge ->jsonb with structural key checks, NULL handling - Register agtype_jsonb_cast in Makefile REGRESS list Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters