You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3a775bc
Browse filesBrowse the repository at this point in the historyBrowse files
description: "Engine-specific quality checks expressed in the native syntax of an engine such as DQX, SodaCL, or Great Expectations."
4
+
description: "Engine-specific quality checks expressed in the native syntax of an engine such as DQX, SodaCL, or Great Expectations, and user-defined SQL checks run by datacontract test."
5
5
---
6
6
7
7
# Custom Quality Rules
@@ -10,27 +10,138 @@ A `type: custom` rule carries a check written in the **native syntax of a specif
10
10
11
11
Custom rules are consumed by the matching engine — typically via the corresponding [exporter](../exports/index.md) — rather than being compiled to portable SQL.
12
12
13
-
:::note
14
-
`datacontract test` does not run custom quality rules except for those with `engine: datacontract-cli`. Consider `type: sql` as an executed alternative.
15
-
:::
13
+
## User-defined quality rules
16
14
17
-
## Custom quality checks
15
+
With `engine: datacontract-cli`, a custom rule uses a custom quality check: a SQL check defined **once**, outside any contract, and used **by its ID** from as many contracts as need it. The contract names the check and supplies its arguments; the check holds the query.
18
16
19
-
With `engine: datacontract-cli`, a custom rule uses a [custom quality check](./custom-quality-checks.md): a parameterised SQL check defined once in a folder and run by `datacontract test`.
17
+
```yaml
18
+
schema:
19
+
- name: orders
20
+
properties:
21
+
- name: amount
22
+
logicalType: number
23
+
quality:
24
+
- type: custom
25
+
engine: datacontract-cli
26
+
implementation:
27
+
check: between
28
+
arguments:
29
+
min: 0
30
+
max: 10000
31
+
```
32
+
33
+
Point `datacontract test` at the folder the checks live in:
34
+
35
+
```bash
36
+
datacontract test datacontract.yaml --custom-quality-checks ./custom-quality-checks
37
+
```
38
+
39
+
or set `DATACONTRACT_CUSTOM_QUALITY_CHECKS`. See the [`test` command reference](../commands/test.md).
40
+
41
+
### Defining a check
42
+
43
+
The folder holds one YAML file per check. The file name is the check's ID (`between.yaml` defines `between`). IDs are unique and must not contain whitespace or any of `/ \ % ; # ?`.
44
+
45
+
```yaml
46
+
# custom-quality-checks/between.yaml
47
+
name: Value range
48
+
description: All ${column} values are between ${arguments.min} and ${arguments.max}.
49
+
owner: data-platform
50
+
dimension: conformity
51
+
arguments:
52
+
min:
53
+
max:
54
+
queries:
55
+
default: |
56
+
SELECT COUNT(*) FROM ${table}
57
+
WHERE ${column} < ${arguments.min} OR ${column} > ${arguments.max}
58
+
mustBe: 0
59
+
```
60
+
61
+
| Key | Meaning |
62
+
|---|---|
63
+
| `name` | A short display name (optional, only used when exporting to Entropy Data). |
64
+
| `description` | What the check verifies. Becomes the check's name in the results, with the placeholders and arguments filled in, unless the rule has a `description` of its own. |
65
+
| `owner` | Who maintains the check. Documentation only. |
66
+
| `dimension` | The [quality dimension](./index.md#quality-dimensions) the check measures. A `dimension` on the rule takes precedence. |
67
+
| `arguments` | The arguments the check takes, each with an optional `type` and `default`. See [Arguments](#arguments). |
68
+
| `queries` | At least one query, keyed by SQL dialect or `default`. |
69
+
| `mustBe`, `mustBeGreaterThan`, … | The default expected result, one of the [SQL rule comparators](./sql.md#comparators). |
70
+
71
+
### Arguments
72
+
73
+
A check declares its arguments under `arguments`, and a rule passes their values under `implementation.arguments`. `${arguments.<argumentName>}` in the check definition gets replaced by the respective value:
74
+
75
+
```yaml
76
+
# custom-quality-checks/recent_rows.yaml
77
+
arguments:
78
+
timestamp_column: {type: identifier}
79
+
days: {type: number, default: 1}
80
+
queries:
81
+
default: |
82
+
SELECT COUNT(*) FROM ${table}
83
+
WHERE ${arguments.timestamp_column} >= CURRENT_DATE - ${arguments.days} * INTERVAL '1' DAY
84
+
mustBeGreaterThan: 0
85
+
```
86
+
87
+
```yaml
88
+
# datacontract.yaml
89
+
implementation:
90
+
check: recent_rows
91
+
arguments:
92
+
timestamp_column: created_at
93
+
days: 7
94
+
```
95
+
96
+
The `type` in the check definition specifies how arguments are parsed:
97
+
98
+
| Type | Becomes | Example |
99
+
|---|---|---|
100
+
| `any` (default) | An SQL literal; a list becomes comma-separated literals, as in `IN (...)` | `EUR` → `'EUR'`, `[A, B]` → `'A', 'B'` |
101
+
| `number` | A number literal | `"7"` → `7` |
102
+
| `identifier` | A column or table name, quoted where needed like the [placeholders](./sql.md#placeholders) | `order date` → `"order date"` |
103
+
104
+
Values are never inserted as raw SQL. Inside a single-quoted string, an `any` or `number` argument is inserted as escaped text, as in `LIKE '%${arguments.text}%'`.
105
+
106
+
### Placeholders
107
+
108
+
The queries use the same placeholders as [SQL rules](./sql.md#placeholders) — `${table}`, `${column}`, `${schema}`, and so on. A check whose queries use `${column}` (or `${field}`, `${property}`) can only be declared on a property. As in placeholders, the `$` in an argument reference is optional: `{arguments.min}` works the same as `${arguments.min}`.
109
+
110
+
### Dialects
111
+
112
+
A run uses the query for the [SQL dialect of the server](./sql.md#sql-dialect), and otherwise the `default` query (if it exists):
113
+
114
+
```yaml
115
+
queries:
116
+
default: |
117
+
SELECT COUNT(*) FROM ${table}
118
+
WHERE ${arguments.timestamp_column} >= CURRENT_DATE - ${arguments.days} * INTERVAL '1' DAY
119
+
tsql: |
120
+
SELECT COUNT(*) FROM ${table}
121
+
WHERE ${arguments.timestamp_column} >= DATEADD(day, -${arguments.days}, CAST(GETDATE() AS date))
122
+
```
123
+
124
+
The keys are the dialect names in the [SQL dialect table](./sql.md#sql-dialect), so a check for a `mysql` server needs a `duckdb` query. Write the `default` query in ANSI SQL. Note that on SAP HANA, only the `default` query runs.
125
+
126
+
### Overriding the expected result
127
+
128
+
The check's expected result is a default. A comparator on the rule replaces it:
20
129
21
130
```yaml
22
131
quality:
23
132
- type: custom
24
133
engine: datacontract-cli
25
134
implementation:
26
-
check: between
135
+
check: recent_rows
27
136
arguments:
28
-
min: 0
29
-
max: 10000
137
+
timestamp_column: created_at
138
+
mustBeGreaterThan: 10000
30
139
```
31
140
32
141
## Databricks DQX
33
-
142
+
:::note
143
+
`datacontract test`does not run custom quality rules except [user-defined quality rules](#user-defined-quality-rules) (`engine: datacontract-cli`). Consider `type: sql` as an executed alternative.
144
+
:::
34
145
```yaml
35
146
schema:
36
147
- name: events
@@ -54,7 +165,9 @@ schema:
54
165
Export these to a DQX check file with [`datacontract export dqx`](../exports/dqx.md).
55
166
56
167
## SodaCL
57
-
168
+
:::note
169
+
`datacontract test`does not run custom quality rules except [user-defined quality rules](#user-defined-quality-rules) (`engine: datacontract-cli`). Consider `type: sql` as an executed alternative.
170
+
:::
58
171
```yaml
59
172
schema:
60
173
- name: orders
@@ -68,10 +181,6 @@ schema:
68
181
69
182
Export to a SodaCL file with [`datacontract export sodacl`](../exports/sodacl.md).
70
183
71
-
:::warning
72
-
Raw SodaCL custom checks (`type: custom`, `engine: soda`) are **no longer executed** by `datacontract test` since `soda-core` was removed; such a rule is reported as a warning. Migrate it to a [SQL rule](./sql.md) (`type: sql`) to keep it executable, or keep it as a custom rule purely for the SodaCL export.
73
-
:::
74
-
75
184
## Other engines
76
185
77
186
The same pattern applies to other engines (for example Great Expectations). Provide `engine` and an `implementation` understood by that engine, and use the matching [exporter](../exports/index.md) to generate its native artifact.
0 commit comments