Which Sensor Records Should Reach the Training Table?
A query can run successfully over duplicate identities, missing readings and impossible timestamps. Turn the dataset’s assumptions into visible checks before building model features.
13 sections · 4 questions · 1 coding challenge
The arrow keys move between sections too.
Checking the Facts Before Combining Them
BOTIE
Machine-learning data tools later added schema and distribution comparisons to that practice. Our SQL audit addresses selected deterministic rules for invented sensor records. It cannot establish that labels are correct or that a dataset represents the population a model will encounter.
The Training Query Ran, but the Input Was Wrong
Botie builds a feature table without a SQL error. Later we discover a repeated sensor message, a reading linked to an absent pump and a temperature with the wrong scale. Successful query execution did not establish useful training data.
We will inspect a raw staging table, report every failed contract per reading identity, and keep rejected data available for repair. The rules are explicit so a reader can see what a clean report does and does not establish.
Invented maintenance records. SQL programs run in an isolated SQLite database; the workshop illustrates the operation with records rather than parsing SQL.
Step 1 of 1
A plausible number can still belong to a bad record
Each row carries identity and timing as well as its measured value.
| Record problem | Why a model pipeline cares | Check |
|---|---|---|
| Duplicate reading ID | Repeated evidence changes weighting | Count identities |
| Absent asset | Features attach to no registered entity | Check parent membership |
| Unknown value | Missingness changes the statistic | Test IS NULL |
| Event after ingestion | The stated timeline is inconsistent | Compare declared clocks |
Duplicate reading ID
- Why a model pipeline cares
- Repeated evidence changes weighting
- Check
- Count identities
Absent asset
- Why a model pipeline cares
- Features attach to no registered entity
- Check
- Check parent membership
Unknown value
- Why a model pipeline cares
- Missingness changes the statistic
- Check
- Test IS NULL
Event after ingestion
- Why a model pipeline cares
- The stated timeline is inconsistent
- Check
- Compare declared clocks
A successful SELECT does not validate its inputs
Identity, relationship, measurement and time checks all affect whether a raw record can enter this curated set.
Which reading identities fail the declared contract?
Check known assets, required readings, range and timing, then expose the failing identities.
BOTIE
Reading IDs needing review
- 0R2
- 1R3
Follow the operations
1. Raw reading records
[ { "id": "R1", "asset_id": "P7", "value": 20, "event_seq": 1, "ingest_seq": 2 }, { "id": "R2", "asset_id": "P99", "value": 20, "event_seq": 1, "ingest_seq": 2 }, { "id": "R3", "asset_id": "P7", "value": null, "event_seq": 1, "ingest_seq": 2 } ]2. Registered asset IDs
- 0P7
3. Inclusive temperature range
{ "min": 0, "max": 100 }4. Check the reading contract
[ { "id": "R1", "valid": true }, { "id": "R2", "valid": false }, { "id": "R3", "valid": false } ]5. Keep IDs that failed a check
- 0R2
- 1R3
6. Result
- 0R2
- 1R3
The toy contract assumes comparable clocks and a declared unit. It does not prove label truth, representativeness or freedom from leakage.
1. Keep raw evidence separate from accepted records
A raw table preserves what arrived and its source identity. A curated table contains records that satisfy the declared contract after any justified transformation. A quarantine or issue table records what failed and why.
Do not silently drop failed rows and report the remaining count as if the original batch were complete. Record accepted and rejected counts, source batch IDs and the rule version. A downstream feature query should know whether a missing interval means no events or an unresolved ingestion failure.
The audit output is part of the dataset, not an incidental debug message.
2. Check identity before computing aggregates
Duplicate event identities can count the same physical observation twice. Distinct identities with equal temperatures are different observations and should not automatically collapse. Define which source fields identify an event before applying deduplication.
A duplicate ID with conflicting payloads is a different problem from an exact replay. Choose a policy based on source revisions or quarantine the conflict. Taking the first row the database happens to return is not a reproducible rule.
The exercise reports a duplicate identity once, while retaining all issue categories it violates.
Check yourself
Questions on Parts 1 to 2
Question 1 of 2
Which equality establishes a duplicate under the declared identity contract?
Use the information below.
Two rows share event ID R1. Their values are equal. Another row has a different ID and the same value.3. Report each violated rule independently
Our audit returns id and issue. Separate SELECT branches identify duplicates, unknown assets, missing values, out-of-range values and future events. UNION makes each identity/reason pair appear once. A single CASE expression would stop after one branch and could hide a second problem on the same record.
The range is an invented accepted interval in one declared unit. It is inclusive at both ends. NULL is checked separately because ordinary numeric comparisons with NULL do not return true.
Explain the boundary values and clock assumptions beside each rule.
Find orphaned asset references
A left join keeps the raw record even when the register has no match.
| id |
|---|
| P7 |
| P8 |
| id | asset_id | value | event_seq | ingest_seq |
|---|---|---|---|---|
| R1 | P7 | 20 | 1 | 2 |
| R2 | P99 | 25 | 2 | 3 |
| R3 | P8 | NULL | 3 | 4 |
| R4 | P7 | 200 | 6 | 5 |
| R1 | P7 | 20 | 1 | 2 |
Database setup for this example
CREATE TABLE assets(id TEXT PRIMARY KEY NOT NULL); INSERT INTO assets VALUES('P7'),('P8');
CREATE TABLE raw_readings(id TEXT,asset_id TEXT,value REAL,event_seq INTEGER,ingest_seq INTEGER);
INSERT INTO raw_readings VALUES('R1','P7',20,1,2),('R2','P99',25,2,3),('R3','P8',NULL,3,4),('R4','P7',200,6,5),('R1','P7',20,1,2);SELECT DISTINCT r.id,r.asset_id FROM raw_readings r
LEFT JOIN assets a ON a.id=r.asset_id WHERE a.id IS NULL ORDER BY r.id;| id | asset_id |
|---|---|
| R2 | P99 |
The raw row survives for diagnosis. An inner join would hide it from this report.
4. Check time relative to the decision being modelled
Event time describes when something happened. Ingestion time describes when the pipeline received it. Prediction time determines whether the information was available for a training example. A historical event that arrived later can be valid raw data while being unavailable to an earlier prediction.
The simple future-event check only verifies consistency under a shared-clock assumption. It is not a point-in-time feature join. Use the earlier feature-table lesson’s availability conditions when assembling model inputs, and keep label availability separate from feature availability.
A clean range-and-identity audit does not rule out target leakage.
5. Look beyond individual rows
A batch can satisfy every row constraint while losing half the expected sensors. Monitor freshness, volume, null rates and distribution changes at meaningful groups such as site or device family. Compare these with documented expectations and investigate changes rather than automatically declaring every shift wrong.
A real equipment upgrade can change the measurement distribution. A stale ingestion job can leave a perfectly shaped but obsolete table. SQL summaries provide evidence for these checks; deciding what action to take requires the application’s context.
Choose thresholds from observed operating requirements, and record the uncertainty behind them.
6. Keep validation reproducible
Store the SQL or rule version, source snapshot, accepted units and data cutoffs with the validation report. Rebuilding a model from today’s corrected table may differ from the data used by the original run. Preserve a versioned snapshot or a reproducible change history when that distinction matters.
Test the audit with simultaneous failures, boundary values and an empty batch. A no-issue output for zero records should not automatically be labelled a healthy delivery. The pipeline also needs a separate completeness expectation.
The incremental lesson will preserve source change position so repairs and replays can be reconciled.
Check yourself
Questions on Parts 1 to 6
Question 1 of 2
What must the feature join consider?
Use the information below.
A historical measurement occurred before prediction time but arrived in the system afterward.Try it with Botie
Build with Botie
Write and run SQL