Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-46875][SQL] When the mode is null, a NullPointException should not be thrown #44900

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -47,12 +47,17 @@ object ParseMode extends Logging {
/**
* Returns the parse mode from the given string.
*/
def fromString(mode: String): ParseMode = mode.toUpperCase(Locale.ROOT) match {
case PermissiveMode.name => PermissiveMode
case DropMalformedMode.name => DropMalformedMode
case FailFastMode.name => FailFastMode
case _ =>
logWarning(s"$mode is not a valid parse mode. Using ${PermissiveMode.name}.")
PermissiveMode
def fromString(mode: String): ParseMode = Option(mode).map {
v => v.toUpperCase(Locale.ROOT) match {
case PermissiveMode.name => PermissiveMode
case DropMalformedMode.name => DropMalformedMode
case FailFastMode.name => FailFastMode
case _ =>
logWarning(s"$v is not a valid parse mode. Using ${PermissiveMode.name}.")
PermissiveMode
}
}.getOrElse {
logWarning(s"mode is null and not a valid parse mode. Using ${PermissiveMode.name}.")
PermissiveMode
Comment on lines +55 to +61
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we show an error on the wrong error instead of silently fallback to the PERMISSIVE mode? cc @HyukjinKwon

I mean both case - null and a wrong mode.

Copy link
Contributor Author

@panbingkun panbingkun Jan 26, 2024

Choose a reason for hiding this comment

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

@MaxGekk
In the current logic, when mode is null, the following error is displayed:
image

Copy link
Contributor Author

@panbingkun panbingkun Jan 26, 2024

Choose a reason for hiding this comment

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

I suggest we have two options as follows:
1.If the mode is null, it will fall back to PermissiveMode, just like setting a non-null error value for the mode.
2.If mode is null, we throw a user-friendly error prompt instead of a NullPointerException.

The fix change for this PR adopts the first option.

Copy link
Member

Choose a reason for hiding this comment

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

Probably, the fallback to the PERMISSIVE mode looks much more consistent with current approach of not-recognised value. We could stick on it in your PR, but I think it is better to change behaviour and throw appropriate error in both cases.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ abstract class CSVSuite
}
}

test("mode is null") {
val df = spark.read.schema("a DOUBLE")
.option("mode", null)
.csv(Seq("10u12").toDS())
assert(df.collect() sameElements Array(Row(null)))
}

test("test for blank column names on read and select columns") {
val cars = spark.read
.format("csv")
Expand Down