Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

Spark fails to read a Parquet column whose stored type differs from the schema

Your error

org.apache.spark.sql.execution.QueryExecutionException: Parquet column cannot be converted in file file:///data/events/part-00000.parquet. Column: [timestamp], Expected: bigint, Found: INT322 more lines it can show

Fix it fast

Most common cause, 1 of 2

Spark 3.x cannot read INT32 data as bigint in the vectorized reader

Run the job on Spark 4.0 or later, where the reader promotes INT32 to bigint without touching the files.

Fix
pip install "pyspark==4.3.0"
python -c "import pyspark; print(pyspark.__version__)"
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Parquet column cannot be converted (Spark 3.x) or FAILED_READ_FILE.PARQUET_COLUMN_DATA_TYPE_MISMATCH (Spark 4.x) means one file stores the column as a different physical type than the schema Spark reads with. Spark 3's vectorized reader cannot widen INT32 to bigint; Spark 4.0 added that. Any other mismatch needs the odd files rewritten with a cast. Applies to Spark 3.5 and 4.x.

  • Applies to Apache Spark 4.3 (PySpark 4.3.0), Apache Spark 4.0 and later, Apache Spark 3.5, pyarrow (to read file schemas)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the type of the named column in every file under the path. One group of files with a narrower integer type is cause 1; a group with an unrelated type (string against INT64, or a wider type than the schema) is cause 2.

Run this first
python -c "import glob, pyarrow.parquet as pq; [print(f, pq.read_schema(f).field('timestamp').type) for f in sorted(glob.glob('data/events/**/*.parquet', recursive=True))]"

Then find the line you see in this list and open its fix:

  1. 1Spark 3.x cannot read INT32 data as bigint in the vectorized readerExpected: bigint, Found: INT32 on Spark 3.x, and the older files hold int32 while newer files or the table schema use long
  2. 2The column changed to an unrelated type between writesThe pyarrow listing shows two unrelated types for the column, such as string in some files and int64 in others, and the error persists on Spark 4.x

What does the error look like?

  • org.apache.spark.sql.execution.QueryExecutionException: Parquet column cannot be converted in file file:///data/events/part-00000.parquet. Column: [timestamp], Expected: bigint, Found: INT32
  • [FAILED_READ_FILE.PARQUET_COLUMN_DATA_TYPE_MISMATCH] Encountered error while reading file
  • Data type mismatches when reading Parquet column

Cause 1 of 2

Spark 3.x cannot read INT32 data as bigint in the vectorized reader

You seeExpected: bigint, Found: INT32 on Spark 3.x, and the older files hold int32 while newer files or the table schema use long

Check

When the requested schema widens a column from int to long, for example because later files or a table definition use bigint, Spark 3's VectorizedColumnReader has no updater for that pair and throws the conversion error. SPARK-40876 added these type promotions to the vectorized Parquet reader, and the fix shipped in Spark 4.0.0.

Check
python -c "import pyspark; print(pyspark.__version__)"

Fix

Run the job on Spark 4.0 or later, where the reader promotes INT32 to bigint without touching the files. A cluster that has to stay on 3.5 needs the int32 files rewritten with a bigint cast, the same way as the rewrite under cause 2.

Fix
pip install "pyspark==4.3.0"
python -c "import pyspark; print(pyspark.__version__)"

Verify

On the new version, a full scan of the column covers old and new files and finishes without the exception.

Verify
spark.read.parquet("data/events").select("timestamp").count()

Confirmed by issues.apache.org

Did this fix it?

Cause 2 of 2

The column changed to an unrelated type between writes

You seeThe pyarrow listing shows two unrelated types for the column, such as string in some files and int64 in others, and the error persists on Spark 4.x

Check

Spark's Parquet source can combine files whose schemas differ but stay mutually compatible, such as files that only add columns. A column that is binary text in one file and INT64 in another has no safe conversion, so Spark 4 reports FAILED_READ_FILE.PARQUET_COLUMN_DATA_TYPE_MISMATCH with the expected Spark type and the actual Parquet type.

Check
spark.read.parquet("data/events/part-00000.parquet").printSchema()
spark.read.parquet("data/events").printSchema()

Fix

Read each group of files on its own, cast the column to the type you want to keep, combine them by column name, and write the result to a new path. Switch readers to the new path, then retire the old files.

Fix
from pyspark.sql import functions as F
a = spark.read.parquet(*files_with_string).withColumn("timestamp", F.col("timestamp").cast("bigint"))
b = spark.read.parquet(*files_with_int64)
a.unionByName(b).write.mode("overwrite").parquet("data/events_v2")

Verify

The pyarrow listing over the new path prints a single type for the column, and a full scan finishes.

Verify
python -c "import glob, pyarrow.parquet as pq; print({str(pq.read_schema(f).field('timestamp').type) for f in glob.glob('data/events_v2/**/*.parquet', recursive=True)})"

Confirmed by spark.apache.org

Did this fix it?

How do I stop it coming back?

  • Write Parquet with an explicit schema (or cast columns before the write) instead of letting each job infer types from its input.
  • Add a CI or pipeline check that reads the footer of each new file with pyarrow and fails when a column type differs from the table contract.
  • When a column really must change type, write to a new path or table version rather than appending files of the new type to the old folder.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.