DB Olympics

Queries · q315 · Chief Information Security Officer · week 2, night 2

Invoice void and uncollectible traceability, with timestamp completeness check

“Has any invoice actually been voided or marked uncollectible on the platform, and if so, is the event dated and traceable in audit_log — or was last night's zero result just because voided_at is missing?”

Manager · asked the questionHenrik AaltoChief Information Security Officer. Joined from a cloud provider's security team. Several enterprise deals depend on passing security reviews.
Analyst · planned the reportNoah FischerSystems thinker: maps how one process feeds the next before measuring anything.
Engineer · wrote the SQLViktorOld-school warehouse engineer: explicit JOIN chains straight to the base tables, derived tables, GROUP BY with CASE pivots, few CTEs.

Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told

  1. Henrik Aalto writes to Noah Fischer

    The manager, at the start of week 2

    Subject: Re: Account security snapshot — good start, three gaps worry me more now

    Noah,

    This is a strong first week — thank you. The sign-in and access-governance findings give me confidence I can defend in a questionnaire. Three things genuinely surprised me and I want to understand them better before Friday's memo, plus one follow-up.

    1. The audit trail gap is bigger than I initially feared. If we can't show who deleted something and when for one of our most common objects, I have to assume the same is true for the things a security reviewer or a merchant's own auditor would actually ask about — customer records, orders, discounts, financial documents. Can you check whether this is a product-wide blind spot or specific to a few areas? I need to know how wide the hole is before I can tell leadership whether this is "fix one thing" or "fix how deletion logging works everywhere."
    2. Shopper MFA at zero is either a non-issue or a real liability — I need to know which. Before this goes anywhere near a customer-facing security questionnaire, I want to understand what actually protects a shopper's account today if MFA genuinely isn't offered to them — is there anything else standing in for it (unusual-device checks, step-up verification at checkout, that sort of thing)? I don't want to either overstate a gap that doesn't matter or understate one that does.
    3. The churn-deactivation problem — how far does it go? You found it through privileged roles, but I suspect it's broader: if accounts aren't deprovisioned when a tenant churns, that likely applies to their sessions, their API access, everything tied to that tenant, not just role holders. I'd like a sense of the overall scale of "access that should have been turned off but wasn't" so I can size the fix properly.
    4. On the old API keys — are any of them doing something sensitive? Age alone isn't the risk; age combined with broad access is. Before I recommend a rotation mandate, I'd like to know whether those oldest keys are concentrated among a few tenants or integrations, and whether any of them look like they carry elevated access rather than routine, narrow use.

    If we can only chase a couple of these deeply this week, I'd prioritize the deletion-audit scope (#1) and the churn-deactivation scope (#3) — those are the two a due-diligence team is most likely to ask us to prove, one way or the other.

    Thanks, Henrik

  2. Noah Fischer plans the night

    The analyst · week 2, night 2, request 2 of 3

    Night 1 reshapes the picture a lot. The 30-table census shows that products (615 rows, 91 tenants, 0% audit coverage) and users (39 rows, 69.2% coverage) are the ONLY two tables where a soft-delete has ever happened in three years — every other one of the 28 tables shows zero soft-deleted rows ever, so I can't test coverage there at all; this isn't evidence those tables are "fine," just that the question doesn't apply yet. That reframes #1 from "is it everywhere" to "it's concentrated in products, and separately, orders look terrible" — order cancellations matched an audit_log row only 0.2% of the time (3 of 1,712). But before I report that as a hard gap, I need to check the table built specifically for this — order_status_history — since generic audit_log missing it doesn't mean Tidewick has no record at all; that's tonight's top priority, since it could flip the finding. I also need to pin down the invoice-void result, which came back as "0 events" only because the engineer excluded void invoices with a null voided_at timestamp — I don't yet know if that's because no invoice has ever been voided, or because voided invoices exist but aren't timestamped (itself a data-quality finding). On #3, report 3 showed 100% of churned tenants' users are still "active" status and sessions were never explicitly revoked (46-300 per tenant) — but "never revoked" isn't the same as "still valid"; I need to check whether those sessions' expiry dates have actually lapsed before I can size the real exposure.

    Invoice void and uncollectible traceability, with timestamp completeness check
    Question
    Has any invoice actually been voided or marked uncollectible on the platform, and if so, is the event dated and traceable in audit_log — or was last night's zero result just because voided_at is missing?
    One row per
    terminal invoice status: 'void' and 'uncollectible'
    Columns
    • invoice status
    • total invoices in this status (all-time, regardless of voided_at)
    • of those, rows with a populated voided_at timestamp
    • of those, rows with voided_at null (undated, can't tell when it happened)
    • matching audit_log rows (any action, entity_table = 'invoices', entity_id = invoices.id)
    • coverage rate among all invoices in this status (%)
    • distinct tenants represented
  3. Viktor writes the SQL

    The engineer · 1 dry run on the empty schema, then the submission

    Dry run 1 · STATUS: COMPILES (dry run on the empty schema, no data read)
    SELECT
      i.status AS invoice_status,
      COUNT(*) AS total_invoices,
      COUNT(i.voided_at) AS voided_at_populated,
      SUM(CASE WHEN i.voided_at IS NULL THEN 1 ELSE 0 END) AS voided_at_null,
      SUM(CASE WHEN EXISTS (
          SELECT 1 FROM audit_log a
          WHERE a.entity_table = 'invoices'
            AND a.entity_id = i.id
            AND a.tenant_id = i.tenant_id
      ) THEN 1 ELSE 0 END) AS matching_audit_log_rows,
      ROUND(
        100.0 * SUM(CASE WHEN EXISTS (
          SELECT 1 FROM audit_log a
          WHERE a.entity_table = 'invoices'
            AND a.entity_id = i.id
            AND a.tenant_id = i.tenant_id
        ) THEN 1 ELSE 0 END) / COUNT(*)
      , 1) AS coverage_rate_pct,
      COUNT(DISTINCT i.tenant_id) AS distinct_tenants
    FROM invoices i
    WHERE i.status IN ('void', 'uncollectible')
    GROUP BY i.status
    ORDER BY total_invoices DESC, invoice_status

    “Query compiles and matches the requested grain and columns.”

    Submitted · the SQL of the last dry run, unchanged · Submitted to tonight's batch. Your work on this request is done.

    “Submitted. The report gives, per terminal status (void, uncollectible): total invoice count, how many have a populated voided_at vs null, how many have at least one matching audit_log row, the resulting coverage rate, and distinct tenants — which should directly answer whether voids/uncollectibles have ever happened and whether they're traceable or just undated.”

  4. The report comes back overnight

    As Noah Fischer saw it, from the small dataset the agents worked on

    invoice_status | total_invoices | voided_at_populated | voided_at_null | matching_audit_log_rows | coverage_rate_pct | distinct_tenants
    ---------------+----------------+---------------------+----------------+-------------------------+-------------------+-----------------
    uncollectible  |             85 |                   0 |             85 |                       0 |               0.0 |               11
    (1 rows)
  5. What came of it

    The memo and the queries that followed; links found by title, so inferred

    It builds on:

  6. It becomes benchmark query q315

    Changed only where the benchmark needs the same result on every system

    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • determinismthe standard leaves the place of NULLs in a sort to the implementation: NULLS LAST ascending, NULLS FIRST descending, as in the reference results (2×)
    The benchmark text, against the submitted SQL
    SELECT  i.status AS invoice_status,  COUNT(*) AS total_invoices,  COUNT(i.voided_at) AS voided_at_populated,  SUM(CASE WHEN i.voided_at IS NULL THEN 1 ELSE 0 END) AS voided_at_null,  SUM(CASE WHEN EXISTS (      SELECT 1 FROM audit_log a      WHERE a.entity_table = 'invoices'        AND a.entity_id = i.id        AND a.tenant_id = i.tenant_id  ) THEN 1 ELSE 0 END) AS matching_audit_log_rows,  ROUND(    100.0 * SUM(CASE WHEN EXISTS (      SELECT 1 FROM audit_log a      WHERE a.entity_table = 'invoices'        AND a.entity_id = i.id        AND a.tenant_id = i.tenant_id    ) THEN 1 ELSE 0 END) / COUNT(*)  , 1) AS coverage_rate_pct,  100.0 * SUM(CASE WHEN EXISTS (    SELECT 1 FROM audit_log a    WHERE a.entity_table = 'invoices'      AND a.entity_id = i.id      AND a.tenant_id = i.tenant_id  ) THEN 1 ELSE 0 END) / COUNT(*) AS coverage_rate_pct,  COUNT(DISTINCT i.tenant_id) AS distinct_tenantsFROM invoices iWHERE i.status IN ('void', 'uncollectible')GROUP BY i.statusORDER BY total_invoices DESC, invoice_statusORDER BY total_invoices DESC NULLS FIRST, invoice_status NULLS LAST
    SELECT
      i.status AS invoice_status,
      COUNT(*) AS total_invoices,
      COUNT(i.voided_at) AS voided_at_populated,
      SUM(CASE WHEN i.voided_at IS NULL THEN 1 ELSE 0 END) AS voided_at_null,
      SUM(CASE WHEN EXISTS (
          SELECT 1 FROM audit_log a
          WHERE a.entity_table = 'invoices'
            AND a.entity_id = i.id
            AND a.tenant_id = i.tenant_id
      ) THEN 1 ELSE 0 END) AS matching_audit_log_rows,
      100.0 * SUM(CASE WHEN EXISTS (
        SELECT 1 FROM audit_log a
        WHERE a.entity_table = 'invoices'
          AND a.entity_id = i.id
          AND a.tenant_id = i.tenant_id
      ) THEN 1 ELSE 0 END) / COUNT(*) AS coverage_rate_pct,
      COUNT(DISTINCT i.tenant_id) AS distinct_tenants
    FROM invoices i
    WHERE i.status IN ('void', 'uncollectible')
    GROUP BY i.status
    ORDER BY total_invoices DESC NULLS FIRST, invoice_status NULLS LAST
    10 ms100 ms1 sHyper11.4 msDuckDB27.9 msGizmoSQL33.5 msclickhouse_brew117 msFirebolt42.5 msDataFusionerrorClickHouseerror
    • DataFusionThis feature is not implemented: Physical plan does not support logical expression Exists(Exists { subquery: <subquery>, negated: false })
    • ClickHouseCode: 48. DB::Exception: Correlated subqueries in aggregate function argument are not supported. (NOT_IMPLEMENTED)
  7. Everything, as recorded

    The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted

    Viktor's whole session, 7 turns
    • The request, as Viktor received it
      Report request from Noah Fischer (analyst for the Chief Information Security Officer):
      
      Title: Invoice void and uncollectible traceability, with timestamp completeness check
      Question: Has any invoice actually been voided or marked uncollectible on the platform, and if so, is the event dated and traceable in audit_log — or was last night's zero result just because voided_at is missing?
      Grain: one row per terminal invoice status: 'void' and 'uncollectible'
      Columns:
      - invoice status
      - total invoices in this status (all-time, regardless of voided_at)
      - of those, rows with a populated voided_at timestamp
      - of those, rows with voided_at null (undated, can't tell when it happened)
      - matching audit_log rows (any action, entity_table = 'invoices', entity_id = invoices.id)
      - coverage rate among all invoices in this status (%)
      - distinct tenants represented
      Details: Source: invoices where status in ('void','uncollectible'), grouped by status. Do NOT filter out rows with null voided_at from the total count this time — count all of them, then separately report how many have voided_at populated vs null, so we can tell whether last night's zero was 'no voids ever happened' or 'voids happened but aren't timestamped.' Coverage test is an EXISTS against audit_log by (entity_table='invoices', entity_id=invoices.id, same tenant_id), any action. Order by total invoices descending, status as tie-break (only 2 rows expected).
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Viktor calls dry_run
      SELECT
        i.status AS invoice_status,
        COUNT(*) AS total_invoices,
        COUNT(i.voided_at) AS voided_at_populated,
        SUM(CASE WHEN i.voided_at IS NULL THEN 1 ELSE 0 END) AS voided_at_null,
        SUM(CASE WHEN EXISTS (
            SELECT 1 FROM audit_log a
            WHERE a.entity_table = 'invoices'
              AND a.entity_id = i.id
              AND a.tenant_id = i.tenant_id
        ) THEN 1 ELSE 0 END) AS matching_audit_log_rows,
        ROUND(
          100.0 * SUM(CASE WHEN EXISTS (
            SELECT 1 FROM audit_log a
            WHERE a.entity_table = 'invoices'
              AND a.entity_id = i.id
              AND a.tenant_id = i.tenant_id
          ) THEN 1 ELSE 0 END) / COUNT(*)
        , 1) AS coverage_rate_pct,
        COUNT(DISTINCT i.tenant_id) AS distinct_tenants
      FROM invoices i
      WHERE i.status IN ('void', 'uncollectible')
      GROUP BY i.status
      ORDER BY total_invoices DESC, invoice_status
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: invoice_status, total_invoices, voided_at_populated, voided_at_null, matching_audit_log_rows, coverage_rate_pct, distinct_tenants
    • ViktorQuery compiles and matches the requested grain and columns.
    • Viktor calls submit
      SELECT
        i.status AS invoice_status,
        COUNT(*) AS total_invoices,
        COUNT(i.voided_at) AS voided_at_populated,
        SUM(CASE WHEN i.voided_at IS NULL THEN 1 ELSE 0 END) AS voided_at_null,
        SUM(CASE WHEN EXISTS (
            SELECT 1 FROM audit_log a
            WHERE a.entity_table = 'invoices'
              AND a.entity_id = i.id
              AND a.tenant_id = i.tenant_id
        ) THEN 1 ELSE 0 END) AS matching_audit_log_rows,
        ROUND(
          100.0 * SUM(CASE WHEN EXISTS (
            SELECT 1 FROM audit_log a
            WHERE a.entity_table = 'invoices'
              AND a.entity_id = i.id
              AND a.tenant_id = i.tenant_id
          ) THEN 1 ELSE 0 END) / COUNT(*)
        , 1) AS coverage_rate_pct,
        COUNT(DISTINCT i.tenant_id) AS distinct_tenants
      FROM invoices i
      WHERE i.status IN ('void', 'uncollectible')
      GROUP BY i.status
      ORDER BY total_invoices DESC, invoice_status
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • ViktorSubmitted. The report gives, per terminal status (`void`, `uncollectible`): total invoice count, how many have a populated `voided_at` vs null, how many have at least one matching `audit_log` row, the resulting coverage rate, and distinct tenants — which should directly answer whether voids/uncollectibles have ever happened and whether they're traceable or just undated.