Skip to content

Fold a string cast of json_get to json_as_text - #146

Merged
adriangb merged 1 commit into
mainfrom
cast-to-text-uses-json-as-text
Sep 29, 2026
Merged

adriangb merged 1 commit into
mainfrom
cast-to-text-uses-json-as-text

Conversation

@adriangb

Copy link
Copy Markdown
Collaborator

CAST(json_get(j, 'k') AS VARCHAR), and so (j -> 'k')::text, is folded to json_get_str. That function returns NULL for every JSON value that is not a string:

select (j -> 'k')::text from (values ('{"k": 42}'), ('{"k": [1, 2]}'), ('{"k": "x"}')) t(j);
-- before: NULL, NULL, x
-- after:  42, [1, 2], x

Other engines (Postgres, DuckDB, ClickHouse, Spark) return text for a number, boolean, object or array cast to a string. (Postgres and DuckDB quote strings; this keeps the existing unquoted result.) With this change a cast to Utf8, Utf8View or LargeUtf8 folds to json_as_text, the function behind ->>:

  • A JSON string gives its value without quotes, as before.
  • A number, boolean, object or array gives its JSON text as written.
  • A JSON null or a missing key gives NULL, as before.

Only results that were NULL change. For example, json_get(j, 'k')::string = 'abc' is now false instead of NULL when k holds an array or object. Use json_get_str to read only JSON strings.

json_as_text has the same return type rules as json_get_str: Utf8, or Dictionary(Int64, Utf8) for a dictionary-encoded JSON argument. So the cast is dropped or kept in the same cases as before.

Tests cover each JSON value type, a nested path, each string target type, and plain, view, large and dictionary-encoded JSON input.

`CAST(json_get(j, 'k') AS VARCHAR)` folded to `json_get_str`, which is NULL
for any JSON value that is not a string. A cast to a string type now folds to
`json_as_text`, the function behind `->>`: a JSON string still gives its value
without quotes, a number, boolean, object or array now gives its JSON text,
and a JSON null or a missing key still gives NULL.

`json_as_text` has the same return type rules as `json_get_str`, so the cast is
dropped or kept in the same cases as before.
@codecov-commenter

codecov-commenter commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.95%. Comparing base (f4f0a6e) to head (949e58e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #146      +/-   ##
==========================================
- Coverage   94.00%   93.95%   -0.06%     
==========================================
  Files          18       18              
  Lines        1902     1902              
  Branches     1902     1902              
==========================================
- Hits         1788     1787       -1     
  Misses         64       64              
- Partials       50       51       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@adriangb
adriangb merged commit 39a94db into main Sep 29, 2026
7 checks passed
@adriangb
adriangb deleted the cast-to-text-uses-json-as-text branch September 29, 2026 00:47
This was referenced Sep 29, 2026
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.

2 participants