Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ class Analyzer(override val catalogManager: CatalogManager)
if (metaCols.isEmpty) {
node
} else {
val newNode = addMetadataCol(node)
val newNode = addMetadataCol(node, attr => metaCols.exists(_.exprId == attr.exprId))
// We should not change the output schema of the plan. We should project away the extra
// metadata columns if necessary.
if (newNode.sameOutput(node)) {
Expand Down Expand Up @@ -1012,15 +1012,18 @@ class Analyzer(override val catalogManager: CatalogManager)
})
}

private def addMetadataCol(plan: LogicalPlan): LogicalPlan = plan match {
case s: ExposesMetadataColumns => s.withMetadataColumns()
case p: Project =>
private def addMetadataCol(
plan: LogicalPlan,
isRequired: Attribute => Boolean): LogicalPlan = plan match {
case s: ExposesMetadataColumns if s.metadataOutput.exists(isRequired) =>
s.withMetadataColumns()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are doing this, shall we propagate the required metadata columns only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, but we need to change the ExposesMetadataColumns.withMetadataColumns API to pass required attributes, which may break custom logical plans. Since the benefit is small, this may not worth.

case p: Project if p.metadataOutput.exists(isRequired) =>
val newProj = p.copy(
projectList = p.projectList ++ p.metadataOutput,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto: shall we propagate the required metadata columns only?

child = addMetadataCol(p.child))
child = addMetadataCol(p.child, isRequired))
newProj.copyTagsFrom(p)
newProj
case _ => plan.withNewChildren(plan.children.map(addMetadataCol))
case _ => plan.withNewChildren(plan.children.map(addMetadataCol(_, isRequired)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.connector

import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
import org.apache.spark.sql.functions.struct

class MetadataColumnSuite extends DatasourceV2SQLBase {
Expand Down Expand Up @@ -232,4 +233,20 @@ class MetadataColumnSuite extends DatasourceV2SQLBase {
)
}
}

test("SPARK-41660: only propagate metadata columns if they are used") {
withTable(tbl) {
prepareTable()
val df = sql(s"SELECT t2.id FROM $tbl t1 JOIN $tbl t2 USING (id)")
val scans = df.logicalPlan.collect {
case d: DataSourceV2Relation => d
}
assert(scans.length == 2)
scans.foreach { scan =>
// The query only access join hidden columns, and scan nodes should not expose its metadata
// columns.
assert(scan.output.map(_.name) == Seq("id", "data"))
}
}
}
}