Skip to content

Commit

Permalink
[SPARK-47099][SQL] Use ordinalNumber to uniformly set the value of …
Browse files Browse the repository at this point in the history
…`paramIndex` for the error class `UNEXPECTED_INPUT_TYPE`

### What changes were proposed in this pull request?
The pr aims to use `ordinalNumber` to `uniformly` set the value of `paramIndex` for the error class `UNEXPECTED_INPUT_TYPE`.

### Why are the changes needed?
When I was reviewing the spark code, I found that:
- Some expressions may have a starting value of 1 when throwing error `DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE`, eg:
https://github.com/apache/spark/blob/b0aad59f123581b66515c864873f46ea4ec4e762/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala#L300-L302

- Some are 0, eg:
https://github.com/apache/spark/blob/b0aad59f123581b66515c864873f46ea4ec4e762/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/bitmapExpressions.scala#L117-L119

**We should `unify` it and avoid understanding differences.**

### Does this PR introduce _any_ user-facing change?
Yes, the value of 'paramIndex' for the error class `UNEXPECTED-INPUT-TYPE` is uniformly set by `ordinalNumber`.

### How was this patch tested?
- Updated existed UT.
- Pass GA.

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

Closes apache#45177 from panbingkun/SPARK-47099.

Authored-by: panbingkun <[email protected]>
Signed-off-by: Max Gekk <[email protected]>
  • Loading branch information
