Back to Blog

AI Analytics

Building Governed NL2SQL with Gemini and BigQuery

How to combine Gemini on Vertex AI with BigQuery semantics, metric definitions, and permission filters so natural-language analytics stays accurate and safe.

Overview

  • Ground SQL generation in metric definitions and the business glossary, not raw schemas alone.
  • Validate generated SQL against data permissions before execution.
  • Return business summaries with traceable sources instead of raw result tables.

Context

Semantic context decides NL2SQL quality

Gemini generates better SQL when prompts carry governed context: metric calculation logic, allowed joins, business terms, and known reporting conventions from BigQuery datasets.

  • Maintain a metric dictionary with owners, formulas, and grain definitions.
  • Expose table and column semantics as retrievable context for generation.

Safety

Validate before execution, not after

Generated SQL should pass permission filtering, cost and scan-scope checks, and metric-consistency validation before it touches BigQuery, so unauthorized or runaway queries never execute.

  • Apply row, column, and dataset permissions from the requesting user, not a service account.
  • Reject queries that bypass governed metrics or exceed scan budgets.

Adoption

Answers should read like analysis, not query output

Business users adopt NL2SQL when results arrive as concise summaries with the metric definition, time range, filters, and source datasets attached for verification.

  • Attach the executed SQL and source lineage to every answer.
  • Escalate ambiguous questions to clarification instead of guessing.