Skip to the page
Tecyfy

PySpark interview question · medium

What are the corrupt record handling modes in Spark when reading data?

The short answerSpark's CSV and JSON readers have three modes for malformed records, set with the mode option: PERMISSIVE (the default) keeps the row, sets fields that can't be parsed to null and can save the raw text in a corrupt-record column; DROPMALFORMED silently drops malformed rows; FAILFAST stops the read with an error on the first one.

The full answer

Spark's CSV and JSON readers have three modes for malformed records, set with the mode option: PERMISSIVE (the default) keeps the row, sets fields that can't be parsed to null and can save the raw text in a corrupt-record column; DROPMALFORMED silently drops malformed rows; FAILFAST stops the read with an error on the first one. Separate options handle whole files that can't be read.

PERMISSIVE (default)

orders = (spark.read
    .option("header", True)
    .option("mode", "PERMISSIVE")
    .option("columnNameOfCorruptRecord", "_corrupt_record")
    .schema("id INT, name STRING, amount DOUBLE, _corrupt_record STRING")
    .csv("/landing/orders/"))

orders.cache()      # needed before querying the corrupt column on its own
bad = orders.filter("_corrupt_record IS NOT NULL")
good = orders.filter("_corrupt_record IS NULL").drop("_corrupt_record")

With the rows 1,Ann,10, 2,Bo,oops and 3,Cy, row 1 is clean; row 2 gets amount null and its raw text in _corrupt_record; row 3 has too few fields, so it is also treated as malformed. Spark doesn't allow a query that references only the corrupt-record column straight from raw CSV or JSON files, so cache or save the parsed result first.

DROPMALFORMED Only well-formed rows are returned (row 1 above). Simple, but you lose the bad data and don't know how much was dropped, so use it only when that's acceptable and you count rows elsewhere.

FAILFAST The read fails with a FAILED_READ_FILE error naming the file. Good for tests and for sources that must always be clean.

Whole files that can't be read

  • ignoreCorruptFiles (option or spark.sql.files.ignoreCorruptFiles): skip files that can't be read at all, such as truncated Parquet files, and return the rest.
  • ignoreMissingFiles: skip files deleted after the file list was made. Both are off by default; turning them on hides problems, so log or monitor what is skipped.

Databricks: badRecordsPath On Databricks, .option("badRecordsPath", "/quarantine/orders") writes malformed records and unreadable files, with the reason, to that location as JSON files, and continues the read. Auto Loader adds a rescued-data column that captures values that don't match the schema.

Recommended practice

  • Use PERMISSIVE with an explicit schema and a corrupt-record column in pipelines.
  • Write bad rows to a quarantine table with the file name (_metadata.file_path) and load time.
  • Alert on the bad-row count or rate, rather than only failing or silently dropping.
  • Use FAILFAST in tests.
  • Parquet and other self-describing formats don't use these modes, since rows can't be partially malformed; there, the issue is whole corrupt files.

Last updated 16 October 2026