-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
ARROW-17004: [Java] Add utility to bind Arrow data to JDBC parameters #13589
Changes from 6 commits
6322a4b
92415db
b13bc00
d28297e
7e20ab8
80551c6
0e80e0e
52ae3bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this intentionally package private instead of private? Maybe add a comment on the relationship between the last two parameters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, changed it to private, and added some docstrings + an explicit Preconditions check for the last two parameters. |
||
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 {@link VectorSchemaRoot} to pull data from. The binder does not maintain ownership | ||
* of the vector schema 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 final V vector; | ||
protected final 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a type other than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In principle, I wanted to allow things like binding an Int64 vector to an Int field, maybe that is too much flexibility though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Thanks for the clarification. |
||
} | ||
|
||
@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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what would happen when a timezone is absent, the program would thrown an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll just call
setTimestamp(int, Timestamp)
instead ofsetTimestamp(int, Timestamp, Calendar)
, I'll update the docThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification.