# Map Reference Fields to the Target

Source: https://userguide.qualytics.io/data-quality-checks/data-diff/deep-dive/field-mapping/

Two copies of the same data rarely agree on names and types. A replica can call `customer_id` what the source calls `legacy_customer_id`, or store an amount as text where the source stores a decimal. A Data Diff check can line the reference up with the target itself through the reference's **Select Clause**, so you do not need a computed table that exists only to rename or cast fields.

## When to Use the Select Clause

Use the Select Clause when renaming or casting is the only reason the two sides differ:

- **Different field names.** `legacy_customer_id` on the reference holds what `customer_id` holds on the target.
- **Different types.** An amount stored as text on the reference and as a decimal on the target, or a date on one side and a timestamp on the other.
- **Different precision.** `numeric(15,2)` on one side and `numeric(38,18)` on the other.

Keep using a [computed table](https://userguide.qualytics.io/container/computed-tables/deep-dive/how-it-works/) or [computed file](https://userguide.qualytics.io/container/computed-files/deep-dive/lifecycle/) when you need joins, aggregations, or a reshaped dataset that other checks also read.

## Where It Is Set

The Select Clause sits in the **Right Reference** panel of the check form, under the **Field Mapping** heading, after **Table** and before **Filter Clause**. In the API it is `properties.ref_select_clause`.

The target side is not projected. The check compares its own **Fields**, chosen as usual, and the Select Clause renames and casts the reference's fields to match them.

The form fills the Select Clause in for you. It starts as the check's **Fields** and **Row Identifiers** under their own names, for example `customer_id, total`, and keeps up as you change them. Edit only the fields the reference names or types differently. Once you change the clause in any way, by renaming, casting or converting a field or by adding or removing one, it is yours and the form stops updating it.

The form saves the clause as written, even when it only lists fields under their own names. Validation then checks every field against the reference, as described below, so a field you remove from the clause makes the check fail validation rather than go unchecked.

### Fields and SQL Views

A toggle beside the **Field Mapping** heading shows the mapping in one of two views. Both edit the same Select Clause.

**Fields** shows the mapping as a table of the check's **Fields** and **Row Identifiers**, with the **Status**, **Left** and **Right** columns of the [Data Reconciliation recipe's Field Mapping step](https://userguide.qualytics.io/recipes/data-reconciliation/deep-dive/build-and-validate/#field-mapping). The **Mapped** tab lists the fields that have a reference field to be compared with, and **Not mapped** those that don't. Click a field's **Right** cell to open the **Map Field** dialog, pick the reference field that holds the same data, and choose its **Conversion**:

- **As is** compares the reference field without changes, and writes `legacy_customer_id AS customer_id`.
- **Cast** casts it to a type you pick or type, and writes `CAST(total_amount AS DECIMAL(18, 2)) AS total`.
- **Expression** converts it with a Spark SQL expression over the reference's fields, and writes `TO_DATE(order_date_text, 'yyyy-MM-dd') AS order_date` when the reference stores the date as text.

**Clear mapping** returns a field to the reference field of its own name, when the reference has one. When one field of a pair is text and the other is not, the reference field is cast to the target field's type for you.

A message under the table names any compared field or row identifier the clause doesn't map. Map a field that has no reference field of its own name under **Not mapped**. A field that has one but is left out of the clause can only be added back in the **SQL** view.

**SQL** shows the clause itself, to read or edit as text. Name each output after the field it is compared with. When the clause renames, casts or converts a field, a **Reference mapping** summary under it lists each field with the reference expression it reads. When it leaves out a compared field, or names one the check doesn't have, an error under it says which.

The form opens on **Fields** when it can read the clause as mappings. A clause it can't, such as one with an expression that has no output name, opens in **SQL**, and **Fields** stays unavailable with the tooltip "This Select Clause can only be edited as SQL" until the clause reads as mappings again.

On a check you can only view, **Fields** summarizes the mapping, for example **3 mapped, all by name**, lists the fields a mapping renames, casts or converts, and shows the fields compared under their own names on one **Same name** line.

## Writing the Clause

The clause is a comma-separated list of expressions, written the same way as the select clause of a computed file. Each expression produces one reference field, named after the target field it is compared with:

- Rename with `AS`: `legacy_customer_id AS customer_id`.
- Cast with `CAST(... AS ...)` and name the result: `CAST(total_amount AS DECIMAL(18, 2)) AS total`.
- A bare field keeps its own name: `status` is the same as `status AS status`.
- List every field explicitly. `*` is not accepted.
- Give each output its own name. Names are matched without regard to case.

## What the Clause Must Project

Fields are matched **by name**, not by position: each output of the clause is compared with the target field of the same name, whatever order the clause lists them in.

The clause must project every field the check compares:

- every field in the check's **Fields**,
- every **Row Identifier**.

**Passthrough Fields** are optional in the clause. Project one to rename or cast it. A passthrough field that the clause leaves out is read from the reference by its own name, and it is simply left empty there if the reference doesn't have it, exactly as without a clause.

The check fails validation in any of these cases:

- A compared field or row identifier is not projected, so the reference has fewer fields than the target.
- An output doesn't name one of the check's fields, row identifiers or passthrough fields.
- A projected field's type can't be compared with the target field's type, for example text against a number. Cast the reference field to the target's type.

Row Identifiers and Passthrough Fields keep naming target fields, exactly as they do without a clause.

A check with no Select Clause, such as one created through the API without it or saved before the clause existed, compares its **Fields** by name on both sides, exactly as before. Validation then checks that each field exists on both sides, but not that their types fall in the same group. Opening such a check in the form fills the clause in, and saving it turns on the full check.

> **Warning: Validation cannot read intent**
>
> Validation catches names and types that do not line up. It cannot tell that a reference field holds different data than the target field it is named after. `surname AS first_name` is accepted and compares first names with surnames. Map each reference field to the target field that holds the same data.

## The Filter Reads the Original Fields

The reference is filtered **before** it is projected:

1. The **Right Reference** **Filter Clause** scopes the reference, using its original field names.
2. The filtered reference is projected through the Select Clause.
3. The projected fields are compared with the target's fields of the same name.

The reference filter therefore says `legacy_customer_id < 900000`, not `customer_id < 900000`, and it can use fields the clause does not project. The check's own **Filter Clause** scopes the target as usual. Adding a Select Clause to an existing check does not change what either filter matches.

## Types

Each target field and the reference output named after it must fall in the same comparison group:

| Group | Types |
| --- | --- |
| Numbers | `Integral`, `Fractional`, including decimals of any precision |
| Dates | `Date`, `Timestamp` |
| Text | `String` |
| Booleans | `Boolean` |
| Arrays | `Array` |
| Structs | `Struct` |

A pair in the same group that differs in detail is compared in a common type: whole numbers as whole numbers, decimals at the larger scale, other numbers as floating point, and dates as timestamps. A pair from different groups, such as text against a decimal, is refused until the reference field is cast to the target's type: `CAST(total_amount AS DECIMAL(18, 2)) AS total`.

Comparators apply to each field by the target's type, so a text reference field cast to a decimal is compared with the **Numeric** comparator.

## Validation Messages

The clause's structure is checked when you save the check. Expressions and types are also checked against both containers' schemas when the check is validated, and again at every scan, so a schema change surfaces as a failed check rather than a wrong comparison.

| Message contains | What to change |
| --- | --- |
| `does not project 'amount', which the check compares` | Add an expression for that field, for example `total_amount AS amount`. |
| `which is not a field of this check` | Rename the output after the target field it matches, or remove it. |
| `can't be compared` | Cast the reference field to the target's type: `CAST(total_amount AS DECIMAL(18, 2)) AS amount`. |
| `needs an output name` | Add `AS name` after the expression. |
| `are both named` | Give each output its own name. |
| `The reference Select Clause could not be applied` | Check that each field exists on the reference, that names with special characters are wrapped in backquotes, and that each cast names a valid type. |
| `The reference filter could not be applied` | The message gives the reason, then reads "The filter reads the reference's original fields, not the names the Select Clause gives them". Rewrite the reference filter with the reference's original field names, and check that each field it reads exists on the reference. |

## Masked Fields

A reference field computed from a masked reference field stays masked under its new name. `ssn AS member_ref` keeps `member_ref` masked:

- **Source records:** Qualytics masks `member_ref` when it displays the check's source records. Because `member_ref` is a target field, it is also masked in the container's other source records. See [Masked Fields in Source Records](https://userguide.qualytics.io/anomalies/deep-dive/anomaly-details/source-record/#masked-fields).
- **Enrichment datastore:** the source records a scan writes there hold the original values, because masking is applied when Qualytics reads them. Anyone who queries the enrichment datastore directly sees the unmasked data, so restrict direct access to it.
- **Reconciliation outputs:** a [Materialize operation](https://userguide.qualytics.io/operations/materialize-operation/materialize-operation/) writes `member_ref` masked, unless **Reveal Masked Values** is enabled when it is triggered.

## Example

A target `orders` table and a reference `orders_legacy` table hold the same customer totals with different names and types.

| Side | Fields |
| --- | --- |
| Target `orders` | `customer_id` (Integral), `total` (Fractional), `region` (String) |
| Reference `orders_legacy` | `legacy_customer_id` (Integral), `total_amount` (String) |

The check compares European customers only, and the legacy system marks test accounts with ids of 900000 and above:

| Setting | Value |
| --- | --- |
| **Fields** | `total` |
| **Row Identifiers** | `customer_id` |
| **Filter Clause** (target) | `region = 'EU'` |
| **Table** (Right Reference) | `orders_legacy` |
| **Select Clause** (Right Reference) | `legacy_customer_id AS customer_id, CAST(total_amount AS DECIMAL(18, 2)) AS total` |
| **Filter Clause** (Right Reference) | `legacy_customer_id < 900000` |

The reference's `legacy_customer_id` is compared with the target's `customer_id`, and its text `total_amount`, cast to a decimal, with the target's `total`. The target filter reads `region`, and the reference filter reads `legacy_customer_id`, the original name. If customer 2 has a total of `20.00` on the target and `25.00` on the reference, the anomaly reports customer 2 as **changed** on `total`. A **Numeric** comparator with a tolerance of `5` would accept the difference.

The [Data Reconciliation recipe](https://userguide.qualytics.io/recipes/data-reconciliation/deep-dive/build-and-validate/#field-mapping) writes this clause for you when you map fields on its **Field Mapping** step.

## See Also

- **How It Works**

  The evaluation flow, the three diff statuses, filters, and comparators.

  [How It Works](https://userguide.qualytics.io/data-quality-checks/data-diff/deep-dive/how-it-works/)
- **Best Practices**

  Guidelines for choosing Row Identifiers, tuning Comparators, scoping both sides, and keeping the cost down.

  [Best Practices](https://userguide.qualytics.io/data-quality-checks/data-diff/deep-dive/best-practices/)

*Last updated: November 23, 2026.*
