The situation
Each morning every support team gets the number of open tickets it has to look at. Low-priority tickets are left out, because they are handled in a weekly batch.
New tickets arrive without a priority; someone on the team sets it during triage. The morning number has been too low for weeks, and the tickets that are missing from it are exactly the ones nobody has triaged yet.
Input tables
tickets
One row per ticket.
| Column | Type | Description |
|---|---|---|
ticket_id |
BIGINT | Unique per ticket. |
team |
STRING | The team that owns the ticket. |
priority |
STRING | LOW, MEDIUM, HIGH or URGENT. NULL until the ticket has been triaged. |
status |
STRING | OPEN or CLOSED. Never NULL. |
Requirement
Return one row per team.
tickets_to_reviewis the number of the team'sOPENtickets whose priority is notLOW. A ticket with no priority is not known to be low, so it counts.untriagedis the number of the team'sOPENtickets with no priority.- Every team in
ticketsappears, with 0 where it has no such tickets.
Expected output
One row per team.
| Column | Type | Description |
|---|---|---|
team |
STRING | |
tickets_to_review |
BIGINT | Open tickets that are not LOW, untriaged ones included. |
untriaged |
BIGINT | Open tickets with no priority. |
Explanation
Show explanationGives the answer away. Try the problem first.
The approach
SQL and Spark do not have two truth values. They have three: true, false and unknown, and unknown is written NULL. Any comparison with a NULL in it is unknown:
| Expression | Result when priority is NULL |
|---|---|
priority = 'LOW' |
NULL |
priority != 'LOW' |
NULL |
NOT (priority = 'LOW') |
NULL |
priority NOT IN ('LOW') |
NULL |
A filter, a when and a join condition all keep a row only when the condition is
true. Unknown is not true, so the row is dropped. "Is this ticket's priority
something other than LOW?" is answered with "no idea" for an untriaged ticket, and
no idea is treated as no.
The requirement says an untriaged ticket counts. So it has to be let in by name:
not_low = F.col("priority").isNull() | (F.col("priority") != "LOW")
The second half of the problem is the shape of the result: every team appears, with 0 where it has nothing. That rules out filtering first, because a team whose rows are all filtered away has no group left. Instead, keep all rows and count conditionally:
tickets.groupBy("team").agg(
F.count(F.when(is_open & not_low, 1)).alias("tickets_to_review"),
F.count(F.when(is_open & F.col("priority").isNull(), 1)).alias("untriaged"),
)
when without otherwise returns NULL for rows that do not match, and count
skips NULLs and returns 0 when there is nothing to count.
There are 219 tickets, 134 of them open. Of the open ones, 42 are LOW and 31 have
no priority. The right total across the teams is 92.
Common wrong answers
F.col("priority") != "LOW". The bug from the story. The 31 untriaged tickets
are not counted, and the total is 61 instead of 92. Five of the six teams are too
low. The sixth, onboarding, is right only because it has nothing to review at all.
~(F.col("priority") == "LOW") and ~F.col("priority").isin("LOW"). The same
thing. Negating unknown gives unknown.
isin("MEDIUM", "HIGH", "URGENT", None). Putting None in the list does not
help. isin compares with =, and nothing equals NULL, not even NULL.
Filtering first, then groupBy().count(). With the != filter the result has 4
rows instead of 6. onboarding is gone, and so is search: its open tickets are
five untriaged ones and one LOW, so the filter removes all six. With a correct,
NULL-aware filter there are 5 rows, and onboarding is still missing. A team with
nothing to do is information, and a report that drops it looks the same as a report
that lost it.
F.sum(F.when(condition, 1)). sum of nothing but NULLs is NULL, so
onboarding shows NULL instead of 0. F.sum(F.when(condition, 1).otherwise(0)) is
correct; count is shorter.
fillna({"priority": "LOW"}) before counting. Declares every untriaged ticket
low priority. It reproduces the bug and makes untriaged 0 for everyone.
fillna({"priority": "UNKNOWN"}). This does fix tickets_to_review. But the
NULLs are now gone, so untriaged counted with isNull() is 0. If you replace
NULL with a marker, every later step has to look for the marker.
Leaving out status = "OPEN". Counts closed tickets too. Wrong for 5 teams.
Other ways to write it
Both give the same result as the reference solution:
~F.col("priority").eqNullSafe("LOW")
F.coalesce(F.col("priority"), F.lit("")) != "LOW"
eqNullSafe is the null-safe comparison (<=> in SQL): it returns true or false,
never NULL. Negating it is safe.
The habit to take away
Whenever you write "not equal", "not in" or "not like" on a column that can be NULL, ask what should happen to the NULLs and write it down in the code. The default is that they disappear, without an error and without a warning.
Edge cases in the data
| Case | Where | What it catches |
|---|---|---|
| Open tickets with no priority | 31 tickets in five teams | !=, ~isin, negated ==. |
A team whose open tickets are all LOW |
onboarding |
Filtering before grouping; sum without otherwise. Expected 0 and 0. |
A team whose open tickets are untriaged or LOW |
search |
Disappears completely under the != filter. Expected 5 and 5. |
| Closed tickets with no priority | 21 tickets | Forgetting the status in untriaged. |