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 @@ -925,7 +925,7 @@ class Analyzer(override val catalogManager: CatalogManager)
if (metaCols.isEmpty) {
node
} else {
val newNode = addMetadataCol(node)
val newNode = node.mapChildren(addMetadataCol(_, metaCols.map(_.exprId).toSet))
Copy link
Member

Choose a reason for hiding this comment

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

Why this only does addMetadataCol on children?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change is from this fix #39895

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks correct to me because we need to propagate metadata cols to this node and need to add metadata cols in its children. Does master branch have the same code here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, master branch has the same code.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, there was follow up.

// 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 @@ -959,16 +959,20 @@ 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,
requiredAttrIds: Set[ExprId]): LogicalPlan = plan match {
case s: ExposesMetadataColumns if s.metadataOutput.exists(a =>
requiredAttrIds.contains(a.exprId)) =>
s.withMetadataColumns()
case p: Project if p.metadataOutput.exists(a => requiredAttrIds.contains(a.exprId)) =>
val newProj = p.copy(
// Do not leak the qualified-access-only restriction to normal plan outputs.
projectList = p.projectList ++ p.metadataOutput.map(_.markAsAllowAnyAccess()),
child = addMetadataCol(p.child))
child = addMetadataCol(p.child, requiredAttrIds))
newProj.copyTagsFrom(p)
newProj
case _ => plan.withNewChildren(plan.children.map(addMetadataCol))
case _ => plan.withNewChildren(plan.children.map(addMetadataCol(_, requiredAttrIds)))
}
}

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"))
}
}
}
}