@@ -171,12 +171,10 @@ use crate::plans::WindowOrderBy;
171171use crate :: BaseTableColumn ;
172172use crate :: BindContext ;
173173use crate :: ColumnBinding ;
174- use crate :: ColumnBindingBuilder ;
175174use crate :: ColumnEntry ;
176175use crate :: DefaultExprBinder ;
177176use crate :: IndexType ;
178177use crate :: MetadataRef ;
179- use crate :: Visibility ;
180178
181179/// A helper for type checking.
182180///
@@ -601,15 +599,6 @@ impl<'a> TypeChecker<'a> {
601599 // as we cast JSON null to SQL NULL.
602600 let target_type = if data_type. remove_nullable ( ) == DataType :: Variant {
603601 let target_type = checked_expr. data_type ( ) . nest_wrap_nullable ( ) ;
604-
605- if let Some ( new_scalar) = self . try_rewrite_virtual_column_cast (
606- expr. span ( ) ,
607- & scalar,
608- & target_type,
609- false ,
610- ) {
611- return Ok ( Box :: new ( ( new_scalar, target_type) ) ) ;
612- }
613602 target_type
614603 // if the source type is nullable, cast target type should also be nullable.
615604 } else if data_type. is_nullable_or_null ( ) {
@@ -659,15 +648,6 @@ impl<'a> TypeChecker<'a> {
659648 // as we cast JSON null to SQL NULL.
660649 let target_type = if data_type. remove_nullable ( ) == DataType :: Variant {
661650 let target_type = checked_expr. data_type ( ) . nest_wrap_nullable ( ) ;
662-
663- if let Some ( new_scalar) = self . try_rewrite_virtual_column_cast (
664- expr. span ( ) ,
665- & scalar,
666- & target_type,
667- true ,
668- ) {
669- return Ok ( Box :: new ( ( new_scalar, target_type) ) ) ;
670- }
671651 target_type
672652 } else {
673653 checked_expr. data_type ( ) . clone ( )
@@ -1194,105 +1174,6 @@ impl<'a> TypeChecker<'a> {
11941174 Ok ( Box :: new ( ( scalar, data_type) ) )
11951175 }
11961176
1197- fn try_rewrite_virtual_column_cast (
1198- & mut self ,
1199- span : Span ,
1200- scalar : & ScalarExpr ,
1201- target_type : & DataType ,
1202- is_try : bool ,
1203- ) -> Option < ScalarExpr > {
1204- let Ok ( cast_ty) = infer_schema_type ( target_type) else {
1205- return None ;
1206- } ;
1207- let ScalarExpr :: BoundColumnRef ( BoundColumnRef { ref column, .. } ) = scalar else {
1208- return None ;
1209- } ;
1210- let table_index = column. table_index ?;
1211-
1212- if column. index >= self . metadata . read ( ) . columns ( ) . len ( ) {
1213- return None ;
1214- }
1215-
1216- // Change the type of virtual column to user specified cast type avoids additional casting overhead,
1217- // since the user usually knows the real type.
1218- let column_entry = self . metadata . read ( ) . column ( column. index ) . clone ( ) ;
1219- let ColumnEntry :: VirtualColumn ( virtual_column) = column_entry else {
1220- return None ;
1221- } ;
1222-
1223- let virtual_column_name = if is_try {
1224- format ! (
1225- "try_cast({} as {})" ,
1226- column. column_name,
1227- target_type. remove_nullable( ) . to_string( ) . to_lowercase( )
1228- )
1229- } else {
1230- format ! (
1231- "{}::{}" ,
1232- column. column_name,
1233- target_type. remove_nullable( ) . to_string( ) . to_lowercase( )
1234- )
1235- } ;
1236-
1237- // Try resolve the virtual column with the cast type.
1238- if let Ok ( box ( new_scalar, _) ) = self . resolve ( & Expr :: ColumnRef {
1239- span,
1240- column : ColumnRef {
1241- database : column
1242- . database_name
1243- . as_ref ( )
1244- . map ( |name| Identifier :: from_name ( span, name) ) ,
1245- table : column
1246- . table_name
1247- . as_ref ( )
1248- . map ( |name| Identifier :: from_name ( span, name) ) ,
1249- column : ColumnID :: Name ( Identifier :: from_name ( span, & virtual_column_name) ) ,
1250- } ,
1251- } ) {
1252- return Some ( new_scalar) ;
1253- }
1254-
1255- // Generate a new virtual column with the cast type.
1256- let database_name = column. database_name . clone ( ) ;
1257- let table_name = column. table_name . clone ( ) ;
1258-
1259- let mut guard = self . metadata . write ( ) ;
1260- let new_column_index = guard. add_virtual_column (
1261- virtual_column. table_index ,
1262- virtual_column. source_column_name . clone ( ) ,
1263- virtual_column. source_column_id ,
1264- virtual_column. column_id ,
1265- virtual_column_name. clone ( ) ,
1266- cast_ty,
1267- is_try,
1268- ) ;
1269-
1270- let new_column_binding = ColumnBindingBuilder :: new (
1271- virtual_column_name,
1272- new_column_index,
1273- Box :: new ( target_type. clone ( ) ) ,
1274- Visibility :: InVisible ,
1275- )
1276- . table_name ( table_name)
1277- . database_name ( database_name)
1278- . table_index ( Some ( table_index) )
1279- . build ( ) ;
1280- // Add virtual column with the cast type to the context.
1281- self . bind_context
1282- . add_column_binding ( new_column_binding. clone ( ) ) ;
1283-
1284- if let Some ( scan_id) = guard. base_column_scan_id ( virtual_column. column_index ) {
1285- let mut base_column_scan_id = HashMap :: new ( ) ;
1286- base_column_scan_id. insert ( new_column_index, scan_id) ;
1287- guard. add_base_column_scan_id ( base_column_scan_id) ;
1288- }
1289-
1290- Some ( ScalarExpr :: BoundColumnRef ( BoundColumnRef {
1291- span,
1292- column : new_column_binding,
1293- } ) )
1294- }
1295-
12961177 // TODO: remove this function
12971178 fn rewrite_substring ( args : & mut [ ScalarExpr ] ) {
12981179 if let ScalarExpr :: ConstantExpr ( expr) = & args[ 1 ] {
@@ -3996,22 +3877,12 @@ impl<'a> TypeChecker<'a> {
39963877 {
39973878 if data_type. remove_nullable ( ) == DataType :: Variant {
39983879 let target_type = DataType :: Nullable ( Box :: new ( DataType :: String ) ) ;
3999- let new_scalar = if let Some ( new_scalar) = self
4000- . try_rewrite_virtual_column_cast (
4001- scalar. span ( ) ,
4002- & scalar,
4003- & target_type,
4004- false ,
4005- ) {
4006- new_scalar
4007- } else {
4008- ScalarExpr :: CastExpr ( CastExpr {
4009- span : scalar. span ( ) ,
4010- is_try : false ,
4011- argument : Box :: new ( scalar) ,
4012- target_type : Box :: new ( target_type. clone ( ) ) ,
4013- } )
4014- } ;
3880+ let new_scalar = ScalarExpr :: CastExpr ( CastExpr {
3881+ span : scalar. span ( ) ,
3882+ is_try : false ,
3883+ argument : Box :: new ( scalar) ,
3884+ target_type : Box :: new ( target_type. clone ( ) ) ,
3885+ } ) ;
40153886 return Some ( Ok ( Box :: new ( ( new_scalar, target_type) ) ) ) ;
40163887 }
40173888 }
@@ -4161,21 +4032,12 @@ impl<'a> TypeChecker<'a> {
41614032 ) {
41624033 if func_name == "get_by_keypath_string" {
41634034 let target_type = DataType :: Nullable ( Box :: new ( DataType :: String ) ) ;
4164- let new_scalar = if let Some ( new_scalar) = self . try_rewrite_virtual_column_cast (
4165- scalar. span ( ) ,
4166- & scalar,
4167- & target_type,
4168- false ,
4169- ) {
4170- new_scalar
4171- } else {
4172- ScalarExpr :: CastExpr ( CastExpr {
4173- span : scalar. span ( ) ,
4174- is_try : false ,
4175- argument : Box :: new ( scalar) ,
4176- target_type : Box :: new ( target_type. clone ( ) ) ,
4177- } )
4178- } ;
4035+ let new_scalar = ScalarExpr :: CastExpr ( CastExpr {
4036+ span : scalar. span ( ) ,
4037+ is_try : false ,
4038+ argument : Box :: new ( scalar) ,
4039+ target_type : Box :: new ( target_type. clone ( ) ) ,
4040+ } ) ;
41794041 return Some ( Ok ( Box :: new ( ( new_scalar, target_type) ) ) ) ;
41804042 } else {
41814043 return Some ( Ok ( Box :: new ( ( scalar, data_type) ) ) ) ;
0 commit comments