Skip to content

daft.functions.compress#

compress #

compress(expr: Expression, codec: COMPRESSION_CODEC) -> Expression

Compress binary or string values using the specified codec.

Parameters:

Name Type Description Default
expr String | Binary Expression

The expression to compress.

required

Returns:

Name Type Description
Expression Binary Expression

A binary expression with the compressed value.

Examples:

1
2
3
4
>>> import daft
>>> from daft.functions import compress
>>> df = daft.from_pydict({"text": [b"hello, world!"]})  # binary
>>> df.select(compress(df["text"], "zlib")).show()
╭────────────────────────────────╮
│ text                           │
│ ---                            │
│ Binary                         │
╞════════════════════════════════╡
│ b"x\x9c\xcbH\xcd\xc9\xc9\xd7Q… │
╰────────────────────────────────╯
(Showing first 1 of 1 rows)
1
2
>>> df = daft.from_pydict({"text": ["hello, world!"]})  # string
>>> df.select(compress(df["text"], "zlib")).show()
╭────────────────────────────────╮
│ text                           │
│ ---                            │
│ Binary                         │
╞════════════════════════════════╡
│ b"x\x9c\xcbH\xcd\xc9\xc9\xd7Q… │
╰────────────────────────────────╯
(Showing first 1 of 1 rows)
Source code in daft/functions/binary.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def compress(expr: Expression, codec: COMPRESSION_CODEC) -> Expression:
    r"""Compress binary or string values using the specified codec.

    Args:
        expr (String | Binary Expression): The expression to compress.
        codec (str) The compression codec (deflate, gzip, or zlib)

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

    Examples:
        >>> import daft
        >>> from daft.functions import compress
        >>> df = daft.from_pydict({"text": [b"hello, world!"]})  # binary
        >>> df.select(compress(df["text"], "zlib")).show()
        ╭────────────────────────────────╮
        │ text                           │
        │ ---                            │
        │ Binary                         │
        ╞════════════════════════════════╡
        │ b"x\x9c\xcbH\xcd\xc9\xc9\xd7Q… │
        ╰────────────────────────────────╯
        <BLANKLINE>
        (Showing first 1 of 1 rows)

        >>> df = daft.from_pydict({"text": ["hello, world!"]})  # string
        >>> df.select(compress(df["text"], "zlib")).show()
        ╭────────────────────────────────╮
        │ text                           │
        │ ---                            │
        │ Binary                         │
        ╞════════════════════════════════╡
        │ b"x\x9c\xcbH\xcd\xc9\xc9\xd7Q… │
        ╰────────────────────────────────╯
        <BLANKLINE>
        (Showing first 1 of 1 rows)

    """
    return Expression._call_builtin_scalar_fn("encode", expr, codec=codec)