Skip to content

Commit 5c2844c

Browse files
cloud-fanmarmbrus
authored andcommitted
[SQL][minor] move resolveGetField into a object
The method `resolveGetField` isn't belong to `LogicalPlan` logically and didn't access any members of it. Author: Wenchen Fan <[email protected]> Closes #5435 from cloud-fan/tmp and squashes the following commits: 9a66c83 [Wenchen Fan] code clean up
1 parent 6d4e854 commit 5c2844c

3 files changed

Lines changed: 39 additions & 35 deletions

File tree

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class Analyzer(
308308
logDebug(s"Resolving $u to $result")
309309
result
310310
case UnresolvedGetField(child, fieldName) if child.resolved =>
311-
q.resolveGetField(child, fieldName, resolver)
311+
GetField(child, fieldName, resolver)
312312
}
313313
}
314314

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypes.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package org.apache.spark.sql.catalyst.expressions
1919

2020
import scala.collection.Map
2121

22+
import org.apache.spark.sql.AnalysisException
23+
import org.apache.spark.sql.catalyst.analysis.Resolver
2224
import org.apache.spark.sql.types._
2325

2426
/**
@@ -81,6 +83,41 @@ trait GetField extends UnaryExpression {
8183
def field: StructField
8284
}
8385

86+
object GetField {
87+
/**
88+
* Returns the resolved `GetField`, and report error if no desired field or over one
89+
* desired fields are found.
90+
*/
91+
def apply(
92+
expr: Expression,
93+
fieldName: String,
94+
resolver: Resolver): GetField = {
95+
def findField(fields: Array[StructField]): Int = {
96+
val checkField = (f: StructField) => resolver(f.name, fieldName)
97+
val ordinal = fields.indexWhere(checkField)
98+
if (ordinal == -1) {
99+
throw new AnalysisException(
100+
s"No such struct field $fieldName in ${fields.map(_.name).mkString(", ")}")
101+
} else if (fields.indexWhere(checkField, ordinal + 1) != -1) {
102+
throw new AnalysisException(
103+
s"Ambiguous reference to fields ${fields.filter(checkField).mkString(", ")}")
104+
} else {
105+
ordinal
106+
}
107+
}
108+
expr.dataType match {
109+
case StructType(fields) =>
110+
val ordinal = findField(fields)
111+
StructGetField(expr, fields(ordinal), ordinal)
112+
case ArrayType(StructType(fields), containsNull) =>
113+
val ordinal = findField(fields)
114+
ArrayGetField(expr, fields(ordinal), ordinal, containsNull)
115+
case otherType =>
116+
throw new AnalysisException(s"GetField is not valid on fields of type $otherType")
117+
}
118+
}
119+
}
120+
84121
/**
85122
* Returns the value of fields in the Struct `child`.
86123
*/

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/LogicalPlan.scala

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging {
210210
// For example, consider "a.b.c", where "a" is resolved to an existing attribute.
211211
// Then this will add GetField("c", GetField("b", a)), and alias
212212
// the final expression as "c".
213-
val fieldExprs = nestedFields.foldLeft(a: Expression)(resolveGetField(_, _, resolver))
213+
val fieldExprs = nestedFields.foldLeft(a: Expression)(GetField(_, _, resolver))
214214
val aliasName = nestedFields.last
215215
Some(Alias(fieldExprs, aliasName)())
216216
} catch {
@@ -229,39 +229,6 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging {
229229
s"Reference '$name' is ambiguous, could be: $referenceNames.")
230230
}
231231
}
232-
233-
/**
234-
* Returns the resolved `GetField`, and report error if no desired field or over one
235-
* desired fields are found.
236-
*/
237-
def resolveGetField(
238-
expr: Expression,
239-
fieldName: String,
240-
resolver: Resolver): Expression = {
241-
def findField(fields: Array[StructField]): Int = {
242-
val checkField = (f: StructField) => resolver(f.name, fieldName)
243-
val ordinal = fields.indexWhere(checkField)
244-
if (ordinal == -1) {
245-
throw new AnalysisException(
246-
s"No such struct field $fieldName in ${fields.map(_.name).mkString(", ")}")
247-
} else if (fields.indexWhere(checkField, ordinal + 1) != -1) {
248-
throw new AnalysisException(
249-
s"Ambiguous reference to fields ${fields.filter(checkField).mkString(", ")}")
250-
} else {
251-
ordinal
252-
}
253-
}
254-
expr.dataType match {
255-
case StructType(fields) =>
256-
val ordinal = findField(fields)
257-
StructGetField(expr, fields(ordinal), ordinal)
258-
case ArrayType(StructType(fields), containsNull) =>
259-
val ordinal = findField(fields)
260-
ArrayGetField(expr, fields(ordinal), ordinal, containsNull)
261-
case otherType =>
262-
throw new AnalysisException(s"GetField is not valid on fields of type $otherType")
263-
}
264-
}
265232
}
266233

267234
/**

0 commit comments

Comments
 (0)