cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Governance
Join discussions on data governance practices, compliance, and security within the Databricks Community. Exchange strategies and insights to ensure data integrity and regulatory compliance.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Schema Changes to External table

Dp15
Contributor

Hi,
I have an external table which reads data from a S3 bucket. The s3 bucket is expected to get new files frequently. With some changes to the underlying schema. I used Refresh Table table command to load new files from the s3 location and it worked fine. But when there are schema changes, either addition or deletion, the refresh table is not working,

Is it possible to refresh the metadata of the external table when there are schema changes? Should I alter the table each time there are changes to the schema? Could someone please help?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

shan_chandra
Databricks Employee
Databricks Employee

@Dp15 - yes you are correct. Dropping a column from an managed table in Databricks works different from the external table(as the schema is inferred by the underlying source). Below hack can help. AFAIK. Please let me know if this works for you. 

1. create or replace new external table B on the new schema (new set of columns you want to keep) and new data source path
2. insert into new table B as select (required columns) from table A(old table). 
3. Drop table A 
4. Alter table - Rename table B to table A

View solution in original post

7 REPLIES 7

shan_chandra
Databricks Employee
Databricks Employee

@Dp15  -  Please refer to the below illustration.

  • Considering a streaming pipeline from table A to table B
  • Without column mapping:
    1. you can only ADD COLUMN on table A
    2. If spark.databricks.delta.schema.autoMerge.enabled is set
    3. Any addition of columns to A (say c )would cause c to be added to table B as well (assuming no transformation or filtering)
    4. This is considered safe because adding column to a table won't cause data miss or duplication, e.g. a select * from table B now will just show nulls for all historical data in B
    5. If ^ is not set: the stream would fail anyway
  • With column mapping:
    1. For ADD COLUMN, same story. So when you use schemaTrackingLocation and ADD COLUMN, we won't require you to enter the allowDropOrRenameColumn SQL conf.

Please refer to the below doc for additional details - https://docs.databricks.com/en/delta/delta-column-mapping.html#streaming-with-column-mapping-and-sch...

 

HI @shan_chandra how about deletions from the external location? And what if I am not using a streaming table? 

 

 

 

shan_chandra
Databricks Employee
Databricks Employee

@Dp15 - you can drop column manually using the below 

ALTER TABLE table_name DROP COLUMN col_name

1. Please note dropping a column from a metadata does not delete the underlying data for column in files.

2. Purging the data column can be done using REORG TABLE to rewrite files.

3. Use VACUUM to physically delete the files that contain the dropped column data.

Reference:

https://docs.databricks.com/en/delta/delta-column-mapping.html#drop-columns

https://docs.databricks.com/en/delta/update-schema.html#explicitly-update-schema-to-drop-columns

 

Hi @shan_chandra This drop works for a delta table which is managed table, however it does work for an external table, I am looking specifically for schema changes in external table, now a  refresh might work to load new metadata in the external table, however when there are schema modifications, only addition of columns are possible dropping a column has not worked for me, Correct me if I am wrong here

shan_chandra
Databricks Employee
Databricks Employee

@Dp15 - yes you are correct. Dropping a column from an managed table in Databricks works different from the external table(as the schema is inferred by the underlying source). Below hack can help. AFAIK. Please let me know if this works for you. 

1. create or replace new external table B on the new schema (new set of columns you want to keep) and new data source path
2. insert into new table B as select (required columns) from table A(old table). 
3. Drop table A 
4. Alter table - Rename table B to table A

@shan_chandra This worked thank you

shan_chandra
Databricks Employee
Databricks Employee

@Dp15 - I am glad it worked. Happy to help!!!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group