Skip to content
Snippets Groups Projects
Commit 7ad579ee authored by Michael Armbrust's avatar Michael Armbrust
Browse files

[SPARK-3698][SQL] Fix case insensitive resolution of GetField.

Based on #2543.

Author: Michael Armbrust <michael@databricks.com>

Closes #3724 from marmbrus/resolveGetField and squashes the following commits:

0a47aae [Michael Armbrust] Fix case insensitive resolution of GetField.
parent 4782def0
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@ import org.apache.spark.sql.catalyst.errors.TreeNodeException
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules._
import org.apache.spark.sql.catalyst.types.StructType
/**
* A trivial [[Analyzer]] with an [[EmptyCatalog]] and [[EmptyFunctionRegistry]]. Used for testing
......@@ -187,6 +188,15 @@ class Analyzer(catalog: Catalog,
val result = q.resolveChildren(name, resolver).getOrElse(u)
logDebug(s"Resolving $u to $result")
result
// Resolve field names using the resolver.
case f @ GetField(child, fieldName) if !f.resolved && child.resolved =>
child.dataType match {
case StructType(fields) =>
val resolvedFieldName = fields.map(_.name).find(resolver(_, fieldName))
resolvedFieldName.map(n => f.copy(fieldName = n)).getOrElse(f)
case _ => f
}
}
}
......
......@@ -92,7 +92,13 @@ case class GetField(child: Expression, fieldName: String) extends UnaryExpressio
lazy val ordinal = structType.fields.indexOf(field)
override lazy val resolved = childrenResolved && child.dataType.isInstanceOf[StructType]
override lazy val resolved = childrenResolved && fieldResolved
/** Returns true only if the fieldName is found in the child struct. */
private def fieldResolved = child.dataType match {
case StructType(fields) => fields.map(_.name).contains(fieldName)
case _ => false
}
override def eval(input: Row): Any = {
val baseValue = child.eval(input).asInstanceOf[Row]
......
......@@ -27,6 +27,17 @@ case class Data(a: Int, B: Int, n: Nested, nestedArray: Seq[Nested])
* A set of test cases expressed in Hive QL that are not covered by the tests included in the hive distribution.
*/
class HiveResolutionSuite extends HiveComparisonTest {
case class NestedData(a: Seq[NestedData2], B: NestedData2)
case class NestedData2(a: NestedData3, B: NestedData3)
case class NestedData3(a: Int, B: Int)
test("SPARK-3698: case insensitive test for nested data") {
sparkContext.makeRDD(Seq.empty[NestedData]).registerTempTable("nested")
// This should be successfully analyzed
sql("SELECT a[0].A.A from nested").queryExecution.analyzed
}
createQueryTest("table.attr",
"SELECT src.key FROM src ORDER BY key LIMIT 1")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment