Skip to content

Commit

Permalink
ARROW-17004: [Java] Add utility to bind Arrow data to JDBC parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lidavidm committed Jul 13, 2022
1 parent f1737f9 commit 7adaca4
Show file tree
Hide file tree
Showing 23 changed files with 2,323 additions and 2 deletions.
87 changes: 85 additions & 2 deletions docs/source/java/jdbc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ The JDBC to Arrow type mapping can be obtained at runtime from
+--------------------+--------------------+-------+
| DOUBLE | Double | |
+--------------------+--------------------+-------+
| FLOAT | Float | |
| FLOAT | Float32 | |
+--------------------+--------------------+-------+
| INTEGER | Int32 | |
+--------------------+--------------------+-------+
Expand All @@ -138,7 +138,7 @@ The JDBC to Arrow type mapping can be obtained at runtime from
+--------------------+--------------------+-------+
| NVARCHAR | Utf8 | |
+--------------------+--------------------+-------+
| REAL | Float | |
| REAL | Float32 | |
+--------------------+--------------------+-------+
| SMALLINT | Int16 | |
+--------------------+--------------------+-------+
Expand Down Expand Up @@ -172,3 +172,86 @@ The JDBC to Arrow type mapping can be obtained at runtime from
.. _setArraySubTypeByColumnIndexMap: https://arrow.apache.org/docs/java/reference/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigBuilder.html#setArraySubTypeByColumnIndexMap-java.util.Map-
.. _setArraySubTypeByColumnNameMap: https://arrow.apache.org/docs/java/reference/org/apache/arrow/adapter/jdbc/JdbcToArrowConfigBuilder.html#setArraySubTypeByColumnNameMap-java.util.Map-
.. _ARROW-17006: https://issues.apache.org/jira/browse/ARROW-17006

VectorSchemaRoot to PreparedStatement Parameter Conversion
==========================================================

The adapter can bind rows of Arrow data from a VectorSchemaRoot to
parameters of a JDBC PreparedStatement. This can be accessed via the
JdbcParameterBinder class. Each call to next() will bind parameters
from the next row of data, and then the application can execute the
statement, call addBatch(), etc. as desired. Null values will lead to
a setNull call with an appropriate JDBC type code (listed below).

.. code-block:: java
final JdbcParameterBinder binder =
JdbcParameterBinder.builder(statement, root).bindAll().build();
while (binder.next()) {
statement.executeUpdate();
}
The mapping of vectors to parameters, the JDBC type code used by the
converters, and the type conversions themselves can all be customized:

.. code-block:: java
final JdbcParameterBinder binder =
JdbcParameterBinder.builder(statement, root)
.bind(/*parameterIndex*/2, /*columnIndex*/0)
.bind(/*parameterIndex*/1, customColumnBinderInstance)
.build();
Type Mapping
------------

The Arrow to JDBC type mapping can be obtained at runtime via
a method on ColumnBinder.

+----------------------------+----------------------------+-------+
| Arrow Type | JDBC Type | Notes |
+============================+============================+=======+
| Bool | BOOLEAN (setBoolean) | |
+----------------------------+----------------------------+-------+
| Date32 | DATE (setDate) | |
+----------------------------+----------------------------+-------+
| Date64 | DATE (setDate) | |
+----------------------------+----------------------------+-------+
| Float32 | REAL (setFloat) | |
+----------------------------+----------------------------+-------+
| Int8 | TINYINT (setByte) | |
+----------------------------+----------------------------+-------+
| Int16 | SMALLINT (setShort) | |
+----------------------------+----------------------------+-------+
| Int32 | INTEGER (setInt) | |
+----------------------------+----------------------------+-------+
| Int64 | BIGINT (setLong) | |
+----------------------------+----------------------------+-------+
| LargeUtf8 | LONGVARCHAR (setString) | \(1) |
+----------------------------+----------------------------+-------+
| Time[s] | TIME (setTime) | |
+----------------------------+----------------------------+-------+
| Time[ms] | TIME (setTime) | |
+----------------------------+----------------------------+-------+
| Time[us] | TIME (setTime) | |
+----------------------------+----------------------------+-------+
| Time[ns] | TIME (setTime) | |
+----------------------------+----------------------------+-------+
| Timestamp[s] | TIMESTAMP (setTimestamp) | \(2) |
+----------------------------+----------------------------+-------+
| Timestamp[ms] | TIMESTAMP (setTimestamp) | \(2) |
+----------------------------+----------------------------+-------+
| Timestamp[us] | TIMESTAMP (setTimestamp) | \(2) |
+----------------------------+----------------------------+-------+
| Timestamp[ns] | TIMESTAMP (setTimestamp) | \(2) |
+----------------------------+----------------------------+-------+
| Utf8 | VARCHAR (setString) | |
+----------------------------+----------------------------+-------+

* \(1) Strings longer than Integer.MAX_VALUE bytes (the maximum length
of a Java ``byte[]``) will cause a runtime exception.
* \(2) If the timestamp has a timezone, the JDBC type defaults to
TIMESTAMP_WITH_TIMEZONE. If the timestamp has no timezone,
technically there is not a correct conversion from Arrow value to
JDBC value, because a JDBC Timestamp is in UTC, and we have no
timezone information.
6 changes: 6 additions & 0 deletions java/adapter/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.arrow.adapter.jdbc.binder.ColumnBinder;
import org.apache.arrow.util.Preconditions;
import org.apache.arrow.vector.VectorSchemaRoot;

