Repository navigation
Conversation
…and precision loss in BigQueryResultImpl
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
java-bigquery/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryResultImpl.java(L122-L133,L387-L567,L751-L769)ResultSetnumeric, decimal, time, and array getters.getInt,getLong, andgetDoubleonFieldValueListrows calledfieldValue.getNumericValue()(new BigDecimal(getStringValue())) on every cell instead of parsing primitiveint,long, anddoublevalues directly. In addition,getInt(int columnIndex)omittedwasNull = falseon non-nullFieldValueListcells.getDoubleconverted an already-numericDoubleorBigDecimalobject into aStringand parsed a newBigDecimal(new BigDecimal(curVal.toString()).doubleValue()).getBigDecimalcalledBigDecimal.valueOf(fieldValue.getNumericValue().doubleValue())onFieldValueListrows andBigDecimal.valueOf(getDouble(fieldName))on Read API rows, allocating twoBigDecimalobjects per cell and truncating 38-digitNUMERICand 76-digitBIGNUMERICvalues to 53-bit IEEE-754doubleprecision.getTimeFromFieldValinvokedstrTime.split(":")andtimeSplt[2].split("\\.")(compiling a regexPatternon every fractionalTIMEcell because"\\."is two characters).BigQueryArrayResultcopiedObject[]intonew ArrayList<>(Arrays.asList(...))and copiedFieldValueListintonew ArrayList<>((FieldValueList) array)on everygetArraycall.Changes
wasNullfix:parseFieldValueIntandparseFieldValueLongto parse integer strings directly viaInteger.parseIntandLong.parseLong(falling back togetNumericValue()only when decimal or exponent characters are present), and updatedgetInt(int columnIndex)to setwasNull = false.getDoubleto callfieldValue.getDoubleValue()onFieldValueListrows and((Number) curVal).doubleValue()on Read APINumbervalues.BigDecimalretrieval withoutdoubletruncation:getBigDecimalto returnfieldValue.getNumericValue()onFieldValueListrows and return(BigDecimal) curValdirectly when the Read API value is already aBigDecimal.TIMEparsing and zero-copyBigQueryArrayResult:strTime.split(":")andsplit("\\.")ingetTimeFromFieldValwithindexOf(':')andindexOf('.').BigQueryArrayResultto wrapObject[]viaArrays.asListand retainList<?>(includingFieldValueList) without copying elements into a newArrayList.Expected Impact
BigDecimalandStringallocations acrossgetInt,getLong,getDouble, andgetBigDecimal.doubleprecision loss onNUMERIC(38 digits) andBIGNUMERIC(76 digits) columns inResultSet.getBigDecimal.Patterncompilation andString[]allocations on everyResultSet.getTimecall and eliminates redundantArrayListcopies onResultSet.getArray.Verification
mvn test -pl java-bigquery/google-cloud-bigquery -Dtest=BigQueryResultImplTest,ConnectionImplTest(26 tests run, 0 failures).BigQueryResultImplTest.testResultSetNumericPrecisionWasNullAndFractionalTimeverifying 38-digitBigDecimalprecision preservation across bothFieldValueListand Read API rows,wasNullreset ongetInt(int), and fractionalTIMEparsing.