Fix the latest status rollup per user

Review the AI's codeEasy10 minDatabricks and Snowflake style

Your task

Agent-written pipeline PRs keep using a DISTINCT ON query to keep the latest event per user, with a LIMIT 1000 that quietly drops users. You want one plain function to check those queries against.

An assistant wrote latest_status_per_user(rows). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • rows: event rows, each "user_id|status|updated_at". updated_at is an ISO date such as 2026-03-04, or empty when the database holds NULL.
  • Return one "user_id=status" string per user.

Rules the tests check:

  • Skip a row that does not split on | into exactly 3 fields.
  • For each user, keep the row with the latest updated_at. Compare the dates as plain strings.
  • An empty updated_at is older than every real date, as NULLS LAST orders it. A row with an empty date still counts: a user whose rows all have empty dates is still in the output.
  • On a tie, keep the row that came first.
  • Every user appears, with no cap on the count. Sort the output by user id, in ascending order by plain string comparison.

Examples

latest_status_per_user(rows: list[str]) -> list[str]
Example 1
Inputlatest_status_per_user(["u1|active|2026-01-02", "u1|paused|2026-03-04", "u2|active|2026-02-02"])
Output["u1=paused", "u2=active"]
Example 2
Inputlatest_status_per_user(["u3|active|", "u3|churned|2025-12-31"])
Output["u3=churned"]
Example 3
Inputlatest_status_per_user(["u2|active|2026-05-05", "u1|churned|2026-05-05", "u2|paused|2026-05-05"])
Output["u1=churned", "u2=active"]