Skip to content

Commit 655cb24

Browse files
committed
return some results in SCRIPT if the last statement is a SELECT
1 parent 03b4426 commit 655cb24

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

bigframes/session/_io/bigquery/read_gbq_query.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@
3232
import bigframes.session
3333

3434

35+
def should_return_query_results(query_job: bigquery.QueryJob) -> bool:
36+
"""Returns True if query_job is the kind of query we expect results from.
37+
38+
If the query was DDL or DML, return some job metadata. See
39+
https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobStatistics2.FIELDS.statement_type
40+
for possible statement types. Note that destination table does exist
41+
for some DDL operations such as CREATE VIEW, but we don't want to
42+
read from that. See internal issue b/444282709.
43+
"""
44+
45+
if query_job.statement_type == "SELECT":
46+
return True
47+
48+
if query_job.statement_type == "SCRIPT":
49+
# Try to determine if the last statement is a SELECT. Alternatively, we
50+
# could do a jobs.list request using query_job as the parent job and
51+
# try to determine the statement type of the last child job.
52+
return query_job.destination != query_job.ddl_target_table
53+
54+
return False
55+
56+
3557
def create_dataframe_from_query_job_stats(
3658
query_job: Optional[bigquery.QueryJob], *, session: bigframes.session.Session
3759
) -> dataframe.DataFrame:

bigframes/session/loader.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,29 +1045,30 @@ def read_gbq_query(
10451045
# local node. Likely there are a wide range of sizes in which it
10461046
# makes sense to download the results beyond the first page, even if
10471047
# there is a job and destination table available.
1048-
if (
1049-
rows is not None
1050-
and destination is None
1051-
and (
1052-
query_job_for_metrics is None
1053-
or query_job_for_metrics.statement_type == "SELECT"
1054-
)
1055-
):
1048+
if query_job_for_metrics is None and rows is not None:
10561049
return bf_read_gbq_query.create_dataframe_from_row_iterator(
10571050
rows,
10581051
session=self._session,
10591052
index_col=index_col,
10601053
columns=columns,
10611054
)
10621055

1056+
# We already checked rows, so if there's no destination table, then
1057+
# there are no results to return.
1058+
if destination is None:
1059+
return bf_read_gbq_query.create_dataframe_from_query_job_stats(
1060+
query_job_for_metrics,
1061+
session=self._session,
1062+
)
1063+
10631064
# If the query was DDL or DML, return some job metadata. See
10641065
# https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobStatistics2.FIELDS.statement_type
10651066
# for possible statement types. Note that destination table does exist
10661067
# for some DDL operations such as CREATE VIEW, but we don't want to
10671068
# read from that. See internal issue b/444282709.
1068-
if destination is None or (
1069+
if (
10691070
query_job_for_metrics is not None
1070-
and query_job_for_metrics.statement_type != "SELECT"
1071+
and not bf_read_gbq_query.should_return_query_results(query_job_for_metrics)
10711072
):
10721073
return bf_read_gbq_query.create_dataframe_from_query_job_stats(
10731074
query_job_for_metrics,

0 commit comments

Comments
 (0)