Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/functions/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataType, FunctionDefinition } from '../interfaces-private';
import { DataType, FunctionDefinition, nil } from '../interfaces-private';

export const stringFunctions: FunctionDefinition[] = [
{
Expand Down Expand Up @@ -27,6 +27,11 @@ export const stringFunctions: FunctionDefinition[] = [
argsVariadic: DataType.text,
returns: DataType.text,
allowNullArguments: true,
implementation: (separator: string, ...x: string[]) => x?.join(separator),
implementation: (separator: string | nil, ...x: (string | nil)[]) => {
if (separator === null || separator === undefined) {
return null;
}
return x.filter(v => v !== null && v !== undefined).join(separator);
},
},
]
15 changes: 15 additions & 0 deletions src/tests/function-calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ describe('Functions', () => {
.toEqual([{ concat: 'text-123-end' }]);
});

it('concat_ws skips null arguments', () => {
expect(many(`select concat_ws(',', 'a', null, 'b') as v;`))
.toEqual([{ v: 'a,b' }]);
});

it('concat_ws returns null when the separator is null', () => {
expect(many(`select concat_ws(null, 'a', 'b') as v;`))
.toEqual([{ v: null }]);
});

it('concat_ws returns an empty string when every value is null', () => {
expect(many(`select concat_ws(',', null, null) as v;`))
.toEqual([{ v: '' }]);
});

it('GREATEST 2 arguments', () => {
expect(many(`select GREATEST(0, -1);`))
.toEqual([{ greatest: 0 }]);
Expand Down