Skip to content

Commit b0a7b68

Browse files
kushamanaman252252birschick-bq
authored
feat(csharp/src/Drivers/Apache): Add support for Hive ADBC Driver with unit tests (#2540)
1. Added support for the Hive ADBC driver with HTTP transport protocol. 2. Added new unit tests for Hive ADBC driver support. --------- Co-authored-by: Aman Goyal <[email protected]> Co-authored-by: Bruce Irschick <[email protected]>
1 parent 80094ce commit b0a7b68

Some content is hidden

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

44 files changed

+1604
-103
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
19+
{
20+
internal enum HiveServer2AuthType
21+
{
22+
Invalid = 0,
23+
None,
24+
UsernameOnly,
25+
Basic,
26+
Empty = int.MaxValue,
27+
}
28+
29+
internal static class HiveServer2AuthTypeParser
30+
{
31+
internal static bool TryParse(string? authType, out HiveServer2AuthType authTypeValue)
32+
{
33+
switch (authType?.Trim().ToLowerInvariant())
34+
{
35+
case null:
36+
case "":
37+
authTypeValue = HiveServer2AuthType.Empty;
38+
return true;
39+
case HiveServer2AuthTypeConstants.None:
40+
authTypeValue = HiveServer2AuthType.None;
41+
return true;
42+
case HiveServer2AuthTypeConstants.UsernameOnly:
43+
authTypeValue = HiveServer2AuthType.UsernameOnly;
44+
return true;
45+
case HiveServer2AuthTypeConstants.Basic:
46+
authTypeValue = HiveServer2AuthType.Basic;
47+
return true;
48+
default:
49+
authTypeValue = HiveServer2AuthType.Invalid;
50+
return false;
51+
}
52+
}
53+
}
54+
}

csharp/src/Drivers/Apache/Hive2/HiveServer2Connection.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,10 @@ public override IArrowArrayStream GetTableTypes()
609609
TRowSet rowSet = GetRowSetAsync(resp, cancellationToken).Result;
610610
StringArray tableTypes = rowSet.Columns[0].StringVal.Values;
611611

612+
HashSet<string> distinctTableTypes = new HashSet<string>(tableTypes);
613+
612614
StringArray.Builder tableTypesBuilder = new StringArray.Builder();
613-
tableTypesBuilder.AppendRange(tableTypes);
615+
tableTypesBuilder.AppendRange(distinctTableTypes);
614616

615617
IArrowArray[] dataArrays = new IArrowArray[]
616618
{
@@ -722,11 +724,19 @@ protected internal string GetProductVersion()
722724
return fileVersionInfo.ProductVersion ?? GetProductVersionDefault();
723725
}
724726

725-
protected static Uri GetBaseAddress(string? uri, string? hostName, string? path, string? port)
727+
protected static Uri GetBaseAddress(string? uri, string? hostName, string? path, string? port, string hostOptionName)
726728
{
727729
// Uri property takes precedent.
728730
if (!string.IsNullOrWhiteSpace(uri))
729731
{
732+
if (!string.IsNullOrWhiteSpace(hostName))
733+
{
734+
throw new ArgumentOutOfRangeException(
735+
AdbcOptions.Uri,
736+
hostOptionName,
737+
$"Conflicting server arguments. Please provide only one of the following options: '{Adbc.AdbcOptions.Uri}' or '{hostOptionName}'.");
738+
}
739+
730740
var uriValue = new Uri(uri);
731741
if (uriValue.Scheme != Uri.UriSchemeHttp && uriValue.Scheme != Uri.UriSchemeHttps)
732742
throw new ArgumentOutOfRangeException(
@@ -752,11 +762,12 @@ protected static Uri GetBaseAddress(string? uri, string? hostName, string? path,
752762
return baseAddress;
753763
}
754764

755-
// Note data source's Position may be one-indexed or zero-indexed
756765
protected IReadOnlyDictionary<string, int> GetColumnIndexMap(List<TColumnDesc> columns) => columns
757-
.Select(t => new { Index = t.Position - PositionRequiredOffset, t.ColumnName })
766+
.Select(t => new { Index = t.Position - ColumnMapIndexOffset, t.ColumnName })
758767
.ToDictionary(t => t.ColumnName, t => t.Index);
759768

769+
protected abstract int ColumnMapIndexOffset { get; }
770+
760771
protected abstract Task<TRowSet> GetRowSetAsync(TGetTableTypesResp response, CancellationToken cancellationToken = default);
761772
protected abstract Task<TRowSet> GetRowSetAsync(TGetColumnsResp response, CancellationToken cancellationToken = default);
762773
protected abstract Task<TRowSet> GetRowSetAsync(TGetTablesResp response, CancellationToken cancellationToken = default);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
using System;
19+
using System.Collections.Generic;
20+
21+
namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
22+
{
23+
internal class HiveServer2ConnectionFactory
24+
{
25+
public static HiveServer2Connection NewConnection(IReadOnlyDictionary<string, string> properties)
26+
{
27+
if (!properties.TryGetValue(HiveServer2Parameters.TransportType, out string? type))
28+
{
29+
30+
throw new ArgumentException($"Required property '{HiveServer2Parameters.TransportType}' is missing. Supported types: {HiveServer2TransportTypeParser.SupportedList}", nameof(properties));
31+
}
32+
if (!HiveServer2TransportTypeParser.TryParse(type, out HiveServer2TransportType typeValue))
33+
{
34+
throw new ArgumentOutOfRangeException(nameof(properties), $"Unsupported or unknown value '{type}' given for property '{HiveServer2Parameters.TransportType}'. Supported types: {HiveServer2TransportTypeParser.SupportedList}");
35+
}
36+
return new HiveServer2HttpConnection(properties);
37+
}
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
using System;
19+
using System.Collections.Generic;
20+
using System.Linq;
21+
22+
namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
23+
{
24+
internal class HiveServer2Database : AdbcDatabase
25+
{
26+
readonly IReadOnlyDictionary<string, string> properties;
27+
28+
internal HiveServer2Database(IReadOnlyDictionary<string, string> properties)
29+
{
30+
this.properties = properties;
31+
}
32+
33+
public override AdbcConnection Connect(IReadOnlyDictionary<string, string>? options)
34+
{
35+
// connection options takes precedence over database properties for the same option
36+
IReadOnlyDictionary<string, string> mergedProperties = options == null
37+
? properties
38+
: options
39+
.Concat(properties.Where(x => !options.Keys.Contains(x.Key, StringComparer.OrdinalIgnoreCase)))
40+
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
41+
HiveServer2Connection connection = HiveServer2ConnectionFactory.NewConnection(mergedProperties);
42+
connection.OpenAsync().Wait();
43+
return connection;
44+
}
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
using System.Collections.Generic;
19+
20+
namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
21+
{
22+
public class HiveServer2Driver : AdbcDriver
23+
{
24+
public override AdbcDatabase Open(IReadOnlyDictionary<string, string> parameters)
25+
{
26+
return new HiveServer2Database(parameters);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)