Skip to content

fix: concat_ws should skip null values and honour a null separator - #479

Open
spokodev wants to merge 1 commit into
oguimbal:masterfrom
spokodev:fix/concat-ws-null-args
Open

fix: concat_ws should skip null values and honour a null separator#479
spokodev wants to merge 1 commit into
oguimbal:masterfrom
spokodev:fix/concat-ws-null-args

Conversation

@spokodev

@spokodev spokodev commented Aug 3, 2026

Copy link
Copy Markdown

Problem

concat_ws mishandles nulls three ways:

select concat_ws(',', 'a', null, 'b');  -- pg-mem: 'a,,b'
select concat_ws(null, 'a', 'b');       -- pg-mem: 'anullb'
select concat_ws(',', null, null);      -- pg-mem: ','

Postgres skips null value arguments, returns null when the separator is null, and returns an empty string when every value argument is null:

select concat_ws(',', 'a', null, 'b');  -- postgres: 'a,b'
select concat_ws(null, 'a', 'b');       -- postgres: null
select concat_ws(',', null, null);      -- postgres: ''

Ref: Postgres string functions (concat_ws)

Cause

The implementation was (separator, ...x) => x?.join(separator), which turns each null value into an empty field (the separator is still emitted), coerces a null separator to the string "null", and can never return null.

Fix

Return null when the separator is null, and skip null values before joining (an all-null value list yields an empty string). Unit tests added in function-calls.spec.ts.

concat_ws joined null values as empty fields (keeping the separator),
coerced a null separator to the text "null", and never returned null.
Postgres skips null value arguments, returns null when the separator is
null, and returns an empty string when every value is null.

  select concat_ws(',', 'a', null, 'b');  -- was 'a,,b', postgres 'a,b'
  select concat_ws(null, 'a', 'b');        -- was 'anullb', postgres null
  select concat_ws(',', null, null);       -- was ',', postgres ''
@spokodev
spokodev force-pushed the fix/concat-ws-null-args branch from e18cdb1 to 75de9a3 Compare August 3, 2026 20:23
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.

1 participant