cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Managed table overwrites existing location for delta but not for oth

Dhruv-22
New Contributor II

I am working on Azure Databricks, with Databricks Runtime version being - 14.3 LTS (includes Apache Spark 3.5.0, Scala 2.12). I am facing the following issue.

Suppose I have a view named v1 and a database f1_processed created from the following command.

CREATE DATABASE IF NOT EXISTS f1_processed
LOCATION "abfss://processed@formula1dl679student.dfs.core.windows.net/"

This is creating a database in the container named processed. Suppose I already have some folder named circuits in that container.

If I run the following command to create a managed table in parquet format from a dataframe in that location using the command below.

circuits_final_df.write.mode("overwrite").format("parquet").saveAsTable("f1_processed.circuits")

It gives an error as follows

SparkRuntimeException: [LOCATION_ALREADY_EXISTS] Cannot name the managed table as 
`spark_catalog`.`f1_processed`.`circuits`, as its associated location 
'abfss://processed@formula1dl679student.dfs.core.windows.net/circuits' already exists. 
Please pick a different table name, or remove the existing location first. SQLSTATE: 42710

However, if I try the same thing in delta format, it runs fine. So the following code runs fine.

circuits_final_df.write.mode("overwrite").format("delta").saveAsTable("f1_processed.circuits")

Also, while creating this delta table, it doesn't remove any files from the folder. It just adds the new files.

Since the result mixes the existing data and new data, it seems it is a bug and it should not happen. Any help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Hey @Dhruv-22  , yes, this is how I understand your problem. You have a folder perhaps with delta files and try to overwrite it with parquet files? I'm not familiar with parquet but I believe you need to delete parquet files each time before you create the table in the same location. With delta like you said, its not neccessary anymore. Delta keeps each file and when you do a action like overwrite, merge or just add data to the table. Delta creates a new file in these cases in order to maintain history. Then you can use the time travel feature to move back to a prior version of the table. 

View solution in original post

6 REPLIES 6

Red_blue_green
New Contributor III

Hi,

this is how the delta format work. With overwrite you are not deleting the files in the folder or replacing them. Delta is creating a new file with the overwritten schema and data. This way you are also able to return to former versions of the delta table. 

I believe you are having a table in delta formath in the path, its not possible to overwrite it as parquet format. You need to delete the folder in the abfss location. Then you can create a parquet file there again. 

Hey, my main doubt was that a managed table cannot be created at a location that already exists. So the error should come for all formats (delta, parquet ...). The code is giving an error for parquet but not for delta. Why is this behaviour? Why is delta able to create a managed table at a non-empty location?

Also, I just kept a random csv file in a folder named circuits to check for the error.

Hey @Red_blue_green , I think I get it. The 'delta' mode does not need to delete files to overwrite data. It can keep all files by maintaining history, even the existing ones. Am I right?

Hey @Dhruv-22  , yes, this is how I understand your problem. You have a folder perhaps with delta files and try to overwrite it with parquet files? I'm not familiar with parquet but I believe you need to delete parquet files each time before you create the table in the same location. With delta like you said, its not neccessary anymore. Delta keeps each file and when you do a action like overwrite, merge or just add data to the table. Delta creates a new file in these cases in order to maintain history. Then you can use the time travel feature to move back to a prior version of the table. 

Kaniz
Community Manager
Community Manager

Thank you for posting your question in our community! We are happy to assist you.

To help us provide you with the most accurate information, could you please take a moment to review the responses and select the one that best answers your question?

This will also help other community members who may have similar questions in the future. Thank you for your participation and let us know if you need any further assistance! 
 

Kaniz
Community Manager
Community Manager

Hey there! Thanks a bunch for being part of our awesome community! 🎉 

We love having you around and appreciate all your questions. Take a moment to check out the responses – you'll find some great info. Your input is valuable, so pick the best solution for you. And remember, if you ever need more help , we're here for you! 

Keep being awesome! 😊🚀

 

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.