Skip to content

Commit

Permalink
apacheGH-39484: [Java] Support 256 bit decimals in JdbcToArrowUtils (a…
Browse files Browse the repository at this point in the history
…pache#39485)

### Rationale for this change

This PR allows users of `JdbcToArrowUtils` to convert 256 bit decimals.

### What changes are included in this PR?

Add a `Decimal256Consumer` and logic to 

### Are these changes tested?

No, at this point there are no good tests for JDBC consumers.

### Are there any user-facing changes?

Converting 256 bit decimals and ints bigger than 64 bit should now work

* Closes: apache#39484

Authored-by: Diego Fernandez <aiguo.fernandez@gmail.com>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
aiguofer authored Jan 8, 2024
1 parent 60b89ff commit 1622a2e
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.arrow.adapter.jdbc.consumer.BitConsumer;
import org.apache.arrow.adapter.jdbc.consumer.CompositeJdbcConsumer;
import org.apache.arrow.adapter.jdbc.consumer.DateConsumer;
import org.apache.arrow.adapter.jdbc.consumer.Decimal256Consumer;
import org.apache.arrow.adapter.jdbc.consumer.DecimalConsumer;
import org.apache.arrow.adapter.jdbc.consumer.DoubleConsumer;
import org.apache.arrow.adapter.jdbc.consumer.FloatConsumer;
Expand All @@ -64,6 +65,7 @@
import org.apache.arrow.vector.BigIntVector;
import org.apache.arrow.vector.BitVector;
import org.apache.arrow.vector.DateDayVector;
import org.apache.arrow.vector.Decimal256Vector;
import org.apache.arrow.vector.DecimalVector;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.Float4Vector;
Expand Down Expand Up @@ -170,7 +172,11 @@ public static ArrowType getArrowTypeFromJdbcType(final JdbcFieldInfo fieldInfo,
case Types.DECIMAL:
int precision = fieldInfo.getPrecision();
int scale = fieldInfo.getScale();
return new ArrowType.Decimal(precision, scale, 128);
if (precision > 38) {
return new ArrowType.Decimal(precision, scale, 256);
} else {
return new ArrowType.Decimal(precision, scale, 128);
}
case Types.REAL:
case Types.FLOAT:
return new ArrowType.FloatingPoint(SINGLE);
Expand Down Expand Up @@ -465,7 +471,12 @@ public static JdbcConsumer getConsumer(ArrowType arrowType, int columnIndex, boo
}
case Decimal:
final RoundingMode bigDecimalRoundingMode = config.getBigDecimalRoundingMode();
return DecimalConsumer.createConsumer((DecimalVector) vector, columnIndex, nullable, bigDecimalRoundingMode);
if (((ArrowType.Decimal) arrowType).getBitWidth() == 256) {
return Decimal256Consumer.createConsumer((Decimal256Vector) vector, columnIndex, nullable,
bigDecimalRoundingMode);
} else {
return DecimalConsumer.createConsumer((DecimalVector) vector, columnIndex, nullable, bigDecimalRoundingMode);
}
case FloatingPoint:
switch (((ArrowType.FloatingPoint) arrowType).getPrecision()) {
case SINGLE:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.adapter.jdbc.consumer;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.arrow.vector.Decimal256Vector;

/**
* Consumer which consume decimal type values from {@link ResultSet}.
* Write the data to {@link org.apache.arrow.vector.Decimal256Vector}.
*/
public abstract class Decimal256Consumer extends BaseConsumer<Decimal256Vector> {
private final RoundingMode bigDecimalRoundingMode;
private final int scale;

/**
* Constructs a new consumer.
*
* @param vector the underlying vector for the consumer.
* @param index the column id for the consumer.
*/
public Decimal256Consumer(Decimal256Vector vector, int index) {
this(vector, index, null);
}

/**
* Constructs a new consumer, with optional coercibility.
* @param vector the underlying vector for the consumer.
* @param index the column index for the consumer.
* @param bigDecimalRoundingMode java.math.RoundingMode to be applied if the BigDecimal scale does not match that
* of the target vector. Set to null to retain strict matching behavior (scale of
* source and target vector must match exactly).
*/
public Decimal256Consumer(Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) {
super(vector, index);
this.bigDecimalRoundingMode = bigDecimalRoundingMode;
this.scale = vector.getScale();
}

/**
* Creates a consumer for {@link Decimal256Vector}.
*/
public static JdbcConsumer<Decimal256Vector> createConsumer(
Decimal256Vector vector,
int index,
boolean nullable,
RoundingMode bigDecimalRoundingMode
) {
if (nullable) {
return new NullableDecimal256Consumer(vector, index, bigDecimalRoundingMode);
} else {
return new NonNullableDecimal256Consumer(vector, index, bigDecimalRoundingMode);
}
}

protected void set(BigDecimal value) {
if (bigDecimalRoundingMode != null && value.scale() != scale) {
value = value.setScale(scale, bigDecimalRoundingMode);
}
vector.set(currentIndex, value);
}


/**
* Consumer for nullable decimal.
*/
static class NullableDecimal256Consumer extends Decimal256Consumer {

/**
* Instantiate a Decimal256Consumer.
*/
public NullableDecimal256Consumer(Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) {
super(vector, index, bigDecimalRoundingMode);
}

@Override
public void consume(ResultSet resultSet) throws SQLException {
BigDecimal value = resultSet.getBigDecimal(columnIndexInResultSet);
if (!resultSet.wasNull()) {
// for fixed width vectors, we have allocated enough memory proactively,
// so there is no need to call the setSafe method here.
set(value);
}
currentIndex++;
}
}

/**
* Consumer for non-nullable decimal.
*/
static class NonNullableDecimal256Consumer extends Decimal256Consumer {

/**
* Instantiate a Decimal256Consumer.
*/
public NonNullableDecimal256Consumer(Decimal256Vector vector, int index, RoundingMode bigDecimalRoundingMode) {
super(vector, index, bigDecimalRoundingMode);
}

@Override
public void consume(ResultSet resultSet) throws SQLException {
BigDecimal value = resultSet.getBigDecimal(columnIndexInResultSet);
// for fixed width vectors, we have allocated enough memory proactively,
// so there is no need to call the setSafe method here.
set(value);
currentIndex++;
}
}
}

0 comments on commit 1622a2e

Please sign in to comment.