Concatenates strings with a separator, skipping null values.
Null values in any expression are skipped rather than propagating nulls. The separator is only inserted between non-null values. Returns null only if all inputs are null for that row.
Parameters:
| Name | Type | Description | Default |
sep | str | The separator string to place between values. | required |
*exprs | Expression | Two or more string expressions to concatenate. | () |
Returns:
| Name | Type | Description |
Expression | String Expression | An expression with the joined strings, or null if all inputs are null for that row. |
Examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | >>> import daft
>>> from daft import col, lit
>>> from daft.functions import concat_ws
>>>
>>> # Basic usage with a separator
>>> df = daft.from_pydict({"a": ["foo"], "b": ["bar"]})
>>> df.select(concat_ws(",", col("a"), col("b"))).collect()
>>>
>>> # Nulls are skipped, not propagated
>>> df = daft.from_pydict({"first": ["Alice", "Bob", None], "last": ["Smith", None, "Jones"]})
>>> df.select(concat_ws(" ", col("first"), col("last"))).collect()
>>>
>>> # All nulls returns null
>>> df = daft.from_pydict({"a": [None], "b": [None]})
>>> df.select(concat_ws(",", col("a"), col("b"))).collect()
>>>
>>> # Works with literals and columns
>>> df = daft.from_pydict({"name": ["alice", "bob"]})
>>> df.select(concat_ws("-", lit("my-prefix"), col("name"))).collect()
|
╭─────────╮
│ a │
│ --- │
│ String │
╞═════════╡
│ foo,bar │
╰─────────╯
(Showing first 1 of 1 rows)
╭─────────────╮
│ first │
│ --- │
│ String │
╞═════════════╡
│ Alice Smith │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ Bob │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ Jones │
╰─────────────╯
(Showing first 3 of 3 rows)
╭────────╮
│ a │
│ --- │
│ String │
╞════════╡
│ None │
╰────────╯
(Showing first 1 of 1 rows)
╭─────────────────╮
│ literal │
│ --- │
│ String │
╞═════════════════╡
│ my-prefix-alice │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ my-prefix-bob │
╰─────────────────╯
(Showing first 2 of 2 rows)
Source code in daft/functions/str.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 | def concat_ws(sep: str, *exprs: Expression) -> Expression:
"""Concatenates strings with a separator, skipping null values.
Null values in any expression are skipped rather than propagating nulls.
The separator is only inserted between non-null values. Returns null only
if all inputs are null for that row.
Args:
sep (str): The separator string to place between values.
*exprs (Expression): Two or more string expressions to concatenate.
Returns:
Expression (String Expression): An expression with the joined strings,
or null if all inputs are null for that row.
Examples:
>>> import daft
>>> from daft import col, lit
>>> from daft.functions import concat_ws
>>>
>>> # Basic usage with a separator
>>> df = daft.from_pydict({"a": ["foo"], "b": ["bar"]})
>>> df.select(concat_ws(",", col("a"), col("b"))).collect()
╭─────────╮
│ a │
│ --- │
│ String │
╞═════════╡
│ foo,bar │
╰─────────╯
<BLANKLINE>
(Showing first 1 of 1 rows)
>>>
>>> # Nulls are skipped, not propagated
>>> df = daft.from_pydict({"first": ["Alice", "Bob", None], "last": ["Smith", None, "Jones"]})
>>> df.select(concat_ws(" ", col("first"), col("last"))).collect()
╭─────────────╮
│ first │
│ --- │
│ String │
╞═════════════╡
│ Alice Smith │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ Bob │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ Jones │
╰─────────────╯
<BLANKLINE>
(Showing first 3 of 3 rows)
>>>
>>> # All nulls returns null
>>> df = daft.from_pydict({"a": [None], "b": [None]})
>>> df.select(concat_ws(",", col("a"), col("b"))).collect()
╭────────╮
│ a │
│ --- │
│ String │
╞════════╡
│ None │
╰────────╯
<BLANKLINE>
(Showing first 1 of 1 rows)
>>>
>>> # Works with literals and columns
>>> df = daft.from_pydict({"name": ["alice", "bob"]})
>>> df.select(concat_ws("-", lit("my-prefix"), col("name"))).collect()
╭─────────────────╮
│ literal │
│ --- │
│ String │
╞═════════════════╡
│ my-prefix-alice │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ my-prefix-bob │
╰─────────────────╯
<BLANKLINE>
(Showing first 2 of 2 rows)
"""
return Expression._call_builtin_scalar_fn("concat_ws", sep, *exprs)
|