Skip to content

Commit

Permalink
Added ability for constant literals in DataChain.mutate(...) (#869)
Browse files Browse the repository at this point in the history
Added ability for constant literals in `DataChain.mutate(...)`
  • Loading branch information
ilongin authored Jan 28, 2025
1 parent c0f23b4 commit 7332322
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/datachain/lib/dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from sqlalchemy.sql.sqltypes import NullType

from datachain.dataset import DatasetRecord
from datachain.func import literal
from datachain.func.base import Function
from datachain.func.func import Func
from datachain.lib.convert.python_to_sql import python_to_sql
Expand Down Expand Up @@ -1129,8 +1130,12 @@ def mutate(self, **kwargs) -> "Self":
)
```
"""
primitives = (bool, str, int, float)

for col_name, expr in kwargs.items():
if not isinstance(expr, (Column, Func)) and isinstance(expr.type, NullType):
if not isinstance(expr, (*primitives, Column, Func)) and isinstance(
expr.type, NullType
):
raise DataChainColumnError(
col_name, f"Cannot infer type with expression {expr}"
)
Expand All @@ -1145,6 +1150,11 @@ def mutate(self, **kwargs) -> "Self":
elif isinstance(value, Func):
# adding new signal
mutated[name] = value.get_column(schema)
elif isinstance(value, primitives):
# adding simple python constant primitives like str, int, float, bool
val = literal(value)
val.type = python_to_sql(type(value))()
mutated[name] = val # type: ignore[assignment]
else:
# adding new signal
mutated[name] = value
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ def test_lt_mutate(dc):
assert list(res) == [0, 0, 0, 0, 0]


@pytest.mark.parametrize("value", [1, 0.5, "a", True])
def test_mutate_with_literal(dc, value):
res = dc.mutate(test=value).collect("test")
assert list(res) == [value] * 5


def test_le():
rnd1, rnd2 = rand(), rand()

Expand Down

0 comments on commit 7332322

Please sign in to comment.