Skip to content

daft.functions.encode#

encode #

encode(expr: Expression, charset: ENCODING_CHARSET) -> Expression

Encode binary or string values using the specified character set.

If an invalid encoding is encountered, an error will be raised. To handle invalid encodings, use try_encode instead.

Parameters:

Name Type Description Default
expr Binary or String Expression

The expression to encode.

required
charset str

The encoding character set (utf-8, base64).

required

Returns:

Name Type Description
Expression Binary Expression

A binary expression with the encoded value.

Note

This inputs either a string or binary and returns a binary. If the input value is a string and 'utf-8' is the character set, then it's just a cast to binary. If the input value is a binary and 'utf-8' is the character set, we verify the bytes are valid utf-8.

Source code in daft/functions/binary.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def encode(expr: Expression, charset: ENCODING_CHARSET) -> Expression:
    """Encode binary or string values using the specified character set.

    If an invalid encoding is encountered, an error will be raised.
    To handle invalid encodings, use `try_encode` instead.

    Args:
        expr (Binary or String Expression): The expression to encode.
        charset (str): The encoding character set (utf-8, base64).

    Returns:
        Expression (Binary Expression): A binary expression with the encoded value.

    Note:
        This inputs either a string or binary and returns a binary.
        If the input value is a string and 'utf-8' is the character set, then it's just a cast to binary.
        If the input value is a binary and 'utf-8' is the character set, we verify the bytes are valid utf-8.
    """
    return Expression._call_builtin_scalar_fn("encode", expr, codec=charset)