Repository navigation
GH-1377: [FlightRPC] Bind DOUBLE to DECIMAL parameters #1378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shahriyar-novruzov
wants to merge
4
commits into
apache:main
Choose a base branch
from
shahriyar-novruzov:gh-1377-decimal-double-binding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7ae68b8
GH-1377: [FlightRPC][Java] Bind DOUBLE to DECIMAL parameters
shahriyar-novruzov db68d89
Make the valueOf note a code comment on the line it explains
shahriyar-novruzov 7149c6a
Rename DecimalParameterNarrower to DecimalParameterCoercer
shahriyar-novruzov 0542622
fix: report NaN and infinity as an unsupported decimal binding
shahriyar-novruzov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
...re/src/main/java/org/apache/arrow/driver/jdbc/converter/impl/DecimalParameterCoercer.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.arrow.driver.jdbc.converter.impl; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import org.apache.calcite.avatica.remote.TypedValue; | ||
|
|
||
| /** | ||
| * Widens Avatica {@link TypedValue}s bound to DECIMAL parameters to {@link BigDecimal}, so calls | ||
| * like {@code setDouble} against an Arrow {@code Decimal} parameter bind instead of throwing. | ||
| */ | ||
| final class DecimalParameterCoercer { | ||
|
|
||
| private DecimalParameterCoercer() {} | ||
|
|
||
| /** | ||
| * Returns the {@link BigDecimal} value of a {@link TypedValue} bound to a DECIMAL parameter, | ||
| * scaled to what the target vector stores. | ||
| * | ||
| * <p>A value is scaled rather than rejected on a mismatch: the client sent the number it had, and | ||
| * the column's scale is what it has to fit. Rounding is HALF_UP, as {@code setBigDecimal} does. | ||
| */ | ||
| static BigDecimal toBigDecimal(TypedValue typedValue, int scale) { | ||
| return widen(typedValue).setScale(scale, RoundingMode.HALF_UP); | ||
| } | ||
|
|
||
| private static BigDecimal widen(TypedValue typedValue) { | ||
| final Object local = typedValue.toLocal(); | ||
| if (local instanceof BigDecimal) { | ||
| return (BigDecimal) local; | ||
| } | ||
| if (local instanceof Double || local instanceof Float) { | ||
| final double value = ((Number) local).doubleValue(); | ||
| if (Double.isNaN(value) || Double.isInfinite(value)) { | ||
| // No decimal representation exists. Thrown as a cast error so the binder reports it the | ||
| // same way it reported every double before this class existed; BigDecimal.valueOf would | ||
| // raise NumberFormatException, which the binder does not catch. | ||
| throw new ClassCastException("NaN and infinity have no " + BigDecimal.class.getName()); | ||
| } | ||
| // valueOf, not new BigDecimal(double): the constructor keeps the binary expansion, so 0.1 | ||
| // arrives as 0.1000000000000000055511151231257827. | ||
| return BigDecimal.valueOf(value); | ||
| } | ||
|
shahriyar-novruzov marked this conversation as resolved.
|
||
| if (local instanceof Number) { | ||
| return BigDecimal.valueOf(((Number) local).longValue()); | ||
| } | ||
|
shahriyar-novruzov marked this conversation as resolved.
|
||
| // Anything else keeps the existing behaviour: a cast error AvaticaParameterBinder catches and | ||
| // reports as an unsupported binding, naming the rep and the Arrow type. | ||
| return (BigDecimal) local; | ||
| } | ||
| } | ||
188 changes: 188 additions & 0 deletions
188
...ava/org/apache/arrow/driver/jdbc/converter/impl/DecimalAvaticaParameterConverterTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.arrow.driver.jdbc.converter.impl; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import org.apache.arrow.driver.jdbc.utils.RootAllocatorTestExtension; | ||
| import org.apache.arrow.vector.DecimalVector; | ||
| import org.apache.arrow.vector.IntVector; | ||
| import org.apache.arrow.vector.types.pojo.ArrowType; | ||
| import org.apache.calcite.avatica.ColumnMetaData; | ||
| import org.apache.calcite.avatica.remote.TypedValue; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| /** | ||
| * Tests for {@link DecimalAvaticaParameterConverter}. | ||
| * | ||
| * <p>Clients call {@code setDouble} on parameters whose Arrow type is {@code Decimal}. The | ||
| * converter must widen the Avatica reps to BigDecimal and scale them to the vector instead of | ||
| * throwing. | ||
| */ | ||
| public class DecimalAvaticaParameterConverterTest { | ||
|
|
||
| @RegisterExtension | ||
| public static RootAllocatorTestExtension rootAllocatorTestExtension = | ||
| new RootAllocatorTestExtension(); | ||
|
|
||
| private static final int PRECISION = 38; | ||
| private static final int SCALE = 3; | ||
|
|
||
| private DecimalVector vector; | ||
| private DecimalAvaticaParameterConverter converter; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| vector = | ||
| new DecimalVector( | ||
| "decimal_param", rootAllocatorTestExtension.getRootAllocator(), PRECISION, SCALE); | ||
| vector.allocateNew(5); | ||
| converter = new DecimalAvaticaParameterConverter(new ArrowType.Decimal(PRECISION, SCALE, 128)); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| vector.close(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindDouble() { | ||
| // The reported shape: setDouble of 123.456 against a DECIMAL(38,3) parameter. | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, 123.456d); | ||
|
shahriyar-novruzov marked this conversation as resolved.
|
||
|
|
||
| boolean result = converter.bindParameter(vector, typedValue, 0); | ||
|
|
||
| assertTrue(result); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("123.456"))); | ||
|
shahriyar-novruzov marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| public void testBindDoubleKeepsTheDigitsTheClientSent() { | ||
| // new BigDecimal(0.1) would store 0.1000000000000000055511151231257827 and fail the scale | ||
| // check. | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, 0.1d); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("0.100"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindDoubleRoundsToTheVectorScale() { | ||
| // 1.2345 has one digit more than DECIMAL(38,3) holds; HALF_UP is what setBigDecimal does. | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, 1.2345d); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("1.235"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindFloat() { | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.FLOAT, 2.5f); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("2.500"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindInteger() { | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.INTEGER, 42); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("42.000"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindLong() { | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.LONG, 1000L); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("1000.000"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindBigDecimalStillWorks() { | ||
| TypedValue typedValue = | ||
| TypedValue.ofLocal(ColumnMetaData.Rep.NUMBER, new BigDecimal("1000.000")); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("1000.000"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindBigDecimalOfAnotherScaleIsScaled() { | ||
| // setBigDecimal of a value the client scaled differently used to fail the vector's scale check. | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.NUMBER, new BigDecimal("7.5")); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("7.500"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindNegativeDouble() { | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, -123.456d); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(new BigDecimal("-123.456"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindBigIntegerArrivesAsBigDecimal() { | ||
| // Avatica has no rep for BigInteger, so toLocal() hands one over as a BigDecimal and the | ||
| // value survives whole. Nothing reaches the longValue() branch, which would overflow here. | ||
| final BigDecimal tooBigForALong = new BigDecimal("123456789012345678901234567890"); | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.NUMBER, tooBigForALong); | ||
|
|
||
| assertTrue(converter.bindParameter(vector, typedValue, 0)); | ||
| assertThat(vector.getObject(0), is(tooBigForALong.setScale(SCALE))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindNaNIsReportedAsUnsupported() { | ||
| // A cast error is what the binder turns into "Binding value of type DOUBLE is not yet | ||
| // supported"; NumberFormatException would escape it. | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, Double.NaN); | ||
|
|
||
| assertThrows(ClassCastException.class, () -> converter.bindParameter(vector, typedValue, 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindInfinityIsReportedAsUnsupported() { | ||
| for (double value : new double[] {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}) { | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, value); | ||
|
|
||
| assertThrows(ClassCastException.class, () -> converter.bindParameter(vector, typedValue, 0)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testBindReturnsFalseForANonDecimalVector() { | ||
| try (IntVector other = new IntVector("other", rootAllocatorTestExtension.getRootAllocator())) { | ||
| other.allocateNew(1); | ||
| TypedValue typedValue = TypedValue.ofLocal(ColumnMetaData.Rep.DOUBLE, 1.0d); | ||
|
|
||
| // Returning false is what makes the binder report the type as unsupported. | ||
| assertThat(converter.bindParameter(other, typedValue, 0), is(false)); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.