Skip to content

Commit

Permalink
feat(csharp/src/Drivers/Apache): Add support for Hive ADBC Driver wit…
Browse files Browse the repository at this point in the history
…h 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]>
  • Loading branch information
3 people authored Mar 3, 2025
1 parent 80094ce commit b0a7b68
Show file tree
Hide file tree
Showing 44 changed files with 1,604 additions and 103 deletions.
54 changes: 54 additions & 0 deletions csharp/src/Drivers/Apache/Hive2/HiveServer2AuthType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
{
internal enum HiveServer2AuthType
{
Invalid = 0,
None,
UsernameOnly,
Basic,
Empty = int.MaxValue,
}

internal static class HiveServer2AuthTypeParser
{
internal static bool TryParse(string? authType, out HiveServer2AuthType authTypeValue)
{
switch (authType?.Trim().ToLowerInvariant())
{
case null:
case "":
authTypeValue = HiveServer2AuthType.Empty;
return true;
case HiveServer2AuthTypeConstants.None:
authTypeValue = HiveServer2AuthType.None;
return true;
case HiveServer2AuthTypeConstants.UsernameOnly:
authTypeValue = HiveServer2AuthType.UsernameOnly;
return true;
case HiveServer2AuthTypeConstants.Basic:
authTypeValue = HiveServer2AuthType.Basic;
return true;
default:
authTypeValue = HiveServer2AuthType.Invalid;
return false;
}
}
}
}
19 changes: 15 additions & 4 deletions csharp/src/Drivers/Apache/Hive2/HiveServer2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,10 @@ public override IArrowArrayStream GetTableTypes()
TRowSet rowSet = GetRowSetAsync(resp, cancellationToken).Result;
StringArray tableTypes = rowSet.Columns[0].StringVal.Values;

HashSet<string> distinctTableTypes = new HashSet<string>(tableTypes);

StringArray.Builder tableTypesBuilder = new StringArray.Builder();
tableTypesBuilder.AppendRange(tableTypes);
tableTypesBuilder.AppendRange(distinctTableTypes);

IArrowArray[] dataArrays = new IArrowArray[]
{
Expand Down Expand Up @@ -722,11 +724,19 @@ protected internal string GetProductVersion()
return fileVersionInfo.ProductVersion ?? GetProductVersionDefault();
}

protected static Uri GetBaseAddress(string? uri, string? hostName, string? path, string? port)
protected static Uri GetBaseAddress(string? uri, string? hostName, string? path, string? port, string hostOptionName)
{
// Uri property takes precedent.
if (!string.IsNullOrWhiteSpace(uri))
{
if (!string.IsNullOrWhiteSpace(hostName))
{
throw new ArgumentOutOfRangeException(
AdbcOptions.Uri,
hostOptionName,
$"Conflicting server arguments. Please provide only one of the following options: '{Adbc.AdbcOptions.Uri}' or '{hostOptionName}'.");
}

var uriValue = new Uri(uri);
if (uriValue.Scheme != Uri.UriSchemeHttp && uriValue.Scheme != Uri.UriSchemeHttps)
throw new ArgumentOutOfRangeException(
Expand All @@ -752,11 +762,12 @@ protected static Uri GetBaseAddress(string? uri, string? hostName, string? path,
return baseAddress;
}

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

protected abstract int ColumnMapIndexOffset { get; }

protected abstract Task<TRowSet> GetRowSetAsync(TGetTableTypesResp response, CancellationToken cancellationToken = default);
protected abstract Task<TRowSet> GetRowSetAsync(TGetColumnsResp response, CancellationToken cancellationToken = default);
protected abstract Task<TRowSet> GetRowSetAsync(TGetTablesResp response, CancellationToken cancellationToken = default);
Expand Down
39 changes: 39 additions & 0 deletions csharp/src/Drivers/Apache/Hive2/HiveServer2ConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;

namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
{
internal class HiveServer2ConnectionFactory
{
public static HiveServer2Connection NewConnection(IReadOnlyDictionary<string, string> properties)
{
if (!properties.TryGetValue(HiveServer2Parameters.TransportType, out string? type))
{

throw new ArgumentException($"Required property '{HiveServer2Parameters.TransportType}' is missing. Supported types: {HiveServer2TransportTypeParser.SupportedList}", nameof(properties));
}
if (!HiveServer2TransportTypeParser.TryParse(type, out HiveServer2TransportType typeValue))
{
throw new ArgumentOutOfRangeException(nameof(properties), $"Unsupported or unknown value '{type}' given for property '{HiveServer2Parameters.TransportType}'. Supported types: {HiveServer2TransportTypeParser.SupportedList}");
}
return new HiveServer2HttpConnection(properties);
}
}
}
46 changes: 46 additions & 0 deletions csharp/src/Drivers/Apache/Hive2/HiveServer2Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;

namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
{
internal class HiveServer2Database : AdbcDatabase
{
readonly IReadOnlyDictionary<string, string> properties;

internal HiveServer2Database(IReadOnlyDictionary<string, string> properties)
{
this.properties = properties;
}

public override AdbcConnection Connect(IReadOnlyDictionary<string, string>? options)
{
// connection options takes precedence over database properties for the same option
IReadOnlyDictionary<string, string> mergedProperties = options == null
? properties
: options
.Concat(properties.Where(x => !options.Keys.Contains(x.Key, StringComparer.OrdinalIgnoreCase)))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
HiveServer2Connection connection = HiveServer2ConnectionFactory.NewConnection(mergedProperties);
connection.OpenAsync().Wait();
return connection;
}
}
}
29 changes: 29 additions & 0 deletions csharp/src/Drivers/Apache/Hive2/HiveServer2Driver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.Generic;

namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
{
public class HiveServer2Driver : AdbcDriver
{
public override AdbcDatabase Open(IReadOnlyDictionary<string, string> parameters)
{
return new HiveServer2Database(parameters);
}
}
}
Loading

0 comments on commit b0a7b68

Please sign in to comment.