Skip to content

perf(bigquery): [T1-6] eliminate DateTimeParseException control flow and Pattern.compile in FieldValue.getPeriodDuration - #10

Open
lqiu96 wants to merge 1 commit into
mainfrom
perf/bq-t1-06-fieldvalue-period-duration
Open

lqiu96 wants to merge 1 commit into
mainfrom
perf/bq-t1-06-fieldvalue-period-duration

Conversation

@lqiu96

@lqiu96 lqiu96 commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Summary & Bottleneck

BigQuery's REST API encodes INTERVAL values in canonical "[sign]Y-M [sign]D [sign]H:M:S[.F]" format (for example, "123-7 -19 0:24:12.000006"). Previously, FieldValue.getPeriodDuration() and FieldValue.parseCanonicalInterval(String) suffered from three per-cell bottlenecks ($O(\text{rows} \times \text{interval_cols})$):

  • getPeriodDuration() unconditionally called PeriodDuration.parse(getStringValue()) inside a try block first. Because ISO-8601 durations always start with 'P' (optionally preceded by '+' or '-'), 100% of canonical BigQuery INTERVAL cells threw and populated a DateTimeParseException stack trace on the normal execution path before falling back to parseCanonicalInterval.
  • parseCanonicalInterval(String) compiled a 10-group named-capture regular expression (Pattern.compile(...)) from scratch as a local variable on every cell invocation.
  • After extracting hours, minutes, seconds, and fraction from the regex matcher, parseCanonicalInterval concatenated a synthetic "PT...H...M...S" string and parsed the numbers a second time via Duration.parse(durationString).

Changes

  • In java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java:
    • Hoist the canonical interval regular expression to a private static final Pattern CANONICAL_INTERVAL_PATTERN constant.
    • Add isIso8601PeriodDuration(String) in getPeriodDuration() so PeriodDuration.parse is only attempted when the string begins with 'P' or 'p' (optionally preceded by '+' or '-'), dispatching directly to parseCanonicalInterval for canonical interval strings without throwing DateTimeParseException.
    • Compute Duration.ofSeconds(totalSeconds, nanos) directly from the parsed hours, minutes, seconds, and fraction groups instead of concatenating a "PT..." string and calling Duration.parse.
  • In java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldValueTest.java:
    • Expand testParseCanonicalInterval to test getPeriodDuration() across canonical intervals, signed ISO-8601 intervals, and invalid strings.

Expected Impact

  • Eliminates 1 DateTimeParseException stack-trace capture per canonical INTERVAL cell.
  • Eliminates 1 full 10-group regex Pattern.compile per INTERVAL cell.
  • Eliminates 1 "PT..." string concatenation and Duration.parse call per INTERVAL cell.

Verification

  • Ran unit tests in FieldValueTest and FieldValueListTest:
    mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=FieldValueTest,FieldValueListTest

…and Pattern.compile in FieldValue.getPeriodDuration
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