← Back to the journal

DataFocus · Journal

How to Stop an LLM From Joining Tables on the Wrong Keys

A join that runs without error can still be wrong. This tutorial covers declaring relationships once, forcing explicit join conditions, checking schema bindings, and regression-testing with questions whose answers you already know.

You stop an LLM from guessing join keys by taking the decision away from it: declare each relationship between tables once, in a place the agent must read, and let the agent only choose among declared joins. Then make the join condition visible in every query (an explicit JOIN ... ON), bind queries to a known table schema so a drifted column fails loudly, and regression-test with questions whose correct answers you already know. None of this removes the need for review, but it turns a silent wrong answer into a checkable one.

How do wrong joins happen?

A wrong join rarely raises an error. The database accepts any join whose columns have compatible types, so a model that picks orders.customer_id = accounts.id because both look like identifiers gets a result set, a plausible total and no warning.

The usual causes are mundane:

  • Two tables each hold a column that looks like a key, but only one of them is the real relationship (an internal surrogate id versus a business customer number).
  • The same entity has different identifiers in different systems, so a name or code match looks right and is not. DataFocus's own homepage demo makes this point from the other side: until a customer identifier mapping across CRM, ERP and contracts is confirmed, agents do not treat two similar names as one company.
  • A join on a non-unique key fans out rows, and a later SUM counts them twice.

How much this costs on real warehouses is hard to say from a single number, but the direction is documented. The BEAVER benchmark, built from private enterprise data warehouses, reported in its 2026 revision on arXiv that state-of-the-art agentic frameworks using GPT-5.2 reached only 10.8% accuracy on its text-to-SQL tasks. The paper measures overall accuracy, not joins specifically, so treat it as evidence that private schemas are hard for models, not as a join-error rate.

Declare relationships once

The fix starts where the knowledge lives. In a semantic layer or an ontology, "orders connect to customers" is a stored fact, not something re-derived from column names on every question. dbt's MetricFlow documentation describes the same idea for metrics: it uses entity types to navigate joins and avoid fan-out and chasm joins.

In DataFocus OT Flash (r57 manual), the steps are:

  1. Create the ontology with a name and an icon.
  2. Import the related tables in Data Tables, and upload the business documents that define terms in Document Center.
  3. Open the Relationships tab in Data Tables to set up the joins between the tables bound to the ontology.

In OT Flash, objects connect (orders to customers, production tasks and contracts), each field has a source and each definition an owner, and uncertain relationships remain unconfirmed. That last property matters for the problem at hand. A relationship nobody has confirmed should not be silently promoted to a join the agent can use.

Every query returns the SQL that ran, the tables it used and a data fingerprint, and the AI follows the objects and relationships defined in the ontology instead of guessing.

Hypothetical example: two customer keys

Consider a hypothetical distributor with two tables (illustrative, not run):

TableColumns that look like keysReal relationship
orderscustomer_id, billing_account_idcustomer_id references customers.customer_id
customerscustomer_id, account_noaccount_no is a finance code, unrelated to billing_account_id

A model asked for "revenue by customer region" might join orders.billing_account_id to customers.account_no if both columns hold similar-looking strings. If you declared the single correct pair in the relationship definition, that choice is no longer open.

Make every join condition visible

A declared relationship helps only if you can see the agent used it. The OT Flash agent manual (r56) sets some useful SQL constraints for the Doris query entry point dfc_table:

  • Joins need an explicit JOIN ... ON. NATURAL JOIN and JOIN USING are rejected.
  • SELECT * and table.* are forbidden.
  • Queries use the displayed table and column names, double-quoted.

The first rule closes off a quiet failure. NATURAL JOIN joins on every same-named column without saying which, so a new column added to either table changes the join. With an explicit ON, the key is written in the SQL, and a reviewer or a script can compare it with the declared relationship.

The query you want to see (illustrative, not run, table and column names are the hypothetical ones above):

SELECT c."region", SUM(o."amount") AS revenue
FROM "orders" o
JOIN "customers" c
  ON o."customer_id" = c."customer_id"
GROUP BY c."region"

A simple review check is mechanical: parse the ON clause of each query the agent ran, and flag any column pair that is not in your declared list. The query receipts above give you the SQL to parse.

Check the schema the agent is bound to

Declared relationships go stale when a column is renamed, retyped or dropped. The r56 manual describes dfc_ontology_table_schema, a schema check keyed by a table's sourceAssetRef of the form table:<id>, with v4 Binding fingerprints. The manual's guidance on coordinates is the habit to copy even if your stack differs: identifiers come from the server's listings, not from the model.

  • A table resource id comes from the tables list, not from a physical name or display name.
  • Objects, properties, bindings, scopes and versions are selected from returned values, not generated or replaced with "latest".
  • For graph routing, only the locator object fully returned by the server may be passed on; an agent must not wrap an id into a locator itself.

The principle for any stack: store a fingerprint of each table's columns and types next to the relationship that depends on it, recompute it before the agent runs, and refuse to answer when it differs. The manual also lists TABLE_SCHEMA_INCOMPLETE as an error meaning the agent must obtain the complete structure before continuing.

The ontology graph offers a related signal. Its Graph quality panel lists nodes, relations, isolated nodes, connected components, unmapped mentions and failed sources. A high isolated-node count is worth checking before an agent decides to improvise a join between tables the graph does not connect.

Test with questions whose answers you already know

Build a small set of questions that exercise each declared relationship, with the expected result computed independently (for example, a hand-written SQL you trust, or a report your finance team has already signed off).

  1. Pick one question per relationship, plus one that needs two hops.
  2. Add a "trap" question per ambiguous column pair, like the billing_account_id case above.
  3. Add an "unknown" question where the correct behavior is to say there is no declared path, not to find one.
  4. Run the set after every schema change, ontology edit or model change. Compare totals, not just whether SQL executed.
  5. Check row counts against the unique grain: if a join should be one-to-one and the output has more rows than the left table, stop.

The OT Flash agent manual also gives rules for reading results. Zero-row, failed or truncated results must not be described as complete statistics, and answer text or an HTTP 200 does not prove success on its own. Put the same rule in your tests: pass only on the server's receipt and a number that matches.

For routing in OT Flash, the manual says to use dfc_ontology_graph for locating objects, dfc_table for statistics and TopN, and dfc_ontology_query or knowledge search for policies and contracts. A join question belongs in the second group, after the graph has located the tables.

Remaining risks

  • A declared relationship can be wrong. The agent will faithfully reuse a mistaken join. Have the data owner confirm each relationship, and keep the regression set.
  • Declared is not the same as unique. A correct key on a non-unique column still fans out. Test row counts.
  • Business meaning lives outside keys. Whether "delivered" means shipped or accepted is not a join problem. DataFocus's documents bind definitions in Document Center for this, but a human still writes them.
  • Product scope. The Flash manuals describe a workspace of tables, documents, graph and relationships. They do not say the engine rejects every query that departs from a declared relationship, so keep your own check on the ON clause.

Next step

Read the ontology chapter of the OT Flash user manual and the Agent and MCP manual, then write your own five-question join test before connecting an agent. Installers and release notes, including system requirements, are linked from the download page.