fix(bigquery): address review comments on memory-limited fetch

- Use has_app_context()/has_request_context() guards so fetch_data
  is safe to call outside a Flask request (fixes RuntimeError on g
  writes and current_app access in non-request paths)
- Replace sys.getsizeof(str(batch)) with per-row getsizeof sum for
  more accurate memory estimation without the str() allocation
- Fix false-positive truncation: fetch remaining+1 rows and check
  len > remaining to confirm more data exists beyond the cap
- Reset g.bq_memory_limited/g.bq_memory_limited_row_count after
  reading in get_df_payload to prevent flag leaking across multiple
  queries in the same request
- Wrap warning string in _() for i18n
- Add warning field to ChartDataResponseResult Marshmallow schema
- Pass noDuplicate: true to addWarningToast to suppress duplicate
  toasts when a multi-query chart has multiple truncated responses

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Claude Code
2026-04-07 13:16:58 -07:00
parent ffe7addcd2
commit 1894b77b44
4 changed files with 38 additions and 21 deletions

View File

@@ -193,11 +193,17 @@ class QueryContextProcessor:
warning: str | None = None
if getattr(g, "bq_memory_limited", False):
row_count = getattr(g, "bq_memory_limited_row_count", len(cache.df))
# Reset flags immediately so subsequent queries in the same request
# don't inherit this warning
g.bq_memory_limited = False
g.bq_memory_limited_row_count = 0
chart_id = (self._query_context.form_data or {}).get("slice_id", "")
prefix = f"Chart {chart_id}: " if chart_id else ""
warning = (
f"{prefix}Results truncated to {row_count:,} rows"
" due to memory constraints."
warning = _(
"%(prefix)sResults truncated to %(row_count)s rows"
" due to memory constraints.",
prefix=prefix,
row_count=f"{row_count:,}",
)
return {