Skip to content

Commit 3aed495

Browse files
authored
Rename hook bigquery function _bq_cast to bq_cast (#27543)
1 parent eb06c65 commit 3aed495

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

β€Žairflow/providers/google/cloud/hooks/bigquery.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
from airflow.exceptions import AirflowException
5959
from airflow.providers.common.sql.hooks.sql import DbApiHook
60+
from airflow.providers.google.cloud.utils.bigquery import bq_cast
6061
from airflow.providers.google.common.consts import CLIENT_INFO
6162
from airflow.providers.google.common.hooks.base_google import GoogleBaseAsyncHook, GoogleBaseHook, get_field
6263
from airflow.utils.helpers import convert_camel_to_snake
@@ -2740,7 +2741,7 @@ def next(self) -> list | None:
27402741
rows = query_results["rows"]
27412742

27422743
for dict_row in rows:
2743-
typed_row = [_bq_cast(vs["v"], col_types[idx]) for idx, vs in enumerate(dict_row["f"])]
2744+
typed_row = [bq_cast(vs["v"], col_types[idx]) for idx, vs in enumerate(dict_row["f"])]
27442745
self.buffer.append(typed_row)
27452746

27462747
if not self.page_token:
@@ -2845,25 +2846,6 @@ def _escape(s: str) -> str:
28452846
return e
28462847

28472848

2848-
def _bq_cast(string_field: str, bq_type: str) -> None | int | float | bool | str:
2849-
"""
2850-
Helper method that casts a BigQuery row to the appropriate data types.
2851-
This is useful because BigQuery returns all fields as strings.
2852-
"""
2853-
if string_field is None:
2854-
return None
2855-
elif bq_type == "INTEGER":
2856-
return int(string_field)
2857-
elif bq_type in ("FLOAT", "TIMESTAMP"):
2858-
return float(string_field)
2859-
elif bq_type == "BOOLEAN":
2860-
if string_field not in ["true", "false"]:
2861-
raise ValueError(f"{string_field} must have value 'true' or 'false'")
2862-
return string_field == "true"
2863-
else:
2864-
return string_field
2865-
2866-
28672849
def split_tablename(
28682850
table_input: str, default_project_id: str, var_name: str | None = None
28692851
) -> tuple[str, str, str]:
@@ -3070,7 +3052,7 @@ def get_records(self, query_results: dict[str, Any]) -> list[Any]:
30703052
fields = query_results["schema"]["fields"]
30713053
col_types = [field["type"] for field in fields]
30723054
for dict_row in rows:
3073-
typed_row = [_bq_cast(vs["v"], col_types[idx]) for idx, vs in enumerate(dict_row["f"])]
3055+
typed_row = [bq_cast(vs["v"], col_types[idx]) for idx, vs in enumerate(dict_row["f"])]
30743056
buffer.append(typed_row)
30753057
return buffer
30763058

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
20+
def bq_cast(string_field: str, bq_type: str) -> None | int | float | bool | str:
21+
"""
22+
Helper method that casts a BigQuery row to the appropriate data types.
23+
This is useful because BigQuery returns all fields as strings.
24+
"""
25+
if string_field is None:
26+
return None
27+
elif bq_type == "INTEGER":
28+
return int(string_field)
29+
elif bq_type in ("FLOAT", "TIMESTAMP"):
30+
return float(string_field)
31+
elif bq_type == "BOOLEAN":
32+
if string_field not in ["true", "false"]:
33+
raise ValueError(f"{string_field} must have value 'true' or 'false'")
34+
return string_field == "true"
35+
else:
36+
return string_field

0 commit comments

Comments
 (0)