Skip to content

Commit ccd9359

Browse files
authored
fix(api): deal with null literals in Struct.__getitem__ (#11299)
1 parent 116e336 commit ccd9359

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

ibis/expr/rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _promote_integral_binop(exprs, op):
9292
bounds, dtypes = [], []
9393
for arg in exprs:
9494
dtypes.append(arg.dtype)
95-
if isinstance(arg, ops.Literal):
95+
if isinstance(arg, ops.Literal) and arg.value is not None:
9696
bounds.append([arg.value])
9797
else:
9898
bounds.append(arg.dtype.bounds)

ibis/expr/types/structs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ def __getitem__(self, name: str) -> ir.Value:
215215
return op.values[op.names.index(name)].to_expr()
216216
# and then do the same if the underlying value is a field access
217217
elif isinstance(op, ops.Literal):
218-
return ops.Literal(op.value[name], dtype=self.fields[name]).to_expr()
218+
return ops.Literal(
219+
op.value[name] if op.value is not None else None,
220+
dtype=self.fields[name],
221+
).to_expr()
219222
else:
220223
return ops.StructField(self, name).to_expr()
221224

ibis/tests/expr/test_struct.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def test_struct_getattr():
4343
expr.bad # # noqa: B018
4444

4545

46+
def test_null_literal_getitem():
47+
expr = ibis.literal(None, type="struct<a: int64, b: string>")
48+
field = expr["a"]
49+
assert isinstance(field, ir.IntegerValue)
50+
assert isinstance(field.op(), ops.Literal)
51+
assert field.op().value is None
52+
53+
4654
def test_struct_tab_completion():
4755
t = ibis.table([("struct_col", "struct<my_field: string, for: int64>")])
4856
# Only valid python identifiers in getattr completions

ibis/tests/expr/test_value_exprs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,14 @@ def test_literal_promotions(table, op, name, case, ex_type):
793793
assert result.type() == dt.dtype(ex_type)
794794

795795

796+
def test_null_literal_promotions():
797+
added = ibis.literal(12, "int8") + ibis.literal(None, type="int16")
798+
assert added.type() == dt.int32
799+
800+
added = ibis.null("int8") + ibis.literal(None, type="int16")
801+
assert added.type() == dt.int32
802+
803+
796804
@pytest.mark.parametrize(
797805
("op", "left_fn", "right_fn", "ex_type"),
798806
[

0 commit comments

Comments
 (0)