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 | |
6 | using namespace std; |
7 | |
8 | namespace duckdb { |
9 | |
10 | AlterBinder::AlterBinder(Binder &binder, ClientContext &context, string table, vector<ColumnDefinition> &columns, |
11 | vector<column_t> &bound_columns, SQLType target_type) |
12 | : ExpressionBinder(binder, context), table(table), columns(columns), bound_columns(bound_columns) { |
13 | this->target_type = target_type; |
14 | } |
15 | |
16 | BindResult AlterBinder::BindExpression(ParsedExpression &expr, idx_t depth, bool root_expression) { |
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((ColumnRefExpression &)expr); |
24 | default: |
25 | return ExpressionBinder::BindExpression(expr, 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.table_name.empty() && colref.table_name != table) { |
35 | throw BinderException("Cannot reference table %s from within alter statement for table %s!" , |
36 | colref.table_name.c_str(), table.c_str()); |
37 | } |
38 | for (idx_t i = 0; i < columns.size(); i++) { |
39 | if (colref.column_name == columns[i].name) { |
40 | bound_columns.push_back(i); |
41 | return BindResult( |
42 | make_unique<BoundReferenceExpression>(GetInternalType(columns[i].type), bound_columns.size() - 1), |
43 | columns[i].type); |
44 | } |
45 | } |
46 | throw BinderException("Table does not contain column %s referenced in alter statement!" , |
47 | colref.column_name.c_str()); |
48 | } |
49 | |
50 | } // namespace duckdb |
51 | |