panbingkun authored and MaxGekk committed Feb 21, 2024
1 parent f60ae4d commit 9b53b80
Show file tree
Hide file tree
Showing 93 changed files with 516 additions and 507 deletions.
2 changes: 1 addition & 1 deletion common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@
},
"UNEXPECTED_INPUT_TYPE" : {
"message" : [
"Parameter <paramIndex> requires the <requiredType> type, however <inputSql> has the type <inputType>."
"The <paramIndex> parameter requires the <requiredType> type, however <inputSql> has the type <inputType>."
]
},
"UNEXPECTED_NULL" : {
Expand Down
2 changes: 1 addition & 1 deletion docs/sql-error-conditions-datatype-mismatch-error-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class `<className>` not found.

## UNEXPECTED_INPUT_TYPE

Parameter `<paramIndex>` requires the `<requiredType>` type, however `<inputSql>` has the type `<inputType>`.
The `<paramIndex>` parameter requires the `<requiredType>` type, however `<inputSql>` has the type `<inputType>`.

## UNEXPECTED_NULL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,12 +567,7 @@ trait CheckAnalysis extends PredicateHelper with LookupCatalog with QueryErrorsB

case e @ (_: Union | _: SetOperation) if operator.children.length > 1 =>
def dataTypes(plan: LogicalPlan): Seq[DataType] = plan.output.map(_.dataType)
def ordinalNumber(i: Int): String = i match {
case 0 => "first"
case 1 => "second"
case 2 => "third"
case i => s"${i + 1}th"
}

val ref = dataTypes(operator.children.head)
operator.children.tail.zipWithIndex.foreach { case (child, ti) =>
// Check the number of columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ case class CallMethodViaReflection(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> (idx + 1).toString,
"paramIndex" -> ordinalNumber(idx),
"requiredType" -> toSQLType(
TypeCollection(BooleanType, ByteType, ShortType,
IntegerType, LongType, FloatType, DoubleType, StringType)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object ExpectsInputTypes extends QueryErrorsBase {
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> (idx + 1).toString,
"paramIndex" -> ordinalNumber(idx),
"requiredType" -> toSQLType(expected),
"inputSql" -> toSQLExpr(input),
"inputType" -> toSQLType(input.dataType)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ case class UnwrapUDT(child: Expression) extends UnaryExpression with NonSQLExpre
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType("UserDefinedType"),
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ case class ApproxCountDistinctForIntervals(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "2",
"paramIndex" -> ordinalNumber(1),
"requiredType" -> s"ARRAY OF $requiredElemTypes",
"inputSql" -> toSQLExpr(endpointsExpression),
"inputType" -> toSQLType(inputType)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ case class BitmapCount(child: Expression)
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "0",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(BinaryType),
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType)
Expand Down Expand Up @@ -261,7 +261,7 @@ case class BitmapOrAgg(child: Expression,
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "0",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(BinaryType),
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ case class MapFromEntries(child: Expression)
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> s"${toSQLType(ArrayType)} of pair ${toSQLType(StructType)}",
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType)
Expand Down Expand Up @@ -1066,7 +1066,7 @@ case class SortArray(base: Expression, ascendingOrder: Expression)
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "2",
"paramIndex" -> ordinalNumber(1),
"requiredType" -> toSQLType(BooleanType),
"inputSql" -> toSQLExpr(ascendingOrder),
"inputType" -> toSQLType(ascendingOrder.dataType))
Expand All @@ -1084,7 +1084,7 @@ case class SortArray(base: Expression, ascendingOrder: Expression)
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(ArrayType),
"inputSql" -> toSQLExpr(base),
"inputType" -> toSQLType(base.dataType))
Expand Down Expand Up @@ -1320,7 +1320,7 @@ case class ArrayContains(left: Expression, right: Expression)
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(ArrayType),
"inputSql" -> toSQLExpr(left),
"inputType" -> toSQLType(left.dataType))
Expand Down Expand Up @@ -1427,7 +1427,7 @@ trait ArrayPendBase extends RuntimeReplaceable
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "0",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(ArrayType),
"inputSql" -> toSQLExpr(left),
"inputType" -> toSQLType(left.dataType)
Expand Down Expand Up @@ -2221,7 +2221,7 @@ case class ArrayPosition(left: Expression, right: Expression)
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(ArrayType),
"inputSql" -> toSQLExpr(left),
"inputType" -> toSQLType(left.dataType))
Expand Down Expand Up @@ -2381,7 +2381,7 @@ case class ElementAt(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "2",
"paramIndex" -> ordinalNumber(1),
"requiredType" -> toSQLType(IntegerType),
"inputSql" -> toSQLExpr(right),
"inputType" -> toSQLType(right.dataType))
Expand All @@ -2400,7 +2400,7 @@ case class ElementAt(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(TypeCollection(ArrayType, MapType)),
"inputSql" -> toSQLExpr(left),
"inputType" -> toSQLType(left.dataType))
Expand Down Expand Up @@ -2606,7 +2606,7 @@ case class Concat(children: Seq[Expression]) extends ComplexTypeMergingExpressio
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> (idx + 1).toString,
"paramIndex" -> ordinalNumber(idx),
"requiredType" -> toSQLType(TypeCollection(allowedTypes: _*)),
"inputSql" -> toSQLExpr(e),
"inputType" -> toSQLType(e.dataType))
Expand Down Expand Up @@ -2823,7 +2823,7 @@ case class Flatten(child: Expression) extends UnaryExpression with NullIntoleran
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> s"${toSQLType(ArrayType)} of ${toSQLType(ArrayType)}",
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType))
Expand Down Expand Up @@ -4747,7 +4747,7 @@ case class ArrayInsert(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "2",
"paramIndex" -> ordinalNumber(1),
"requiredType" -> toSQLType(IntegerType),
"inputSql" -> toSQLExpr(second),
"inputType" -> toSQLType(second.dataType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ case class UpdateFields(structExpr: Expression, fieldOps: Seq[StructFieldsOperat
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(StructType),
"inputSql" -> toSQLExpr(structExpr),
"inputType" -> toSQLType(structExpr.dataType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
import org.apache.spark.sql.catalyst.trees.TernaryLike
import org.apache.spark.sql.catalyst.trees.TreePattern.{CASE_WHEN, IF, TreePattern}
import org.apache.spark.sql.catalyst.util.TypeUtils.{toSQLExpr, toSQLId, toSQLType}
import org.apache.spark.sql.catalyst.util.TypeUtils.{ordinalNumber, toSQLExpr, toSQLId, toSQLType}
import org.apache.spark.sql.types._
import org.apache.spark.util.ArrayImplicits._

Expand Down Expand Up @@ -70,7 +70,7 @@ case class If(predicate: Expression, trueValue: Expression, falseValue: Expressi
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(BooleanType),
"inputSql" -> toSQLExpr(predicate),
"inputType" -> toSQLType(predicate.dataType)
Expand Down Expand Up @@ -201,7 +201,7 @@ case class CaseWhen(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> (index + 1).toString,
"paramIndex" -> ordinalNumber(index),
"requiredType" -> toSQLType(BooleanType),
"inputSql" -> toSQLExpr(branches(index)._1),
"inputType" -> toSQLType(branches(index)._1.dataType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ case class Stack(children: Seq[Expression]) extends Generator {
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(IntegerType),
"inputSql" -> toSQLExpr(children.head),
"inputType" -> toSQLType(children.head.dataType))
Expand Down Expand Up @@ -342,7 +342,7 @@ abstract class ExplodeBase extends UnaryExpression with CollectionGenerator with
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(TypeCollection(ArrayType, MapType)),
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType))
Expand Down Expand Up @@ -557,7 +557,7 @@ case class Inline(child: Expression) extends UnaryExpression with CollectionGene
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType("ARRAY<STRUCT>"),
"inputSql" -> toSQLExpr(child),
"inputType" -> toSQLType(child.dataType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ case class ArraySort(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(ArrayType),
"inputSql" -> toSQLExpr(argument),
"inputType" -> toSQLType(argument.dataType)
Expand Down Expand Up @@ -840,7 +840,7 @@ case class ArrayAggregate(
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "3",
"paramIndex" -> ordinalNumber(2),
"requiredType" -> toSQLType(zero.dataType),
"inputSql" -> toSQLExpr(merge),
"inputType" -> toSQLType(merge.dataType)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ case class Elt(
return DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(IntegerType),
"inputSql" -> toSQLExpr(indexExpr),
"inputType" -> toSQLType(indexType)))
Expand All @@ -308,10 +308,10 @@ case class Elt(
return DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "2...",
"paramIndex" -> (ordinalNumber(1) + "..."),
"requiredType" -> (toSQLType(StringType) + " or " + toSQLType(BinaryType)),
"inputSql" -> inputExprs.map(toSQLExpr(_)).mkString(","),
"inputType" -> inputTypes.map(toSQLType(_)).mkString(",")
"inputSql" -> inputExprs.map(toSQLExpr).mkString(","),
"inputType" -> inputTypes.map(toSQLType).mkString(",")
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ case class NTile(buckets: Expression) extends RowNumberLike with SizeBasedWindow
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> toSQLType(IntegerType),
"inputSql" -> toSQLExpr(buckets),
"inputType" -> toSQLType(buckets.dataType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ object TypeUtils extends QueryErrorsBase {
DataTypeMismatch(
errorSubClass = "UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"paramIndex" -> "1",
"paramIndex" -> ordinalNumber(0),
"requiredType" -> Seq(NumericType, AnsiIntervalType).map(toSQLType).mkString(" or "),
"inputSql" -> toSQLExpr(input),
"inputType" -> toSQLType(other)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,12 @@ private[sql] trait QueryErrorsBase extends DataTypeErrorsBase {
else l.sql
case l => l.sql
}

def ordinalNumber(i: Int): String = i match {
case 0 => "first"
case 1 => "second"
case 2 => "third"
case i => s"${i + 1}th"
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
errorClass = "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"sqlExpr" -> "\"testfunction(NULL)\"",
"paramIndex" -> "1",
"paramIndex" -> "first",
"inputSql" -> "\"NULL\"",
"inputType" -> "\"DATE\"",
"requiredType" -> "\"INT\""))
Expand All @@ -174,7 +174,7 @@ class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
errorClass = "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"sqlExpr" -> "\"testfunction(NULL, NULL)\"",
"paramIndex" -> "2",
"paramIndex" -> "second",
"inputSql" -> "\"NULL\"",
"inputType" -> "\"DATE\"",
"requiredType" -> "\"INT\""))
Expand All @@ -186,7 +186,7 @@ class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
errorClass = "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"sqlExpr" -> "\"testfunction(NULL, NULL)\"",
"paramIndex" -> "1",
"paramIndex" -> "first",
"inputSql" -> "\"NULL\"",
"inputType" -> "\"DATE\"",
"requiredType" -> "\"INT\""))
Expand Down Expand Up @@ -389,7 +389,7 @@ class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
errorClass = "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE",
messageParameters = Map(
"sqlExpr" -> "\"nth_value(b, true)\"",
"paramIndex" -> "2",
"paramIndex" -> "second",
"inputSql" -> "\"true\"",
"inputType" -> "\"BOOLEAN\"",
"requiredType" -> "\"INT\""))
Expand Down Expand Up @@ -1198,7 +1198,7 @@ class AnalysisErrorSuite extends AnalysisTest with DataTypeErrorsBase {
expectedErrorClass = "DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE",
expectedMessageParameters = Map(
"sqlExpr" -> sqlExpr,
"paramIndex" -> "1",
"paramIndex" -> "first",
"inputSql" -> inputSql,
"inputType" -> inputType,
"requiredType" -> "(\"INT\" or \"BIGINT\")"),
Expand Down
Loading

0 comments on commit 9b53b80

Please sign in to comment.