Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
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.Decimal32Consumer;
import org.apache.arrow.adapter.jdbc.consumer.Decimal64Consumer;
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 @@ -66,6 +68,8 @@
import org.apache.arrow.vector.BitVector;
import org.apache.arrow.vector.DateDayVector;
import org.apache.arrow.vector.Decimal256Vector;
import org.apache.arrow.vector.Decimal32Vector;
import org.apache.arrow.vector.Decimal64Vector;
import org.apache.arrow.vector.DecimalVector;
import org.apache.arrow.vector.FieldVector;
import org.apache.arrow.vector.Float4Vector;
Expand Down Expand Up @@ -510,10 +514,21 @@ public static JdbcConsumer getConsumer(
}
case Decimal:
final RoundingMode bigDecimalRoundingMode = config.getBigDecimalRoundingMode();
if (((ArrowType.Decimal) arrowType).getBitWidth() == 256) {
final int decimalBitWidth = ((ArrowType.Decimal) arrowType).getBitWidth();
if (decimalBitWidth == 256) {
return Decimal256Consumer.createConsumer(
(Decimal256Vector) vector, columnIndex, nullable, bigDecimalRoundingMode);
} else if (decimalBitWidth == 128) {
return DecimalConsumer.createConsumer(
(DecimalVector) vector, columnIndex, nullable, bigDecimalRoundingMode);
} else if (decimalBitWidth == 64) {
return Decimal64Consumer.createConsumer(
(Decimal64Vector) vector, columnIndex, nullable, bigDecimalRoundingMode);
} else if (decimalBitWidth == 32) {
return Decimal32Consumer.createConsumer(
(Decimal32Vector) vector, columnIndex, nullable, bigDecimalRoundingMode);
} else {
// Any other bit width maps to MinorType.DECIMAL, so the root created a DecimalVector.
return DecimalConsumer.createConsumer(
(DecimalVector) vector, columnIndex, nullable, bigDecimalRoundingMode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.Decimal32Vector;

/**
* Consumer which consume decimal type values from {@link ResultSet}. Write the data to {@link
* org.apache.arrow.vector.Decimal32Vector}.
*/
public abstract class Decimal32Consumer extends BaseConsumer<Decimal32Vector> {
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 Decimal32Consumer(Decimal32Vector 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 Decimal32Consumer(Decimal32Vector vector, int index, RoundingMode bigDecimalRoundingMode) {
super(vector, index);
this.bigDecimalRoundingMode = bigDecimalRoundingMode;
this.scale = vector.getScale();
}

/** Creates a consumer for {@link Decimal32Vector}. */
public static JdbcConsumer<Decimal32Vector> createConsumer(
Decimal32Vector vector, int index, boolean nullable, RoundingMode bigDecimalRoundingMode) {
if (nullable) {
return new NullableDecimal32Consumer(vector, index, bigDecimalRoundingMode);
} else {
return new NonNullableDecimal32Consumer(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 NullableDecimal32Consumer extends Decimal32Consumer {

/** Instantiate a Decimal32Consumer. */
public NullableDecimal32Consumer(
Decimal32Vector 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 NonNullableDecimal32Consumer extends Decimal32Consumer {

/** Instantiate a Decimal32Consumer. */
public NonNullableDecimal32Consumer(
Decimal32Vector 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++;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.Decimal64Vector;

/**
* Consumer which consume decimal type values from {@link ResultSet}. Write the data to {@link
* org.apache.arrow.vector.Decimal64Vector}.
*/
public abstract class Decimal64Consumer extends BaseConsumer<Decimal64Vector> {
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 Decimal64Consumer(Decimal64Vector 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 Decimal64Consumer(Decimal64Vector vector, int index, RoundingMode bigDecimalRoundingMode) {
super(vector, index);
this.bigDecimalRoundingMode = bigDecimalRoundingMode;
this.scale = vector.getScale();
}

/** Creates a consumer for {@link Decimal64Vector}. */
public static JdbcConsumer<Decimal64Vector> createConsumer(
Decimal64Vector vector, int index, boolean nullable, RoundingMode bigDecimalRoundingMode) {
if (nullable) {
return new NullableDecimal64Consumer(vector, index, bigDecimalRoundingMode);
} else {
return new NonNullableDecimal64Consumer(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 NullableDecimal64Consumer extends Decimal64Consumer {

/** Instantiate a Decimal64Consumer. */
public NullableDecimal64Consumer(
Decimal64Vector 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 NonNullableDecimal64Consumer extends Decimal64Consumer {

/** Instantiate a Decimal64Consumer. */
public NonNullableDecimal64Consumer(
Decimal64Vector 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++;
}
}
}
Loading
Loading