Skip to content

Commit 3f3ad87

Browse files
committed
refactor: fix ops.len_op for array
1 parent b487cf1 commit 3f3ad87

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

bigframes/core/compile/sqlglot/expressions/string_ops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import sqlglot.expressions as sge
2020

21+
from bigframes import dtypes
2122
from bigframes import operations as ops
2223
from bigframes.core.compile.sqlglot.expressions.typed_expr import TypedExpr
2324
import bigframes.core.compile.sqlglot.scalar_compiler as scalar_compiler
@@ -195,6 +196,9 @@ def _(expr: TypedExpr) -> sge.Expression:
195196

196197
@register_unary_op(ops.len_op)
197198
def _(expr: TypedExpr) -> sge.Expression:
199+
if dtypes.is_array_like(expr.dtype):
200+
return sge.func("ARRAY_LENGTH", expr.expr)
201+
198202
return sge.Length(this=expr.expr)
199203

200204

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`int_list_col`
4+
FROM `bigframes-dev`.`sqlglot_test`.`repeated_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
ARRAY_LENGTH(`int_list_col`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `int_list_col`
13+
FROM `bfcte_1`

tests/unit/core/compile/sqlglot/expressions/test_string_ops.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ def test_len(scalar_types_df: bpd.DataFrame, snapshot):
120120
snapshot.assert_match(sql, "out.sql")
121121

122122

123+
def test_len_w_array(repeated_types_df: bpd.DataFrame, snapshot):
124+
col_name = "int_list_col"
125+
bf_df = repeated_types_df[[col_name]]
126+
sql = utils._apply_ops_to_sql(bf_df, [ops.len_op.as_expr(col_name)], [col_name])
127+
128+
snapshot.assert_match(sql, "out.sql")
129+
130+
123131
def test_lower(scalar_types_df: bpd.DataFrame, snapshot):
124132
col_name = "string_col"
125133
bf_df = scalar_types_df[[col_name]]

0 commit comments

Comments
 (0)