Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 53fda30

Browse files
committed
implement ZfillOp
1 parent 64ad238 commit 53fda30

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,3 +838,31 @@ def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
838838
@UNARY_OP_REGISTRATION.register(ops.year_op)
839839
def _(op: ops.base_ops.UnaryOp, expr: TypedExpr) -> sge.Expression:
840840
return sge.Extract(this=sge.Identifier(this="YEAR"), expression=expr.expr)
841+
842+
843+
@UNARY_OP_REGISTRATION.register(ops.ZfillOp)
844+
def _(op: ops.ZfillOp, expr: TypedExpr) -> sge.Expression:
845+
return sge.Case(
846+
ifs=[
847+
sge.If(
848+
this=sge.EQ(
849+
this=sge.Substring(
850+
this=expr.expr, start=sge.convert(1), length=sge.convert(1)
851+
),
852+
expression=sge.convert("-"),
853+
),
854+
true=sge.Concat(
855+
expressions=[
856+
sge.convert("-"),
857+
sge.func(
858+
"LPAD",
859+
sge.Substring(this=expr.expr, start=sge.convert(1)),
860+
sge.convert(op.width - 1),
861+
sge.convert("0"),
862+
),
863+
]
864+
),
865+
)
866+
],
867+
default=sge.func("LPAD", expr.expr, sge.convert(op.width), sge.convert("0")),
868+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
WITH `bfcte_0` AS (
2+
SELECT
3+
`string_col` AS `bfcol_0`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
CASE
9+
WHEN SUBSTRING(`bfcol_0`, 1, 1) = '-'
10+
THEN CONCAT('-', LPAD(SUBSTRING(`bfcol_0`, 1), 9, '0'))
11+
ELSE LPAD(`bfcol_0`, 10, '0')
12+
END AS `bfcol_1`
13+
FROM `bfcte_0`
14+
)
15+
SELECT
16+
`bfcol_1` AS `string_col`
17+
FROM `bfcte_1`

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,9 @@ def test_year(scalar_types_df: bpd.DataFrame, snapshot):
820820
sql = _apply_unary_op(bf_df, ops.year_op, "timestamp_col")
821821

822822
snapshot.assert_match(sql, "out.sql")
823+
824+
825+
def test_zfill(scalar_types_df: bpd.DataFrame, snapshot):
826+
bf_df = scalar_types_df[["string_col"]]
827+
sql = _apply_unary_op(bf_df, ops.ZfillOp(width=10), "string_col")
828+
snapshot.assert_match(sql, "out.sql")

0 commit comments

Comments
 (0)