@@ -40,20 +40,39 @@ DuckLakeFieldId::DuckLakeFieldId(DuckLakeColumnData column_data_p, string name_p
4040 }
4141}
4242
43- static Value ExtractDefaultValue (optional_ptr<const ParsedExpression> default_expr, const LogicalType &type) {
43+ static unique_ptr<ParsedExpression> ExtractDefaultExpression (optional_ptr<const ParsedExpression> default_expr,
44+ const LogicalType &type) {
4445 if (!default_expr) {
46+ return make_uniq<ConstantExpression>(Value (type));
47+ }
48+ if (default_expr->HasSubquery ()) {
49+ throw NotImplementedException (" Expressions with subqueries are not yet supported as default expressions" );
50+ }
51+ if (default_expr->IsWindow ()) {
52+ throw NotImplementedException (" Expressions with window functions are not yet supported as default expressions" );
53+ }
54+ return default_expr->Copy ();
55+ }
56+
57+ static Value ExtractInitialValue (optional_ptr<const ParsedExpression> initial_expr, const LogicalType &type,
58+ bool add_column) {
59+ if (!initial_expr) {
4560 return Value (type);
4661 }
47- if (default_expr->type != ExpressionType::VALUE_CONSTANT) {
48- throw NotImplementedException (" Only literals (e.g. 42 or 'hello world') are supported as default values" );
62+ if (initial_expr->type != ExpressionType::VALUE_CONSTANT) {
63+ if (!add_column) {
64+ return Value (type);
65+ }
66+ throw NotImplementedException (" We cannot add a column with a non-literal default value. Add the column and "
67+ " then explicitly set the default for new values using \" ALTER ... SET DEFAULT\" " );
4968 }
50- auto &const_default = default_expr ->Cast <ConstantExpression>();
69+ auto &const_default = initial_expr ->Cast <ConstantExpression>();
5170 return const_default.value .DefaultCastAs (type);
5271}
5372
5473unique_ptr<DuckLakeFieldId> DuckLakeFieldId::FieldIdFromType (const string &name, const LogicalType &type,
5574 optional_ptr<const ParsedExpression> default_expr,
56- idx_t &column_id) {
75+ idx_t &column_id, bool add_column ) {
5776 DuckLakeColumnData column_data;
5877 column_data.id = FieldIndex (column_id++);
5978 vector<unique_ptr<DuckLakeFieldId>> field_children;
@@ -64,52 +83,53 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::FieldIdFromType(const string &name,
6483 throw NotImplementedException (" Default value for STRUCT type not supported" );
6584 }
6685 for (auto &entry : StructType::GetChildTypes (type)) {
67- field_children.push_back (FieldIdFromType (entry.first , entry.second , nullptr , column_id));
86+ field_children.push_back (FieldIdFromType (entry.first , entry.second , nullptr , column_id, add_column ));
6887 }
6988 break ;
7089 }
7190 case LogicalTypeId::LIST:
7291 if (default_expr) {
7392 throw NotImplementedException (" Default value for LIST type not supported" );
7493 }
75- field_children.push_back (FieldIdFromType (" element" , ListType::GetChildType (type), nullptr , column_id));
94+ field_children.push_back (
95+ FieldIdFromType (" element" , ListType::GetChildType (type), nullptr , column_id, add_column));
7696 break ;
7797 case LogicalTypeId::ARRAY:
7898 if (default_expr) {
7999 throw NotImplementedException (" Default value for LIST type not supported" );
80100 }
81- field_children.push_back (FieldIdFromType (" element" , ArrayType::GetChildType (type), nullptr , column_id));
101+ field_children.push_back (
102+ FieldIdFromType (" element" , ArrayType::GetChildType (type), nullptr , column_id, add_column));
82103 break ;
83104 case LogicalTypeId::MAP:
84105 if (default_expr) {
85106 throw NotImplementedException (" Default value for MAP type not supported" );
86107 }
87- field_children.push_back (FieldIdFromType (" key" , MapType::KeyType (type), nullptr , column_id));
88- field_children.push_back (FieldIdFromType (" value" , MapType::ValueType (type), nullptr , column_id));
108+ field_children.push_back (FieldIdFromType (" key" , MapType::KeyType (type), nullptr , column_id, add_column ));
109+ field_children.push_back (FieldIdFromType (" value" , MapType::ValueType (type), nullptr , column_id, add_column ));
89110 break ;
90111 default :
91112 break ;
92113 }
93- column_data.initial_default = ExtractDefaultValue (default_expr, type);
94- column_data.default_value = column_data.initial_default ;
114+ column_data.initial_default = ExtractInitialValue (default_expr, type, add_column);
115+ if (default_expr) {
116+ column_data.default_value = default_expr->Copy ();
117+ }
118+
95119 return make_uniq<DuckLakeFieldId>(std::move (column_data), name, type, std::move (field_children));
96120}
97121
98122unique_ptr<ParsedExpression> DuckLakeFieldId::GetDefault () const {
99- if (children.empty ()) {
100- if (column_data.default_value .IsNull ()) {
101- // no default value defined
102- return nullptr ;
103- }
104- return make_uniq<ConstantExpression>(column_data.default_value );
123+ if (column_data.default_value ) {
124+ return column_data.default_value ->Copy ();
105125 }
106- // FIXME: default not supported for entries with children
107126 return nullptr ;
108127}
109128
110- unique_ptr<DuckLakeFieldId> DuckLakeFieldId::FieldIdFromColumn (const ColumnDefinition &col, idx_t &column_id) {
129+ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::FieldIdFromColumn (const ColumnDefinition &col, idx_t &column_id,
130+ bool add_column) {
111131 auto default_val = col.HasDefaultValue () ? optional_ptr<const ParsedExpression>(col.DefaultValue ()) : nullptr ;
112- return DuckLakeFieldId::FieldIdFromType (col.Name (), col.Type (), default_val, column_id);
132+ return DuckLakeFieldId::FieldIdFromType (col.Name (), col.Type (), default_val, column_id, add_column );
113133}
114134
115135shared_ptr<DuckLakeFieldData> DuckLakeFieldData::FromColumns (const ColumnList &columns) {
@@ -132,7 +152,7 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::Copy() const {
132152 for (auto &child : children) {
133153 new_children.push_back (child->Copy ());
134154 }
135- return make_uniq<DuckLakeFieldId>(column_data, name, type, std::move (new_children));
155+ return make_uniq<DuckLakeFieldId>(column_data. Copy () , name, type, std::move (new_children));
136156}
137157
138158unique_ptr<DuckLakeFieldId> DuckLakeFieldId::Rename (const DuckLakeFieldId &field_id, const string &new_name) {
@@ -144,7 +164,7 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::Rename(const DuckLakeFieldId &field
144164unique_ptr<DuckLakeFieldId> DuckLakeFieldId::SetDefault (const DuckLakeFieldId &field_id,
145165 optional_ptr<const ParsedExpression> default_expr) {
146166 auto result = field_id.Copy ();
147- result->column_data .default_value = ExtractDefaultValue (default_expr, field_id.Type ());
167+ result->column_data .default_value = ExtractDefaultExpression (default_expr, field_id.Type ());
148168 return result;
149169}
150170
@@ -200,7 +220,7 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::AddField(const vector<string> &colu
200220 }
201221 }
202222 LogicalType new_type = GetNewNestedType (type, new_children);
203- return make_uniq<DuckLakeFieldId>(column_data, Name (), std::move (new_type), std::move (new_children));
223+ return make_uniq<DuckLakeFieldId>(column_data. Copy () , Name (), std::move (new_type), std::move (new_children));
204224}
205225
206226unique_ptr<DuckLakeFieldId> DuckLakeFieldId::RemoveField (const vector<string> &column_path, idx_t depth) const {
@@ -231,7 +251,7 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::RemoveField(const vector<string> &c
231251 throw InternalException (" DuckLakeFieldId::AddField - child not found in struct path" );
232252 }
233253 LogicalType new_type = GetNewNestedType (type, new_children);
234- return make_uniq<DuckLakeFieldId>(column_data, Name (), std::move (new_type), std::move (new_children));
254+ return make_uniq<DuckLakeFieldId>(column_data. Copy () , Name (), std::move (new_type), std::move (new_children));
235255}
236256
237257unique_ptr<DuckLakeFieldId> DuckLakeFieldId::RenameField (const vector<string> &column_path, const string &new_name,
@@ -248,8 +268,8 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::RenameField(const vector<string> &c
248268 // leaf - rename the column at this level
249269 auto copied_entry = child.Copy ();
250270 auto renamed_entry =
251- make_uniq<DuckLakeFieldId>(copied_entry->column_data , new_name, std::move (copied_entry-> type ) ,
252- std::move (copied_entry->children ));
271+ make_uniq<DuckLakeFieldId>(copied_entry->column_data . Copy () , new_name,
272+ std::move (copied_entry->type ), std::move (copied_entry-> children ));
253273 new_children.push_back (std::move (renamed_entry));
254274 } else {
255275 // not the leaf - find the child to rename it and recurse
@@ -264,7 +284,7 @@ unique_ptr<DuckLakeFieldId> DuckLakeFieldId::RenameField(const vector<string> &c
264284 throw InternalException (" DuckLakeFieldId::AddField - child not found in struct path" );
265285 }
266286 auto new_type = GetStructType (new_children);
267- return make_uniq<DuckLakeFieldId>(column_data, Name (), std::move (new_type), std::move (new_children));
287+ return make_uniq<DuckLakeFieldId>(column_data. Copy () , Name (), std::move (new_type), std::move (new_children));
268288}
269289
270290shared_ptr<DuckLakeFieldData> DuckLakeFieldData::RenameColumn (const DuckLakeFieldData &field_data,
@@ -288,7 +308,7 @@ shared_ptr<DuckLakeFieldData> DuckLakeFieldData::AddColumn(const DuckLakeFieldDa
288308 for (auto &existing_id : field_data.field_ids ) {
289309 result->Add (existing_id->Copy ());
290310 }
291- auto field_id = DuckLakeFieldId::FieldIdFromColumn (new_col, next_column_id);
311+ auto field_id = DuckLakeFieldId::FieldIdFromColumn (new_col, next_column_id, true );
292312 result->Add (std::move (field_id));
293313 return result;
294314}
0 commit comments