Skip to content

Commit ef70af6

Browse files
authored
[Java] Add basic Flight SQL driver (#47)
* [Java] Add basic Flight SQL driver * [Java] Add partition descriptor support * [Java] Properly handle FlightEndpoint.locations * [Java] Refactor handling of FlightEndpoint * [Java] Consolidate catches
1 parent 97c032d commit ef70af6

File tree

33 files changed

+1774
-81
lines changed

33 files changed

+1774
-81
lines changed

java/core/src/main/java/org/apache/arrow/adbc/core/AdbcDriver.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
/** A handle to an ADBC database driver. */
2323
public interface AdbcDriver {
24+
/** The standard parameter name for a connection URL (type String). */
25+
String PARAM_URL = "adbc.url";
26+
/** The standard parameter name for SQL quirks configuration (type SqlQuirks). */
27+
String PARAM_SQL_QUIRKS = "adbc.sql.quirks";
28+
2429
/**
2530
* Open a database via this driver.
2631
*

java/core/src/main/java/org/apache/arrow/adbc/core/AdbcException.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public static AdbcException invalidArgument(String message) {
4848
return new AdbcException(message, /*cause*/ null, AdbcStatusCode.INVALID_ARGUMENT, null, 0);
4949
}
5050

51+
/** Create a new exception with code {@link AdbcStatusCode#IO}. */
52+
public static AdbcException io(String message) {
53+
return new AdbcException(message, /*cause*/ null, AdbcStatusCode.IO, null, 0);
54+
}
55+
5156
/** Create a new exception with code {@link AdbcStatusCode#INVALID_STATE}. */
5257
public static AdbcException invalidState(String message) {
5358
return new AdbcException(message, /*cause*/ null, AdbcStatusCode.INVALID_STATE, null, 0);
@@ -70,6 +75,13 @@ public int getVendorCode() {
7075
return vendorCode;
7176
}
7277

78+
/**
79+
* Copy this exception with a different cause (a convenience for use with the static factories).
80+
*/
81+
public AdbcException withCause(Throwable cause) {
82+
return new AdbcException(this.getMessage(), cause, status, sqlState, vendorCode);
83+
}
84+
7385
@Override
7486
public String toString() {
7587
return "AdbcException{"

java/core/src/main/java/org/apache/arrow/adbc/core/PartitionDescriptor.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121

2222
/** An opaque descriptor for a part of a potentially distributed or partitioned result set. */
2323
public final class PartitionDescriptor {
24-
private final String friendlyName;
2524
private final ByteBuffer descriptor;
2625

27-
public PartitionDescriptor(final String friendlyName, final ByteBuffer descriptor) {
28-
this.friendlyName = friendlyName;
26+
public PartitionDescriptor(final ByteBuffer descriptor) {
2927
this.descriptor = Objects.requireNonNull(descriptor);
3028
}
3129

@@ -42,23 +40,16 @@ public boolean equals(Object o) {
4240
return false;
4341
}
4442
PartitionDescriptor that = (PartitionDescriptor) o;
45-
return Objects.equals(friendlyName, that.friendlyName)
46-
&& getDescriptor().equals(that.getDescriptor());
43+
return descriptor.equals(that.descriptor);
4744
}
4845

4946
@Override
5047
public int hashCode() {
51-
return Objects.hash(friendlyName, getDescriptor());
48+
return Objects.hash(descriptor);
5249
}
5350

5451
@Override
5552
public String toString() {
56-
return "PartitionDescriptor{"
57-
+ "friendlyName='"
58-
+ friendlyName
59-
+ '\''
60-
+ ", descriptor="
61-
+ descriptor
62-
+ '}';
53+
return "PartitionDescriptor{" + "descriptor=" + descriptor + '}';
6354
}
6455
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0"?>
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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
13+
<modelVersion>4.0.0</modelVersion>
14+
<parent>
15+
<artifactId>arrow-adbc-java-root</artifactId>
16+
<groupId>org.apache.arrow.adbc</groupId>
17+
<version>9.0.0-SNAPSHOT</version>
18+
<relativePath>../../pom.xml</relativePath>
19+
</parent>
20+
21+
<artifactId>adbc-driver-flight-sql-validation</artifactId>
22+
<packaging>jar</packaging>
23+
<name>Arrow ADBC Driver Flight SQL Validation</name>
24+
<description>Tests validating the Flight SQL driver.</description>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.apache.arrow.adbc</groupId>
29+
<artifactId>adbc-core</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.apache.arrow.adbc</groupId>
34+
<artifactId>adbc-driver-flight-sql</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<!-- Testing -->
39+
<dependency>
40+
<groupId>org.assertj</groupId>
41+
<artifactId>assertj-core</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.junit.jupiter</groupId>
46+
<artifactId>junit-jupiter</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.apache.arrow.adbc</groupId>
51+
<artifactId>adbc-driver-validation</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.adbc.driver.flightsql;
19+
20+
import org.apache.arrow.adbc.driver.testsuite.AbstractConnectionMetadataTest;
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.Disabled;
23+
24+
public class FlightSqlConnectionMetadataTest extends AbstractConnectionMetadataTest {
25+
@BeforeAll
26+
public static void beforeAll() {
27+
quirks = new FlightSqlQuirks();
28+
}
29+
30+
@Override
31+
@Disabled("Not yet implemented")
32+
public void getObjectsColumns() throws Exception {}
33+
34+
@Override
35+
@Disabled("Not yet implemented")
36+
public void getObjectsCatalogs() throws Exception {}
37+
38+
@Override
39+
@Disabled("Not yet implemented")
40+
public void getObjectsDbSchemas() throws Exception {
41+
super.getObjectsDbSchemas();
42+
}
43+
44+
@Override
45+
@Disabled("Not yet implemented")
46+
public void getObjectsTables() throws Exception {
47+
super.getObjectsTables();
48+
}
49+
50+
@Override
51+
@Disabled("Not yet implemented")
52+
public void getTableSchema() throws Exception {
53+
super.getTableSchema();
54+
}
55+
56+
@Override
57+
@Disabled("Not yet implemented")
58+
public void getTableTypes() throws Exception {
59+
super.getTableTypes();
60+
}
61+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.adbc.driver.flightsql;
19+
20+
import org.apache.arrow.adbc.driver.testsuite.AbstractConnectionTest;
21+
import org.junit.jupiter.api.BeforeAll;
22+
23+
public class FlightSqlConnectionTest extends AbstractConnectionTest {
24+
@BeforeAll
25+
public static void beforeAll() {
26+
quirks = new FlightSqlQuirks();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.adbc.driver.flightsql;
19+
20+
import org.apache.arrow.adbc.driver.testsuite.AbstractPartitionDescriptorTest;
21+
import org.junit.jupiter.api.BeforeAll;
22+
23+
class FlightSqlPartitionDescriptorTest extends AbstractPartitionDescriptorTest {
24+
@BeforeAll
25+
public static void beforeAll() {
26+
quirks = new FlightSqlQuirks();
27+
}
28+
}
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.adbc.driver.flightsql;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import org.apache.arrow.adbc.core.AdbcDatabase;
23+
import org.apache.arrow.adbc.core.AdbcDriver;
24+
import org.apache.arrow.adbc.core.AdbcException;
25+
import org.apache.arrow.adbc.driver.testsuite.SqlValidationQuirks;
26+
import org.apache.arrow.flight.FlightClient;
27+
import org.apache.arrow.flight.FlightRuntimeException;
28+
import org.apache.arrow.flight.Location;
29+
import org.apache.arrow.flight.sql.FlightSqlClient;
30+
import org.apache.arrow.memory.BufferAllocator;
31+
import org.apache.arrow.memory.RootAllocator;
32+
import org.junit.jupiter.api.Assumptions;
33+
34+
public class FlightSqlQuirks extends SqlValidationQuirks {
35+
static final String FLIGHT_SQL_LOCATION_ENV_VAR = "ADBC_FLIGHT_SQL_LOCATION";
36+
37+
static String getFlightLocation() {
38+
final String location = System.getenv(FLIGHT_SQL_LOCATION_ENV_VAR);
39+
Assumptions.assumeFalse(
40+
location == null || location.isEmpty(),
41+
"Flight SQL server not found, set " + FLIGHT_SQL_LOCATION_ENV_VAR);
42+
return location;
43+
}
44+
45+
@Override
46+
public AdbcDatabase initDatabase() throws AdbcException {
47+
String url = getFlightLocation();
48+
49+
final Map<String, Object> parameters = new HashMap<>();
50+
parameters.put(AdbcDriver.PARAM_URL, url);
51+
return FlightSqlDriver.INSTANCE.open(parameters);
52+
}
53+
54+
@Override
55+
public void cleanupTable(String name) throws Exception {
56+
try (final BufferAllocator allocator = new RootAllocator();
57+
final FlightSqlClient client =
58+
new FlightSqlClient(
59+
FlightClient.builder(allocator, new Location(getFlightLocation())).build())) {
60+
client.executeUpdate("DROP TABLE " + name);
61+
} catch (FlightRuntimeException e) {
62+
// Ignored
63+
}
64+
}
65+
66+
@Override
67+
public String caseFoldTableName(String name) {
68+
return name.toUpperCase();
69+
}
70+
71+
@Override
72+
public String caseFoldColumnName(String name) {
73+
return name.toUpperCase();
74+
}
75+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.adbc.driver.flightsql;
19+
20+
import org.apache.arrow.adbc.driver.testsuite.AbstractStatementTest;
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.Disabled;
23+
24+
class FlightSqlStatementTest extends AbstractStatementTest {
25+
@BeforeAll
26+
public static void beforeAll() {
27+
quirks = new FlightSqlQuirks();
28+
}
29+
30+
@Override
31+
@Disabled("Requires spec clarification")
32+
public void prepareQueryWithParameters() {}
33+
}

0 commit comments

Comments
 (0)