Skip to content

Commit

Permalink
[SPARK-50659][SQL] Move Union-related errors to QueryCompilationErrors
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Move Union-related `NUM_COLUMNS_MISMATCH` and `INCOMPATIBLE_COLUMN_TYPE` errors to `QueryCompilationErrors`.

### Why are the changes needed?

To improve the code health and to reuse those in the single-pass Analyzer.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Existing tests.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #49284 from vladimirg-db/vladimirg-db/move-union-related-errors-to-query-compilation-errors.

Authored-by: Vladimir Golubev <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
  • Loading branch information
vladimirg-db authored and cloud-fan committed Dec 25, 2024
1 parent 2c1c4d2 commit f9e117e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -702,29 +702,29 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB
operator.children.tail.zipWithIndex.foreach { case (child, ti) =>
// Check the number of columns
if (child.output.length != ref.length) {
e.failAnalysis(
errorClass = "NUM_COLUMNS_MISMATCH",
messageParameters = Map(
"operator" -> toSQLStmt(operator.nodeName),
"firstNumColumns" -> ref.length.toString,
"invalidOrdinalNum" -> ordinalNumber(ti + 1),
"invalidNumColumns" -> child.output.length.toString))
throw QueryCompilationErrors.numColumnsMismatch(
operator = operator.nodeName,
firstNumColumns = ref.length,
invalidOrdinalNum = ti + 1,
invalidNumColumns = child.output.length,
origin = operator.origin
)
}

val dataTypesAreCompatibleFn = getDataTypesAreCompatibleFn(operator)
// Check if the data types match.
dataTypes(child).zip(ref).zipWithIndex.foreach { case ((dt1, dt2), ci) =>
// SPARK-18058: we shall not care about the nullability of columns
if (!dataTypesAreCompatibleFn(dt1, dt2)) {
e.failAnalysis(
errorClass = "INCOMPATIBLE_COLUMN_TYPE",
messageParameters = Map(
"operator" -> toSQLStmt(operator.nodeName),
"columnOrdinalNumber" -> ordinalNumber(ci),
"tableOrdinalNumber" -> ordinalNumber(ti + 1),
"dataType1" -> toSQLType(dt1),
"dataType2" -> toSQLType(dt2),
"hint" -> extraHintForAnsiTypeCoercionPlan(operator)))
throw QueryCompilationErrors.incompatibleColumnTypeError(
operator = operator.nodeName,
columnOrdinalNumber = ci,
tableOrdinalNumber = ti + 1,
dataType1 = dt1,
dataType2 = dt2,
hint = extraHintForAnsiTypeCoercionPlan(operator),
origin = operator.origin
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4271,4 +4271,44 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
)
)
}

def numColumnsMismatch(
operator: String,
firstNumColumns: Int,
invalidOrdinalNum: Int,
invalidNumColumns: Int,
origin: Origin): Throwable = {
new AnalysisException(
errorClass = "NUM_COLUMNS_MISMATCH",
messageParameters = Map(
"operator" -> toSQLStmt(operator),
"firstNumColumns" -> firstNumColumns.toString,
"invalidOrdinalNum" -> ordinalNumber(invalidOrdinalNum),
"invalidNumColumns" -> invalidNumColumns.toString
),
origin = origin
)
}

def incompatibleColumnTypeError(
operator: String,
columnOrdinalNumber: Int,
tableOrdinalNumber: Int,
dataType1: DataType,
dataType2: DataType,
hint: String,
origin: Origin): Throwable = {
new AnalysisException(
errorClass = "INCOMPATIBLE_COLUMN_TYPE",
messageParameters = Map(
"operator" -> toSQLStmt(operator),
"columnOrdinalNumber" -> ordinalNumber(columnOrdinalNumber),
"tableOrdinalNumber" -> ordinalNumber(tableOrdinalNumber),
"dataType1" -> toSQLType(dataType1),
"dataType2" -> toSQLType(dataType2),
"hint" -> hint
),
origin = origin
)
}
}

0 comments on commit f9e117e

Please sign in to comment.