Skip to content

fix: generate GoogleSQL instead of legacy SQL data types for dry_run=True from bpd._read_gbq_colab with local pandas DataFrame #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 30, 2025
13 changes: 12 additions & 1 deletion bigframes/core/tools/bigquery_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

import google.cloud.bigquery

_LEGACY_TO_GOOGLESQL_TYPES = {
"BOOLEAN": "BOOL",
"INTEGER": "INT64",
"FLOAT": "FLOAT64",
}


def _type_to_sql(field: google.cloud.bigquery.SchemaField):
"""Turn the type information of the field into SQL.
Expand All @@ -26,7 +32,12 @@ def _type_to_sql(field: google.cloud.bigquery.SchemaField):
"""
if field.field_type.casefold() in ("record", "struct"):
return _to_struct(field.fields)
return field.field_type

# Map from legacy SQL names (the ones used in the BigQuery schema API) to
# the GoogleSQL types. Importantly, FLOAT is from legacy SQL, but not valid
# in GoogleSQL. See internal issue b/428190014.
type_ = _LEGACY_TO_GOOGLESQL_TYPES.get(field.field_type.upper(), field.field_type)
return type_


def _field_to_sql(field: google.cloud.bigquery.SchemaField):
Expand Down
51 changes: 35 additions & 16 deletions bigframes/pandas/io/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,27 @@ def read_gbq(
read_gbq.__doc__ = inspect.getdoc(bigframes.session.Session.read_gbq)


def _run_read_gbq_colab_sessionless_dry_run(
query: str,
*,
pyformat_args: Dict[str, Any],
) -> pandas.Series:
"""Run a dry_run without a session."""

query_formatted = bigframes.core.pyformat.pyformat(
query,
pyformat_args=pyformat_args,
dry_run=True,
)
bqclient = _get_bqclient()
job = _dry_run(query_formatted, bqclient)
return dry_runs.get_query_stats_with_inferred_dtypes(job, (), ())


def _try_read_gbq_colab_sessionless_dry_run(
create_query: Callable[[], str],
query: str,
*,
pyformat_args: Dict[str, Any],
) -> Optional[pandas.Series]:
"""Run a dry_run without a session, only if the session hasn't yet started."""

Expand All @@ -230,10 +249,9 @@ def _try_read_gbq_colab_sessionless_dry_run(
# to local data and not any BigQuery tables.
with _default_location_lock:
if not config.options.bigquery._session_started:
bqclient = _get_bqclient()
query = create_query()
job = _dry_run(query, bqclient)
return dry_runs.get_query_stats_with_inferred_dtypes(job, (), ())
return _run_read_gbq_colab_sessionless_dry_run(
query, pyformat_args=pyformat_args
)

# Explicitly return None to indicate that we didn't run the dry run query.
return None
Expand Down Expand Up @@ -286,21 +304,13 @@ def _read_gbq_colab(
if pyformat_args is None:
pyformat_args = {}

# Delay formatting the query with the special "session-less" logic. This
# avoids doing unnecessary work if the session already has a location or has
# already started.
create_query = functools.partial(
bigframes.core.pyformat.pyformat,
query_or_table,
pyformat_args=pyformat_args,
dry_run=True,
)

# Only try to set the global location if it's not a dry run. We don't want
# to bind to a location too early. This is especially important if the query
# only refers to local data and not any BigQuery tables.
if dry_run:
result = _try_read_gbq_colab_sessionless_dry_run(create_query)
result = _try_read_gbq_colab_sessionless_dry_run(
query_or_table, pyformat_args=pyformat_args
)

if result is not None:
return result
Expand All @@ -309,6 +319,15 @@ def _read_gbq_colab(
# started. That means we can safely call the "real" _read_gbq_colab,
# which generates slightly nicer SQL.
else:
# Delay formatting the query with the special "session-less" logic. This
# avoids doing unnecessary work if the session already has a location or has
# already started.
create_query = functools.partial(
bigframes.core.pyformat.pyformat,
query_or_table,
pyformat_args=pyformat_args,
dry_run=True,
)
_set_default_session_location_if_possible_deferred_query(create_query)

return global_session.with_default_session(
Expand Down
13 changes: 13 additions & 0 deletions tests/system/small/pandas/io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
13 changes: 13 additions & 0 deletions tests/system/small/pandas/io/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading