mihailoale-db commented on code in PR #60754:
URL: https://github.com/apache/spark/pull/60754#discussion_r4642165146
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -420,6 +420,30 @@ object Analyzer {
}
functionConf
}
+
+ /**
+ * Replaces references to the aliases in `sqlFunctionInputs` with the
aliased input expressions.
+ */
+ def inlineSQLFunctionInputs(
+ expression: Expression,
+ sqlFunctionInputs: Seq[NamedExpression]): Expression =
+ replaceSQLFunctionInputAttributes(expression,
buildSQLFunctionInputAliasMap(sqlFunctionInputs))
+
+ /** Maps the attribute of each alias in `sqlFunctionInputs` to the aliased
input expression. */
+ def buildSQLFunctionInputAliasMap(
+ sqlFunctionInputs: Seq[NamedExpression]): Map[Attribute, Expression] =
+ sqlFunctionInputs.collect { case a: Alias => a.toAttribute -> a.child
}.toMap
+
+ /**
+ * Replaces the attributes in `aliasMap` with the input expressions they map
to. The input
+ * expressions cannot reference the alias attributes, so replacements are
not traversed again.
+ */
+ def replaceSQLFunctionInputAttributes(
+ expression: Expression,
+ aliasMap: Map[Attribute, Expression]): Expression =
+ expression.transformUp {
+ case a: Attribute => aliasMap.getOrElse(a, a)
Review Comment:
nit, let's avoid abbreviations for better readability
```suggestion
case attribute: Attribute => aliasMap.getOrElse(attribute, attribute)
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -420,6 +420,30 @@ object Analyzer {
}
functionConf
}
+
+ /**
+ * Replaces references to the aliases in `sqlFunctionInputs` with the
aliased input expressions.
+ */
+ def inlineSQLFunctionInputs(
+ expression: Expression,
+ sqlFunctionInputs: Seq[NamedExpression]): Expression =
+ replaceSQLFunctionInputAttributes(expression,
buildSQLFunctionInputAliasMap(sqlFunctionInputs))
+
+ /** Maps the attribute of each alias in `sqlFunctionInputs` to the aliased
input expression. */
+ def buildSQLFunctionInputAliasMap(
+ sqlFunctionInputs: Seq[NamedExpression]): Map[Attribute, Expression] =
+ sqlFunctionInputs.collect { case a: Alias => a.toAttribute -> a.child
}.toMap
+
+ /**
+ * Replaces the attributes in `aliasMap` with the input expressions they map
to. The input
+ * expressions cannot reference the alias attributes, so replacements are
not traversed again.
+ */
+ def replaceSQLFunctionInputAttributes(
+ expression: Expression,
+ aliasMap: Map[Attribute, Expression]): Expression =
+ expression.transformUp {
Review Comment:
Can we use `withPruning` here? for `Attribute` tree pattern bits.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]