Skip to content

Commit

Permalink
feat(clickhouse): table unnest
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jun 24, 2024
1 parent 63556bd commit ede6499
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions ibis/backends/clickhouse/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,58 @@ def visit_RandomUUID(self, op, **kwargs):
@staticmethod
def _generate_groups(groups):
return groups

def visit_TableUnnest(
self, op, *, parent, column, offset: str | None, keep_empty: bool
):
quoted = self.quoted

# tmpname = sg.to_identifier(util.gen_name("table_unnest"), quoted=quoted)
column_alias = sg.to_identifier(
util.gen_name("table_unnest_column"), quoted=quoted
)

expression = column

replace = sge.Column(
this=sge.Star(replace=[column_alias.as_(op.column.name)]),
table=column.args["table"],
)

if offset is not None:
offset = sg.to_identifier(offset, quoted=quoted)
expression = self.f.arrayZip(
expression, self.f.range(self.f.len(expression))
)
replace = sge.Column(
this=sge.Star(
replace=[
sge.Dot(this=column_alias, expression=sge.Var(this="1")).as_(
op.column.name, quoted=quoted
)
]
),
table=column.args["table"],
)
expr = sg.select(
replace,
sge.Dot(this=column_alias, expression=sge.Var(this="2")).as_(offset),
)
else:
replace = sge.Column(
this=sge.Star(
replace=[column_alias.as_(op.column.name, quoted=quoted)]
),
table=column.args["table"],
)
expr = sg.select(replace)

# unnest = sge.Unnest(expressions=[expression], alias=alias)
res = expr.from_(parent).join(
sge.Join(
this=expression.as_(column_alias),
kind="ARRAY",
side=None if not keep_empty else "LEFT",
)
)
return res

0 comments on commit ede6499

Please sign in to comment.