Skip to content

Commit 7bb437d

Browse files
jduoiurysalinovfraga
authored andcommitted
ARROW-7744: [Java][FlightRPC] JDBC Driver for Arrow Flight SQL (apache#13800)
Lead-authored-by: James Duong <duong.james@gmail.com> Co-authored-by: iurysalino <iurysalino@gmail.com> Co-authored-by: Vinicius Fraga <62815192+vfraga@users.noreply.github.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent 14fc642 commit 7bb437d

File tree

126 files changed

+24041
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+24041
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ install_manifest.txt
2323
target/
2424
?/
2525
!/c/
26+
27+
# Generated properties file
28+
flight/flight-sql-jdbc-driver/src/main/resources/properties/flight.properties

dev/checkstyle/suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@
3939
<!-- Suppress certain checks requiring many code changes, that add little benefit -->
4040
<suppress checks="NoFinalizer|OverloadMethodsDeclarationOrder|VariableDeclarationUsageDistance" files=".*" />
4141

42+
<!-- No license header in generated file -->
43+
<suppress checks="header" files="flight.properties"/>
4244
</suppressions>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
3+
license agreements. See the NOTICE file distributed with this work for additional
4+
information regarding copyright ownership. The ASF licenses this file to
5+
You under the Apache License, Version 2.0 (the "License"); you may not use
6+
this file except in compliance with the License. You may obtain a copy of
7+
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
8+
by applicable law or agreed to in writing, software distributed under the
9+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
10+
OF ANY KIND, either express or implied. See the License for the specific
11+
language governing permissions and limitations under the License. -->
12+
13+
<FindBugsFilter>
14+
<!-- These elements are supposed to be mutable -->
15+
<Match>
16+
<Package name="~org\.apache\.arrow\.driver\.jdbc\.accessor\.impl.*"/>
17+
<Bug pattern="EI_EXPOSE_REP2"/>
18+
</Match>
19+
<Match>
20+
<Class name="org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler"/>
21+
<Bug pattern="EI_EXPOSE_REP2"/>
22+
</Match>
23+
<Match>
24+
<Class name="org.apache.arrow.driver.jdbc.utils.ConnectionWrapper"/>
25+
<Bug pattern="EI_EXPOSE_REP2"/>
26+
</Match>
27+
<Match>
28+
<Class name="org.apache.arrow.driver.jdbc.ArrowFlightJdbcDataSource"/>
29+
<Bug pattern="EI_EXPOSE_REP2"/>
30+
</Match>
31+
<Match>
32+
<Class name="org.apache.arrow.driver.jdbc.ArrowFlightJdbcCursor"/>
33+
<Bug pattern="EI_EXPOSE_REP2"/>
34+
</Match>
35+
36+
<Match>
37+
<Class name="org.apache.arrow.driver.jdbc.ArrowFlightJdbcDataSource"/>
38+
<Bug pattern="EI_EXPOSE_REP"/>
39+
</Match>
40+
</FindBugsFilter>

flight/flight-sql-jdbc-driver/pom.xml

Lines changed: 375 additions & 0 deletions
Large diffs are not rendered by default.

flight/flight-sql-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/ArrowDatabaseMetadata.java

Lines changed: 1218 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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.driver.jdbc;
19+
20+
import static org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.replaceSemiColons;
21+
22+
import java.sql.SQLException;
23+
import java.util.Properties;
24+
import java.util.concurrent.ExecutorService;
25+
import java.util.concurrent.Executors;
26+
27+
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
28+
import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl;
29+
import org.apache.arrow.flight.FlightClient;
30+
import org.apache.arrow.memory.BufferAllocator;
31+
import org.apache.arrow.util.AutoCloseables;
32+
import org.apache.arrow.util.Preconditions;
33+
import org.apache.calcite.avatica.AvaticaConnection;
34+
import org.apache.calcite.avatica.AvaticaFactory;
35+
36+
import io.netty.util.concurrent.DefaultThreadFactory;
37+
38+
/**
39+
* Connection to the Arrow Flight server.
40+
*/
41+
public final class ArrowFlightConnection extends AvaticaConnection {
42+
43+
private final BufferAllocator allocator;
44+
private final ArrowFlightSqlClientHandler clientHandler;
45+
private final ArrowFlightConnectionConfigImpl config;
46+
private ExecutorService executorService;
47+
48+
/**
49+
* Creates a new {@link ArrowFlightConnection}.
50+
*
51+
* @param driver the {@link ArrowFlightJdbcDriver} to use.
52+
* @param factory the {@link AvaticaFactory} to use.
53+
* @param url the URL to use.
54+
* @param properties the {@link Properties} to use.
55+
* @param config the {@link ArrowFlightConnectionConfigImpl} to use.
56+
* @param allocator the {@link BufferAllocator} to use.
57+
* @param clientHandler the {@link ArrowFlightSqlClientHandler} to use.
58+
*/
59+
private ArrowFlightConnection(final ArrowFlightJdbcDriver driver, final AvaticaFactory factory,
60+
final String url, final Properties properties,
61+
final ArrowFlightConnectionConfigImpl config,
62+
final BufferAllocator allocator,
63+
final ArrowFlightSqlClientHandler clientHandler) {
64+
super(driver, factory, url, properties);
65+
this.config = Preconditions.checkNotNull(config, "Config cannot be null.");
66+
this.allocator = Preconditions.checkNotNull(allocator, "Allocator cannot be null.");
67+
this.clientHandler = Preconditions.checkNotNull(clientHandler, "Handler cannot be null.");
68+
}
69+
70+
/**
71+
* Creates a new {@link ArrowFlightConnection} to a {@link FlightClient}.
72+
*
73+
* @param driver the {@link ArrowFlightJdbcDriver} to use.
74+
* @param factory the {@link AvaticaFactory} to use.
75+
* @param url the URL to establish the connection to.
76+
* @param properties the {@link Properties} to use for this session.
77+
* @param allocator the {@link BufferAllocator} to use.
78+
* @return a new {@link ArrowFlightConnection}.
79+
* @throws SQLException on error.
80+
*/
81+
static ArrowFlightConnection createNewConnection(final ArrowFlightJdbcDriver driver,
82+
final AvaticaFactory factory,
83+
String url, final Properties properties,
84+
final BufferAllocator allocator)
85+
throws SQLException {
86+
url = replaceSemiColons(url);
87+
final ArrowFlightConnectionConfigImpl config = new ArrowFlightConnectionConfigImpl(properties);
88+
final ArrowFlightSqlClientHandler clientHandler = createNewClientHandler(config, allocator);
89+
return new ArrowFlightConnection(driver, factory, url, properties, config, allocator, clientHandler);
90+
}
91+
92+
private static ArrowFlightSqlClientHandler createNewClientHandler(
93+
final ArrowFlightConnectionConfigImpl config,
94+
final BufferAllocator allocator) throws SQLException {
95+
try {
96+
return new ArrowFlightSqlClientHandler.Builder()
97+
.withHost(config.getHost())
98+
.withPort(config.getPort())
99+
.withUsername(config.getUser())
100+
.withPassword(config.getPassword())
101+
.withTrustStorePath(config.getTrustStorePath())
102+
.withTrustStorePassword(config.getTrustStorePassword())
103+
.withSystemTrustStore(config.useSystemTrustStore())
104+
.withBufferAllocator(allocator)
105+
.withEncryption(config.useEncryption())
106+
.withDisableCertificateVerification(config.getDisableCertificateVerification())
107+
.withToken(config.getToken())
108+
.withCallOptions(config.toCallOption())
109+
.build();
110+
} catch (final SQLException e) {
111+
try {
112+
allocator.close();
113+
} catch (final Exception allocatorCloseEx) {
114+
e.addSuppressed(allocatorCloseEx);
115+
}
116+
throw e;
117+
}
118+
}
119+
120+
void reset() throws SQLException {
121+
// Clean up any open Statements
122+
try {
123+
AutoCloseables.close(statementMap.values());
124+
} catch (final Exception e) {
125+
throw AvaticaConnection.HELPER.createException(e.getMessage(), e);
126+
}
127+
128+
statementMap.clear();
129+
130+
// Reset Holdability
131+
this.setHoldability(this.metaData.getResultSetHoldability());
132+
133+
// Reset Meta
134+
((ArrowFlightMetaImpl) this.meta).setDefaultConnectionProperties();
135+
}
136+
137+
/**
138+
* Gets the client {@link #clientHandler} backing this connection.
139+
*
140+
* @return the handler.
141+
*/
142+
ArrowFlightSqlClientHandler getClientHandler() throws SQLException {
143+
return clientHandler;
144+
}
145+
146+
/**
147+
* Gets the {@link ExecutorService} of this connection.
148+
*
149+
* @return the {@link #executorService}.
150+
*/
151+
synchronized ExecutorService getExecutorService() {
152+
return executorService = executorService == null ?
153+
Executors.newFixedThreadPool(config.threadPoolSize(),
154+
new DefaultThreadFactory(getClass().getSimpleName())) :
155+
executorService;
156+
}
157+
158+
@Override
159+
public Properties getClientInfo() {
160+
final Properties copy = new Properties();
161+
copy.putAll(info);
162+
return copy;
163+
}
164+
165+
@Override
166+
public void close() throws SQLException {
167+
if (executorService != null) {
168+
executorService.shutdown();
169+
}
170+
171+
try {
172+
AutoCloseables.close(clientHandler);
173+
allocator.getChildAllocators().forEach(AutoCloseables::closeNoChecked);
174+
AutoCloseables.close(allocator);
175+
176+
super.close();
177+
} catch (final Exception e) {
178+
throw AvaticaConnection.HELPER.createException(e.getMessage(), e);
179+
}
180+
}
181+
182+
BufferAllocator getBufferAllocator() {
183+
return allocator;
184+
}
185+
186+
public ArrowFlightMetaImpl getMeta() {
187+
return (ArrowFlightMetaImpl) this.meta;
188+
}
189+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.driver.jdbc;
19+
20+
import java.sql.SQLException;
21+
import java.sql.Statement;
22+
23+
import org.apache.arrow.flight.FlightInfo;
24+
25+
/**
26+
* A {@link Statement} that deals with {@link FlightInfo}.
27+
*/
28+
public interface ArrowFlightInfoStatement extends Statement {
29+
30+
@Override
31+
ArrowFlightConnection getConnection() throws SQLException;
32+
33+
/**
34+
* Executes the query this {@link Statement} is holding.
35+
*
36+
* @return the {@link FlightInfo} for the results.
37+
* @throws SQLException on error.
38+
*/
39+
FlightInfo executeFlightInfoQuery() throws SQLException;
40+
}

0 commit comments

Comments
 (0)