Skip to content

Commit 3ec08f8

Browse files
Replace the attribute in comparing its exprId other than itself
1 parent 29fabb1 commit 3ec08f8

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
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 & 1 deletion
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

@@ -76,4 +77,15 @@ class HiveTableScanSuite extends HiveComparisonTest {
7677
=== Array(Row(java.sql.Timestamp.valueOf("2014-12-11 00:00:00")),Row(null)))
7778
TestHive.sql("DROP TABLE timestamp_query_null")
7879
}
80+
81+
test("Spark-4959 Attributes are case sensitive when using a select query from a projection") {
82+
sql("create table spark_4959 (col1 string)")
83+
sql("""insert into table spark_4959 select "hi" from src limit 1""")
84+
table("spark_4959").select(
85+
'col1.as('CaseSensitiveColName),
86+
'col1.as('CaseSensitiveColName2)).registerTempTable("spark_4959_2")
87+
88+
assert(sql("select CaseSensitiveColName from spark_4959_2").first() === Row("hi"))
89+
assert(sql("select casesensitivecolname from spark_4959_2").first() === Row("hi"))
90+
}
7991
}

0 commit comments

Comments
 (0)