This is an automated email from the ASF dual-hosted git repository. goldmedal pushed a commit to branch feature/13793-unnest-unparse in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 5d5e55b452cbca220668e0e594060b46a717f7ac Author: Jax Liu <[email protected]> AuthorDate: Sat Dec 21 16:30:40 2024 +0800 refactor for `check_unnest_placeholder_with_outer_ref` --- datafusion/sql/src/unparser/plan.rs | 41 +++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/datafusion/sql/src/unparser/plan.rs b/datafusion/sql/src/unparser/plan.rs index 0b46709637..303669ea22 100644 --- a/datafusion/sql/src/unparser/plan.rs +++ b/datafusion/sql/src/unparser/plan.rs @@ -320,12 +320,12 @@ impl Unparser<'_> { // Projection can be top-level plan for unnest relation // The projection generated by the `RecursiveUnnestRewriter` from a UNNEST relation will have // only one expression, which is the placeholder column generated by the rewriter. - let (is_unnest, is_lateral) = if p.expr.len() == 1 { - Self::is_unnest_placeholder_with_outer_ref(&p.expr[0]) + let unnest_input_type = if p.expr.len() == 1 { + Self::check_unnest_placeholder_with_outer_ref(&p.expr[0]) } else { - (false, false) + None }; - if self.dialect.unnest_as_table_factor() && is_unnest { + if self.dialect.unnest_as_table_factor() && unnest_input_type.is_some() { if let LogicalPlan::Unnest(unnest) = &p.input.as_ref() { return self .unnest_to_table_factor_sql(unnest, query, select, relation); @@ -338,7 +338,7 @@ impl Unparser<'_> { "derived_projection", plan, relation, - is_lateral, + unnest_input_type.filter(|t| matches!(t, UnnestInputType::OuterReference)).is_some(), ); } self.reconstruct_select_statement(plan, p, select)?; @@ -759,22 +759,27 @@ impl Unparser<'_> { } } - /// Try to find the placeholder column name generated by `RecursiveUnnestRewriter` - /// The first return value is a boolean indicating if the column is a placeholder column: - /// Try to match the pattern `Expr::Alias(Expr::Column("__unnest_placeholder(...)"))` - /// The second return value is a boolean indicating if the column uses an outer reference: - /// Try to match the pattern `Expr::Alias(Expr::Column("__unnest_placeholder(outer_ref(...)))")` + /// Try to find the placeholder column name generated by `RecursiveUnnestRewriter`. + /// + /// - If the column is a placeholder column match the pattern `Expr::Alias(Expr::Column("__unnest_placeholder(...)"))`, + /// it means it is a scalar column, return [UnnestInputType::Scalar]. + /// - If the column is a placeholder column match the pattern `Expr::Alias(Expr::Column("__unnest_placeholder(outer_ref(...)))")`, + /// it means it is an outer reference column, return [UnnestInputType::OuterReference]. + /// - If the column is not a placeholder column, return [None]. /// /// `outer_ref` is the display result of [Expr::OuterReferenceColumn] - fn is_unnest_placeholder_with_outer_ref(expr: &Expr) -> (bool, bool) { + fn check_unnest_placeholder_with_outer_ref(expr: &Expr) -> Option<UnnestInputType> { if let Expr::Alias(Alias { expr, .. }) = expr { if let Expr::Column(Column { name, .. }) = expr.as_ref() { if let Some(prefix) = name.strip_prefix(UNNEST_PLACEHOLDER) { - return (true, prefix.starts_with("(outer_ref(")); + if prefix.starts_with("(outer_ref(") { + return Some(UnnestInputType::OuterReference) + } + return Some(UnnestInputType::Scalar) } } } - (false, false) + None } fn unnest_to_table_factor_sql( @@ -1124,3 +1129,13 @@ impl From<BuilderError> for DataFusionError { DataFusionError::External(Box::new(e)) } } + + +/// The type of the input to the UNNEST table factor. +#[derive(Debug)] +enum UnnestInputType { + /// The input is a column reference. It will be presented like `outer_ref(column_name)`. + OuterReference, + /// The input is a scalar value. It will be presented like a scalar array or struct. + Scalar, +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
