Skip to content

Commit 508b017

Browse files
committed
chore: Migrate RemoteFunctionOp operator to SQLGlot
1 parent 34b5975 commit 508b017

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ def _(left: TypedExpr, right: TypedExpr) -> sge.Expression:
140140
return sge.Coalesce(this=left.expr, expressions=[right.expr])
141141

142142

143+
@register_unary_op(ops.RemoteFunctionOp, pass_op=True)
144+
def _(expr: TypedExpr, op: ops.RemoteFunctionOp) -> sge.Expression:
145+
routine_ref = op.function_def.routine_ref
146+
# Quote project, dataset, and routine IDs to avoid keyword clashes.
147+
func_name = (
148+
f"`{routine_ref.project}`.`{routine_ref.dataset_id}`.`{routine_ref.routine_id}`"
149+
)
150+
func = sge.func(func_name, expr.expr)
151+
152+
if not op.apply_on_null:
153+
return sge.Case(
154+
ifs=[
155+
sge.If(
156+
this=sge.Is(this=expr.expr, expression=sge.Null()), true=expr.expr
157+
)
158+
],
159+
default=func,
160+
)
161+
162+
return func
163+
164+
143165
@register_binary_op(ops.BinaryRemoteFunctionOp, pass_op=True)
144166
def _(
145167
left: TypedExpr, right: TypedExpr, op: ops.BinaryRemoteFunctionOp
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+
`int64_col`
4+
FROM `bigframes-dev`.`sqlglot_test`.`scalar_types`
5+
), `bfcte_1` AS (
6+
SELECT
7+
*,
8+
`my_project`.`my_dataset`.`my_routine`(`int64_col`) AS `bfcol_1`
9+
FROM `bfcte_0`
10+
)
11+
SELECT
12+
`bfcol_1` AS `int64_col`
13+
FROM `bfcte_1`

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,37 @@ def test_astype_json_invalid(
168168
)
169169

170170

171+
def test_remote_function_op(scalar_types_df: bpd.DataFrame, snapshot):
172+
from google.cloud import bigquery
173+
174+
from bigframes.functions import udf_def
175+
176+
bf_df = scalar_types_df[["int64_col"]]
177+
op = ops.RemoteFunctionOp(
178+
function_def=udf_def.BigqueryUdf(
179+
routine_ref=bigquery.RoutineReference.from_string(
180+
"my_project.my_dataset.my_routine"
181+
),
182+
signature=udf_def.UdfSignature(
183+
input_types=(
184+
udf_def.UdfField(
185+
"x",
186+
bigquery.StandardSqlDataType(
187+
type_kind=bigquery.StandardSqlTypeNames.INT64
188+
),
189+
),
190+
),
191+
output_bq_type=bigquery.StandardSqlDataType(
192+
type_kind=bigquery.StandardSqlTypeNames.FLOAT64
193+
),
194+
),
195+
),
196+
apply_on_null=True,
197+
)
198+
sql = utils._apply_ops_to_sql(bf_df, [op.as_expr("int64_col")], ["int64_col"])
199+
snapshot.assert_match(sql, "out.sql")
200+
201+
171202
def test_binary_remote_function_op(scalar_types_df: bpd.DataFrame, snapshot):
172203
from google.cloud import bigquery
173204

0 commit comments

Comments
 (0)