@@ -74,7 +74,6 @@ use databend_common_expression::infer_schema_type;
74
74
use databend_common_expression:: infer_table_schema;
75
75
use databend_common_expression:: types:: DataType ;
76
76
use databend_common_expression:: AutoIncrementExpr ;
77
- use databend_common_expression:: ColumnId ;
78
77
use databend_common_expression:: ComputedExpr ;
79
78
use databend_common_expression:: DataField ;
80
79
use databend_common_expression:: DataSchemaRefExt ;
@@ -1093,7 +1092,7 @@ impl Binder {
1093
1092
. get_table ( & catalog, & database, & table)
1094
1093
. await ?
1095
1094
. schema ( ) ;
1096
- let ( field, comment, is_deterministic, is_nextval) =
1095
+ let ( field, comment, is_deterministic, is_nextval, is_autoincrement ) =
1097
1096
self . analyze_add_column ( column, schema) . await ?;
1098
1097
let option = match ast_option {
1099
1098
AstAddColumnOption :: First => AddColumnOption :: First ,
@@ -1112,6 +1111,7 @@ impl Binder {
1112
1111
option,
1113
1112
is_deterministic,
1114
1113
is_nextval,
1114
+ is_autoincrement,
1115
1115
} ) ) )
1116
1116
}
1117
1117
AlterTableAction :: AddConstraint { constraint } => {
@@ -1182,7 +1182,7 @@ impl Binder {
1182
1182
. await ?
1183
1183
. schema ( ) ;
1184
1184
for column in column_def_vec {
1185
- let ( field, comment, _, _) =
1185
+ let ( field, comment, _, _, _ ) =
1186
1186
self . analyze_add_column ( column, schema. clone ( ) ) . await ?;
1187
1187
field_and_comment. push ( ( field, comment) ) ;
1188
1188
}
@@ -1659,12 +1659,13 @@ impl Binder {
1659
1659
& self ,
1660
1660
column : & ColumnDefinition ,
1661
1661
table_schema : TableSchemaRef ,
1662
- ) -> Result < ( TableField , String , bool , bool ) > {
1662
+ ) -> Result < ( TableField , String , bool , bool , bool ) > {
1663
1663
let name = normalize_identifier ( & column. name , & self . name_resolution_ctx ) . name ;
1664
1664
let not_null = self . is_column_not_null ( ) ;
1665
1665
let data_type = resolve_type_name ( & column. data_type , not_null) ?;
1666
1666
let mut is_deterministic = true ;
1667
1667
let mut is_nextval = false ;
1668
+ let mut is_autoincrement = false ;
1668
1669
let mut field = TableField :: new ( & name, data_type) ;
1669
1670
1670
1671
if let Some ( expr) = & column. expr {
@@ -1710,16 +1711,23 @@ impl Binder {
1710
1711
) ) ;
1711
1712
}
1712
1713
field. auto_increment_expr = Some ( AutoIncrementExpr {
1713
- column_id : table_schema. fields ( ) . len ( ) as ColumnId ,
1714
+ column_id : table_schema. next_column_id ( ) ,
1714
1715
start : * start,
1715
1716
step : * step,
1716
1717
is_ordered : * is_ordered,
1717
1718
} ) ;
1719
+ is_autoincrement = true ;
1718
1720
}
1719
1721
}
1720
1722
}
1721
1723
let comment = column. comment . clone ( ) . unwrap_or_default ( ) ;
1722
- Ok ( ( field, comment, is_deterministic, is_nextval) )
1724
+ Ok ( (
1725
+ field,
1726
+ comment,
1727
+ is_deterministic,
1728
+ is_nextval,
1729
+ is_autoincrement,
1730
+ ) )
1723
1731
}
1724
1732
1725
1733
#[ async_backtrace:: framed]
@@ -1728,11 +1736,12 @@ impl Binder {
1728
1736
columns : & [ ColumnDefinition ] ,
1729
1737
) -> Result < ( TableSchemaRef , Vec < String > ) > {
1730
1738
let mut has_computed = false ;
1739
+ let mut has_autoincrement = false ;
1731
1740
let mut fields = Vec :: with_capacity ( columns. len ( ) ) ;
1732
1741
let mut fields_comments = Vec :: with_capacity ( columns. len ( ) ) ;
1733
1742
let not_null = self . is_column_not_null ( ) ;
1734
1743
let mut default_expr_binder = DefaultExprBinder :: try_new ( self . ctx . clone ( ) ) ?;
1735
- for ( i , column) in columns. iter ( ) . enumerate ( ) {
1744
+ for column in columns. iter ( ) {
1736
1745
let name = normalize_identifier ( & column. name , & self . name_resolution_ctx ) . name ;
1737
1746
let schema_data_type = resolve_type_name ( & column. data_type , not_null) ?;
1738
1747
fields_comments. push ( column. comment . clone ( ) . unwrap_or_default ( ) ) ;
@@ -1757,8 +1766,9 @@ impl Binder {
1757
1766
"AUTO INCREMENT only supports Decimal or Numeric (e.g. INT32) types" ,
1758
1767
) ) ;
1759
1768
}
1769
+ has_autoincrement = true ;
1760
1770
field. auto_increment_expr = Some ( AutoIncrementExpr {
1761
- column_id : i as ColumnId ,
1771
+ column_id : 0 ,
1762
1772
start : * start,
1763
1773
step : * step,
1764
1774
is_ordered : * is_ordered,
@@ -1769,6 +1779,18 @@ impl Binder {
1769
1779
}
1770
1780
fields. push ( field) ;
1771
1781
}
1782
+ // update auto increment expr column id
1783
+ if has_autoincrement {
1784
+ let table_schema = TableSchema :: new ( fields. clone ( ) ) ;
1785
+
1786
+ for ( i, table_field) in table_schema. fields ( ) . iter ( ) . enumerate ( ) {
1787
+ let Some ( auto_increment_expr) = fields[ i] . auto_increment_expr . as_mut ( ) else {
1788
+ continue ;
1789
+ } ;
1790
+
1791
+ auto_increment_expr. column_id = table_field. column_id ;
1792
+ }
1793
+ }
1772
1794
1773
1795
let fields = if has_computed {
1774
1796
let mut source_fields = Vec :: with_capacity ( fields. len ( ) ) ;
0 commit comments