Dandandan commented on code in PR #7833:
URL: https://github.com/apache/arrow-datafusion/pull/7833#discussion_r1361792738
##########
datafusion/optimizer/src/push_down_projection.rs:
##########
@@ -423,7 +426,93 @@ pub fn collect_projection_expr(projection: &Projection) ->
HashMap<String, Expr>
.collect::<HashMap<_, _>>()
}
-// Get the projection exprs from columns in the order of the schema
+/// Accumulate the memory size of a data type measured in bits.
+///
+/// Nested types are traversed and increment `nesting` on every level.
+fn nested_size(data_type: &DataType, nesting: &mut usize) -> usize {
+ use DataType::*;
+ if data_type.is_primitive() {
+ return data_type.primitive_width().unwrap_or(1) * 8;
+ }
+
+ if data_type.is_nested() {
+ *nesting += 1;
+ }
+
+ match data_type {
+ Null => 0,
+ Boolean => 1,
+ Binary | Utf8 => 128,
+ LargeBinary | LargeUtf8 => 256,
+ FixedSizeBinary(bytes) => (*bytes * 8) as usize,
+ // primitive types
+ Int8
+ | Int16
+ | Int32
+ | Int64
+ | UInt8
+ | UInt16
+ | UInt32
+ | UInt64
+ | Float16
+ | Float32
+ | Float64
+ | Timestamp(_, _)
+ | Date32
+ | Date64
+ | Time32(_)
+ | Time64(_)
+ | Duration(_)
+ | Interval(_)
+ | Dictionary(_, _)
+ | Decimal128(_, _)
+ | Decimal256(_, _) => data_type.primitive_width().unwrap_or(1) * 8,
+ // nested types
+ List(f) => nested_size(f.data_type(), nesting),
+ FixedSizeList(_, s) => (s * 8) as usize,
+ LargeList(f) => nested_size(f.data_type(), nesting),
+ Struct(fields) => fields
+ .iter()
+ .map(|f| nested_size(f.data_type(), nesting))
+ .sum(),
+ Union(fields, _) => fields
+ .iter()
+ .map(|(_, f)| nested_size(f.data_type(), nesting))
+ .sum(),
+ Map(field, _) => nested_size(field.data_type(), nesting),
+ RunEndEncoded(run_ends, values) => {
+ nested_size(run_ends.data_type(), nesting)
+ + nested_size(values.data_type(), nesting)
+ }
+ }
+}
+
+/// Find a field with a presumable small memory footprint based on its data
type's memory size
+/// and the level of nesting.
+fn find_small_field(fields: &[DFField]) -> DFField {
+ let mut indexed_fields = fields
+ .iter()
+ .map(|f| {
+ let nesting = &mut 0;
+ let size = nested_size(f.field().data_type(), nesting);
+ (*nesting, size)
+ })
+ .enumerate()
+ .collect_vec();
+
+ let (i, _) = indexed_fields
+ .select_nth_unstable_by(
Review Comment:
We can use `min_by` instead of sorting?
##########
datafusion/optimizer/src/push_down_projection.rs:
##########
@@ -423,7 +426,93 @@ pub fn collect_projection_expr(projection: &Projection) ->
HashMap<String, Expr>
.collect::<HashMap<_, _>>()
}
-// Get the projection exprs from columns in the order of the schema
+/// Accumulate the memory size of a data type measured in bits.
+///
+/// Nested types are traversed and increment `nesting` on every level.
+fn nested_size(data_type: &DataType, nesting: &mut usize) -> usize {
+ use DataType::*;
+ if data_type.is_primitive() {
+ return data_type.primitive_width().unwrap_or(1) * 8;
+ }
+
+ if data_type.is_nested() {
+ *nesting += 1;
+ }
+
+ match data_type {
+ Null => 0,
+ Boolean => 1,
+ Binary | Utf8 => 128,
+ LargeBinary | LargeUtf8 => 256,
+ FixedSizeBinary(bytes) => (*bytes * 8) as usize,
+ // primitive types
+ Int8
+ | Int16
+ | Int32
+ | Int64
+ | UInt8
+ | UInt16
+ | UInt32
+ | UInt64
+ | Float16
+ | Float32
+ | Float64
+ | Timestamp(_, _)
+ | Date32
+ | Date64
+ | Time32(_)
+ | Time64(_)
+ | Duration(_)
+ | Interval(_)
+ | Dictionary(_, _)
+ | Decimal128(_, _)
+ | Decimal256(_, _) => data_type.primitive_width().unwrap_or(1) * 8,
+ // nested types
+ List(f) => nested_size(f.data_type(), nesting),
+ FixedSizeList(_, s) => (s * 8) as usize,
+ LargeList(f) => nested_size(f.data_type(), nesting),
+ Struct(fields) => fields
+ .iter()
+ .map(|f| nested_size(f.data_type(), nesting))
+ .sum(),
+ Union(fields, _) => fields
+ .iter()
+ .map(|(_, f)| nested_size(f.data_type(), nesting))
+ .sum(),
+ Map(field, _) => nested_size(field.data_type(), nesting),
+ RunEndEncoded(run_ends, values) => {
+ nested_size(run_ends.data_type(), nesting)
+ + nested_size(values.data_type(), nesting)
+ }
+ }
+}
+
+/// Find a field with a presumable small memory footprint based on its data
type's memory size
+/// and the level of nesting.
+fn find_small_field(fields: &[DFField]) -> DFField {
+ let mut indexed_fields = fields
+ .iter()
+ .map(|f| {
+ let nesting = &mut 0;
+ let size = nested_size(f.field().data_type(), nesting);
+ (*nesting, size)
+ })
+ .enumerate()
+ .collect_vec();
+
+ let (i, _) = indexed_fields
+ .select_nth_unstable_by(
Review Comment:
We can use `min_by` instead of selecting/sorting?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]