The situation
Sales wants orders grouped by size: how many small, medium and large orders there are, and what they are worth.
An AI assistant wrote the job in the answer cell. It runs, the grand total matches finance to the paisa, and the report went out. The head of enterprise sales then asked why the company has not taken a single large order all year.
Find the bug and fix it.
Input tables
orders
One row per order.
| Column | Type | Description |
|---|---|---|
order_id |
BIGINT | Unique per order. |
amount |
DECIMAL(10,2) | The order value in rupees. Never NULL, always positive. |
Requirement
Put every order in one bucket and return one row per bucket.
| Bucket | Amount |
|---|---|
SMALL |
less than 1000 |
MEDIUM |
1000 or more, and less than 10000 |
LARGE |
10000 or more |
- An order of exactly 1000.00 is
MEDIUM. An order of exactly 10000.00 isLARGE. ordersis the number of orders in the bucket.total_amountis the sum of their amounts.
Expected output
One row per bucket: three rows.
| Column | Type | Description |
|---|---|---|
bucket |
STRING | SMALL, MEDIUM or LARGE. |
orders |
BIGINT | |
total_amount |
DECIMAL(14,2) |
The AI-written code
from pyspark.sql import functions as F def solve(spark): """Orders and total amount per order-size bucket.""" orders = spark.table("orders") # Classify each order by its amount bucket = ( F.when(F.col("amount") < 1000, "SMALL") .when(F.col("amount") >= 1000, "MEDIUM") .when(F.col("amount") >= 10000, "LARGE") ) orders = orders.withColumn("bucket", bucket) # Aggregate per bucket result = orders.groupBy("bucket").agg( F.count("*").alias("orders"), F.sum("amount").cast("decimal(14,2)").alias("total_amount"), ) return result
Explanation
Show explanationGives the answer away. Try the problem first.
The bug
bucket = (
F.when(F.col("amount") < 1000, "SMALL")
.when(F.col("amount") >= 1000, "MEDIUM")
.when(F.col("amount") >= 10000, "LARGE")
)
A when chain is not three independent rules. It is read from top to bottom, and
the first branch that is true wins. The rest are never looked at for that row.
An order of 25,000 is not less than 1000, so the first branch fails. It is at least
1000, so the second branch is true and the order is MEDIUM. The third branch can
never be reached: every amount that is at least 10000 is also at least 1000.
So the LARGE bucket does not exist in the output, and MEDIUM holds both:
| Bucket | Correct | The buggy job |
|---|---|---|
SMALL |
111 orders, 56,626.91 | 111 orders, 56,626.91 |
MEDIUM |
73 orders, 392,965.21 | 99 orders, 1,678,694.63 |
LARGE |
26 orders, 1,285,729.42 | no row |
Why it got through:
- Nothing is lost. All 210 orders are in a bucket and the grand total, 1,735,321.54, is exactly right. A check on totals passes.
SMALLis right.- The code reads like the requirement. Each line on its own is a true statement about its bucket.
The same thing happens in SQL with CASE WHEN, which works the same way.
The fix
Put the narrowest condition first, so that each branch can rely on the ones above it having failed:
bucket = (
F.when(F.col("amount") >= 10000, "LARGE")
.when(F.col("amount") >= 1000, "MEDIUM")
.otherwise("SMALL")
)
By the time the second branch is evaluated, the amount is known to be below 10000,
so >= 1000 means exactly "1000 up to, but not including, 10000".
Two other correct ways:
- Go upwards with upper bounds:
when(amount < 1000, "SMALL"),.when(amount < 10000, "MEDIUM"),.otherwise("LARGE"). - Give every branch both of its bounds:
(amount >= 1000) & (amount < 10000). Then the order of the branches does not matter. It is more to type and it is the hardest version to break later.
Common wrong answers
Adding .otherwise("LARGE") to the end of the original chain. otherwise is
used when no branch matched. Here the first two branches already cover every
possible amount, so it never fires. Still two rows, same numbers.
Reordering, but with > instead of >=. The statement puts 1000.00 in MEDIUM
and 10000.00 in LARGE. With >, each of them falls one bucket down. All three
rows are wrong. MEDIUM even keeps its count of 73, because it loses one order and
gains one, and only the amount gives it away.
between(1000, 10000) for MEDIUM. between includes both ends, so the order
of exactly 10000.00 is MEDIUM. Two rows wrong.
<= on the upper bounds. when(amount <= 1000, "SMALL") puts 1000.00 in
SMALL. All three rows wrong.
In each of these the mistake moves one or two orders out of 210. On real data nobody would notice. That is what the planted orders at 999.99, 1000.00, 1000.01, 9999.99, 10000.00 and 10000.01 are for: when you write bucket logic, write one test row on each boundary and one on either side.
What a missing otherwise does
A chain with no otherwise returns NULL when nothing matches. In this problem every
amount matches something. With a NULL amount, though, every comparison is NULL, no
branch is true, and the order ends up in a bucket named NULL. If NULL amounts are
possible, decide where they go and say so with an explicit branch.
Edge cases in the data
| Amount | Bucket | What it catches |
|---|---|---|
| 999.99 | SMALL |
|
| 1000.00 | MEDIUM |
> for the lower bound; <= for the upper bound of SMALL. |
| 1000.01 | MEDIUM |
|
| 9999.99 | MEDIUM |
|
| 10000.00 | LARGE |
between; > for the lower bound. |
| 10000.01 | LARGE |