Skip to content

Commit 1da5ba8

Browse files
Merge pull request #43 from rafael-telles/fix-all-attributes-as-headers
Fix attributes as headers bug and token value parsing error
2 parents c9cdcac + d66a60a commit 1da5ba8

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,19 @@
2727
import java.net.URI;
2828
import java.nio.charset.StandardCharsets;
2929
import java.sql.SQLException;
30-
import java.util.List;
3130
import java.util.Map;
3231
import java.util.Objects;
3332
import java.util.Properties;
3433

3534
import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty;
35+
import org.apache.arrow.driver.jdbc.utils.UrlParser;
3636
import org.apache.arrow.flight.FlightRuntimeException;
3737
import org.apache.arrow.memory.RootAllocator;
3838
import org.apache.arrow.util.Preconditions;
3939
import org.apache.calcite.avatica.AvaticaConnection;
4040
import org.apache.calcite.avatica.DriverVersion;
4141
import org.apache.calcite.avatica.Meta;
4242
import org.apache.calcite.avatica.UnregisteredDriver;
43-
import org.apache.calcite.avatica.org.apache.http.NameValuePair;
44-
import org.apache.calcite.avatica.org.apache.http.client.utils.URLEncodedUtils;
4543

4644
/**
4745
* JDBC driver for querying data from an Apache Arrow Flight server.
@@ -248,8 +246,8 @@ private Map<Object, Object> getUrlsArgs(String url)
248246

249247
final String extraParams = uri.getRawQuery(); // optional params
250248

251-
final List<NameValuePair> keyValuePairs = URLEncodedUtils.parse(extraParams, StandardCharsets.UTF_8);
252-
keyValuePairs.forEach(p -> resultMap.put(p.getName(), p.getValue()));
249+
final Map<String, String> keyValuePairs = UrlParser.parse(extraParams, "&");
250+
resultMap.putAll(keyValuePairs);
253251

254252
return resultMap;
255253
}

java/flight/flight-jdbc-driver/src/main/java/org/apache/arrow/driver/jdbc/utils/ArrowFlightConnectionConfigImpl.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import static java.lang.String.format;
2121

22+
import java.util.Arrays;
23+
import java.util.HashMap;
24+
import java.util.Map;
2225
import java.util.Objects;
2326
import java.util.Properties;
2427

@@ -137,10 +140,30 @@ public int threadPoolSize() {
137140
*/
138141
public CallOption toCallOption() {
139142
final CallHeaders headers = new FlightCallHeaders();
140-
properties.forEach((key, val) -> headers.insert(key.toString(), val.toString()));
143+
Map<String, String> headerAttributes = getHeaderAttributes();
144+
headerAttributes.forEach(headers::insert);
141145
return new HeaderCallOption(headers);
142146
}
143147

148+
/**
149+
* Gets which properties should be added as headers.
150+
*
151+
* @return {@link Map}
152+
*/
153+
public Map<String, String> getHeaderAttributes() {
154+
Map<String, String> headers = new HashMap<>();
155+
ArrowFlightConnectionProperty[] builtInProperties = ArrowFlightConnectionProperty.values();
156+
properties.forEach(
157+
(key, val) -> {
158+
// For built-in properties before adding new headers
159+
if (Arrays.stream(builtInProperties)
160+
.noneMatch(builtInProperty -> builtInProperty.camelName.equalsIgnoreCase(key.toString()))) {
161+
headers.put(key.toString(), val.toString());
162+
}
163+
});
164+
return headers;
165+
}
166+
144167
/**
145168
* Custom {@link ConnectionProperty} for the {@link ArrowFlightConnectionConfigImpl}.
146169
*/
@@ -248,6 +271,7 @@ public Class<?> valueClass() {
248271

249272
/**
250273
* Replaces the semicolons in the URL to the proper format.
274+
*
251275
* @param url the current connection string
252276
* @return the formatted url
253277
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.utils;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
/**
24+
* URL Parser for extracting key values from a connection string.
25+
*/
26+
public class UrlParser {
27+
/**
28+
* Parse a url key value parameters.
29+
*
30+
* @param url {@link String}
31+
* @return {@link Map}
32+
*/
33+
public static Map<String, String> parse(String url, String separator) {
34+
Map<String, String> resultMap = new HashMap<>();
35+
String[] keyValues = url.split(separator);
36+
37+
for (String keyValue : keyValues) {
38+
int separatorKey = keyValue.indexOf("="); // Find the first equal sign to split key and value.
39+
String key = keyValue.substring(0, separatorKey);
40+
String value = "";
41+
if (!keyValue.endsWith("=")) { // Avoid crashes for empty values.
42+
value = keyValue.substring(separatorKey + 1);
43+
}
44+
resultMap.put(key, value);
45+
}
46+
47+
return resultMap;
48+
}
49+
}

0 commit comments

Comments
 (0)