Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ object Cast extends QueryErrorsBase {
"config" -> toSQLConf(fallbackConf.get._1),
"configVal" -> toSQLValue(fallbackConf.get._2, StringType)))

case _ if fallbackConf.isEmpty && Cast.canTryCast(from, to) =>
// Suggest try_cast for valid casts that fail in ANSI mode
withFunSuggest("try_cast")

case _ =>
DataTypeMismatch(
errorSubClass = "CAST_WITHOUT_SUGGESTION",
Expand Down Expand Up @@ -588,8 +592,19 @@ case class Cast(
Some(SQLConf.STORE_ASSIGNMENT_POLICY.key ->
SQLConf.StoreAssignmentPolicy.LEGACY.toString))
} else {
Cast.typeCheckFailureMessage(child.dataType, dataType,
Some(SQLConf.ANSI_ENABLED.key -> "false"))
// Check if there's a config workaround for this cast failure:
// - If canTryCast supports this cast, pass None here and let typeCheckFailureMessage
// suggest try_cast (which is more user-friendly than disabling ANSI mode)
// - If canTryCast doesn't support it BUT the cast works in non-ANSI mode,
// suggest disabling ANSI mode as a migration path
// - Otherwise, pass None and let typeCheckFailureMessage decide
val fallbackConf = if (!Cast.canTryCast(child.dataType, dataType) &&
Cast.canCast(child.dataType, dataType)) {
Some(SQLConf.ANSI_ENABLED.key -> "false")
} else {
None
}
Cast.typeCheckFailureMessage(child.dataType, dataType, fallbackConf)
}
case EvalMode.TRY =>
Cast.typeCheckFailureMessage(child.dataType, dataType, None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ class CastWithAnsiOnSuite extends CastSuiteBase with QueryErrorsBase {
}
}

test("SPARK-49635: suggest try_cast for complex type casts") {
// Array[Int] to Array[Binary]: canTryCast=true (uses canCast), canAnsiCast=false
// Should suggest try_cast, not config
val arrayIntType = ArrayType(IntegerType, containsNull = false)
val arrayBinaryType = ArrayType(BinaryType, containsNull = false)
val arrayIntLiteral = Literal.create(Seq(1, 2, 3), arrayIntType)

val arrayResult = cast(arrayIntLiteral, arrayBinaryType).checkInputDataTypes()
evalMode match {
case EvalMode.ANSI =>
assert(arrayResult ==
DataTypeMismatch(
errorSubClass = "CAST_WITH_FUNC_SUGGESTION",
messageParameters = Map(
"srcType" -> toSQLType(arrayIntType),
"targetType" -> toSQLType(arrayBinaryType),
"functionNames" -> "`try_cast`"
)
)
)
case _ =>
}
}

test("ANSI mode: disallow variant cast to non-nullable types") {
// Array
val variantVal = new VariantVal(Array[Byte](12, 3), Array[Byte](1, 0, 0))
Expand Down