Skip to content

Commit b270044

Browse files
author
Mike Pigott
committed
Updated validaton & documentation, and unit tests for the new JdbcToArrowConfig.
1 parent da77cbe commit b270044

File tree

3 files changed

+119
-5
lines changed

3 files changed

+119
-5
lines changed

java/adapter/jdbc/src/main/java/org/apache/arrow/adapter/jdbc/JdbcToArrowConfig.java

+39-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,27 @@
2525

2626
/**
2727
* This class configures the JDBC-to-Arrow conversion process.
28+
* <p>
29+
* The allocator is used to construct the {@link org.apache.arrow.vector.VectorSchemaRoot},
30+
* and the calendar is used to define the time zone of any {@link org.apahe.arrow.vector.pojo.ArrowType.Timestamp}
31+
* fields that are created during the conversion.
32+
* </p>
33+
* <p>
34+
* Neither field may be <code>null</code>.
35+
* </p>
2836
*/
2937
public final class JdbcToArrowConfig {
3038
private Calendar calendar;
3139
private BaseAllocator allocator;
3240

41+
/**
42+
* Constructs a new configuration from the provided allocator and calendar. The <code>allocator</code>
43+
* is used when constructing the Arrow vectors from the ResultSet, and the calendar is used to define
44+
* Arrow Timestamp fields, and to read time-based fields from the JDBC <code>ResultSet</code>.
45+
*
46+
* @param allocator The memory allocator to construct the Arrow vectors with.
47+
* @param calendar The calendar to use when constructing Timestamp fields and reading time-based results.
48+
*/
3349
public JdbcToArrowConfig(BaseAllocator allocator, Calendar calendar) {
3450
Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
3551
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
@@ -48,10 +64,16 @@ public Calendar getCalendar() {
4864
}
4965

5066
/**
67+
* Sets the {@link Calendar} to use when constructing timestamp fields in the
68+
* Arrow schema, and reading time-based fields from the JDBC <code>ResultSet</code>.
69+
*
5170
* @param calendar the calendar to set.
71+
* @exception NullPointerExeption if <code>calendar</code> is <code>null</code>.
5272
*/
53-
public void setCalendar(Calendar calendar) {
73+
public JdbcToArrowConfig setCalendar(Calendar calendar) {
74+
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
5475
this.calendar = calendar;
76+
return this;
5577
}
5678

5779
/**
@@ -63,13 +85,27 @@ public BaseAllocator getAllocator() {
6385
}
6486

6587
/**
88+
* Sets the memory allocator to use when construting the Arrow vectors from the ResultSet.
89+
*
6690
* @param allocator the allocator to set.
91+
* @exception NullPointerException if <code>allocator</code> is null.
6792
*/
68-
public void setAllocator(BaseAllocator allocator) {
93+
public JdbcToArrowConfig setAllocator(BaseAllocator allocator) {
94+
Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
6995
this.allocator = allocator;
96+
return this;
7097
}
7198

99+
/**
100+
* Whether this configuration is valid. The configuration is valid when:
101+
* <ul>
102+
* <li>A memory allocator is provided.</li>
103+
* <li>A calendar is provided.</li>
104+
* </ul>
105+
*
106+
* @return Whether this configuration is valid.
107+
*/
72108
public boolean isValid() {
73-
return (calendar != null) || (allocator != null);
109+
return (calendar != null) && (allocator != null);
74110
}
75111
}

java/adapter/jdbc/src/test/java/org/apache/arrow/adapter/jdbc/AbstractJdbcToArrowTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public abstract class AbstractJdbcToArrowTest {
4545
* @return Table object
4646
* @throws IOException on error
4747
*/
48-
protected static Table getTable(String ymlFilePath, Class clss) throws IOException {
48+
protected static Table getTable(String ymlFilePath, @SuppressWarnings("rawtypes") Class clss) throws IOException {
4949
return new ObjectMapper(new YAMLFactory()).readValue(
5050
clss.getClassLoader().getResourceAsStream(ymlFilePath), Table.class);
5151
}
@@ -94,7 +94,7 @@ public void destroy() throws SQLException {
9494
* @throws ClassNotFoundException on error
9595
* @throws IOException on error
9696
*/
97-
public static Object[][] prepareTestData(String[] testFiles, Class clss)
97+
public static Object[][] prepareTestData(String[] testFiles, @SuppressWarnings("rawtypes") Class clss)
9898
throws SQLException, ClassNotFoundException, IOException {
9999
Object[][] tableArr = new Object[testFiles.length][];
100100
int i = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.arrow.adapter.jdbc;
19+
20+
import static org.junit.Assert.*;
21+
22+
import java.util.Calendar;
23+
import java.util.Locale;
24+
import java.util.TimeZone;
25+
26+
import org.apache.arrow.memory.BaseAllocator;
27+
import org.apache.arrow.memory.RootAllocator;
28+
import org.junit.Test;
29+
30+
public class JdbcToArrowConfigTest {
31+
32+
private static final RootAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
33+
private static final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT);
34+
35+
@Test(expected = NullPointerException.class)
36+
public void testNullArguments() {
37+
new JdbcToArrowConfig(null, null);
38+
}
39+
40+
@Test(expected = NullPointerException.class)
41+
public void testNullCalendar() {
42+
new JdbcToArrowConfig(allocator, null);
43+
}
44+
45+
@Test(expected = NullPointerException.class)
46+
public void testNullAllocator() {
47+
new JdbcToArrowConfig(null, calendar);
48+
}
49+
50+
@Test(expected = NullPointerException.class)
51+
public void testSetNullAllocator() {
52+
JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, calendar);
53+
config.setAllocator(null);
54+
}
55+
56+
@Test(expected = NullPointerException.class)
57+
public void testSetNullCalendar() {
58+
JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, calendar);
59+
config.setCalendar(null);
60+
}
61+
62+
@Test
63+
public void testConfig() {
64+
JdbcToArrowConfig config = new JdbcToArrowConfig(allocator, calendar);
65+
assertTrue(config.isValid());
66+
assertTrue(allocator == config.getAllocator());
67+
assertTrue(calendar == config.getCalendar());
68+
69+
Calendar newCalendar = Calendar.getInstance();
70+
BaseAllocator newAllocator = new RootAllocator(Integer.SIZE);
71+
72+
config.setAllocator(newAllocator).setCalendar(newCalendar);
73+
74+
assertTrue(config.isValid());
75+
assertTrue(newAllocator == config.getAllocator());
76+
assertTrue(newCalendar == config.getCalendar());
77+
}
78+
}

0 commit comments

Comments
 (0)