Skip to content

Commit

Permalink
Catch exception when resolving with backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
icexelloss committed Aug 27, 2018
1 parent 01f9cd5 commit a8a5976
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.sql.{Date, Timestamp}
import scala.collection.JavaConverters._
import scala.language.implicitConversions
import scala.reflect.runtime.universe.TypeTag
import scala.util.{Failure, Success, Try}
import scala.util.control.NonFatal

import org.apache.commons.lang3.StringUtils
Expand Down Expand Up @@ -217,17 +218,25 @@ class Dataset[T] private[sql](
val resolver = sparkSession.sessionState.analyzer.resolver
queryExecution.analyzed.resolveQuoted(colName, resolver)
.getOrElse {
if (queryExecution.analyzed.resolveQuoted(s"`$colName`", resolver).isDefined) {
throw new AnalysisException(
s"""Cannot resolve column name "$colName" among (${schema.fieldNames.mkString(", ")}).
| Try adding backticks to the column name, i.e., `$colName`,
| if "$colName" is the name of the whole column"""
.stripMargin.replaceAll("\n", ""))
} else {
throw new AnalysisException(
s"""Cannot resolve column name "$colName" among (${schema.fieldNames.mkString(", ")})"""
.stripMargin)
val defaultMessage =
s"""Cannot resolve column name "$colName" among (${schema.fieldNames.mkString(", ")})"""
.stripMargin

val improvedMessage =
s"""Cannot resolve column name "$colName" among (${schema.fieldNames.mkString(", ")}).
| Try adding backticks to the column name, i.e., `$colName`,
| if "$colName" is the name of the whole column"""
.stripMargin.replaceAll("\n", "")

// Try to resolve colName with backticks and give a better exception message if the
// colName can be resolved with backticks. If this fails, return the default
// error message
val message = Try(queryExecution.analyzed.resolveQuoted(s"`$colName`", resolver)) match {
case Success(result) => if (result.isDefined) improvedMessage else defaultMessage
case Failure(_) => defaultMessage
}

throw new AnalysisException(message)
}
}

Expand Down

0 comments on commit a8a5976

Please sign in to comment.