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 271a5378d06ff78763a26ac9eb6e447659721495 Author: Jax Liu <[email protected]> AuthorDate: Sat Dec 21 16:42:50 2024 +0800 add const for the prefix string of unnest and outer refernece column --- datafusion/expr/src/expr.rs | 9 +++++++-- datafusion/sql/src/unparser/plan.rs | 15 +++++++++------ datafusion/sql/src/unparser/rewrite.rs | 9 +++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rs index c82572ebd5..86e9f6fc4e 100644 --- a/datafusion/expr/src/expr.rs +++ b/datafusion/expr/src/expr.rs @@ -2536,6 +2536,9 @@ pub fn schema_name_from_sorts(sorts: &[Sort]) -> Result<String, fmt::Error> { Ok(s) } +pub const OUTER_REFERENCE_COLUMN_PREFIX: &str = "outer_ref"; +pub const UNNEST_COLUMN_PREFIX: &str = "UNNEST"; + /// Format expressions for display as part of a logical plan. In many cases, this will produce /// similar output to `Expr.name()` except that column names will be prefixed with '#'. impl Display for Expr { @@ -2543,7 +2546,9 @@ impl Display for Expr { match self { Expr::Alias(Alias { expr, name, .. }) => write!(f, "{expr} AS {name}"), Expr::Column(c) => write!(f, "{c}"), - Expr::OuterReferenceColumn(_, c) => write!(f, "outer_ref({c})"), + Expr::OuterReferenceColumn(_, c) => { + write!(f, "{OUTER_REFERENCE_COLUMN_PREFIX}({c})") + } Expr::ScalarVariable(_, var_names) => write!(f, "{}", var_names.join(".")), Expr::Literal(v) => write!(f, "{v:?}"), Expr::Case(case) => { @@ -2736,7 +2741,7 @@ impl Display for Expr { }, Expr::Placeholder(Placeholder { id, .. }) => write!(f, "{id}"), Expr::Unnest(Unnest { expr }) => { - write!(f, "UNNEST({expr})") + write!(f, "{UNNEST_COLUMN_PREFIX}({expr})") } } } diff --git a/datafusion/sql/src/unparser/plan.rs b/datafusion/sql/src/unparser/plan.rs index 1a4978b816..2574ae5d52 100644 --- a/datafusion/sql/src/unparser/plan.rs +++ b/datafusion/sql/src/unparser/plan.rs @@ -40,6 +40,7 @@ use datafusion_common::{ tree_node::{TransformedResult, TreeNode}, Column, DataFusionError, Result, TableReference, }; +use datafusion_expr::expr::OUTER_REFERENCE_COLUMN_PREFIX; use datafusion_expr::{ expr::Alias, BinaryExpr, Distinct, Expr, JoinConstraint, JoinType, LogicalPlan, LogicalPlanBuilder, Operator, Projection, SortExpr, TableScan, Unnest, @@ -338,7 +339,9 @@ impl Unparser<'_> { "derived_projection", plan, relation, - unnest_input_type.filter(|t| matches!(t, UnnestInputType::OuterReference)).is_some(), + unnest_input_type + .filter(|t| matches!(t, UnnestInputType::OuterReference)) + .is_some(), ); } self.reconstruct_select_statement(plan, p, select)?; @@ -772,10 +775,11 @@ impl Unparser<'_> { 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) { - if prefix.starts_with("(outer_ref(") { - return Some(UnnestInputType::OuterReference) + if prefix.starts_with(&format!("({}(", OUTER_REFERENCE_COLUMN_PREFIX)) + { + return Some(UnnestInputType::OuterReference); } - return Some(UnnestInputType::Scalar) + return Some(UnnestInputType::Scalar); } } } @@ -1137,7 +1141,6 @@ impl From<BuilderError> for DataFusionError { } } - /// The type of the input to the UNNEST table factor. #[derive(Debug)] enum UnnestInputType { @@ -1145,4 +1148,4 @@ enum UnnestInputType { OuterReference, /// The input is a scalar value. It will be presented like a scalar array or struct. Scalar, -} \ No newline at end of file +} diff --git a/datafusion/sql/src/unparser/rewrite.rs b/datafusion/sql/src/unparser/rewrite.rs index 14bbbb22cf..db98374831 100644 --- a/datafusion/sql/src/unparser/rewrite.rs +++ b/datafusion/sql/src/unparser/rewrite.rs @@ -23,7 +23,7 @@ use datafusion_common::{ tree_node::{Transformed, TransformedResult, TreeNode, TreeNodeRewriter}, Column, HashMap, Result, TableReference, }; -use datafusion_expr::expr::Alias; +use datafusion_expr::expr::{Alias, UNNEST_COLUMN_PREFIX}; use datafusion_expr::{Expr, LogicalPlan, Projection, Sort, SortExpr}; use sqlparser::ast::Ident; @@ -296,7 +296,12 @@ pub(super) fn find_unnest_column_alias( return (plan, None); } if let Some(Expr::Alias(alias)) = projection.expr.first() { - if alias.expr.schema_name().to_string().starts_with("UNNEST(") { + if alias + .expr + .schema_name() + .to_string() + .starts_with(&format!("{UNNEST_COLUMN_PREFIX}(")) + { return (projection.input.as_ref(), Some(alias.name.clone())); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
