Skip to content

Commit 1f6c93d

Browse files
backport to Spark-1.2
1 parent 6d23af6 commit 1f6c93d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,16 @@ object ColumnPruning extends Rule[LogicalPlan] {
142142
case Project(projectList1, Project(projectList2, child)) =>
143143
// Create a map of Aliases to their values from the child projection.
144144
// e.g., 'SELECT ... FROM (SELECT a + b AS c, d ...)' produces Map(c -> Alias(a + b, c)).
145-
val aliasMap = projectList2.collect {
146-
case a @ Alias(e, _) => (a.toAttribute: Expression, a)
147-
}.toMap
145+
val aliasMap = AttributeMap(projectList2.collect {
146+
case a @ Alias(e, _) => (a.toAttribute, a)
147+
})
148148

149149
// Substitute any attributes that are produced by the child projection, so that we safely
150150
// eliminate it.
151151
// e.g., 'SELECT c + 1 FROM (SELECT a + b AS C ...' produces 'SELECT a + b + 1 ...'
152152
// TODO: Fix TransformBase to avoid the cast below.
153153
val substitutedProjection = projectList1.map(_.transform {
154-
case a if aliasMap.contains(a) => aliasMap(a)
154+
case a: Attribute if aliasMap.contains(a) => aliasMap(a)
155155
}).asInstanceOf[Seq[NamedExpression]]
156156

157157
Project(substitutedProjection, child)

sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveTableScanSuite.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
package org.apache.spark.sql.hive.execution
1919

2020
import org.apache.spark.sql.hive.test.TestHive
21-
import org.apache.spark.sql.{Row, SchemaRDD}
21+
import org.apache.spark.sql.hive.test.TestHive._
22+
import org.apache.spark.sql.Row
2223

2324
import org.apache.spark.util.Utils
2425

@@ -68,5 +69,15 @@ class HiveTableScanSuite extends HiveComparisonTest {
6869
=== Array(Row(java.sql.Timestamp.valueOf("2014-12-11 00:00:00")),Row(null)))
6970
TestHive.sql("DROP TABLE timestamp_query_null")
7071
}
71-
72+
73+
test("Spark-4959 Attributes are case sensitive when using a select query from a projection") {
74+
sql("create table spark_4959 (col1 string)")
75+
sql("""insert into table spark_4959 select "hi" from src limit 1""")
76+
table("spark_4959").select(
77+
'col1.as('CaseSensitiveColName),
78+
'col1.as('CaseSensitiveColName2)).registerTempTable("spark_4959_2")
79+
80+
assert(sql("select CaseSensitiveColName from spark_4959_2").first() === Row("hi"))
81+
assert(sql("select casesensitivecolname from spark_4959_2").first() === Row("hi"))
82+
}
7283
}

0 commit comments

Comments
 (0)