Skip to content

Commit

Permalink
feat(api): add ops.StructColumn operation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jul 19, 2022
1 parent a216c18 commit 020bfdb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions ibis/expr/operations/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,23 @@ def has_resolved_name(self):
return True


@public
class StructColumn(Value):
names = rlz.tuple_of(rlz.instance_of(str), min_length=1)
values = rlz.tuple_of(rlz.any, min_length=1)

output_shape = rlz.Shape.COLUMNAR

@immutable_property
def output_dtype(self):
return dt.Struct.from_tuples(
zip(self.names, (value.type() for value in self.values))
)

def root_tables(self):
return distinct_roots(*self.values)


@public
class DecimalPrecision(Unary):
arg = rlz.decimal
Expand Down
10 changes: 9 additions & 1 deletion ibis/expr/types/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ def struct(
Create a struct literal from a [`dict`][dict] with a specified type
>>> t = ibis.struct(dict(a=1, b='foo'), type='struct<a: float, b: string>')
"""
return literal(collections.OrderedDict(value), type=type)
import ibis.expr.operations as ops

items = dict(value)
values = items.values()
if any(isinstance(value, Value) for value in values):
return ops.StructColumn(
names=tuple(items.keys()), values=tuple(values)
).to_expr()
return literal(collections.OrderedDict(items), type=type)


@public
Expand Down

0 comments on commit 020bfdb

Please sign in to comment.