Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
163 changes: 163 additions & 0 deletions sql/catalyst/src/main/scala/org/apache/spark/sql/CatalystErrors.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move org.apache.spark.sql => org.apache.spark.sql.errors


import org.apache.spark.sql.catalyst.expressions.{Expression, GroupingID}
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.util.toPrettySQL
import org.apache.spark.sql.connector.catalog.TableChange
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{AbstractDataType, DataType, StructType}

/**
* Object for grouping all error messages in catalyst.
* Currently it includes all AnalysisExcpetions created and thrown directly in
* org.apache.spark.sql.catalyst.analysis.Analyzer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question; did you propose to group all the analysis exception here? I think we already have too many places (~500) where the analysis exceptions used though... https://gist.github.com/maropu/29bbfa9a93c41bd4ba1e0eaa038af087

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do all of them, I think. This is just an example PR.

This can help us do error message auditing. In the future, we can review this file, improve the error message quality, and make them unified and standard in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks nice, @gatorsmile. I think we might be able to add a new rule in scalastyle for forbidding the use of AnalysisException in the the other files.

*/
object CatalystErrors {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename CatalystErrors => QueryCompilationErrors

def groupingIDMismatchError(groupingID: GroupingID, groupByExprs: Seq[Expression]): Throwable = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: how about moving these methods into object AnalysisException?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a summary sheet of raw exceptions thrown in Catalyst of branch-3.0: OSS Catalyst Exception Messages: Branch-3.0. Apart from AnalysisException and its sub-exceptions, there are a lot of other exceptions, for example, IllegalArgumentException and UnsupportedOperationException.

We generally have two ways to group these messages:

  • By component. All messages from a single components, no matter the exception type, are grouped into one exception file.
  • By exception type. All messages of a single exception type are created in the corresponding exception object. It is great because by calling AnalysisException.groupingIDMismatchError we know the exception type this function throws. But this approach has a problem: those Java/Scala internal exception type cannot follow this way.

Due to the small problem of the latter one, I choose the first one of grouping all exception messages from Catalyst in one file. Or do you have any thoughts on that? Thanks!

new AnalysisException(
s"Columns of grouping_id (${groupingID.groupByExprs.mkString(",")}) " +
s"does not match grouping columns (${groupByExprs.mkString(",")})")
}

def groupingColInvalidError(groupingCol: Expression, groupByExprs: Seq[Expression]): Throwable = {
new AnalysisException(
s"Column of grouping ($groupingCol) can't be found " +
s"in grouping columns ${groupByExprs.mkString(",")}")
}

def groupingSizeTooLargeError(sizeLimit: Int): Throwable = {
new AnalysisException(
s"Grouping sets size cannot be greater than $sizeLimit")
}

def unorderablePivotColError(pivotCol: Expression): Throwable = {
new AnalysisException(
s"Invalid pivot column '$pivotCol'. Pivot columns must be comparable."
)
}

def nonliteralPivotValError(pivotVal: Expression): Throwable = {
new AnalysisException(
s"Literal expressions required for pivot values, found '$pivotVal'")
}

def pivotValDataTypeMismatchError(pivotVal: Expression, pivotCol: Expression): Throwable = {
new AnalysisException(
s"Invalid pivot value '$pivotVal': " +
s"value data type ${pivotVal.dataType.simpleString} does not match " +
s"pivot column data type ${pivotCol.dataType.catalogString}")
}

def unsupportedIfNotExistsError(tableName: String): Throwable = {
new AnalysisException(
s"Cannot write, IF NOT EXISTS is not supported for table: $tableName")
}

def nonPartitionColError(partitionName: String): Throwable = {
new AnalysisException(
s"PARTITION clause cannot contain a non-partition column name: $partitionName")
}

def addStaticValToUnknownColError(staticName: String): Throwable = {
new AnalysisException(
s"Cannot add static value for unknown column: $staticName")
}

def unknownStaticPartitionColError(name: String): Throwable = {
new AnalysisException(s"Unknown static partition column: $name")
}

def nestedGeneratorError(trimmedNestedGenerator: Expression): Throwable = {
new AnalysisException(
"Generators are not supported when it's nested in " +
"expressions, but got: " + toPrettySQL(trimmedNestedGenerator))
}

def moreThanOneGeneratorError(generators: Seq[Expression], clause: String): Throwable = {
new AnalysisException(
s"Only one generator allowed per $clause clause but found " +
generators.size + ": " + generators.map(toPrettySQL).mkString(", "))
}

def generatorOutsideSelectError(plan: LogicalPlan): Throwable = {
new AnalysisException(
"Generators are not supported outside the SELECT clause, but " +
"got: " + plan.simpleString(SQLConf.get.maxToStringFields))
}

def legacyStoreAssignmentPolicyError(): Throwable = {
val configKey = SQLConf.STORE_ASSIGNMENT_POLICY.key
new AnalysisException(
"LEGACY store assignment policy is disallowed in Spark data source V2. " +
s"Please set the configuration $configKey to other values.")
}

def unresolvedUsingColForJoinError(
colName: String, plan: LogicalPlan, side: String): Throwable = {
new AnalysisException(
s"USING column `$colName` cannot be resolved on the $side " +
s"side of the join. The $side-side columns: [${plan.output.map(_.name).mkString(", ")}]")
}

def dataTypeMismatchForDeserializerError(
dataType: DataType, desiredType: String): Throwable = {
val quantifier = if (desiredType.equals("array")) "an" else "a"
new AnalysisException(
s"need $quantifier $desiredType field but got " + dataType.catalogString)
}

def fieldNumberMismatchForDeserializerError(
schema: StructType, maxOrdinal: Int): Throwable = {
new AnalysisException(
s"Try to map ${schema.catalogString} to Tuple${maxOrdinal + 1}, " +
"but failed as the number of fields does not line up.")
}

def upCastFailureError(
fromStr: String, from: Expression, to: DataType, walkedTypePath: Seq[String]): Throwable = {
new AnalysisException(
s"Cannot up cast $fromStr from " +
s"${from.dataType.catalogString} to ${to.catalogString}.\n" +
s"The type path of the target object is:\n" + walkedTypePath.mkString("", "\n", "\n") +
"You can either add an explicit cast to the input data or choose a higher precision " +
"type of the field in the target object")
}

def unsupportedAbstractDataTypeForUpCastError(gotType: AbstractDataType): Throwable = {
new AnalysisException(
s"UpCast only support DecimalType as AbstractDataType yet, but got: $gotType")
}

def outerScopeFailureForNewInstanceError(className: String): Throwable = {
new AnalysisException(
s"Unable to generate an encoder for inner class `$className` without " +
"access to the scope that this class was defined in.\n" +
"Try moving this class out of its parent class.")
}

def referenceColNotFoundForAlterTableChangesError(
after: TableChange.After, parentName: String): Throwable = {
new AnalysisException(
s"Couldn't find the reference column for $after at $parentName")
}

}


Loading