Pick a final label from human and model labels with COALESCE

EasySQL

Why it matters Human-in-the-loop labeling, eval adjudication and feature defaults all rely on a clear fallback order.

Labeling pipelines often store a human label when someone reviewed the item and a model label otherwise. The final label must prefer the human, fall back to the model, and never be NULL.

Write query() returning a SQLite SQL string. Table labels(item_id INTEGER, human_label TEXT, model_label TEXT); either label can be NULL.

The final label of an item is human_label if it is not NULL, else model_label if it is not NULL, else the text 'unlabeled'. Return (final_label, n_items) for each final label. Order by n_items descending, then final_label ascending.

Signaturedef query()

Example

(1, 'cat', NULL), (2, NULL, 'dog'), (3, NULL, NULL), (4, 'dog', 'cat') give [('dog', 2), ('cat', 1), ('unlabeled', 1)].

Understand the idea

NULL means unknown, not empty. Most operations on NULL give NULL, so you need a way to say 'use this, or if it is missing, use that'. COALESCE does exactly this: it returns the first argument that is not NULL.

Worked example: item 4 has human 'dog' and model 'cat'. COALESCE('dog', 'cat', 'unlabeled') = 'dog'. Item 2 has human NULL and model 'dog', so the answer is 'dog'. Item 3 has both NULL, so the answer is 'unlabeled'.

Rule: COALESCE(a, b, c) = a if a is not NULL, else b if b is not NULL, else c. Put the most trusted source first and a safe literal last.

In ML data pipelines this picks gold labels over weak labels, fills missing features with defaults, and merges values from several sources in priority order.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: example_run1(query())
  • Sample 2: human overrides model_run2(query())
  • Test 3: nothing labeled_run3(query())