Yibo Dong created SPARK-62274:
---------------------------------

             Summary: TRUNCATE TABLE after cache rematerialization reads 
deleted Parquet files
                 Key: SPARK-62274
                 URL: https://issues.apache.org/jira/browse/SPARK-62274
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 5.0.0
         Environment: Stock Apache Spark 5.0.0-SNAPSHOT at commit 
082cf160890a0a0a748f70ffb5a993144b0de079, Java 25.0.1, macOS 15.7.4 aarch64, 
local[2], in-memory session catalog, built-in Parquet data source (V1), default 
SQL configuration.
            Reporter: Yibo Dong


h2. What happened

After a cached table is modified and its cache is rematerialized, a subsequent 
TRUNCATE TABLE causes the next read to access deleted Parquet files and fail 
with FILE_NOT_EXIST.

Spark documents that TRUNCATE TABLE clears the cached data and lazily 
repopulates the cache on subsequent access. Instead, the read fails even though 
the truncation succeeds.

h2. How to reproduce

Run the following SQL in a fresh Spark SQL session:

{code:sql}
DROP TABLE IF EXISTS cached_partition_truncate_repro;

CREATE TABLE cached_partition_truncate_repro (id INT)
USING parquet
PARTITIONED BY (p INT);

INSERT INTO cached_partition_truncate_repro
VALUES (1, 1), (2, 2), (3, 2);

CACHE TABLE cached_partition_truncate_repro;
SELECT id, p FROM cached_partition_truncate_repro ORDER BY id;

INSERT INTO cached_partition_truncate_repro
PARTITION (p = 3) SELECT 4;

SELECT id, p FROM cached_partition_truncate_repro ORDER BY id;

TRUNCATE TABLE cached_partition_truncate_repro PARTITION (p = 2);

SELECT id, p FROM cached_partition_truncate_repro ORDER BY id;
{code}

Save as tmp.sql and execute:

{code:bash}
spark-sql --master 'local[2]' --conf spark.sql.catalogImplementation=in-memory 
-f tmp.sql
{code}

h2. Expected result

After truncating partition p = 2, the final query should return:

{noformat}
1  1
4  3
{noformat}

h2. Actual result

The first two SELECT statements return the expected rows, including the newly 
inserted row (4, 3).

However, the final SELECT fails while attempting to read a Parquet file deleted 
by TRUNCATE TABLE:

{noformat}
org.apache.spark.SparkException:
[FAILED_READ_FILE.FILE_NOT_EXIST]
Encountered error while reading file
file:///.../cached_partition_truncate_repro/p=2/part-....snappy.parquet.
File does not exist.

Caused by: java.io.FileNotFoundException:
File .../cached_partition_truncate_repro/p=2/part-....snappy.parquet
does not exist
{noformat}



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to