@@ -23,7 +23,6 @@ use crate::merge_projection::merge_projection;
2323use crate :: optimizer:: ApplyOrder ;
2424use crate :: push_down_filter:: replace_cols_by_name;
2525use crate :: { OptimizerConfig , OptimizerRule } ;
26- use arrow:: datatypes:: DataType ;
2726use arrow:: error:: Result as ArrowResult ;
2827use datafusion_common:: ScalarValue :: UInt8 ;
2928use datafusion_common:: {
@@ -149,10 +148,6 @@ impl OptimizerRule for PushDownProjection {
149148 {
150149 let mut used_columns: HashSet < Column > = HashSet :: new ( ) ;
151150 if projection_is_empty {
152- let field = find_small_field ( scan. projected_schema . fields ( ) ) . ok_or (
153- DataFusionError :: Internal ( "Scan with empty schema" . to_string ( ) ) ,
154- ) ?;
155- used_columns. insert ( field. qualified_column ( ) ) ;
156151 push_down_scan ( & used_columns, scan, true ) ?
157152 } else {
158153 for expr in projection. expr . iter ( ) {
@@ -163,17 +158,6 @@ impl OptimizerRule for PushDownProjection {
163158 plan. with_new_inputs ( & [ new_scan] ) ?
164159 }
165160 }
166- LogicalPlan :: Values ( values) if projection_is_empty => {
167- let field = find_small_field ( values. schema . fields ( ) ) . ok_or (
168- DataFusionError :: Internal ( "Values with empty schema" . to_string ( ) ) ,
169- ) ?;
170- let column = Expr :: Column ( field. qualified_column ( ) ) ;
171-
172- LogicalPlan :: Projection ( Projection :: try_new (
173- vec ! [ column] ,
174- Arc :: new ( child_plan. clone ( ) ) ,
175- ) ?)
176- }
177161 LogicalPlan :: Union ( union) => {
178162 let mut required_columns = HashSet :: new ( ) ;
179163 exprlist_to_columns ( & projection. expr , & mut required_columns) ?;
@@ -429,87 +413,6 @@ pub fn collect_projection_expr(projection: &Projection) -> HashMap<String, Expr>
429413 . collect :: < HashMap < _ , _ > > ( )
430414}
431415
432- /// Accumulate the memory size of a data type measured in bits.
433- ///
434- /// Types with a variable size get assigned with a fixed size which is greater than most
435- /// primitive types.
436- ///
437- /// While traversing nested types, `nesting` is incremented on every level.
438- fn nested_size ( data_type : & DataType , nesting : & mut usize ) -> usize {
439- use DataType :: * ;
440- if data_type. is_primitive ( ) {
441- return data_type. primitive_width ( ) . unwrap_or ( 1 ) * 8 ;
442- }
443-
444- if data_type. is_nested ( ) {
445- * nesting += 1 ;
446- }
447-
448- match data_type {
449- Null => 0 ,
450- Boolean => 1 ,
451- Binary | Utf8 => 128 ,
452- LargeBinary | LargeUtf8 => 256 ,
453- FixedSizeBinary ( bytes) => ( * bytes * 8 ) as usize ,
454- // primitive types
455- Int8
456- | Int16
457- | Int32
458- | Int64
459- | UInt8
460- | UInt16
461- | UInt32
462- | UInt64
463- | Float16
464- | Float32
465- | Float64
466- | Timestamp ( _, _)
467- | Date32
468- | Date64
469- | Time32 ( _)
470- | Time64 ( _)
471- | Duration ( _)
472- | Interval ( _)
473- | Dictionary ( _, _)
474- | Decimal128 ( _, _)
475- | Decimal256 ( _, _) => data_type. primitive_width ( ) . unwrap_or ( 1 ) * 8 ,
476- // nested types
477- List ( f) => nested_size ( f. data_type ( ) , nesting) ,
478- FixedSizeList ( _, s) => ( s * 8 ) as usize ,
479- LargeList ( f) => nested_size ( f. data_type ( ) , nesting) ,
480- Struct ( fields) => fields
481- . iter ( )
482- . map ( |f| nested_size ( f. data_type ( ) , nesting) )
483- . sum ( ) ,
484- Union ( fields, _) => fields
485- . iter ( )
486- . map ( |( _, f) | nested_size ( f. data_type ( ) , nesting) )
487- . sum ( ) ,
488- Map ( field, _) => nested_size ( field. data_type ( ) , nesting) ,
489- RunEndEncoded ( run_ends, values) => {
490- nested_size ( run_ends. data_type ( ) , nesting)
491- + nested_size ( values. data_type ( ) , nesting)
492- }
493- }
494- }
495-
496- /// Find a field with a presumable small memory footprint based on its data type's memory size
497- /// and the level of nesting.
498- fn find_small_field ( fields : & [ DFField ] ) -> Option < DFField > {
499- fields
500- . iter ( )
501- . map ( |f| {
502- let nesting = & mut 0 ;
503- let size = nested_size ( f. data_type ( ) , nesting) ;
504- ( * nesting, size)
505- } )
506- . enumerate ( )
507- . min_by ( |( _, ( nesting_a, size_a) ) , ( _, ( nesting_b, size_b) ) | {
508- nesting_a. cmp ( nesting_b) . then ( size_a. cmp ( size_b) )
509- } )
510- . map ( |( i, _) | fields[ i] . clone ( ) )
511- }
512-
513416/// Get the projection exprs from columns in the order of the schema
514417fn get_expr ( columns : & HashSet < Column > , schema : & DFSchemaRef ) -> Result < Vec < Expr > > {
515418 let expr = schema
@@ -640,7 +543,7 @@ mod tests {
640543 use crate :: optimizer:: Optimizer ;
641544 use crate :: test:: * ;
642545 use crate :: OptimizerContext ;
643- use arrow:: datatypes:: { DataType , Field , Schema , TimeUnit } ;
546+ use arrow:: datatypes:: { DataType , Field , Schema } ;
644547 use datafusion_common:: DFSchema ;
645548 use datafusion_expr:: builder:: table_scan_with_filters;
646549 use datafusion_expr:: expr;
@@ -1232,73 +1135,4 @@ mod tests {
12321135 . unwrap_or ( optimized_plan) ;
12331136 Ok ( optimized_plan)
12341137 }
1235-
1236- #[ test]
1237- fn test_nested_size ( ) {
1238- use DataType :: * ;
1239- let nesting = & mut 0 ;
1240- assert_eq ! ( nested_size( & Null , nesting) , 0 ) ;
1241- assert_eq ! ( * nesting, 0 ) ;
1242- assert_eq ! ( nested_size( & Boolean , nesting) , 1 ) ;
1243- assert_eq ! ( * nesting, 0 ) ;
1244- assert_eq ! ( nested_size( & UInt8 , nesting) , 8 ) ;
1245- assert_eq ! ( * nesting, 0 ) ;
1246- assert_eq ! ( nested_size( & Int64 , nesting) , 64 ) ;
1247- assert_eq ! ( * nesting, 0 ) ;
1248- assert_eq ! ( nested_size( & Decimal256 ( 5 , 2 ) , nesting) , 256 ) ;
1249- assert_eq ! ( * nesting, 0 ) ;
1250- assert_eq ! (
1251- nested_size( & List ( Arc :: new( Field :: new( "A" , Int64 , true ) ) ) , nesting) ,
1252- 64
1253- ) ;
1254- assert_eq ! ( * nesting, 1 ) ;
1255- * nesting = 0 ;
1256- assert_eq ! (
1257- nested_size(
1258- & List ( Arc :: new( Field :: new(
1259- "A" ,
1260- List ( Arc :: new( Field :: new( "AA" , Int64 , true ) ) ) ,
1261- true
1262- ) ) ) ,
1263- nesting
1264- ) ,
1265- 64
1266- ) ;
1267- assert_eq ! ( * nesting, 2 ) ;
1268- }
1269-
1270- #[ test]
1271- fn test_find_small_field ( ) {
1272- use DataType :: * ;
1273- let int32 = DFField :: from ( Field :: new ( "a" , Int32 , false ) ) ;
1274- let bin = DFField :: from ( Field :: new ( "b" , Binary , false ) ) ;
1275- let list_i64 = DFField :: from ( Field :: new (
1276- "c" ,
1277- List ( Arc :: new ( Field :: new ( "c_1" , Int64 , true ) ) ) ,
1278- false ,
1279- ) ) ;
1280- let time_s = DFField :: from ( Field :: new ( "d" , Time32 ( TimeUnit :: Second ) , false ) ) ;
1281-
1282- assert_eq ! (
1283- find_small_field( & [
1284- int32. clone( ) ,
1285- bin. clone( ) ,
1286- list_i64. clone( ) ,
1287- time_s. clone( )
1288- ] ) ,
1289- Some ( int32. clone( ) )
1290- ) ;
1291- assert_eq ! (
1292- find_small_field( & [ bin. clone( ) , list_i64. clone( ) , time_s. clone( ) ] ) ,
1293- Some ( time_s. clone( ) )
1294- ) ;
1295- assert_eq ! (
1296- find_small_field( & [ time_s. clone( ) , int32. clone( ) ] ) ,
1297- Some ( time_s. clone( ) )
1298- ) ;
1299- assert_eq ! (
1300- find_small_field( & [ bin. clone( ) , list_i64. clone( ) ] ) ,
1301- Some ( bin. clone( ) )
1302- ) ;
1303- }
13041138}
0 commit comments