Skip to content

Commit

Permalink
[SPARK-39376][SQL] Hide duplicated columns in star expansion of subqu…
Browse files Browse the repository at this point in the history
…ery alias from NATURAL/USING JOIN

### What changes were proposed in this pull request?

Follows up from #31666. This PR introduced a bug where the qualified star expansion of a subquery alias containing a NATURAL/USING output duplicated columns.

### Why are the changes needed?

Duplicated, hidden columns should not be output from a star expansion.

### Does this PR introduce _any_ user-facing change?

The query

```
val df1 = Seq((3, 8)).toDF("a", "b")
val df2 = Seq((8, 7)).toDF("b", "d")
val joinDF = df1.join(df2, "b")
joinDF.alias("r").select("r.*")
```

Now outputs a single column `b`, instead of two (duplicate) columns for `b`.

### How was this patch tested?

UTs

Closes #36763 from karenfeng/SPARK-39376.

Authored-by: Karen Feng <karen.feng@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
  • Loading branch information
karenfeng authored and cloud-fan committed Jun 6, 2022
1 parent 9e92078 commit 18ca369
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Expand Up @@ -1369,7 +1369,8 @@ case class SubqueryAlias(

override def metadataOutput: Seq[Attribute] = {
val qualifierList = identifier.qualifier :+ alias
child.metadataOutput.map(_.withQualifier(qualifierList))
val nonHiddenMetadataOutput = child.metadataOutput.filter(!_.supportsQualifiedStar)
nonHiddenMetadataOutput.map(_.withQualifier(qualifierList))
}

override def maxRows: Option[Long] = child.maxRows
Expand Down
Expand Up @@ -554,4 +554,26 @@ class DataFrameJoinSuite extends QueryTest
)
}
}

test("SPARK-39376: Hide duplicated columns in star expansion of subquery alias from USING JOIN") {
val joinDf = testData2.as("testData2").join(
testData3.as("testData3"), usingColumns = Seq("a"), joinType = "fullouter")
val equivalentQueries = Seq(
joinDf.select($"*"),
joinDf.as("r").select($"*"),
joinDf.as("r").select($"r.*")
)
equivalentQueries.foreach { query =>
checkAnswer(query,
Seq(
Row(1, 1, null),
Row(1, 2, null),
Row(2, 1, 2),
Row(2, 2, 2),
Row(3, 1, null),
Row(3, 2, null)
)
)
}
}
}

0 comments on commit 18ca369

Please sign in to comment.