viirya commented on a change in pull request #31440:
URL: https://github.com/apache/spark/pull/31440#discussion_r570697705
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
##########
@@ -964,11 +964,37 @@ class Analyzer(override val catalogManager:
CatalogManager)
* columns are not accidentally selected by *.
*/
object AddMetadataColumns extends Rule[LogicalPlan] {
+ import
org.apache.spark.sql.execution.datasources.v2.DataSourceV2Implicits._
+
+ private def hasMetadataCol(plan: LogicalPlan): Boolean = {
+ plan.expressions.exists(_.find {
+ case a: Attribute => a.isMetadataCol
+ case _ => false
+ }.isDefined)
+ }
+
+ private def addMetadataCol(plan: LogicalPlan): LogicalPlan = plan match {
+ case r: DataSourceV2Relation => r.withMetadataColumns()
+ case _ => plan.withNewChildren(plan.children.map(addMetadataCol))
+ }
+
def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperatorsUp {
- case node if node.resolved && node.children.nonEmpty &&
node.missingInput.nonEmpty =>
- node resolveOperatorsUp {
- case rel: DataSourceV2Relation =>
- rel.withMetadataColumns()
+ case node if node.children.nonEmpty && node.resolved &&
hasMetadataCol(node) =>
+ val inputAttrs = AttributeSet(node.children.flatMap(_.output))
+ val metaCols = node.expressions.flatMap(_.collect {
+ case a: Attribute if a.isMetadataCol && !inputAttrs.contains(a) => a
+ })
+ if (metaCols.isEmpty) {
+ node
+ } else {
+ val newNode = addMetadataCol(node)
Review comment:
No matter how many meta cols we actually refer, we always add all meta
cols, right?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]