Proposal: ABSENT ON TRAILING NULL for JSON_ARRAY
Proposal: ABSENT ON TRAILING NULL for JSON_ARRAY
От:
Matias Kenig <matias.kenig@gmail.com>
Дата:
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
Re: Proposal: ABSENT ON TRAILING NULL for JSON_ARRAY
От:
Andreas Karlsson <andreas@proxel.se>
Дата:
On 11/22/26 17:11, Matias Kenig wrote: > 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). Hi, I do not really see a value for this, if you want to store sparse data you should pick another format where you store a list of position + value rather than values and nulls, e.g. [[0, 1], [2, 3]] in your example. Trimming away trailing nulls seems like an ugly hack which only gives modest savings on sparse workloads, and a hack I would not wish to see additional syntax in our parser for. Maybe it could be worth adding a jsonb_array_rtrim() function, but I am skeptical against even that. Best regards, -- Andreas Karlsson Percona