Skip to content
Merged
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
24 changes: 24 additions & 0 deletions bigframes/core/compile/sqlglot/aggregations/unary_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def _(
return apply_window_if_present(sge.func("COUNT", column.expr), window)


@UNARY_OP_REGISTRATION.register(agg_ops.DenseRankOp)
def _(
op: agg_ops.DenseRankOp,
column: typed_expr.TypedExpr,
window: typing.Optional[window_spec.WindowSpec] = None,
) -> sge.Expression:
# Ranking functions do not support window framing clauses.
return apply_window_if_present(
sge.func("DENSE_RANK"), window, include_framing_clauses=False
)


@UNARY_OP_REGISTRATION.register(agg_ops.MaxOp)
def _(
op: agg_ops.MaxOp,
Expand Down Expand Up @@ -106,6 +118,18 @@ def _(
return apply_window_if_present(sge.func("COUNT", sge.convert(1)), window)


@UNARY_OP_REGISTRATION.register(agg_ops.RankOp)
def _(
op: agg_ops.RankOp,
column: typed_expr.TypedExpr,
window: typing.Optional[window_spec.WindowSpec] = None,
) -> sge.Expression:
# Ranking functions do not support window framing clauses.
return apply_window_if_present(
sge.func("RANK"), window, include_framing_clauses=False
)


@UNARY_OP_REGISTRATION.register(agg_ops.SumOp)
def _(
op: agg_ops.SumOp,
Expand Down
4 changes: 4 additions & 0 deletions bigframes/core/compile/sqlglot/aggregations/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
def apply_window_if_present(
value: sge.Expression,
window: typing.Optional[window_spec.WindowSpec] = None,
include_framing_clauses: bool = True,
) -> sge.Expression:
if window is None:
return value
Expand Down Expand Up @@ -64,6 +65,9 @@ def apply_window_if_present(
if not window.bounds and not order:
return sge.Window(this=value, partition_by=group_by)

if not window.bounds and not include_framing_clauses:
return sge.Window(this=value, partition_by=group_by, order=order)

kind = (
"ROWS" if isinstance(window.bounds, window_spec.RowsWindowBounds) else "RANGE"
)
Expand Down
2 changes: 2 additions & 0 deletions bigframes/operations/aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ def implicitly_inherits_order(self):

@dataclasses.dataclass(frozen=True)
class DenseRankOp(UnaryWindowOp):
name: ClassVar[str] = "dense_rank"

@property
def skips_nulls(self):
return False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH `bfcte_0` AS (
SELECT
`int64_col` AS `bfcol_0`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
DENSE_RANK() OVER (ORDER BY `bfcol_0` IS NULL ASC NULLS LAST, `bfcol_0` ASC NULLS LAST) AS `bfcol_1`
FROM `bfcte_0`
)
SELECT
`bfcol_1` AS `agg_int64`
FROM `bfcte_1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH `bfcte_0` AS (
SELECT
`int64_col` AS `bfcol_0`
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
), `bfcte_1` AS (
SELECT
*,
RANK() OVER (ORDER BY `bfcol_0` IS NULL ASC NULLS LAST, `bfcol_0` ASC NULLS LAST) AS `bfcol_1`
FROM `bfcte_0`
)
SELECT
`bfcol_1` AS `agg_int64`
FROM `bfcte_1`
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
import pytest

from bigframes.core import agg_expressions as agg_exprs
from bigframes.core import array_value, identifiers, nodes
from bigframes.core import (
array_value,
expression,
identifiers,
nodes,
ordering,
window_spec,
)
from bigframes.operations import aggregations as agg_ops
import bigframes.pandas as bpd

Expand All @@ -38,6 +45,24 @@ def _apply_unary_agg_ops(
return sql


def _apply_unary_window_op(
obj: bpd.DataFrame,
op: agg_exprs.UnaryAggregation,
window_spec: window_spec.WindowSpec,
new_name: str,
) -> str:
win_node = nodes.WindowOpNode(
obj._block.expr.node,
expression=op,
window_spec=window_spec,
output_name=identifiers.ColumnId(new_name),
)
result = array_value.ArrayValue(win_node).select_columns([new_name])

sql = result.session._executor.to_sql(result, enable_cache=False)
return sql


def test_count(scalar_types_df: bpd.DataFrame, snapshot):
col_name = "int64_col"
bf_df = scalar_types_df[[col_name]]
Expand All @@ -47,6 +72,18 @@ def test_count(scalar_types_df: bpd.DataFrame, snapshot):
snapshot.assert_match(sql, "out.sql")


def test_dense_rank(scalar_types_df: bpd.DataFrame, snapshot):
col_name = "int64_col"
bf_df = scalar_types_df[[col_name]]
agg_expr = agg_exprs.UnaryAggregation(
agg_ops.DenseRankOp(), expression.deref(col_name)
)
window = window_spec.WindowSpec(ordering=(ordering.ascending_over(col_name),))
sql = _apply_unary_window_op(bf_df, agg_expr, window, "agg_int64")

snapshot.assert_match(sql, "out.sql")


def test_max(scalar_types_df: bpd.DataFrame, snapshot):
col_name = "int64_col"
bf_df = scalar_types_df[[col_name]]
Expand Down Expand Up @@ -104,6 +141,17 @@ def test_min(scalar_types_df: bpd.DataFrame, snapshot):
snapshot.assert_match(sql, "out.sql")


def test_rank(scalar_types_df: bpd.DataFrame, snapshot):
col_name = "int64_col"
bf_df = scalar_types_df[[col_name]]
agg_expr = agg_exprs.UnaryAggregation(agg_ops.RankOp(), expression.deref(col_name))

window = window_spec.WindowSpec(ordering=(ordering.ascending_over(col_name),))
sql = _apply_unary_window_op(bf_df, agg_expr, window, "agg_int64")

snapshot.assert_match(sql, "out.sql")


def test_sum(scalar_types_df: bpd.DataFrame, snapshot):
bf_df = scalar_types_df[["int64_col", "bool_col"]]
agg_ops_map = {
Expand Down