imback82 commented on a change in pull request #31440:
URL: https://github.com/apache/spark/pull/31440#discussion_r571131075
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Implicits.scala
##########
@@ -83,7 +85,8 @@ object DataSourceV2Implicits {
implicit class MetadataColumnsHelper(metadata: Array[MetadataColumn]) {
def asStruct: StructType = {
val fields = metadata.map { metaCol =>
- val field = StructField(metaCol.name, metaCol.dataType,
metaCol.isNullable)
+ val fieldMeta = new
MetadataBuilder().putBoolean(METADATA_COL_ATTR_KEY, true).build()
Review comment:
nit: can be created outside the loop (or even at `object` level).
##########
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)
+ // We should not change the output schema of the plan. We should
project away the extr
Review comment:
nit: extr -> extra
----------------------------------------------------------------
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]