Column Default Propagation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 07:46 AM
Hi 🙂 Today I found I somewhat strange behavior when it comes to default values in columns. Apparently, column defaults are propagated to a new table, when you select the column without any operation on it. This is a bit unexpected for me. Here a short example:
I have a table where one of the columns has a default:
create or replace table schema.test (
id int default 0,
name string
)
TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported') -- needed as otherwise we can not define defaults
;
I insert some data and the use this as a basis for a new table:
create or replace table schema.test2
as
(select id, name from schema.test);
this fails with the following error: [WRONG_COLUMN_DEFAULTS_FOR_DELTA_FEATURE_NOT_ENABLED] Failed to execute CREATE TABLE command because it assigned a column DEFAULT value, but the corresponding table feature was not enabled. Please retry the command again after executing ALTER TABLE tableName SET TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported').
When I enable the table properties it works:
create or replace table schema.test2
TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported')
as (select id, name from schema.test);
in this case the default will also be set on the id column of the new table, even though I have not explicitly set this.
or when I do some operation on the column it works also without setting the table properties:
create or replace table schema.test2
as
(select cast(id as int), name from schema.test);
is this expected behavior? Is there a way to disable this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2025 08:45 AM
delta.feature.allowColumnDefaults table property in Delta Lake. Here’s an explanation:-
Default Propagation Without Transformation: When creating a table (
schema.test2) via aCREATE TABLE AS SELECT(CTAS) query, default column values from the source table (schema.test) are transferred to the new table if the column is directly selected without applying transformations or type changes. This is why theidcolumn in the new table retains its default value. However, for this to work, thedelta.feature.allowColumnDefaultsproperty must be enabled on the target table. -
Error Without
TBLPROPERTIESSet: If the target table does not have thedelta.feature.allowColumnDefaultsproperty explicitly set to'supported', the CTAS operation fails with the error stating:Failed to execute CREATE TABLE command because it assigned a column DEFAULT value, but the corresponding table feature was not enabled.<ref id="doc_1"/><ref id="doc_2"/> -
Bypassing Default Propagation with Transformations: If any operation (e.g.,
CAST(id AS INT)) is applied to the column in the SELECT statement, the default value does not propagate. As a result, thedelta.feature.allowColumnDefaultsproperty does not need to be enabled, and the CTAS operation succeeds without the error.
Can This Behavior Be Changed or Disabled?
delta.feature.allowColumnDefaults property is enabled if maintaining defaults is the intended behavior for the target table.