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-40324][SQL] Provide query context in AnalysisException #37841

Closed
wants to merge 6 commits 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
10 changes: 10 additions & 0 deletions core/src/test/scala/org/apache/spark/SparkFunSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ abstract class SparkFunSuite
context: QueryContext): Unit =
checkError(exception, errorClass, Some(errorSubClass), None, Map.empty, false, Array(context))

protected def checkError(
exception: SparkThrowable,
errorClass: String,
errorSubClass: String,
sqlState: Option[String],
parameters: Map[String, String],
context: QueryContext): Unit =
checkError(exception, errorClass, Some(errorSubClass), sqlState, parameters,
false, Array(context))

protected def checkError(
exception: SparkThrowable,
errorClass: String,
Expand Down
6 changes: 5 additions & 1 deletion project/MimaExcludes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ object MimaExcludes {
ProblemFilters.exclude[IncompatibleResultTypeProblem]("org.apache.spark.sql.types.Decimal.fromStringANSI$default$3"),

// [SPARK-36511][MINOR][SQL] Remove ColumnIOUtil
ProblemFilters.exclude[MissingClassProblem]("org.apache.parquet.io.ColumnIOUtil")
ProblemFilters.exclude[MissingClassProblem]("org.apache.parquet.io.ColumnIOUtil"),

// [SPARK-40324][SQL] Provide query context in AnalysisException
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.sql.AnalysisException.copy"),
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.spark.sql.AnalysisException.withPosition")
)

// Defulat exclude rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class AnalysisException protected[sql] (
startPosition = origin.startPosition,
errorClass = Some(errorClass),
errorSubClass = None,
messageParameters = messageParameters)
messageParameters = messageParameters,
context = origin.getQueryContext)

def this(
errorClass: String,
Expand All @@ -115,7 +116,8 @@ class AnalysisException protected[sql] (
startPosition = origin.startPosition,
errorClass = Some(errorClass),
errorSubClass = Option(errorSubClass),
messageParameters = messageParameters)
messageParameters = messageParameters,
context = origin.getQueryContext)

def copy(
message: String = this.message,
Expand All @@ -124,12 +126,16 @@ class AnalysisException protected[sql] (
plan: Option[LogicalPlan] = this.plan,
cause: Option[Throwable] = this.cause,
errorClass: Option[String] = this.errorClass,
messageParameters: Array[String] = this.messageParameters): AnalysisException =
messageParameters: Array[String] = this.messageParameters,
context: Array[QueryContext] = Array.empty): AnalysisException =
new AnalysisException(message, line, startPosition, plan, cause, errorClass, errorSubClass,
messageParameters)
messageParameters, context)

def withPosition(line: Option[Int], startPosition: Option[Int]): AnalysisException = {
val newException = this.copy(line = line, startPosition = startPosition)
def withPosition(origin: Origin): AnalysisException = {
val newException = this.copy(
line = origin.line,
startPosition = origin.startPosition,
gengliangwang marked this conversation as resolved.
Show resolved Hide resolved
context = origin.getQueryContext)
newException.setStackTrace(getStackTrace)
newException
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class Analyzer(override val catalogManager: CatalogManager)
analyzed
} catch {
case e: AnalysisException =>
val ae = e.copy(plan = Option(analyzed))
val ae = e.copy(plan = Option(analyzed),
context = analyzed.origin.getQueryContext)
ae.setStackTrace(e.getStackTrace)
throw ae
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ package object analysis {
def withPosition[A](t: TreeNode[_])(f: => A): A = {
try f catch {
case a: AnalysisException =>
throw a.withPosition(t.origin.line, t.origin.startPosition)
throw a.withPosition(t.origin)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.json4s.JsonAST._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._

import org.apache.spark.QueryContext
import org.apache.spark.sql.catalyst.{AliasIdentifier, CatalystIdentifier}
import org.apache.spark.sql.catalyst.ScalaReflection._
import org.apache.spark.sql.catalyst.catalog.{BucketSpec, CatalogStorageFormat, CatalogTable, CatalogTableType, FunctionResource}
Expand Down Expand Up @@ -68,6 +69,12 @@ case class Origin(

lazy val context: SQLQueryContext = SQLQueryContext(
line, startPosition, startIndex, stopIndex, sqlText, objectType, objectName)

def getQueryContext: Array[QueryContext] = if (context.isValid) {
Array(context)
} else {
Array.empty
}
}

/**
Expand Down
Loading