Skip to content

Commit 9418280

Browse files
cloud-fanmarmbrus
authored andcommitted
[SQL][minor] remove duplicated resolveGetField and update comment
It's after #5189 Author: Wenchen Fan <cloud0fan@outlook.com> Closes #5304 from cloud-fan/tmp and squashes the following commits: c58c9b3 [Wenchen Fan] remove duplicated code and update comment
1 parent 55a92ef commit 9418280

2 files changed

Lines changed: 6 additions & 39 deletions

File tree

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class Analyzer(
293293
logDebug(s"Resolving $u to $result")
294294
result
295295
case UnresolvedGetField(child, fieldName) if child.resolved =>
296-
resolveGetField(child, fieldName)
296+
q.resolveGetField(child, fieldName, resolver)
297297
}
298298
}
299299

@@ -313,36 +313,6 @@ class Analyzer(
313313
*/
314314
protected def containsStar(exprs: Seq[Expression]): Boolean =
315315
exprs.exists(_.collect { case _: Star => true }.nonEmpty)
316-
317-
/**
318-
* Returns the resolved `GetField`, and report error if no desired field or over one
319-
* desired fields are found.
320-
*/
321-
protected def resolveGetField(expr: Expression, fieldName: String): Expression = {
322-
def findField(fields: Array[StructField]): Int = {
323-
val checkField = (f: StructField) => resolver(f.name, fieldName)
324-
val ordinal = fields.indexWhere(checkField)
325-
if (ordinal == -1) {
326-
throw new AnalysisException(
327-
s"No such struct field $fieldName in ${fields.map(_.name).mkString(", ")}")
328-
} else if (fields.indexWhere(checkField, ordinal + 1) != -1) {
329-
throw new AnalysisException(
330-
s"Ambiguous reference to fields ${fields.filter(checkField).mkString(", ")}")
331-
} else {
332-
ordinal
333-
}
334-
}
335-
expr.dataType match {
336-
case StructType(fields) =>
337-
val ordinal = findField(fields)
338-
StructGetField(expr, fields(ordinal), ordinal)
339-
case ArrayType(StructType(fields), containsNull) =>
340-
val ordinal = findField(fields)
341-
ArrayGetField(expr, fields(ordinal), ordinal, containsNull)
342-
case otherType =>
343-
throw new AnalysisException(s"GetField is not valid on fields of type $otherType")
344-
}
345-
}
346316
}
347317

348318
/**

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,10 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging {
205205
// One match, but we also need to extract the requested nested field.
206206
case Seq((a, nestedFields)) =>
207207
try {
208-
209-
// The foldLeft adds UnresolvedGetField for every remaining parts of the name,
210-
// and aliased it with the last part of the name.
211-
// For example, consider name "a.b.c", where "a" is resolved to an existing attribute.
212-
// Then this will add UnresolvedGetField("b") and UnresolvedGetField("c"), and alias
208+
// The foldLeft adds GetFields for every remaining parts of the identifier,
209+
// and aliases it with the last part of the identifier.
210+
// For example, consider "a.b.c", where "a" is resolved to an existing attribute.
211+
// Then this will add GetField("c", GetField("b", a)), and alias
213212
// the final expression as "c".
214213
val fieldExprs = nestedFields.foldLeft(a: Expression)(resolveGetField(_, _, resolver))
215214
val aliasName = nestedFields.last
@@ -234,10 +233,8 @@ abstract class LogicalPlan extends QueryPlan[LogicalPlan] with Logging {
234233
/**
235234
* Returns the resolved `GetField`, and report error if no desired field or over one
236235
* desired fields are found.
237-
*
238-
* TODO: this code is duplicated from Analyzer and should be refactored to avoid this.
239236
*/
240-
protected def resolveGetField(
237+
def resolveGetField(
241238
expr: Expression,
242239
fieldName: String,
243240
resolver: Resolver): Expression = {

0 commit comments

Comments
 (0)