| 1 | #include "duckdb/planner/expression_binder/alter_binder.hpp" |
| 2 | |
| 3 | #include "duckdb/parser/expression/columnref_expression.hpp" |
| 4 | #include "duckdb/planner/expression/bound_reference_expression.hpp" |
| 5 | #include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp" |
| 6 | |
| 7 | namespace duckdb { |
| 8 | |
| 9 | AlterBinder::AlterBinder(Binder &binder, ClientContext &context, TableCatalogEntry &table, |
| 10 | vector<LogicalIndex> &bound_columns, LogicalType target_type) |
| 11 | : ExpressionBinder(binder, context), table(table), bound_columns(bound_columns) { |
| 12 | this->target_type = std::move(target_type); |
| 13 | } |
| 14 | |
| 15 | BindResult AlterBinder::BindExpression(unique_ptr<ParsedExpression> &expr_ptr, idx_t depth, bool root_expression) { |
| 16 | auto &expr = *expr_ptr; |
| 17 | switch (expr.GetExpressionClass()) { |
| 18 | case ExpressionClass::WINDOW: |
| 19 | return BindResult("window functions are not allowed in alter statement" ); |
| 20 | case ExpressionClass::SUBQUERY: |
| 21 | return BindResult("cannot use subquery in alter statement" ); |
| 22 | case ExpressionClass::COLUMN_REF: |
| 23 | return BindColumn(expr&: expr.Cast<ColumnRefExpression>()); |
| 24 | default: |
| 25 | return ExpressionBinder::BindExpression(expr_ptr, depth); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | string AlterBinder::UnsupportedAggregateMessage() { |
| 30 | return "aggregate functions are not allowed in alter statement" ; |
| 31 | } |
| 32 | |
| 33 | BindResult AlterBinder::BindColumn(ColumnRefExpression &colref) { |
| 34 | if (colref.column_names.size() > 1) { |
| 35 | return BindQualifiedColumnName(colref, table_name: table.name); |
| 36 | } |
| 37 | auto idx = table.GetColumnIndex(name&: colref.column_names[0], if_exists: true); |
| 38 | if (!idx.IsValid()) { |
| 39 | throw BinderException("Table does not contain column %s referenced in alter statement!" , |
| 40 | colref.column_names[0]); |
| 41 | } |
| 42 | if (table.GetColumn(idx).Generated()) { |
| 43 | throw BinderException("Using generated columns in alter statement not supported" ); |
| 44 | } |
| 45 | bound_columns.push_back(x: idx); |
| 46 | return BindResult(make_uniq<BoundReferenceExpression>(args: table.GetColumn(idx).Type(), args: bound_columns.size() - 1)); |
| 47 | } |
| 48 | |
| 49 | } // namespace duckdb |
| 50 | |