Skip to content

[SPARK-57458][SQL][FOLLOWUP] Avoid non-local return in XmlInferSchema.tryParseTimestampNTZ#56916

Open
cloud-fan wants to merge 1 commit into
apache:masterfrom
cloud-fan:cloud-fan/xml-nano-nonlocal-return
Open

[SPARK-57458][SQL][FOLLOWUP] Avoid non-local return in XmlInferSchema.tryParseTimestampNTZ#56916
cloud-fan wants to merge 1 commit into
apache:masterfrom
cloud-fan:cloud-fan/xml-nano-nonlocal-return

Conversation

@cloud-fan

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

This is a followup of SPARK-57458. The nanosecond-precision timestamp inference in XmlInferSchema.tryParseTimestampNTZ used a return statement inside an Option.foreach closure, which is a non-local return implemented via a control-flow exception. This replaces it with a plain boolean check plus a local return, keeping the behavior identical while avoiding the non-local return.

Why are the changes needed?

Non-local returns rely on throwing/catching a NonLocalReturnControl exception, which is discouraged and flagged by lint tooling. The rewrite is equivalent and clearer.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Existing tests for nanosecond timestamp inference in the XML datasource cover this code path.

Was this patch authored or co-authored using generative AI tooling?

Yes, this patch was co-authored using generative AI tooling.

@cloud-fan

Copy link
Copy Markdown
Contributor Author

cc @MaxGekk

@MaxGekk MaxGekk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

0 blocking, 0 non-blocking, 1 nit.
Clean, behavior-identical cleanup. filter(p).foreach runs iff Some && p — exactly Option.exists(p) — and the && short-circuit keeps the parse from running when the flag is off, as before; both the old NonLocalReturnControl (a ControlThrowable, not an Exception) and the new plain return escape the surrounding catch { case _: Exception } uncaught, so the result is unchanged (and slightly more robust). One nit inline.

Findings

Nits (1)

  • The relocated // ... more than 6 digits comment is imprecise vs the nanosWithinMicro != 0 check — see inline.

nanosOpt.filter(_.nanosWithinMicro != 0).foreach { _ =>
return Some(TimestampNTZNanosType(9))
}
// Prefer nanosecond type when the fractional seconds part has more than 6 digits,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit (pre-existing wording, but you're moving this comment so it's a natural moment): "more than 6 digits" doesn't quite match the nanosWithinMicro != 0 test below it. A 7-digit fraction with a trailing zero — e.g. .1234560 → 123456000 ns → nanosWithinMicro == 0 — has more than 6 digits yet stays TimestampNTZType; the actual trigger is a nonzero sub-microsecond remainder, not the digit count. Maybe: // Prefer nanosecond type when there is a nonzero sub-microsecond component (nanosWithinMicro != 0) that TimestampNTZType cannot represent.

The refactor itself is exactly equivalent — nice cleanup.

….tryParseTimestampNTZ

### What changes were proposed in this pull request?

This is a followup of SPARK-57458. The nanosecond-precision timestamp
inference in `XmlInferSchema.tryParseTimestampNTZ` used a `return`
statement inside an `Option.foreach` closure, which is a non-local
return implemented via a control-flow exception. This replaces it with a
plain boolean check plus a local `return`, keeping the behavior
identical while avoiding the non-local return.

### Why are the changes needed?

Non-local returns rely on throwing/catching a `NonLocalReturnControl`
exception, which is discouraged and flagged by lint tooling. The
rewrite is equivalent and clearer.

### Does this PR introduce any user-facing change?

No.

### How was this patch tested?

Existing tests for nanosecond timestamp inference in the XML datasource
cover this code path.

### Was this patch authored or co-authored using generative AI tooling?

Yes, this patch was co-authored using generative AI tooling.
@cloud-fan cloud-fan force-pushed the cloud-fan/xml-nano-nonlocal-return branch from 676e18a to 134a178 Compare July 1, 2026 12:23
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.

2 participants