Fix potential NPE/IllegalArgumentException by wrapping toBoolean call with Try and defaulting to false for LEGACY_PARQUET_NANOS_AS_LONG config#54709
Open
wanghui111 wants to merge 1 commit intoapache:branch-3.3from
Conversation
// Before (original code): conf.get(SQLConf.LEGACY_PARQUET_NANOS_AS_LONG.key).toBoolean) // After (modified code): nanosAsLong = Try(conf.get(SQLConf.LEGACY_PARQUET_NANOS_AS_LONG.key).toBoolean).getOrElse(false)) **Change Explanation**: 1. The original code directly calls `toBoolean()` on the result of `conf.get(...)`, which will throw: - `NullPointerException` if the config key `LEGACY_PARQUET_NANOS_AS_LONG.key` is not present (returns `null`), or - `IllegalArgumentException` if the config value is not a valid boolean string (e.g., "abc" instead of "true"/"false"). 2. The modified code wraps the boolean conversion logic in Scala's `Try` block: - `Try(...)` catches any exceptions thrown during the `toBoolean` conversion, - `getOrElse(false)` returns `false` as a safe default value if the conversion fails (instead of crashing the program). 3. This change makes the config parsing more robust and prevents runtime failures caused by missing/invalid values for the `LEGACY_PARQUET_NANOS_AS_LONG` configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
// Before (original code):
conf.get(SQLConf.LEGACY_PARQUET_NANOS_AS_LONG.key).toBoolean)
// After (modified code):
nanosAsLong = Try(conf.get(SQLConf.LEGACY_PARQUET_NANOS_AS_LONG.key).toBoolean).getOrElse(false))
Change Explanation:
toBoolean()on the result ofconf.get(...), which will throw:NullPointerExceptionif the config keyLEGACY_PARQUET_NANOS_AS_LONG.keyis not present (returnsnull), orIllegalArgumentExceptionif the config value is not a valid boolean string (e.g., "abc" instead of "true"/"false").Tryblock:Try(...)catches any exceptions thrown during thetoBooleanconversion,getOrElse(false)returnsfalseas a safe default value if the conversion fails (instead of crashing the program).LEGACY_PARQUET_NANOS_AS_LONGconfigurationWhat changes were proposed in this pull request?
Why are the changes needed?
Does this PR introduce any user-facing change?
How was this patch tested?
Was this patch authored or co-authored using generative AI tooling?