Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6283,3 +6283,11 @@ def test_agg_with_dict_containing_non_existing_col_raise_key_error(scalars_dfs):

with pytest.raises(KeyError):
bf_df.agg(agg_funcs)


def test_dataframe_count_empty_selection_succeeds(session):
# Tests that aggregate ops on empty selections don't trigger invalid empty SELECT syntax
df = session.read_gbq("SELECT 1 AS int_col")
empty_df = df[[]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively: empty_df = bpd.DataFrame()

count_series = empty_df.count().to_pandas()
assert len(count_series) == 0
Original file line number Diff line number Diff line change
Expand Up @@ -1394,9 +1394,17 @@ def _generate_groups(groups):
return map(sge.convert, range(1, len(groups) + 1))

def visit_Aggregate(self, op, *, parent, groups, metrics):
sel = sg.select(
*self._cleanup_names(groups), *self._cleanup_names(metrics), copy=False
).from_(parent, copy=False)
exprs = []
if groups:
exprs.extend(self._cleanup_names(groups))
if metrics:
exprs.extend(self._cleanup_names(metrics))

if not exprs:
# Empty aggregated projections are invalid in BigQuery
exprs = [sge.Literal.number(1)]

sel = sg.select(*exprs, copy=False).from_(parent, copy=False)

if groups:
sel = sel.group_by(*self._generate_groups(groups.values()), copy=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ def visit_TimestampFromUNIX(self, op, *, arg, unit):

def visit_Cast(self, op, *, arg, to):
from_ = op.arg.dtype
if to.is_null():
return sge.Null()
if arg is NULL or (
isinstance(arg, sge.Cast)
and getattr(arg, "to", None) is not None
and str(arg.to).upper() == "NULL"
):
if to.is_struct() or to.is_array():
return sge.Cast(this=NULL, to=self.type_mapper.from_ibis(to))
if from_.is_timestamp() and to.is_integer():
return self.f.unix_micros(arg)
elif from_.is_integer() and to.is_timestamp():
Expand Down
Loading