gregfelice opened a new pull request, #2361:
URL: https://github.com/apache/age/pull/2361

   ## Summary
   
   Adds explicit casts between `agtype` and `jsonb` (issue #350, open since 
2022):
   
   ```sql
   -- agtype -> jsonb: use jsonb operators on graph data
   SELECT properties(n)::jsonb FROM cypher('g', $$ MATCH (n) RETURN n $$) AS (n 
agtype);
   SELECT (props::jsonb)->>'name' FROM cypher('g', $$ MATCH (n) RETURN 
properties(n) $$) AS (props agtype);
   
   -- jsonb -> agtype: pass jsonb data into Cypher context
   SELECT '{"key": "value"}'::jsonb::agtype;
   
   -- Roundtrip
   SELECT ('{"x": 1}'::agtype::jsonb)::agtype;
   
   -- Vertex/edge → jsonb (properties extracted as JSON object)
   SELECT n::jsonb FROM cypher('g', $$ MATCH (n:Person) RETURN n $$) AS (n 
agtype);
   ```
   
   ## Implementation
   
   SQL language functions using proven text-intermediate paths:
   - `agtype → jsonb`: `agtype_to_json($1)::jsonb`
   - `jsonb → agtype`: `$1::text::agtype`
   
   **Why text intermediate?** agtype extends jsonb's binary format with types 
(`AGTV_INTEGER`, `AGTV_FLOAT`, `AGTV_VERTEX`, `AGTV_EDGE`, `AGTV_PATH`) that 
jsonb does not recognize. Direct binary cast causes `"unknown type of jsonb 
container"` errors. The text roundtrip correctly handles all value types — 
graph types are serialized as JSON objects with their properties.
   
   **Files changed:** `sql/agtype_coercions.sql` only (27 lines added). No C 
code changes.
   
   ## Test plan
   - [x] Object with integers: `'{"name": "Alice", "age": 30}'::agtype::jsonb`
   - [x] Arrays: `'[1, 2, 3]'::agtype::jsonb`
   - [x] Strings: `'"hello"'::agtype::jsonb`
   - [x] Nested objects: `'{"a": {"b": [1, true, null]}}'::agtype::jsonb`
   - [x] jsonb → agtype: `'{"key": "value"}'::jsonb::agtype`
   - [x] Roundtrip: `('{"x": 1}'::agtype::jsonb)::agtype`
   - [x] Vertex → jsonb: graph type converted to JSON object
   - [x] properties() → jsonb: direct property extraction
   - [x] JSONB operators on agtype data: `(props::jsonb)->>'name'`
   - [x] All 31 existing regression tests pass
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to