Skip to content

Commit

Permalink
[SPARK-43361][PROTOBUF] update documentation for errors related to en…
Browse files Browse the repository at this point in the history
…um serialization

### What changes were proposed in this pull request?
Follows-up on the comment here: #41075 (comment)

Namely:
- updates `error-classes.json` and `sql-error-conditions.md` to have the updated error name.
- adds an additional test to assert that enum serialization with invalid enum values throws the correct exception.

### Why are the changes needed?
Improve documentation

### Does this PR introduce _any_ user-facing change?
Yes, documentation.

### How was this patch tested?
Existing unit tests

Closes #41188 from justaparth/parth/update-documentation-enum-error-message.

Authored-by: Parth Upadhyay <parth.upadhyay@gmail.com>
Signed-off-by: Ruifeng Zheng <ruifengz@apache.org>
  • Loading branch information
justaparth authored and zhengruifeng committed May 19, 2023
1 parent eb2456b commit b094b98
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private[sql] class ProtobufSerializer(
catalystPath,
toFieldStr(protoPath),
data.toString,
enumValues.mkString("", ",", ""))
enumValues.mkString(", "))
}
fieldDescriptor.getEnumType.findValueByNumber(data)
case (StringType, STRING) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,48 @@ class ProtobufFunctionsSuite extends QueryTest with SharedSparkSession with Prot
}
}

test("raise enum serialization error") {
// Confirm that attempting to serialize an invalid enum value will raise the correct exception.
val df = spark.range(1).select(
struct(
lit("INVALID_VALUE").as("basic_enum")
).as("proto")
)

val dfWithInt = spark.range(1).select(
struct(
lit(9999).as("basic_enum")
).as("proto")
)

checkWithFileAndClassName("SimpleMessageEnum") { case (name, descFilePathOpt) =>
var parseError = intercept[AnalysisException] {
df.select(to_protobuf_wrapper($"proto", name, descFilePathOpt)).collect()
}
checkError(
exception = parseError,
errorClass = "CANNOT_CONVERT_SQL_VALUE_TO_PROTOBUF_ENUM_TYPE",
parameters = Map(
"sqlColumn" -> "`basic_enum`",
"protobufColumn" -> "field 'basic_enum'",
"data" -> "INVALID_VALUE",
"enumString" -> "\"NOTHING\", \"FIRST\", \"SECOND\""))

parseError = intercept[AnalysisException] {
dfWithInt.select(to_protobuf_wrapper($"proto", name, descFilePathOpt)).collect()
}
checkError(
exception = parseError,
errorClass = "CANNOT_CONVERT_SQL_VALUE_TO_PROTOBUF_ENUM_TYPE",
parameters = Map(
"sqlColumn" -> "`basic_enum`",
"protobufColumn" -> "field 'basic_enum'",
"data" -> "9999",
"enumString" -> "0, 1, 2"))
}
}


def testFromProtobufWithOptions(
df: DataFrame,
expectedDf: DataFrame,
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
"Unable to convert <protobufType> of Protobuf to SQL type <toType>."
]
},
"CANNOT_CONVERT_SQL_TYPE_TO_PROTOBUF_ENUM_TYPE" : {
"CANNOT_CONVERT_SQL_TYPE_TO_PROTOBUF_FIELD_TYPE" : {
"message" : [
"Cannot convert SQL <sqlColumn> to Protobuf <protobufColumn> because <data> cannot be written since it's not defined in ENUM <enumString>."
"Cannot convert SQL <sqlColumn> to Protobuf <protobufColumn> because schema is incompatible (protobufType = <protobufType>, sqlType = <sqlType>)."
]
},
"CANNOT_CONVERT_SQL_TYPE_TO_PROTOBUF_FIELD_TYPE" : {
"CANNOT_CONVERT_SQL_VALUE_TO_PROTOBUF_ENUM_TYPE" : {
"message" : [
"Cannot convert SQL <sqlColumn> to Protobuf <protobufColumn> because schema is incompatible (protobufType = <protobufType>, sqlType = <sqlType>)."
"Cannot convert SQL <sqlColumn> to Protobuf <protobufColumn> because <data> is not in defined values for enum: <enumString>."
]
},
"CANNOT_DECODE_URL" : {
Expand Down
8 changes: 4 additions & 4 deletions docs/sql-error-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ SQLSTATE: none assigned

Unable to convert `<protobufType>` of Protobuf to SQL type `<toType>`.

### CANNOT_CONVERT_SQL_TYPE_TO_PROTOBUF_ENUM_TYPE
### CANNOT_CONVERT_SQL_TYPE_TO_PROTOBUF_FIELD_TYPE

SQLSTATE: none assigned

Cannot convert SQL `<sqlColumn>` to Protobuf `<protobufColumn>` because `<data>` cannot be written since it's not defined in ENUM `<enumString>`.
Cannot convert SQL `<sqlColumn>` to Protobuf `<protobufColumn>` because schema is incompatible (protobufType = `<protobufType>`, sqlType = `<sqlType>`).

### CANNOT_CONVERT_SQL_TYPE_TO_PROTOBUF_FIELD_TYPE
### CANNOT_CONVERT_SQL_VALUE_TO_PROTOBUF_ENUM_TYPE

SQLSTATE: none assigned

Cannot convert SQL `<sqlColumn>` to Protobuf `<protobufColumn>` because schema is incompatible (protobufType = `<protobufType>`, sqlType = `<sqlType>`).
Cannot convert SQL `<sqlColumn>` to Protobuf `<protobufColumn>` because `<data>` is not in defined values for enum: `<enumString>`.

### CANNOT_DECODE_URL

Expand Down

0 comments on commit b094b98

Please sign in to comment.