Skip to content

Commit

Permalink
fix: add workaround for NULL map bugs
Browse files Browse the repository at this point in the history
partially addresses ibis-project#8632
  • Loading branch information
NickCrews committed Mar 13, 2024
1 parent 42e99cf commit aaf1582
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ibis/backends/tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
]


@pytest.mark.parametrize(
"kv",
[
param(([1, 2], None), id="null_values"),
param((None, [1, 2]), id="null_keys"),
param((None, None), id="null_both"),
],
)
def test_map_nulls(con, kv):
k, v = kv
k = ibis.literal(k, type="array<int64>")
v = ibis.literal(v, type="array<int64>")
m = ibis.map(k, v)
assert con.execute(m) is None


@pytest.mark.notimpl(["pandas", "dask"])
def test_map_table(backend):
table = backend.map
Expand Down
17 changes: 16 additions & 1 deletion ibis/expr/types/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

from public import public

import ibis
import ibis.expr.operations as ops
import ibis.expr.types as ir
from ibis.common.deferred import deferrable
from ibis.expr import datatypes as dt
from ibis.expr.types.generic import Column, Scalar, Value

if TYPE_CHECKING:
Expand Down Expand Up @@ -488,4 +490,17 @@ def map(
return keys
if values is None:
keys, values = tuple(keys.keys()), tuple(keys.values())
return ops.Map(keys, values).to_expr()

# workaround for https://github.com/ibis-project/ibis/issues/8632
key_array = ibis.array(keys)
value_array = ibis.array(values)
regular = ops.Map(key_array, value_array).to_expr()
either_null = key_array.isnull() | value_array.isnull()
null = ibis.literal(
None,
type=dt.Map(
key_type=key_array.type().value_type,
value_type=value_array.type().value_type,
),
)
return either_null.ifelse(null, regular)
2 changes: 2 additions & 0 deletions ibis/expr/types/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def struct(
import ibis.expr.operations as ops

if isinstance(value, StructValue):
if type is not None:
return value.cast(type)
return value

fields = dict(value)
Expand Down

0 comments on commit aaf1582

Please sign in to comment.