Repository navigation
perf(bigquery): [T1-6] eliminate DateTimeParseException control flow and Pattern.compile in FieldValue.getPeriodDuration - #10
Open
lqiu96 wants to merge 1 commit into
Conversation
…and Pattern.compile in FieldValue.getPeriodDuration
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.
Summary & Bottleneck
BigQuery's REST API encodes
INTERVALvalues 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()andFieldValue.parseCanonicalInterval(String)suffered from three per-cell bottlenecks ($O(\text{rows} \times \text{interval_cols})$):getPeriodDuration()unconditionally calledPeriodDuration.parse(getStringValue())inside atryblock first. Because ISO-8601 durations always start with'P'(optionally preceded by'+'or'-'), 100% of canonical BigQueryINTERVALcells threw and populated aDateTimeParseExceptionstack trace on the normal execution path before falling back toparseCanonicalInterval.parseCanonicalInterval(String)compiled a 10-group named-capture regular expression (Pattern.compile(...)) from scratch as a local variable on every cell invocation.hours,minutes,seconds, andfractionfrom the regex matcher,parseCanonicalIntervalconcatenated a synthetic"PT...H...M...S"string and parsed the numbers a second time viaDuration.parse(durationString).Changes
java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java:private static final Pattern CANONICAL_INTERVAL_PATTERNconstant.isIso8601PeriodDuration(String)ingetPeriodDuration()soPeriodDuration.parseis only attempted when the string begins with'P'or'p'(optionally preceded by'+'or'-'), dispatching directly toparseCanonicalIntervalfor canonical interval strings without throwingDateTimeParseException.Duration.ofSeconds(totalSeconds, nanos)directly from the parsedhours,minutes,seconds, andfractiongroups instead of concatenating a"PT..."string and callingDuration.parse.java-bigquery/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldValueTest.java:testParseCanonicalIntervalto testgetPeriodDuration()across canonical intervals, signed ISO-8601 intervals, and invalid strings.Expected Impact
DateTimeParseExceptionstack-trace capture per canonicalINTERVALcell.Pattern.compileperINTERVALcell."PT..."string concatenation andDuration.parsecall perINTERVALcell.Verification
FieldValueTestandFieldValueListTest:mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=FieldValueTest,FieldValueListTest