Skip to content

daft.functions.concat_ws#

concat_ws #

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.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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)