Apache DataFusion SQL Query Engine
Fix: optimize projections for unnest logical plan. (#16632)
Bert Vermeirende8cbd0ab606
diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rsindex 2ad667e8e9..1ab5ffa758 100644--- a/datafusion/expr/src/logical_plan/builder.rs+++ b/datafusion/expr/src/logical_plan/builder.rs@@ -48,16 +48,14 @@ use crate::{};use super::dml::InsertOp;-use super::plan::ColumnUnnestList;use arrow::compute::can_cast_types;use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};use datafusion_common::display::ToStringifiedPlan;use datafusion_common::file_options::file_type::FileType;use datafusion_common::{- exec_err, get_target_functional_dependencies, internal_err, not_impl_err,- plan_datafusion_err, plan_err, Column, Constraints, DFSchema, DFSchemaRef,- DataFusionError, NullEquality, Result, ScalarValue, TableReference, ToDFSchema,- UnnestOptions,+ exec_err, get_target_functional_dependencies, not_impl_err, plan_datafusion_err,+ plan_err, Column, Constraints, DFSchema, DFSchemaRef, DataFusionError, NullEquality,+ Result, ScalarValue, TableReference, ToDFSchema, UnnestOptions,};use datafusion_expr_common::type_coercion::binary::type_union_resolution;@@ -2089,27 +2087,6 @@ pub fn unnest(input: LogicalPlan, columns: Vec<Column>) -> Result<LogicalPlan> {unnest_with_options(input, columns, UnnestOptions::default())}-// Get the data type of a multi-dimensional type after unnesting it-// with a given depth-fn get_unnested_list_datatype_recursive(- data_type: &DataType,- depth: usize,-) -> Result<DataType> {- match data_type {- DataType::List(field)- | DataType::FixedSizeList(field, _)- | DataType::LargeList(field) => {- if depth == 1 {- return Ok(field.data_type().clone());- }- return get_unnested_list_datatype_recursive(field.data_type(), depth - 1);- }- _ => {}- };-- internal_err!("trying to unnest on invalid data type {:?}", data_type)-}-pub fn get_struct_unnested_columns(col_name: &String,inner_fields: &Fields,@@ -2120,53 +2097,6 @@ pub fn get_struct_unnested_columns(.collect()}-// Based on data type, either struct or a variant of list-// return a set of columns as the result of unnesting-// the input columns.-// For example, given a column with name "a",-// - List(Element) returns ["a"] with data type Element-// - Struct(field1, field2) returns ["a.field1","a.field2"]-// For list data type, an argument depth is used to specify-// the recursion level-pub fn get_unnested_columns(- col_name: &String,- data_type: &DataType,- depth: usize,-) -> Result<Vec<(Column, Arc<Field>)>> {- let mut qualified_columns = Vec::with_capacity(1);-- match data_type {- DataType::List(_) | DataType::FixedSizeList(_, _) | DataType::LargeList(_) => {- let data_type = get_unnested_list_datatype_recursive(data_type, depth)?;- let new_field = Arc::new(Field::new(- col_name, data_type,- // Unnesting may produce NULLs even if the list is not null.- // For example: unnest([1], []) -> 1, null- true,- ));- let column = Column::from_name(col_name);- // let column = Column::from((None, &new_field));- qualified_columns.push((column, new_field));- }- DataType::Struct(fields) => {- qualified_columns.extend(fields.iter().map(|f| {- let new_name = format!("{}.{}", col_name, f.name());- let column = Column::from_name(&new_name);- let new_field = f.as_ref().clone().with_name(new_name);- // let column = Column::from((None, &f));- (column, Arc::new(new_field))- }))- }- _ => {- return internal_err!(- "trying to unnest on invalid data type {:?}",- data_type- );- }- };- Ok(qualified_columns)-}-/// Create a [`LogicalPlan::Unnest`] plan with options/// This function receive a list of columns to be unnested/// because multiple unnest can be performed on the same column (e.g unnest with different depth)@@ -2201,126 +2131,11 @@ pub fn unnest_with_options(columns_to_unnest: Vec<Column>,options: UnnestOptions,) -> Result<LogicalPlan> {- let mut list_columns: Vec<(usize, ColumnUnnestList)> = vec![];- let mut struct_columns = vec![];- let indices_to_unnest = columns_to_unnest- .iter()- .map(|c| Ok((input.schema().index_of_column(c)?, c)))- .collect::<Result<HashMap<usize, &Column>>>()?;-- let input_schema = input.schema();-- let mut dependency_indices = vec![];- // Transform input schema into new schema- // Given this comprehensive example- //- // input schema:- // 1.col1_unnest_placeholder: list[list[int]],- // 2.col1: list[list[int]]- // 3.col2: list[int]- // with unnest on unnest(col1,depth=2), unnest(col1,depth=1) and unnest(col2,depth=1)- // output schema:- // 1.unnest_col1_depth_2: int- // 2.unnest_col1_depth_1: list[int]- // 3.col1: list[list[int]]- // 4.unnest_col2_depth_1: int- // Meaning the placeholder column will be replaced by its unnested variation(s), note- // the plural.- let fields = input_schema- .iter()- .enumerate()- .map(|(index, (original_qualifier, original_field))| {- match indices_to_unnest.get(&index) {- Some(column_to_unnest) => {- let recursions_on_column = options- .recursions- .iter()- .filter(|p| -> bool { &p.input_column == *column_to_unnest })- .collect::<Vec<_>>();- let mut transformed_columns = recursions_on_column- .iter()- .map(|r| {- list_columns.push((- index,- ColumnUnnestList {- output_column: r.output_column.clone(),- depth: r.depth,- },- ));- Ok(get_unnested_columns(- &r.output_column.name,- original_field.data_type(),- r.depth,- )?- .into_iter()- .next()- .unwrap()) // because unnesting a list column always result into one result- })- .collect::<Result<Vec<(Column, Arc<Field>)>>>()?;- if transformed_columns.is_empty() {- transformed_columns = get_unnested_columns(- &column_to_unnest.name,- original_field.data_type(),- 1,- )?;- match original_field.data_type() {- DataType::Struct(_) => {- struct_columns.push(index);- }- DataType::List(_)- | DataType::FixedSizeList(_, _)- | DataType::LargeList(_) => {- list_columns.push((- index,- ColumnUnnestList {- output_column: Column::from_name(- &column_to_unnest.name,- ),- depth: 1,- },- ));- }- _ => {}- };- }-- // new columns dependent on the same original index- dependency_indices- .extend(std::iter::repeat_n(index, transformed_columns.len()));- Ok(transformed_columns- .iter()- .map(|(col, field)| (col.relation.to_owned(), field.to_owned()))- .collect())- }- None => {- dependency_indices.push(index);- Ok(vec![(- original_qualifier.cloned(),- Arc::clone(original_field),- )])- }- }- })- .collect::<Result<Vec<_>>>()?- .into_iter()- .flatten()- .collect::<Vec<_>>();-- let metadata = input_schema.metadata().clone();- let df_schema = DFSchema::new_with_metadata(fields, metadata)?;- // We can use the existing functional dependencies:- let deps = input_schema.functional_dependencies().clone();- let schema = Arc::new(df_schema.with_functional_dependencies(deps)?);-- Ok(LogicalPlan::Unnest(Unnest {- input: Arc::new(input),- exec_columns: columns_to_unnest,- list_type_columns: list_columns,- struct_type_columns: struct_columns,- dependency_indices,- schema,+ Ok(LogicalPlan::Unnest(Unnest::try_new(+ Arc::new(input),+ columns_to_unnest,options,- }))+ )?))}#[cfg(test)]diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rsindex f112b7136d..d68e6cd812 100644--- a/datafusion/expr/src/logical_plan/plan.rs+++ b/datafusion/expr/src/logical_plan/plan.rs@@ -4062,6 +4062,211 @@ impl PartialOrd for Unnest {}}+impl Unnest {+ pub fn try_new(+ input: Arc<LogicalPlan>,+ exec_columns: Vec<Column>,+ options: UnnestOptions,+ ) -> Result<Self> {+ if exec_columns.is_empty() {+ return plan_err!("unnest plan requires at least 1 column to unnest");+ }++ let mut list_columns: Vec<(usize, ColumnUnnestList)> = vec![];+ let mut struct_columns = vec![];+ let indices_to_unnest = exec_columns+ .iter()+ .map(|c| Ok((input.schema().index_of_column(c)?, c)))+ .collect::<Result<HashMap<usize, &Column>>>()?;++ let input_schema = input.schema();++ let mut dependency_indices = vec![];+ // Transform input schema into new schema+ // Given this comprehensive example+ //+ // input schema:+ // 1.col1_unnest_placeholder: list[list[int]],+ // 2.col1: list[list[int]]+ // 3.col2: list[int]+ // with unnest on unnest(col1,depth=2), unnest(col1,depth=1) and unnest(col2,depth=1)+ // output schema:+ // 1.unnest_col1_depth_2: int+ // 2.unnest_col1_depth_1: list[int]+ // 3.col1: list[list[int]]+ // 4.unnest_col2_depth_1: int+ // Meaning the placeholder column will be replaced by its unnested variation(s), note+ // the plural.+ let fields = input_schema+ .iter()+ .enumerate()+ .map(|(index, (original_qualifier, original_field))| {+ match indices_to_unnest.get(&index) {+ Some(column_to_unnest) => {+ let recursions_on_column = options+ .recursions+ .iter()+ .filter(|p| -> bool { &p.input_column == *column_to_unnest })+ .collect::<Vec<_>>();+ let mut transformed_columns = recursions_on_column+ .iter()+ .map(|r| {+ list_columns.push((+ index,+ ColumnUnnestList {+ output_column: r.output_column.clone(),+ depth: r.depth,+ },+ ));+ Ok(get_unnested_columns(+ &r.output_column.name,+ original_field.data_type(),+ r.depth,+ )?+ .into_iter()+ .next()+ .unwrap()) // because unnesting a list column always result into one result+ })+ .collect::<Result<Vec<(Column, Arc<Field>)>>>()?;+ if transformed_columns.is_empty() {+ transformed_columns = get_unnested_columns(+ &column_to_unnest.name,+ original_field.data_type(),+ 1,+ )?;+ match original_field.data_type() {+ DataType::Struct(_) => {+ struct_columns.push(index);+ }+ DataType::List(_)+ | DataType::FixedSizeList(_, _)+ | DataType::LargeList(_) => {+ list_columns.push((+ index,+ ColumnUnnestList {+ output_column: Column::from_name(+ &column_to_unnest.name,+ ),+ depth: 1,+ },+ ));+ }+ _ => {}+ };+ }++ // new columns dependent on the same original index+ dependency_indices.extend(std::iter::repeat_n(+ index,+ transformed_columns.len(),+ ));+ Ok(transformed_columns+ .iter()+ .map(|(col, field)| {+ (col.relation.to_owned(), field.to_owned())+ })+ .collect())+ }+ None => {+ dependency_indices.push(index);+ Ok(vec![(+ original_qualifier.cloned(),+ Arc::clone(original_field),+ )])+ }+ }+ })+ .collect::<Result<Vec<_>>>()?+ .into_iter()+ .flatten()+ .collect::<Vec<_>>();++ let metadata = input_schema.metadata().clone();+ let df_schema = DFSchema::new_with_metadata(fields, metadata)?;+ // We can use the existing functional dependencies:+ let deps = input_schema.functional_dependencies().clone();+ let schema = Arc::new(df_schema.with_functional_dependencies(deps)?);++ Ok(Unnest {+ input,+ exec_columns,+ list_type_columns: list_columns,+ struct_type_columns: struct_columns,+ dependency_indices,+ schema,+ options,+ })+ }+}++// Based on data type, either struct or a variant of list+// return a set of columns as the result of unnesting+// the input columns.+// For example, given a column with name "a",+// - List(Element) returns ["a"] with data type Element+// - Struct(field1, field2) returns ["a.field1","a.field2"]+// For list data type, an argument depth is used to specify+// the recursion level+fn get_unnested_columns(+ col_name: &String,+ data_type: &DataType,+ depth: usize,+) -> Result<Vec<(Column, Arc<Field>)>> {+ let mut qualified_columns = Vec::with_capacity(1);++ match data_type {+ DataType::List(_) | DataType::FixedSizeList(_, _) | DataType::LargeList(_) => {+ let data_type = get_unnested_list_datatype_recursive(data_type, depth)?;+ let new_field = Arc::new(Field::new(+ col_name, data_type,+ // Unnesting may produce NULLs even if the list is not null.+ // For example: unnest([1], []) -> 1, null+ true,+ ));+ let column = Column::from_name(col_name);+ // let column = Column::from((None, &new_field));+ qualified_columns.push((column, new_field));+ }+ DataType::Struct(fields) => {+ qualified_columns.extend(fields.iter().map(|f| {+ let new_name = format!("{}.{}", col_name, f.name());+ let column = Column::from_name(&new_name);+ let new_field = f.as_ref().clone().with_name(new_name);+ // let column = Column::from((None, &f));+ (column, Arc::new(new_field))+ }))+ }+ _ => {+ return internal_err!(+ "trying to unnest on invalid data type {:?}",+ data_type+ );+ }+ };+ Ok(qualified_columns)+}++// Get the data type of a multi-dimensional type after unnesting it+// with a given depth+fn get_unnested_list_datatype_recursive(+ data_type: &DataType,+ depth: usize,+) -> Result<DataType> {+ match data_type {+ DataType::List(field)+ | DataType::FixedSizeList(field, _)+ | DataType::LargeList(field) => {+ if depth == 1 {+ return Ok(field.data_type().clone());+ }+ return get_unnested_list_datatype_recursive(field.data_type(), depth - 1);+ }+ _ => {}+ };++ internal_err!("trying to unnest on invalid data type {:?}", data_type)+}+#[cfg(test)]mod tests {diff --git a/datafusion/expr/src/logical_plan/tree_node.rs b/datafusion/expr/src/logical_plan/tree_node.rsindex b38652bdc1..47088370a1 100644--- a/datafusion/expr/src/logical_plan/tree_node.rs+++ b/datafusion/expr/src/logical_plan/tree_node.rs@@ -315,9 +315,9 @@ impl TreeNode for LogicalPlan {LogicalPlan::Unnest(Unnest {input,exec_columns: input_columns,- dependency_indices,list_type_columns,struct_type_columns,+ dependency_indices,schema,options,})diff --git a/datafusion/optimizer/src/optimize_projections/mod.rs b/datafusion/optimizer/src/optimize_projections/mod.rsindex 023ee4ea5a..7b7be82b70 100644--- a/datafusion/optimizer/src/optimize_projections/mod.rs+++ b/datafusion/optimizer/src/optimize_projections/mod.rs@@ -29,9 +29,9 @@ use datafusion_common::{HashMap, JoinType, Result,};use datafusion_expr::expr::Alias;-use datafusion_expr::Unnest;use datafusion_expr::{- logical_plan::LogicalPlan, Aggregate, Distinct, Expr, Projection, TableScan, Window,+ logical_plan::LogicalPlan, Aggregate, Distinct, Expr, Projection, TableScan, Unnest,+ Window,};use crate::optimize_projections::required_indices::RequiredIndices;@@ -376,11 +376,22 @@ fn optimize_projections();}LogicalPlan::Unnest(Unnest {- dependency_indices, ..+ input,+ dependency_indices,+ ..}) => {- vec![RequiredIndices::new_from_indices(- dependency_indices.clone(),- )]+ // at least provide the indices for the exec-columns as a starting point+ let required_indices =+ RequiredIndices::new().with_plan_exprs(&plan, input.schema())?;++ // Add additional required indices from the parent+ let mut additional_necessary_child_indices = Vec::new();+ indices.indices().iter().for_each(|idx| {+ if let Some(index) = dependency_indices.get(*idx) {+ additional_necessary_child_indices.push(*index);+ }+ });+ vec![required_indices.append(&additional_necessary_child_indices)]}};diff --git a/datafusion/proto/src/logical_plan/mod.rs b/datafusion/proto/src/logical_plan/mod.rsindex 5f02288992..9915d3617f 100644--- a/datafusion/proto/src/logical_plan/mod.rs+++ b/datafusion/proto/src/logical_plan/mod.rs@@ -71,8 +71,7 @@ use datafusion_expr::{Statement, WindowUDF,};use datafusion_expr::{- AggregateUDF, ColumnUnnestList, DmlStatement, FetchType, RecursiveQuery, SkipType,- TableSource, Unnest,+ AggregateUDF, DmlStatement, FetchType, RecursiveQuery, SkipType, TableSource, Unnest,};use self::to_proto::{serialize_expr, serialize_exprs};@@ -916,40 +915,13 @@ impl AsLogicalPlan for LogicalPlanNode {LogicalPlanType::Unnest(unnest) => {let input: LogicalPlan =into_logical_plan!(unnest.input, ctx, extension_codec)?;- Ok(LogicalPlan::Unnest(Unnest {- input: Arc::new(input),- exec_columns: unnest.exec_columns.iter().map(|c| c.into()).collect(),- list_type_columns: unnest- .list_type_columns- .iter()- .map(|c| {- let recursion_item = c.recursion.as_ref().unwrap();- (- c.input_index as _,- ColumnUnnestList {- output_column: recursion_item- .output_column- .as_ref()- .unwrap()- .into(),- depth: recursion_item.depth as _,- },- )- })- .collect(),- struct_type_columns: unnest- .struct_type_columns- .iter()- .map(|c| *c as usize)- .collect(),- dependency_indices: unnest- .dependency_indices- .iter()- .map(|c| *c as usize)- .collect(),- schema: Arc::new(convert_required!(unnest.schema)?),- options: into_required!(unnest.options)?,- }))++ LogicalPlanBuilder::from(input)+ .unnest_columns_with_options(+ unnest.exec_columns.iter().map(|c| c.into()).collect(),+ into_required!(unnest.options)?,+ )?+ .build()}LogicalPlanType::RecursiveQuery(recursive_query_node) => {let static_term = recursive_query_nodediff --git a/datafusion/sqllogictest/test_files/push_down_filter.slt b/datafusion/sqllogictest/test_files/push_down_filter.sltindex a0d3193324..f6d71cad60 100644--- a/datafusion/sqllogictest/test_files/push_down_filter.slt+++ b/datafusion/sqllogictest/test_files/push_down_filter.slt@@ -39,9 +39,9 @@ physical_plan01)ProjectionExec: expr=[__unnest_placeholder(v.column2,depth=1)@0 as uc2]02)--UnnestExec03)----RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1-04)------ProjectionExec: expr=[column2@1 as __unnest_placeholder(v.column2), column1@0 as column1]+04)------ProjectionExec: expr=[column2@0 as __unnest_placeholder(v.column2)]05)--------CoalesceBatchesExec: target_batch_size=8192-06)----------FilterExec: column1@0 = 2+06)----------FilterExec: column1@0 = 2, projection=[column2@1]07)------------DataSourceExec: partitions=1, partition_sizes=[1]query I@@ -59,10 +59,9 @@ physical_plan02)--CoalesceBatchesExec: target_batch_size=819203)----FilterExec: __unnest_placeholder(v.column2,depth=1)@0 > 304)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1-05)--------ProjectionExec: expr=[__unnest_placeholder(v.column2,depth=1)@0 as __unnest_placeholder(v.column2,depth=1)]-06)----------UnnestExec-07)------------ProjectionExec: expr=[column2@1 as __unnest_placeholder(v.column2), column1@0 as column1]-08)--------------DataSourceExec: partitions=1, partition_sizes=[1]+05)--------UnnestExec+06)----------ProjectionExec: expr=[column2@0 as __unnest_placeholder(v.column2)]+07)------------DataSourceExec: partitions=1, partition_sizes=[1]query IIselect uc2, column1 from (select unnest(column2) as uc2, column1 from v) where uc2 > 3 AND column1 = 2;