Skip to content

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
wanghui111:branch-3.3
Open

Conversation

@wanghui111
Copy link

// 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

What 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?

// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant