Skip to content

Commit da77cbe

Browse files
author
Mike Pigott
committed
Creating a configuration class for the JDBC-to-Arrow converter.
1 parent a667fca commit da77cbe

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public static VectorSchemaRoot sqlToArrow(Connection connection, String query, B
8989
Preconditions.checkArgument(query != null && query.length() > 0, "SQL query can not be null or empty");
9090
Preconditions.checkNotNull(allocator, "Memory allocator object can not be null");
9191

92-
return sqlToArrow(connection, query, allocator, Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT));
92+
JdbcToArrowConfig config =
93+
new JdbcToArrowConfig(allocator, Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT));
94+
return sqlToArrow(connection, query, config);
9395
}
9496

9597
/**
@@ -115,8 +117,18 @@ public static VectorSchemaRoot sqlToArrow(
115117
Preconditions.checkNotNull(allocator, "Memory allocator object can not be null");
116118
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
117119

120+
return sqlToArrow(connection, query, new JdbcToArrowConfig(allocator, calendar));
121+
}
122+
123+
public static VectorSchemaRoot sqlToArrow(Connection connection, String query, JdbcToArrowConfig config)
124+
throws SQLException, IOException {
125+
Preconditions.checkNotNull(connection, "JDBC connection object can not be null");
126+
Preconditions.checkArgument(query != null && query.length() > 0, "SQL query can not be null or empty");
127+
Preconditions.checkNotNull(config, "The configuration cannot be null");
128+
Preconditions.checkArgument(config.isValid(), "The configuration must be valid");
129+
118130
try (Statement stmt = connection.createStatement()) {
119-
return sqlToArrow(stmt.executeQuery(query), allocator, calendar);
131+
return sqlToArrow(stmt.executeQuery(query), config);
120132
}
121133
}
122134

@@ -147,7 +159,9 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, BaseAllocator all
147159
Preconditions.checkNotNull(resultSet, "JDBC ResultSet object can not be null");
148160
Preconditions.checkNotNull(allocator, "Memory Allocator object can not be null");
149161

150-
return sqlToArrow(resultSet, allocator, Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT));
162+
JdbcToArrowConfig config =
163+
new JdbcToArrowConfig(allocator, Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT));
164+
return sqlToArrow(resultSet, config);
151165
}
152166

153167
/**
@@ -162,10 +176,7 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, Calendar calendar
162176
Preconditions.checkNotNull(resultSet, "JDBC ResultSet object can not be null");
163177
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
164178

165-
RootAllocator rootAllocator = new RootAllocator(Integer.MAX_VALUE);
166-
VectorSchemaRoot root = sqlToArrow(resultSet, rootAllocator, calendar);
167-
168-
return root;
179+
return sqlToArrow(resultSet, new JdbcToArrowConfig(new RootAllocator(Integer.MAX_VALUE), calendar));
169180
}
170181

171182
/**
@@ -183,9 +194,18 @@ public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, BaseAllocator all
183194
Preconditions.checkNotNull(allocator, "Memory Allocator object can not be null");
184195
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
185196

197+
return sqlToArrow(resultSet, new JdbcToArrowConfig(allocator, calendar));
198+
}
199+
200+
public static VectorSchemaRoot sqlToArrow(ResultSet resultSet, JdbcToArrowConfig config)
201+
throws SQLException, IOException {
202+
Preconditions.checkNotNull(resultSet, "JDBC ResultSet object can not be null");
203+
Preconditions.checkNotNull(config, "The configuration cannot be null");
204+
Preconditions.checkArgument(config.isValid(), "The configuration must be valid");
205+
186206
VectorSchemaRoot root = VectorSchemaRoot.create(
187-
JdbcToArrowUtils.jdbcToArrowSchema(resultSet.getMetaData(), calendar), allocator);
188-
JdbcToArrowUtils.jdbcToArrowVectors(resultSet, root, calendar);
207+
JdbcToArrowUtils.jdbcToArrowSchema(resultSet.getMetaData(), config.getCalendar()), config.getAllocator());
208+
JdbcToArrowUtils.jdbcToArrowVectors(resultSet, root, config.getCalendar());
189209
return root;
190210
}
191211
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 java.util.Calendar;
21+
22+
import org.apache.arrow.memory.BaseAllocator;
23+
24+
import com.google.common.base.Preconditions;
25+
26+
/**
27+
* This class configures the JDBC-to-Arrow conversion process.
28+
*/
29+
public final class JdbcToArrowConfig {
30+
private Calendar calendar;
31+
private BaseAllocator allocator;
32+
33+
public JdbcToArrowConfig(BaseAllocator allocator, Calendar calendar) {
34+
Preconditions.checkNotNull(allocator, "Memory allocator cannot be null");
35+
Preconditions.checkNotNull(calendar, "Calendar object can not be null");
36+
37+
this.allocator = allocator;
38+
this.calendar = calendar;
39+
}
40+
41+
/**
42+
* The calendar to use when defining Arrow Timestamp fields
43+
* and retrieving time-based fields from the database.
44+
* @return the calendar.
45+
*/
46+
public Calendar getCalendar() {
47+
return calendar;
48+
}
49+
50+
/**
51+
* @param calendar the calendar to set.
52+
*/
53+
public void setCalendar(Calendar calendar) {
54+
this.calendar = calendar;
55+
}
56+
57+
/**
58+
* The Arrow memory allocator.
59+
* @return the allocator.
60+
*/
61+
public BaseAllocator getAllocator() {
62+
return allocator;
63+
}
64+
65+
/**
66+
* @param allocator the allocator to set.
67+
*/
68+
public void setAllocator(BaseAllocator allocator) {
69+
this.allocator = allocator;
70+
}
71+
72+
public boolean isValid() {
73+
return (calendar != null) || (allocator != null);
74+
}
75+
}

0 commit comments

Comments
 (0)