Skip to content

Commit a0baec7

Browse files
committed
Partitioning columns can be resolved.
1 parent 1161338 commit a0baec7

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveStrategies.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private[hive] trait HiveStrategies {
7676
}).reduceOption(And).getOrElse(Literal(true))
7777

7878
val unresolvedProjection = projectList.map(_ transform {
79-
case a: AttributeReference => UnresolvedAttribute(a.name)
79+
// Handle non-partitioning columns
80+
case a: AttributeReference if !partitionKeyIds.contains(a.exprId) => UnresolvedAttribute(a.name)
8081
})
8182

8283
if (relation.hiveQlTable.isPartitioned) {
@@ -109,16 +110,27 @@ private[hive] trait HiveStrategies {
109110
}
110111

111112
org.apache.spark.sql.execution.Union(
112-
partitions.par.map(p =>
113+
partitions.par.map { p =>
114+
val partValues = p.getValues()
115+
val internalProjection = unresolvedProjection.map(_ transform {
116+
// Handle partitioning columns
117+
case a: AttributeReference if partitionKeyIds.contains(a.exprId) => {
118+
val idx = relation.partitionKeys.indexWhere(a.exprId == _.exprId)
119+
val key = relation.partitionKeys(idx)
120+
121+
Alias(Cast(Literal(partValues.get(idx), StringType), key.dataType), a.name)()
122+
}
123+
})
124+
113125
hiveContext
114126
.parquetFile(p.getLocation)
115127
.lowerCase
116128
.where(unresolvedOtherPredicates)
117-
.select(unresolvedProjection:_*)
129+
.select(internalProjection:_*)
118130
.queryExecution
119131
.executedPlan
120-
.fakeOutput(projectList.map(_.toAttribute))).seq) :: Nil
121-
132+
.fakeOutput(projectList.map(_.toAttribute))
133+
}.seq) :: Nil
122134
} else {
123135
hiveContext
124136
.parquetFile(relation.hiveQlTable.getDataLocation.getPath)

sql/hive/src/test/scala/org/apache/spark/sql/parquet/ParquetMetastoreSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ class ParquetMetastoreSuite extends QueryTest with BeforeAndAfterAll {
8787
sql(s"ALTER TABLE partitioned_parquet ADD PARTITION (p=$p)")
8888
}
8989

90+
test("project the partitioning column") {
91+
checkAnswer(
92+
sql("SELECT p, count(*) FROM partitioned_parquet group by p"),
93+
(1, 10) ::
94+
(2, 10) ::
95+
(3, 10) ::
96+
(4, 10) ::
97+
(5, 10) ::
98+
(6, 10) ::
99+
(7, 10) ::
100+
(8, 10) ::
101+
(9, 10) ::
102+
(10, 10) :: Nil
103+
)
104+
}
105+
90106
test("simple count") {
91107
checkAnswer(
92108
sql("SELECT COUNT(*) FROM partitioned_parquet"),

0 commit comments

Comments
 (0)