Skip to content

Commit

Permalink
ARROW-3965: JdbcToArrowConfigBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Pigott committed Jan 30, 2019
1 parent d7ca982 commit d6c64a7
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public static VectorSchemaRoot sqlToArrow(Connection connection, String query, J
Preconditions.checkNotNull(connection, "JDBC connection object can not be null");
Preconditions.checkArgument(query != null && query.length() > 0, "SQL query can not be null or empty");
Preconditions.checkNotNull(config, "The configuration cannot be null");
Preconditions.checkArgument(config.isValid(), "The configuration must be valid");

try (Statement stmt = connection.createStatement()) {
return sqlToArrow(stmt.executeQuery(query), config);
Expand Down Expand Up @@ -221,7 +220,6 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, JdbcToArrowConfig
throws SQLException, IOException {
Preconditions.checkNotNull(resultSet, "JDBC ResultSet object can not be null");
Preconditions.checkNotNull(config, "The configuration cannot be null");
Preconditions.checkArgument(config.isValid(), "The configuration must be valid");

VectorSchemaRoot root = VectorSchemaRoot.create(
JdbcToArrowUtils.jdbcToArrowSchema(resultSet.getMetaData(), config), config.getAllocator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class JdbcToArrowConfig {
* @param allocator The memory allocator to construct the Arrow vectors with.
* @param calendar The calendar to use when constructing Timestamp fields and reading time-based results.
*/
public JdbcToArrowConfig(BaseAllocator allocator, Calendar calendar) {
JdbcToArrowConfig(BaseAllocator allocator, Calendar calendar) {
Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
Preconditions.checkNotNull(calendar, "Calendar object can not be null");

Expand All @@ -63,49 +63,11 @@ public Calendar getCalendar() {
return calendar;
}

/**
* Sets the {@link Calendar} to use when constructing timestamp fields in the
* Arrow schema, and reading time-based fields from the JDBC <code>ResultSet</code>.
*
* @param calendar the calendar to set.
* @exception NullPointerExeption if <code>calendar</code> is <code>null</code>.
*/
public JdbcToArrowConfig setCalendar(Calendar calendar) {
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
this.calendar = calendar;
return this;
}

/**
* The Arrow memory allocator.
* @return the allocator.
*/
public BaseAllocator getAllocator() {
return allocator;
}

/**
* Sets the memory allocator to use when construting the Arrow vectors from the ResultSet.
*
* @param allocator the allocator to set.
* @exception NullPointerException if <code>allocator</code> is null.
*/
public JdbcToArrowConfig setAllocator(BaseAllocator allocator) {
Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
this.allocator = allocator;
return this;
}

/**
* Whether this configuration is valid. The configuration is valid when:
* <ul>
* <li>A memory allocator is provided.</li>
* <li>A calendar is provided.</li>
* </ul>
*
* @return Whether this configuration is valid.
*/
public boolean isValid() {
return (calendar != null) && (allocator != null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.util.Calendar;

import org.apache.arrow.memory.BaseAllocator;

import com.google.common.base.Preconditions;

public class JdbcToArrowConfigBuilder {
private Calendar calendar;
private BaseAllocator allocator;

public JdbcToArrowConfigBuilder() {
}

public JdbcToArrowConfigBuilder(BaseAllocator allocator, Calendar calendar) {
this();

Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
Preconditions.checkNotNull(calendar, "Calendar object can not be null");

this.allocator = allocator;
this.calendar = calendar;
}

/**
* Sets the memory allocator to use when constructing the Arrow vectors from the ResultSet.
*
* @param allocator the allocator to set.
* @exception NullPointerException if <code>allocator</code> is null.
*/
public JdbcToArrowConfigBuilder setAllocator(BaseAllocator allocator) {
Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
this.allocator = allocator;
return this;
}

/**
* Sets the {@link Calendar} to use when constructing timestamp fields in the
* Arrow schema, and reading time-based fields from the JDBC <code>ResultSet</code>.
*
* @param calendar the calendar to set.
* @exception NullPointerExeption if <code>calendar</code> is <code>null</code>.
*/
public JdbcToArrowConfigBuilder setCalendar(Calendar calendar) {
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
this.calendar = calendar;
return this;
}

public JdbcToArrowConfig build() {
return new JdbcToArrowConfig(allocator, calendar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, Calendar calendar
public static Schema jdbcToArrowSchema(ResultSetMetaData rsmd, JdbcToArrowConfig config) throws SQLException {
Preconditions.checkNotNull(rsmd, "JDBC ResultSetMetaData object can't be null");
Preconditions.checkNotNull(config, "The configuration object must not be null");
Preconditions.checkArgument(config.isValid(), "The configuration object must be valid");

List<Field> fields = new ArrayList<>();
int columnCount = rsmd.getColumnCount();
Expand Down Expand Up @@ -269,7 +268,6 @@ public static void jdbcToArrowVectors(ResultSet rs, VectorSchemaRoot root, JdbcT
Preconditions.checkNotNull(rs, "JDBC ResultSet object can't be null");
Preconditions.checkNotNull(root, "JDBC ResultSet object can't be null");
Preconditions.checkNotNull(config, "JDBC-to-Arrow configuration cannot be null");
Preconditions.checkArgument(config.isValid(), "JDBC-to-Arrow configuration must be valid");

ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,61 @@ public class JdbcToArrowConfigTest {
private static final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT);

@Test(expected = NullPointerException.class)
public void testNullArguments() {
public void testConfigNullArguments() {
new JdbcToArrowConfig(null, null);
}

@Test(expected = NullPointerException.class)
public void testNullCalendar() {
public void testBuilderNullArguments() {
new JdbcToArrowConfigBuilder(null, null);
}

@Test(expected = NullPointerException.class)
public void testConfigNullCalendar() {
new JdbcToArrowConfig(allocator, null);
}

@Test(expected = NullPointerException.class)
public void testNullAllocator() {
public void testBuilderNullCalendar() {
new JdbcToArrowConfigBuilder(allocator, null);
}

@Test(expected = NullPointerException.class)
public void testConfigNullAllocator() {
new JdbcToArrowConfig(null, calendar);
}

@Test(expected = NullPointerException.class)
public void testBuilderNullAllocator() {
new JdbcToArrowConfigBuilder(null, calendar);
}

@Test(expected = NullPointerException.class)
public void testSetNullAllocator() {
JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, calendar);
config.setAllocator(null);
JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar);
builder.setAllocator(null);
}

@Test(expected = NullPointerException.class)
public void testSetNullCalendar() {
JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, calendar);
config.setCalendar(null);
JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar);
builder.setCalendar(null);
}

@Test
public void testConfig() {
JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, calendar);
assertTrue(config.isValid());
JdbcToArrowConfigBuilder builder = new JdbcToArrowConfigBuilder(allocator, calendar);
JdbcToArrowConfig config = builder.build();

assertTrue(allocator == config.getAllocator());
assertTrue(calendar == config.getCalendar());

Calendar newCalendar = Calendar.getInstance();
BaseAllocator newAllocator = new RootAllocator(Integer.SIZE);

config.setAllocator(newAllocator).setCalendar(newCalendar);
builder.setAllocator(newAllocator).setCalendar(newCalendar);
config = builder.build();

assertTrue(config.isValid());
assertTrue(newAllocator == config.getAllocator());
assertTrue(newCalendar == config.getCalendar());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest;
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VarCharVector;
Expand Down Expand Up @@ -119,11 +119,11 @@ public void testJdbcToArroValues() throws SQLException, IOException {
Calendar.getInstance()));
testDataSets(JdbcToArrow.sqlToArrow(
conn.createStatement().executeQuery(table.getQuery()),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
testDataSets(JdbcToArrow.sqlToArrow(
conn,
table.getQuery(),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest;
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
Expand Down Expand Up @@ -145,11 +145,11 @@ public void testJdbcToArroValues() throws SQLException, IOException {
testDataSets(JdbcToArrow.sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()));
testDataSets(JdbcToArrow.sqlToArrow(
conn.createStatement().executeQuery(table.getQuery()),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
testDataSets(JdbcToArrow.sqlToArrow(
conn,
table.getQuery(),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest;
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
Expand Down Expand Up @@ -102,11 +102,11 @@ public void testJdbcToArroValues() throws SQLException, IOException {
testDataSets(JdbcToArrow.sqlToArrow(conn.createStatement().executeQuery(table.getQuery()), Calendar.getInstance()));
testDataSets(JdbcToArrow.sqlToArrow(
conn.createStatement().executeQuery(table.getQuery()),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
testDataSets(JdbcToArrow.sqlToArrow(
conn,
table.getQuery(),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest;
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.BigIntVector;
Expand Down Expand Up @@ -136,11 +136,11 @@ public void testJdbcToArroValues() throws SQLException, IOException {
Calendar.getInstance()));
testDataSets(JdbcToArrow.sqlToArrow(
conn.createStatement().executeQuery(table.getQuery()),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
testDataSets(JdbcToArrow.sqlToArrow(
conn,
table.getQuery(),
new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance())));
new JdbcToArrowConfigBuilder(new RootAllocator(Integer.MAX_VALUE), Calendar.getInstance()).build()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import org.apache.arrow.adapter.jdbc.AbstractJdbcToArrowTest;
import org.apache.arrow.adapter.jdbc.JdbcToArrow;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig;
import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder;
import org.apache.arrow.adapter.jdbc.Table;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.DateMilliVector;
Expand Down Expand Up @@ -108,15 +108,15 @@ public void testJdbcToArroValues() throws SQLException, IOException {
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))));
testDataSets(JdbcToArrow.sqlToArrow(
conn.createStatement().executeQuery(table.getQuery()),
new JdbcToArrowConfig(
new JdbcToArrowConfigBuilder(
new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone())))));
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))).build()));
testDataSets(JdbcToArrow.sqlToArrow(
conn,
table.getQuery(),
new JdbcToArrowConfig(
new JdbcToArrowConfigBuilder(
new RootAllocator(Integer.MAX_VALUE),
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone())))));
Calendar.getInstance(TimeZone.getTimeZone(table.getTimezone()))).build()));
}

/**
Expand Down

0 comments on commit d6c64a7

Please sign in to comment.