Hi all,
I'd like to propose a small extension to the SQL/JSON JSON_ARRAY constructor:
a new null-handling option that omits only the trailing NULL elements.
SELECT JSON_ARRAY(1, NULL, 3, NULL, NULL ABSENT ON TRAILING NULL);
-- [1, null, 3]
Today JSON_ARRAY offers NULL ON NULL and ABSENT ON NULL. ABSENT ON NULL drops
every NULL, which shifts the remaining elements and is unusable when the array
is positional (e.g. a row serialized as an array of column values). The
proposed clause would keep interior NULLs, so positions stay intact, and drop
only the trailing ones.
Benefits:
* Smaller payloads for wide, sparse, positional data: rows with many
trailing NULLs (or values equal to a column default) no longer carry the
padding.
* Consumers can apply a simple, well-known rule: a missing trailing cell
means "no value" or "use the column default". This mirrors conventions in
other languages, e.g. Java's String.split with limit = 0, which drops
trailing empty strings.
* It removes a common workaround: building the array as text and trimming
it with regexp_replace, which is awkward, slow, and easy to get wrong
(quoting, whitespace differences between json and jsonb output).
* It is backward compatible: it is a new optional clause and does not
change the behavior of any existing query.
Would there be interest in something like this? I'm happy to work on a patch
if the approach and the syntax seem reasonable, and I'm open to alternative
naming (e.g. TRIM TRAILING NULLS).
Thanks,
MatÃas