Skip to content

Commit

Permalink
feat(api): allow mixing literals and columns in ibis.array
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and kszucs committed Jul 17, 2023
1 parent c643fca commit 3355dd8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
14 changes: 2 additions & 12 deletions ibis/expr/operations/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,13 @@

@public
class ArrayColumn(Value):
cols = rlz.tuple_of(rlz.column(rlz.any), min_length=1)
cols = rlz.tuple_of(rlz.any, min_length=1)

output_shape = rlz.Shape.COLUMNAR

def __init__(self, cols):
unique_dtypes = {col.output_dtype for col in cols}
if len(unique_dtypes) > 1:
raise com.IbisTypeError(
f'The types of all input columns must match exactly in a '
f'{type(self).__name__} operation.'
)
super().__init__(cols=cols)

@attribute.default
def output_dtype(self):
first_dtype = self.cols[0].output_dtype
return dt.Array(first_dtype)
return dt.Array(rlz.highest_precedence_dtype(self.cols))


@public
Expand Down
23 changes: 12 additions & 11 deletions ibis/expr/types/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,20 +882,21 @@ def array(values: Iterable[V], type: str | dt.DataType | None = None) -> ArrayVa
>>> ibis.array([1.0, 2.0, 3.0])
[1.0, 2.0, 3.0]
Mixing scalar and column expressions is not allowed
Mixing scalar and column expressions is allowed
>>> ibis.array([t.a, 1.0])
Traceback (most recent call last):
...
ibis.common.exceptions.IbisTypeError: To create an array column using `array`, all input values must be column expressions.
>>> ibis.array([t.a, 42])
┏━━━━━━━━━━━━━━━━━━━━━━┓
┃ ArrayColumn() ┃
┡━━━━━━━━━━━━━━━━━━━━━━┩
│ array<int64> │
├──────────────────────┤
│ [1, 42] │
│ [2, 42] │
│ [3, 42] │
└──────────────────────┘
"""
if all(isinstance(value, Column) for value in values):
if any(isinstance(value, Column) for value in values):
return ops.ArrayColumn(values).to_expr()
elif any(isinstance(value, Column) for value in values):
raise com.IbisTypeError(
'To create an array column using `array`, all input values must '
'be column expressions.'
)
else:
try:
return literal(list(values), type=type)
Expand Down

0 comments on commit 3355dd8

Please sign in to comment.