11use crate :: catalog:: TableName ;
22use crate :: errors:: DatabaseError ;
33use crate :: expression:: ScalarExpression ;
4+ use crate :: types:: tuple:: EMPTY_TUPLE ;
5+ use crate :: types:: value:: ValueRef ;
6+ use crate :: types:: { ColumnId , LogicalType } ;
47use serde:: { Deserialize , Serialize } ;
58use sqlparser:: ast:: CharLengthUnits ;
69use std:: hash:: Hash ;
710use std:: sync:: Arc ;
811
9- use crate :: types:: tuple:: EMPTY_TUPLE ;
10- use crate :: types:: value:: ValueRef ;
11- use crate :: types:: { ColumnId , LogicalType } ;
12-
1312pub type ColumnRef = Arc < ColumnCatalog > ;
1413
15- #[ derive( Debug , Clone , Serialize , Deserialize , Hash , Eq , PartialEq ) ]
14+ #[ derive( Debug , Clone , Hash , Eq , PartialEq ) ]
1615pub struct ColumnCatalog {
1716 pub summary : ColumnSummary ,
1817 pub nullable : bool ,
1918 pub desc : ColumnDesc ,
2019}
2120
21+ #[ derive( Debug , Clone , Serialize , Deserialize , Hash , Eq , PartialEq ) ]
22+ pub enum ColumnRelation {
23+ None ,
24+ Table {
25+ column_id : ColumnId ,
26+ table_name : TableName ,
27+ } ,
28+ }
29+
2230#[ derive( Debug , Clone , Serialize , Deserialize , Hash , Eq , PartialEq ) ]
2331pub struct ColumnSummary {
24- pub id : Option < ColumnId > ,
2532 pub name : String ,
26- pub table_name : Option < TableName > ,
33+ pub relation : ColumnRelation ,
2734}
2835
2936impl ColumnCatalog {
3037 pub fn new ( column_name : String , nullable : bool , column_desc : ColumnDesc ) -> ColumnCatalog {
3138 ColumnCatalog {
3239 summary : ColumnSummary {
33- id : None ,
3440 name : column_name,
35- table_name : None ,
41+ relation : ColumnRelation :: None ,
3642 } ,
3743 nullable,
3844 desc : column_desc,
@@ -42,17 +48,18 @@ impl ColumnCatalog {
4248 pub ( crate ) fn new_dummy ( column_name : String ) -> ColumnCatalog {
4349 ColumnCatalog {
4450 summary : ColumnSummary {
45- id : None ,
4651 name : column_name,
47- table_name : None ,
52+ relation : ColumnRelation :: None ,
4853 } ,
4954 nullable : true ,
55+ // SAFETY: default expr must not be [`ScalarExpression::ColumnRef`]
5056 desc : ColumnDesc :: new (
5157 LogicalType :: Varchar ( None , CharLengthUnits :: Characters ) ,
5258 false ,
5359 false ,
5460 None ,
55- ) ,
61+ )
62+ . unwrap ( ) ,
5663 }
5764 }
5865
@@ -61,7 +68,10 @@ impl ColumnCatalog {
6168 }
6269
6370 pub ( crate ) fn id ( & self ) -> Option < ColumnId > {
64- self . summary . id
71+ match & self . summary . relation {
72+ ColumnRelation :: None => None ,
73+ ColumnRelation :: Table { column_id, .. } => Some ( * column_id) ,
74+ }
6575 }
6676
6777 pub fn name ( & self ) -> & str {
@@ -76,19 +86,21 @@ impl ColumnCatalog {
7686 }
7787
7888 pub fn table_name ( & self ) -> Option < & TableName > {
79- self . summary . table_name . as_ref ( )
89+ match & self . summary . relation {
90+ ColumnRelation :: None => None ,
91+ ColumnRelation :: Table { table_name, .. } => Some ( table_name) ,
92+ }
8093 }
8194
8295 pub fn set_name ( & mut self , name : String ) {
8396 self . summary . name = name;
8497 }
8598
86- pub fn set_id ( & mut self , id : ColumnId ) {
87- self . summary . id = Some ( id) ;
88- }
89-
90- pub fn set_table_name ( & mut self , table_name : TableName ) {
91- self . summary . table_name = Some ( table_name) ;
99+ pub fn set_ref_table ( & mut self , table_name : TableName , column_id : ColumnId ) {
100+ self . summary . relation = ColumnRelation :: Table {
101+ column_id,
102+ table_name,
103+ } ;
92104 }
93105
94106 pub fn datatype ( & self ) -> & LogicalType {
@@ -110,7 +122,7 @@ impl ColumnCatalog {
110122}
111123
112124/// The descriptor of a column.
113- #[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize , Hash ) ]
125+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
114126pub struct ColumnDesc {
115127 pub ( crate ) column_datatype : LogicalType ,
116128 pub ( crate ) is_primary : bool ,
@@ -119,17 +131,23 @@ pub struct ColumnDesc {
119131}
120132
121133impl ColumnDesc {
122- pub const fn new (
134+ pub fn new (
123135 column_datatype : LogicalType ,
124136 is_primary : bool ,
125137 is_unique : bool ,
126138 default : Option < ScalarExpression > ,
127- ) -> ColumnDesc {
128- ColumnDesc {
139+ ) -> Result < ColumnDesc , DatabaseError > {
140+ if let Some ( expr) = & default {
141+ if expr. has_table_ref_column ( ) {
142+ return Err ( DatabaseError :: DefaultNotColumnRef ) ;
143+ }
144+ }
145+
146+ Ok ( ColumnDesc {
129147 column_datatype,
130148 is_primary,
131149 is_unique,
132150 default,
133- }
151+ } )
134152 }
135153}
0 commit comments