Skip to the page

Spark · mid

Spark coding patterns and the Spark 4 upgrade

Two patterns appear in almost every interview: top N per group and keep the latest record then MERGE. The Spark 4 upgrade turned ANSI mode on, which exposes bad data.

The notes

What you must know

  • Top N per group: aggregate first, then rank with a window partitioned by the group. row_number drops ties, rank keeps ties but skips numbers, dense_rank gives the top N levels. Ask which the business wants.
  • Aggregate before the window, so the window sorts a few rows per product, not every order line. Convert to the IST date: to_date on a UTC timestamp puts orders from 00:00 to 05:30 IST on the previous day.
  • Do not rank raw rows or convert to pandas. For incremental runs process only the new day and overwrite that day's output.
  • Latest record per key: a window with row_number ordered by updated_at descending and a tie-breaker, keep rn = 1. dropDuplicates keeps an arbitrary row per key and can keep an old one.
  • Delta MERGE fails when several source rows match one target row, so deduplicate first. Use whenMatchedUpdateAll(condition="s.updated_at > t.updated_at") so reruns and out-of-order files change nothing.
  • Deletes: whenMatchedDelete with an operation flag, or a soft-delete flag. Use liquid clustering on the key or a partition predicate so MERGE does not rewrite the whole table.
  • Spark 4.0 turned ANSI SQL mode on by default (spark.sql.ansi.enabled=true): invalid casts, overflows and division by zero raise errors (CAST_INVALID_INPUT, CAST_OVERFLOW, DIVIDE_BY_ZERO) instead of returning NULL.
  • A job that fails after the upgrade has exposed a data-quality bug: the old numbers silently turned bad values into NULL. Use try_cast now, count the NULLs, tell finance. Properly, parse explicitly, send failing rows to a quarantine table, and keep ANSI on.
  • Before upgrading: run every job on a Spark 4 staging cluster against recent data and read the SQL migration guide. Spark 4 also uses Java 17 by default and Scala 2.13 only.

The red pen

Common traps: mistake → fix

  • Mistake: dropDuplicates keeps the latest row

    Fix: it keeps any one row.

  • Mistake: Turn ANSI off globally and move on

    Fix: you keep silently wrong numbers.

  • Mistake: row_number is fine for ties

    Fix: it silently drops tied products.

Easy to hard

Questions interviewers ask, easy to hard

  1. Write PySpark for the top three products per category per day, with ties.
  2. How do you merge an hourly file of customer updates into a Delta table safely?
  3. A job fails with CAST_INVALID_INPUT after the Spark 4 upgrade. What happened?
  4. What is the difference between row_number, rank and dense_rank?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview