Skip to content

Commit

Permalink
fix: handle empty values in names in pivot operation
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Nov 23, 2024
1 parent 7955dda commit 3e68077
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions insights/insights/doctype/insights_data_source_v3/ibis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,22 +379,6 @@ def apply_pivot(self, pivot_args, pivot_type):
values = [self.translate_measure(measure) for measure in pivot_args["values"]]

if pivot_type == "wider":
other_columns = [
self.translate_dimension(dim).get_name()
for dim in pivot_args["columns"]
if not self.is_date_type(dim.data_type)
]
if other_columns:
names = self.query.select(other_columns).distinct().limit(10).execute()
self.query = self.query.filter(
ibis.or_(
*[
getattr(self.query, col).isin(names[col])
for col in other_columns
]
)
)

self.query = self.query.group_by(*rows, *columns).aggregate(
**{value.get_name(): value for value in values}
)
Expand All @@ -409,10 +393,20 @@ def apply_pivot(self, pivot_args, pivot_type):
{dimension: "string" for dimension in date_dimensions}
)

names_from = [col.get_name() for col in columns]
names = (
self.query.select(names_from)
.order_by(names_from)
.distinct()
.limit(10)
.execute()
)
names = names.fillna("null").values

return self.query.pivot_wider(
id_cols=[row.get_name() for row in rows],
names_from=[col.get_name() for col in columns],
names_sort=True,
names_from=names_from,
names=names,
values_from=[value.get_name() for value in values],
values_agg="sum",
)
Expand Down

0 comments on commit 3e68077

Please sign in to comment.