/**
* A binder binds JDBC prepared statement parameters to rows of Arrow data from a VectorSchemaRoot.
*/
public class JdbcParameterBinder {
private final PreparedStatement statement;
private final VectorSchemaRoot root;
private final ColumnBinder[] binders;
private final int[] parameterIndices;
private int nextRowIndex;

JdbcParameterBinder(
final PreparedStatement statement,
final VectorSchemaRoot root,
final ColumnBinder[] binders,
int[] parameterIndices) {
this.statement = statement;
this.root = root;
this.binders = binders;
this.parameterIndices = parameterIndices;
this.nextRowIndex = 0;
}

/**
* Initialize a binder with a builder.
*
* @param statement The statement to bind to. The binder does not maintain ownership of the statement.
* @param root The root to pull data from. The binder does not maintain ownership of the root.
*/
public static Builder builder(final PreparedStatement statement, final VectorSchemaRoot root) {
return new Builder(statement, root);
}

/** Reset the binder (so the root can be updated with new data). */
public void reset() {
nextRowIndex = 0;
}

/**
* Bind the next row to the statement.
*
* @return true if a row was bound, false if rows were exhausted
*/
public boolean next() throws SQLException {
if (nextRowIndex >= root.getRowCount()) {
return false;
}
for (int i = 0; i < parameterIndices.length; i++) {
final int parameterIndex = parameterIndices[i];
binders[i].bind(statement, parameterIndex, nextRowIndex);
}
nextRowIndex++;
return true;
}

/**
* A builder for a {@link JdbcParameterBinder}.
*/
public static class Builder {
private final PreparedStatement statement;
private final VectorSchemaRoot root;
private final Map<Integer, ColumnBinder> bindings;

Builder(PreparedStatement statement, VectorSchemaRoot root) {
this.statement = statement;
this.root = root;
this.bindings = new HashMap<>();
}

/** Bind each column to the corresponding parameter in order. */
public Builder bindAll() {
for (int i = 0; i < root.getFieldVectors().size(); i++) {
bind(/*parameterIndex=*/ i + 1, /*columnIndex=*/ i);
}
return this;
}

/** Bind the given parameter to the given column using the default binder. */
public Builder bind(int parameterIndex, int columnIndex) {
return bind(
parameterIndex,
ColumnBinder.forVector(root.getVector(columnIndex)));
}

/** Bind the given parameter using the given binder. */
public Builder bind(int parameterIndex, ColumnBinder binder) {
Preconditions.checkArgument(
parameterIndex > 0, "parameterIndex %d must be positive", parameterIndex);
bindings.put(parameterIndex, binder);
return this;
}

/** Build the binder. */
public JdbcParameterBinder build() {
ColumnBinder[] binders = new ColumnBinder[bindings.size()];
int[] parameterIndices = new int[bindings.size()];
int index = 0;
for (Map.Entry<Integer, ColumnBinder> entry : bindings.entrySet()) {
binders[index] = entry.getValue();
parameterIndices[index] = entry.getKey();
index++;
}
return new JdbcParameterBinder(statement, root, binders, parameterIndices);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.binder;

import org.apache.arrow.vector.FieldVector;

/**
* Base class for ColumnBinder implementations.
* @param <V> The concrete FieldVector subtype.
*/
public abstract class BaseColumnBinder<V extends FieldVector> implements ColumnBinder {
protected V vector;
protected int jdbcType;

public BaseColumnBinder(V vector, int jdbcType) {
this.vector = vector;
this.jdbcType = jdbcType;
}

@Override
public int getJdbcType() {
return jdbcType;
}

@Override
public V getVector() {
return vector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.binder;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.arrow.vector.BigIntVector;

/** A column binder for 8-bit integers. */
public class BigIntBinder extends BaseColumnBinder<BigIntVector> {
public BigIntBinder(BigIntVector vector) {
this(vector, Types.BIGINT);
}

public BigIntBinder(BigIntVector vector, int jdbcType) {
super(vector, jdbcType);
}

@Override
public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException {
final long value = vector.getDataBuffer().getLong((long) rowIndex * BigIntVector.TYPE_WIDTH);
statement.setLong(parameterIndex, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.binder;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.arrow.vector.BitVector;

/** A column binder for booleans. */
public class BitBinder extends BaseColumnBinder<BitVector> {
public BitBinder(BitVector vector) {
this(vector, Types.BOOLEAN);
}

public BitBinder(BitVector vector, int jdbcType) {
super(vector, jdbcType);
}

@Override
public void bind(PreparedStatement statement, int parameterIndex, int rowIndex) throws SQLException {
// See BitVector#getBit
final int byteIndex = rowIndex >> 3;
final byte b = vector.getDataBuffer().getByte(byteIndex);
final int bitIndex = rowIndex & 7;
final int value = (b >> bitIndex) & 0x01;
statement.setBoolean(parameterIndex, value != 0);
}
}
Loading

0 comments on commit 7adaca4

Please sign in to comment.