Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-4959] [SQL] Attributes are case sensitive when using a select query from a projection #3796

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ object ColumnPruning extends Rule[LogicalPlan] {
case Project(projectList1, Project(projectList2, child)) =>
// Create a map of Aliases to their values from the child projection.
// e.g., 'SELECT ... FROM (SELECT a + b AS c, d ...)' produces Map(c -> Alias(a + b, c)).
val aliasMap = projectList2.collect {
case a @ Alias(e, _) => (a.toAttribute: Expression, a)
}.toMap
val aliasMap = AttributeMap(projectList2.collect {
case a @ Alias(e, _) => (a.toAttribute, a)
})

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

Project(substitutedProjection, child)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
package org.apache.spark.sql.hive.execution

import org.apache.spark.sql.hive.test.TestHive
import org.apache.spark.sql.{Row, SchemaRDD}
import org.apache.spark.sql.hive.test.TestHive._
import org.apache.spark.sql.Row

import org.apache.spark.util.Utils

Expand Down Expand Up @@ -76,4 +77,15 @@ class HiveTableScanSuite extends HiveComparisonTest {
=== Array(Row(java.sql.Timestamp.valueOf("2014-12-11 00:00:00")),Row(null)))
TestHive.sql("DROP TABLE timestamp_query_null")
}

test("Spark-4959 Attributes are case sensitive when using a select query from a projection") {
sql("create table spark_4959 (col1 string)")
sql("""insert into table spark_4959 select "hi" from src limit 1""")
table("spark_4959").select(
'col1.as('CaseSensitiveColName),
'col1.as('CaseSensitiveColName2)).registerTempTable("spark_4959_2")

assert(sql("select CaseSensitiveColName from spark_4959_2").first() === Row("hi"))
assert(sql("select casesensitivecolname from spark_4959_2").first() === Row("hi"))
}
}