Data contracts
A data contract is a fact you declare in a feature definition about data the compiler never reads. The generated query assumes that fact. When the fact is false, the query still compiles and the rows are wrong.
Compilation reads the definition only. The table, the view, and the text of a SQL() source stay on the warehouse until that generated query runs. That is what lets one definition target several backends, and what keeps your rows out of compilation. The cost is the list below: each line is trusted, and none of it is checked against the data.
validate() prints the list for the query under Unverified assumptions, one section per source the query reads. Each section lists the facts and, for a table or a SQL() source, one SQL query that tests them on your backend. EXPLAIN (FIELDS (DATA_CONTRACTS)) returns the same list. Neither one rejects the query. See Check the data .
Datatype, entity, unit, and additivity checks are a different layer. Those follow from the declarations themselves. See Semantic contracts .
What a definition assumes
| You write | Assumed true | When it is false |
|---|---|---|
EXTERNAL_COLUMNS … FROM TABLE | Each declared column exists and is read as its declared type, including # entity. The BIND TO columns are unique together. An ON filter does not remove a column from this list. A DETERMINED BY on a column is one value per key. | A missing column, or a cast the backend cannot perform, fails when the query runs. A duplicate key returns one output row per source row. A false DETERMINED BY lets a rollup count one business fact more than once. |
EXTERNAL_COLUMNS … FROM SQL | The same column names, types, BIND TO uniqueness, and DETERMINED BY. The SQL text is not read to prove the projection. | Same as a table. |
BIND_COLUMNS … FROM TABLE or FROM SQL | Each listed source column exists and is read as the type of the INPUT it binds. | A wrong type is cast. There is no uniqueness claim: this clause supplies the population. |
INLINE_COLUMNS … FROM CSV | The BIND TO columns are unique. | There is no warehouse “column is read as” claim, because the payload is the query. Repeated keys in that CSV are a compile error. |
INDEX_UNIQUE(array BY key) | The key is unique. | The query still returns one row for that key. |
INDEX_MULTI(array BY key) | Nothing about uniqueness. | Several rows for one key are the meaning of the function. |
A registered datakey cache adds one more fact of the same kind: the cache column is stored as the feature's type.
A column is read as the type you declare
EXTERNAL_COLUMNS names every column the mapping will touch, and the type it will be read as. status VARCHAR means the generated SQL casts that column to VARCHAR. id BIGINT#ACCOUNTS means the same cast, and the value is an account key inside FeatureQL. The entity annotation has no extra warehouse cost. It is still part of the type the column is read as.
An ON filter keeps every declared column in the contract. Filtering ON SELF[status] = 'active' still assumes status exists and is read as the type you wrote.
FROM SQL(...) carries the same contract. Column names and types come from the EXTERNAL_COLUMNS list. A projection that omits a column you declared still compiles.
FROM TABLE(source[table]) is the same unread schema, against a table of a source configured for serving.
BIND_COLUMNS is narrower. It states each source column you listed, read as the bound INPUT type. It does not state that those columns are unique.
The ratings table below stores 1.5. The mapping declares the type the column holds, DECIMAL(10, 1), and the feature converts it.
DEFINE
accounts := ENTITY(),
account_id := INPUT(BIGINT#accounts),
ratings := EXTERNAL_COLUMNS(
account_id BIGINT#accounts BIND TO account_id,
rating DECIMAL(10,1)
FROM TABLE(docs_contracts.ratings)
)
RETURN
account_id,
stored := ratings[rating],
rating := ratings[rating]::BIGINT
FOR
account_id := BIND_VALUE(1)
;| ACCOUNT_ID BIGINT#ACCOUNTS | STORED DECIMAL(10,1) | RATING BIGINT |
|---|---|---|
| 1 | 1.5 | 2 |
CAST rounds half away from zero, so RATING is 2 on every backend.
Declaring that column BIGINT instead is a false declaration. The compiler trusts it and does not read the table, so the value is whatever the engine's own conversion gives. DuckDB, Postgres, Trino, BigQuery and Snowflake round 1.5 to 2:
DEFINE
accounts := ENTITY(),
account_id := INPUT(BIGINT#accounts),
ratings := EXTERNAL_COLUMNS(
account_id BIGINT#accounts BIND TO account_id,
rating BIGINT
FROM TABLE(docs_contracts.ratings)
)
RETURN
account_id,
rating := ratings[rating]
FOR
account_id := BIND_VALUE(1)
;| ACCOUNT_ID BIGINT#ACCOUNTS | RATING BIGINT |
|---|---|
| 1 | 2 |
Databricks, ClickHouse and serving truncate it to 1:
DEFINE
accounts := ENTITY(),
account_id := INPUT(BIGINT#accounts),
ratings := EXTERNAL_COLUMNS(
account_id BIGINT#accounts BIND TO account_id,
rating BIGINT
FROM TABLE(docs_contracts.ratings)
)
RETURN
account_id,
rating := ratings[rating]
FOR
account_id := BIND_VALUE(1)
;| ACCOUNT_ID BIGINT#ACCOUNTS | RATING BIGINT |
|---|---|
| 1 | 1 |
The same query returns two different values. Nothing in compilation reports it. Other mismatches split the backends differently, from a number past the INTEGER range to text read as a BOOLEAN: see the table in Backend compatibility . A cast that fails, and a column that is not on the relation, fail when the backend runs the query. The check reports RATING is stored as BIGINT with one violation, and the stored type in observed.
A BIND TO key is one row
BIND TO declares that the source has at most one row for each value of those columns. Several BIND TO columns are one composite key: the combination is what must be unique.
The accounts table has two rows for account 1. The query binds that one key.
DEFINE
accounts := ENTITY(),
account_id := INPUT(BIGINT#accounts),
accounts_src := EXTERNAL_COLUMNS(
account_id BIGINT#accounts BIND TO account_id,
status VARCHAR
FROM TABLE(docs_contracts.accounts)
)
RETURN
account_id,
status := accounts_src[status]
FOR
account_id := BIND_VALUE(1)
;| ACCOUNT_ID BIGINT#ACCOUNTS | STATUS VARCHAR |
|---|---|
| 1 | gold |
| 1 | silver |
Both statuses come back. Nothing in compilation reports the duplicate.
The fact is ACCOUNT_ID is unique. Its row in the check counts the keys that have more than one row, and shows one of them in observed. For a composite key the fact is (ORDER_ID, LINE_ID) is unique. In the enterprise is where teams put the test.
Rows that are already in the query
An inline CSV is the payload, so validate() states no stored type and gives no SQL to run. It still states that the BIND TO columns are unique. When the CSV itself repeats a key, that repetition is in the text, and compilation fails:
DEFINE
accounts := ENTITY(),
account_id := INPUT(BIGINT#accounts),
accounts_src := INLINE_COLUMNS(
account_id BIGINT#accounts BIND TO account_id,
status VARCHAR
FROM CSV(
account_id,status
1,gold
1,silver
)
)
RETURN
account_id,
status := accounts_src[status]
FOR
account_id := BIND_VALUE(1)
;DETERMINED BY is one value per key
amount DECIMAL(10, 2) DETERMINED BY (order_id, line_id) declares that each distinct (order_id, line_id) has one amount. The compiler uses that when a later SUM would otherwise count a repeated value. It does not read the source to see whether the declaration holds.
The fact is AMOUNT has one value per (ORDER_ID, LINE_ID). Its row in the check counts the keys with more than one distinct amount. Any such key lets a rollup that trusts the declaration count the same fact more than once. See Semantic contracts .
When the rows are inline and they contradict the declaration, the compiler warns SUMMARIZABILITY-DETERMINED-BY-CONTRADICTED. A table, a view, or a SQL source produces no warning.
The visits table has user 7 in two countries. The mapping declares country determined by user_id, so the per-country distinct users add up to the total:
DEFINE
users := ENTITY(),
visits := ENTITY(),
visit_id := INPUT(BIGINT#visits),
src := EXTERNAL_COLUMNS(
visit_id BIGINT#visits BIND TO visit_id,
user_id BIGINT#users,
country VARCHAR DETERMINED BY (USER_ID)
FROM TABLE(docs_contracts.visits)
),
country := src[country],
uniq := COUNT(DISTINCT src[user_id]) GROUP BY (country)
RETURN
total := (SUM(uniq) PER country)::BIGINT
FOR
visit_id := BIND_VALUES(ARRAY(1, 2, 3))
;| TOTAL BIGINT |
|---|
| 3 |
Two distinct users visited, and the rollup returns 3. The check below finds user 7.
INDEX_UNIQUE assumes a unique key
INDEX_UNIQUE(orders BY order_id) declares that order_id occurs once in that array. validate() states ORDER_ID is unique in ORDERS. The array is built in the query, so there is no SQL to run. A duplicate key does not fail the query: one row is still returned for that key. See Lookup with indexes .
INDEX_MULTI maps a key to every matching row. validate() states no uniqueness contract for it.
Check the data
EXPLAIN (FIELDS (DATA_CONTRACTS)) lists, per source, the facts the query assumes. Compilation still reads no table:
EXPLAIN (FIELDS (DATA_CONTRACTS))
DEFINE
USERS := ENTITY(),
VISITS := ENTITY(),
VISIT_ID := INPUT(BIGINT#VISITS),
VISITS_SRC := EXTERNAL_COLUMNS(
visit_id BIGINT#VISITS BIND TO VISIT_ID,
user_id BIGINT#USERS,
country VARCHAR DETERMINED BY (user_id)
FROM TABLE(docs_contracts.visits)
)
RETURN VISIT_ID, COUNTRY := VISITS_SRC[country]
FOR VISIT_ID := BIND_VALUES(ARRAY[1, 2, 3]);| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| length(data_contracts) | 1 |
| data_contracts[0].feature | "VISITS_SRC" |
| data_contracts[0].source | "TABLE(docs_contracts.visits)" |
| data_contracts[0].facts[*].kind | ["COLUMN_TYPE", "COLUMN_TYPE", "COLUMN_TYPE", "UNIQUE_KEY", "DETERMINED_BY"] |
| data_contracts[0].facts[*].fact | ["VISIT_ID is stored as BIGINT", "USER_ID is stored as BIGINT", "COUNTRY is stored as VARCHAR", "VISIT_ID is unique", "COUNTRY has one value per USER_ID"] |
| contains(data_contracts[0].sql, 'docs_contracts.visits') | true |
Its sql is one query for that source, in the backend's dialect. It returns one row per fact, with the same columns on every backend:
| Column | Type | Meaning |
|---|---|---|
n | BIGINT | The fact's position in the list. |
feature, source | VARCHAR | The mapping, and the table or SQL it reads. |
kind | VARCHAR | COLUMN_TYPE, COLUMN_EXISTS, UNIQUE_KEY, or DETERMINED_BY. |
assumption | VARCHAR | The fact, as validate() prints it. |
violations | BIGINT | Keys or columns that break the fact. |
expected | BIGINT | Always 0. |
observed | VARCHAR | The stored type, or one key that breaks the fact. |
A stored type is SQLTYPEOF of one non-NULL value, compared with the declared type family the way EXPECTED_SQLTYPE compares it. Precision, scale, and length are not compared, and no value is cast. A column that is missing fails the whole query. An ON filter made of comparisons is applied, so the check reads the rows the mapping reads.
The check on the visits table, on DuckDB:
WITH src AS (
SELECT
*
FROM docs_contracts.visits
)
SELECT
CAST(1 AS BIGINT) AS n,
CAST('VISITS_SRC' AS TEXT) AS feature,
CAST('TABLE(docs_contracts.visits)' AS TEXT) AS source,
CAST('COLUMN_TYPE' AS TEXT) AS kind,
CAST('VISIT_ID is stored as BIGINT' AS TEXT) AS assumption,
CAST(COUNT(
CASE
WHEN NOT REGEXP_MATCHES(
TRIM(UPPER(TYPEOF((
VISIT_ID
)))),
'^(?:(?:BIGINT|UINT64|INT64|LONG)(?:\([^)]*\))?)$'
)
THEN 1
END
) AS BIGINT) AS violations,
CAST(0 AS BIGINT) AS expected,
CAST(MAX(TYPEOF((
VISIT_ID
))) AS TEXT) AS observed
FROM (
SELECT
VISIT_ID
FROM src
WHERE
NOT VISIT_ID IS NULL
LIMIT 1
) AS one_row
UNION ALL
SELECT
CAST(2 AS BIGINT) AS n,
CAST('VISITS_SRC' AS TEXT) AS feature,
CAST('TABLE(docs_contracts.visits)' AS TEXT) AS source,
CAST('COLUMN_TYPE' AS TEXT) AS kind,
CAST('USER_ID is stored as BIGINT' AS TEXT) AS assumption,
CAST(COUNT(
CASE
WHEN NOT REGEXP_MATCHES(
TRIM(UPPER(TYPEOF((
USER_ID
)))),
'^(?:(?:BIGINT|UINT64|INT64|LONG)(?:\([^)]*\))?)$'
)
THEN 1
END
) AS BIGINT) AS violations,
CAST(0 AS BIGINT) AS expected,
CAST(MAX(TYPEOF((
USER_ID
))) AS TEXT) AS observed
FROM (
SELECT
USER_ID
FROM src
WHERE
NOT USER_ID IS NULL
LIMIT 1
) AS one_row
UNION ALL
SELECT
CAST(3 AS BIGINT) AS n,
CAST('VISITS_SRC' AS TEXT) AS feature,
CAST('TABLE(docs_contracts.visits)' AS TEXT) AS source,
CAST('COLUMN_TYPE' AS TEXT) AS kind,
CAST('COUNTRY is stored as VARCHAR' AS TEXT) AS assumption,
CAST(COUNT(
CASE
WHEN NOT REGEXP_MATCHES(
TRIM(UPPER(TYPEOF((
COUNTRY
)))),
'^(?:(?:CHARACTER\ VARYING|LARGEUTF8|UTF8VIEW|VARCHAR|STRING|UTF8|TEXT)(?:\([^)]*\))?)$'
)
THEN 1
END
) AS BIGINT) AS violations,
CAST(0 AS BIGINT) AS expected,
CAST(MAX(TYPEOF((
COUNTRY
))) AS TEXT) AS observed
FROM (
SELECT
COUNTRY
FROM src
WHERE
NOT COUNTRY IS NULL
LIMIT 1
) AS one_row
UNION ALL
SELECT
CAST(4 AS BIGINT) AS n,
CAST('VISITS_SRC' AS TEXT) AS feature,
CAST('TABLE(docs_contracts.visits)' AS TEXT) AS source,
CAST('UNIQUE_KEY' AS TEXT) AS kind,
CAST('VISIT_ID is unique' AS TEXT) AS assumption,
CAST(COUNT(*) AS BIGINT) AS violations,
CAST(0 AS BIGINT) AS expected,
CAST(MIN(CAST(VISIT_ID AS TEXT)) AS TEXT) AS observed
FROM (
SELECT
VISIT_ID
FROM src
WHERE
NOT VISIT_ID IS NULL
GROUP BY
VISIT_ID
HAVING
COUNT(*) > 1
) AS repeated
UNION ALL
SELECT
CAST(5 AS BIGINT) AS n,
CAST('VISITS_SRC' AS TEXT) AS feature,
CAST('TABLE(docs_contracts.visits)' AS TEXT) AS source,
CAST('DETERMINED_BY' AS TEXT) AS kind,
CAST('COUNTRY has one value per USER_ID' AS TEXT) AS assumption,
CAST(COUNT(*) AS BIGINT) AS violations,
CAST(0 AS BIGINT) AS expected,
CAST(MIN(CAST(USER_ID AS TEXT)) AS TEXT) AS observed
FROM (
SELECT
USER_ID
FROM src
GROUP BY
USER_ID
HAVING
COUNT(DISTINCT COUNTRY) > 1
) AS repeated
ORDER BY
n| n BIGINT | feature VARCHAR | source VARCHAR | kind VARCHAR | assumption VARCHAR | violations BIGINT | expected BIGINT | observed VARCHAR |
|---|---|---|---|---|---|---|---|
| 1 | VISITS_SRC | TABLE(docs_contracts.visits) | COLUMN_TYPE | VISIT_ID is stored as BIGINT | 0 | 0 | BIGINT |
| 2 | VISITS_SRC | TABLE(docs_contracts.visits) | COLUMN_TYPE | USER_ID is stored as BIGINT | 0 | 0 | BIGINT |
| 3 | VISITS_SRC | TABLE(docs_contracts.visits) | COLUMN_TYPE | COUNTRY is stored as VARCHAR | 0 | 0 | VARCHAR |
| 4 | VISITS_SRC | TABLE(docs_contracts.visits) | UNIQUE_KEY | VISIT_ID is unique | 0 | 0 | NULL |
| 5 | VISITS_SRC | TABLE(docs_contracts.visits) | DETERMINED_BY | COUNTRY has one value per USER_ID | 1 | 0 | 7 |
Every fact holds except the declaration on country, and observed names the user that breaks it.
Next
Write the mapping in Mapping to data sources . The checks the compiler does apply, from these declarations, are Semantic contracts .