Quick Overview
This tutorial is presented by Google Cloud AI engineer Annie Wang to explain why AI agents and SQL queries produce inflated metrics across joined tables. It demonstrates how to configure and query BigQuery Graph property graphs and measures to ensure consistent, accurate data aggregation.
Key Points
- 1.Joining relational tables across many-to-many relationships duplicates rows and causes standard SQL aggregate functions to inflate numerical metrics.
- 2.BigQuery Graph measures allow calculations to be defined at the data model level rather than rewritten inside individual SQL queries.
- 3.A graph measure uses entity keys to ensure values like stream counts are calculated once per entity within an aggregation group.
- 4.The AGG function applies graph-defined measure rules within queries using GRAPH_EXPAND, regardless of how groupings or dimensions change.
- 5.Graph structures preserve relationships between entities, enabling topological impact analyses such as determining exposure to specific playlist nodes.
- 6.Defined property graphs and measures integrate into Knowledge Catalog and can be queried directly by BigQuery conversational analytics agents.
Summary
AI engineer Annie Wang demonstrates why AI agents and SQL queries frequently return incorrect numerical totals when querying data spread across multiple relational tables, and how BigQuery Graph measures resolve the issue. In a sample music dataset, a record label named Cymbal Records owns four songs with individual stream counts of 2 billion, 1.5 billion, 1.2 billion, and 400 million, amounting to an actual total of 5.1 billion streams. A separate table stores playlist information. When an AI agent is asked to calculate total streams per label and list their playlist counts, it writes a join query across the songs, labels, and playlists tables.
The resulting SQL join duplicates rows for songs assigned to multiple playlists. The track Neon Summer appears on four playlists, causing its 2 billion streams to be listed four times, while Paper Crown appears on two playlists and is listed twice. When the query executes a standard SUM on the joined stream column, Neon Summer contributes 8 billion streams and Paper Crown contributes 3 billion streams. Cymbal Records' stream count is returned as 12.6 billion instead of 5.1 billion, representing an error of nearly two and a half times the true figure. While this error is obvious on a five-row sample dataset, such inflation silently passes undetected in production tables containing millions of rows.
BigQuery Graph addresses this by embedding calculation rules directly into a property graph data model. A property graph defines how existing BigQuery tables connect as nodes and edges without duplicating or moving the underlying storage. Within the graph definition, developers add a measure to the songs node using the MEASURE keyword, defining the calculation as the sum of streams. Because the measure is bound to the song entity, BigQuery uses the unique song identifier to ensure each song's streams are counted only once within any aggregation group.
To query the measure, analysts write SQL using the AGG function over the defined measure name within a GRAPH_EXPAND clause, replacing the raw SUM function. AGG enforces the deduplication rules configured in the graph, returning the correct 5.1 billion streams for Cymbal Records. The same measure can be reused smoothly when changing query groupings, such as grouping streams by playlist rather than by label, without altering the aggregation logic.
In addition to fixing arithmetic aggregations, the graph retains the topological relationships across nodes. Tracing Cymbal Records through artists and songs to playlists reveals that 4.7 billion of its 5.1 billion streams, or 92 percent of its catalog volume, connect to the Hot Right Now playlist. This topological analysis demonstrates how cutting that single node removes three of eight catalog placements and leaves one song without any playlist coverage, uncovering structural risks invisible in traditional flat summaries. Property graphs and measures can be constructed manually in SQL, visually in the BigQuery console, or with the Data Agent Kit, and they publish to Knowledge Catalog where BigQuery conversational analytics agents can query them directly.
The Join Inflation Problem in Data Analytics
When an AI assistant or analyst queries across multiple relational tables, joined rows multiply based on matching relationships. In an example music catalog, summing song streams for a record label after joining with playlist data inflates the real total of 5.1 billion streams to 12.6 billion streams. This discrepancy occurs because songs appearing on multiple playlists generate repeated rows that standard SQL SUM functions aggregate multiple times.
Defining Property Graphs and Measures in BigQuery
BigQuery Graph solves metric inflation through property graphs that map relationships between tables without moving underlying data. Within this property graph definition, developers attach measures to specific node tables using the MEASURE keyword. The measure attaches calculation logic directly to the primary entity, instructing BigQuery to reference unique entity identifiers and count values exactly once per entity within any aggregation grouping.
Querying Measures with the AGG Function
To query graph measures in SQL, analysts use the AGG function in place of standard aggregations like SUM when querying through GRAPH_EXPAND. AGG automatically executes the predefined deduplication rules established in the graph model. This approach ensures consistent metrics across queries, whether grouping by record labels, playlists, or other attributes, without requiring query authors to craft custom deduplication logic.
Analyzing Network Dependencies and Context
Beyond calculating accurate totals, property graphs preserve connection paths between data nodes. Traversing the music graph reveals that 4.7 billion out of Cymbal Records' 5.1 billion streams, or 92 percent of the catalog's stream volume, route through a single playlist node titled Hot Right Now. This exposes catalog vulnerability to single points of distribution that aggregate totals alone cannot show.
Integration with AI Agents and Governance
Property graphs and measures can be built in code, via the BigQuery Studio visual modeler, or through the Data Agent Kit. Once created, graph metadata registers automatically in Knowledge Catalog for enterprise discovery. BigQuery conversational analytics agents can directly interpret these graph structures, enabling natural language questions to return accurate numerical calculations without hallucinating or miscalculating joins.
The Bottom Line
The video establishes that SQL joins across many-to-many relationships routinely corrupt analytical calculations through row duplication, an issue that automated AI query generators cannot reliably resolve on their own. By moving calculation logic into BigQuery Graph measures, data teams establish reusable semantic definitions that guarantee accurate metric aggregation across any grouping dimension. The presentation demonstrates that graph modeling simultaneously protects mathematical accuracy and enables deep structural dependency analysis across connected business entities.
FAQ
What is BigQuery Graph and what are BigQuery Graph measures?
BigQuery Graph is a capability in BigQuery that defines property graph relationships over existing tabular data without moving it, while measures are calculation rules attached to graph nodes that ensure numerical metrics are aggregated accurately without duplicate counting.
Why do SQL table joins cause AI agents to calculate incorrect numerical totals?
When tables with many-to-many relationships are joined, rows multiply for every matching connection. Standard SQL aggregation functions like SUM aggregate every duplicated row in the joined table, inflating the final totals.
How does the AGG function work with BigQuery Graph measures in SQL queries?
The AGG function is used inside a GRAPH_EXPAND query to apply the calculation rule defined inside the graph measure, automatically deduplicating entities by their unique keys before computing the total.
How can developers create and manage property graphs and measures in BigQuery?
Developers can write SQL definitions using the CREATE OR REPLACE PROPERTY GRAPH statement, build them visually in the BigQuery Studio console modeler, or configure them using the Data Agent Kit.
How does BigQuery Knowledge Catalog support conversational analytics agents using graph measures?
Once a property graph is created, its metadata registers in Knowledge Catalog so conversational analytics agents can discover the graph and query its defined measures directly using natural language.
Worth watching for
Data engineers, analytics architects, and AI developers using BigQuery who need to prevent metric inflation in multi-table joins and provide accurate data models for conversational AI agents.
- bigquery
- bigquery-graph
- sql
- data-modeling
- ai-agents
- google-cloud