Skip to content

Commit 5a7ce96

Browse files
authored
fix(csharp/src/Drivers/Apache): Improve handling of authentication and server type enumeration parsing (apache#2574)
Improves idiomatic handling of authentication and server type enumerations Fixes apache#2569
1 parent 15ad9d1 commit 5a7ce96

12 files changed

+50
-27
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
1919
{
2020
internal enum HiveServer2AuthType
2121
{
22-
Invalid = 0,
2322
None,
2423
UsernameOnly,
2524
Basic,
@@ -46,7 +45,7 @@ internal static bool TryParse(string? authType, out HiveServer2AuthType authType
4645
authTypeValue = HiveServer2AuthType.Basic;
4746
return true;
4847
default:
49-
authTypeValue = HiveServer2AuthType.Invalid;
48+
authTypeValue = default;
5049
return false;
5150
}
5251
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ private void ValidateAuthentication()
6565
Properties.TryGetValue(AdbcOptions.Username, out string? username);
6666
Properties.TryGetValue(AdbcOptions.Password, out string? password);
6767
Properties.TryGetValue(HiveServer2Parameters.AuthType, out string? authType);
68-
bool isValidAuthType = HiveServer2AuthTypeParser.TryParse(authType, out HiveServer2AuthType authTypeValue);
68+
if (!HiveServer2AuthTypeParser.TryParse(authType, out HiveServer2AuthType authTypeValue))
69+
{
70+
throw new ArgumentOutOfRangeException(HiveServer2Parameters.AuthType, authType, $"Unsupported {HiveServer2Parameters.AuthType} value.");
71+
}
6972
switch (authTypeValue)
7073
{
7174
case HiveServer2AuthType.Basic:
@@ -143,10 +146,10 @@ public override AdbcStatement CreateStatement()
143146
}
144147

145148
internal override IArrowArrayStream NewReader<T>(T statement, Schema schema) => new HiveServer2Reader(
146-
statement,
147-
schema,
148-
dataTypeConversion: statement.Connection.DataTypeConversion,
149-
enableBatchSizeStopCondition: false);
149+
statement,
150+
schema,
151+
dataTypeConversion: statement.Connection.DataTypeConversion,
152+
enableBatchSizeStopCondition: false);
150153

151154
protected override TTransport CreateTransport()
152155
{
@@ -155,7 +158,10 @@ protected override TTransport CreateTransport()
155158
Properties.TryGetValue(HiveServer2Parameters.Path, out string? path);
156159
Properties.TryGetValue(HiveServer2Parameters.Port, out string? port);
157160
Properties.TryGetValue(HiveServer2Parameters.AuthType, out string? authType);
158-
bool isValidAuthType = HiveServer2AuthTypeParser.TryParse(authType, out HiveServer2AuthType authTypeValue);
161+
if (!HiveServer2AuthTypeParser.TryParse(authType, out HiveServer2AuthType authTypeValue))
162+
{
163+
throw new ArgumentOutOfRangeException(HiveServer2Parameters.AuthType, authType, $"Unsupported {HiveServer2Parameters.AuthType} value.");
164+
}
159165
Properties.TryGetValue(AdbcOptions.Username, out string? username);
160166
Properties.TryGetValue(AdbcOptions.Password, out string? password);
161167
Properties.TryGetValue(AdbcOptions.Uri, out string? uri);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Apache.Arrow.Adbc.Drivers.Apache.Hive2
1919
{
2020
internal enum HiveServer2TransportType
2121
{
22-
Invalid = 0,
2322
Http,
2423
Empty = int.MaxValue,
2524
}
@@ -40,7 +39,7 @@ internal static bool TryParse(string? serverType, out HiveServer2TransportType s
4039
serverTypeValue = HiveServer2TransportType.Http;
4140
return true;
4241
default:
43-
serverTypeValue = HiveServer2TransportType.Invalid;
42+
serverTypeValue = default;
4443
return false;
4544
}
4645
}

csharp/src/Drivers/Apache/Impala/ImpalaAuthType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Apache.Arrow.Adbc.Drivers.Apache.Impala
1919
{
2020
internal enum ImpalaAuthType
2121
{
22-
Invalid = 0,
2322
None,
2423
UsernameOnly,
2524
Basic,
@@ -46,7 +45,7 @@ internal static bool TryParse(string? authType, out ImpalaAuthType authTypeValue
4645
authTypeValue = ImpalaAuthType.Basic;
4746
return true;
4847
default:
49-
authTypeValue = ImpalaAuthType.Invalid;
48+
authTypeValue = default;
5049
return false;
5150
}
5251
}

csharp/src/Drivers/Apache/Impala/ImpalaHttpConnection.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ protected override void ValidateAuthentication()
4949
Properties.TryGetValue(AdbcOptions.Username, out string? username);
5050
Properties.TryGetValue(AdbcOptions.Password, out string? password);
5151
Properties.TryGetValue(ImpalaParameters.AuthType, out string? authType);
52-
bool isValidAuthType = ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue);
52+
if (!ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue))
53+
{
54+
throw new ArgumentOutOfRangeException(ImpalaParameters.AuthType, authType, $"Unsupported {ImpalaParameters.AuthType} value.");
55+
}
5356
switch (authTypeValue)
5457
{
5558
case ImpalaAuthType.Basic:
@@ -130,7 +133,10 @@ protected override TTransport CreateTransport()
130133
Properties.TryGetValue(ImpalaParameters.Path, out string? path);
131134
Properties.TryGetValue(ImpalaParameters.Port, out string? port);
132135
Properties.TryGetValue(ImpalaParameters.AuthType, out string? authType);
133-
bool isValidAuthType = ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue);
136+
if (!ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue))
137+
{
138+
throw new ArgumentOutOfRangeException(ImpalaParameters.AuthType, authType, $"Unsupported {ImpalaParameters.AuthType} value.");
139+
}
134140
Properties.TryGetValue(AdbcOptions.Username, out string? username);
135141
Properties.TryGetValue(AdbcOptions.Password, out string? password);
136142
Properties.TryGetValue(AdbcOptions.Uri, out string? uri);

csharp/src/Drivers/Apache/Impala/ImpalaServerType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Apache.Arrow.Adbc.Drivers.Apache.Impala
1919
{
2020
internal enum ImpalaServerType
2121
{
22-
Invalid = 0,
2322
Http,
2423
Standard,
2524
Empty = int.MaxValue,
@@ -44,7 +43,7 @@ internal static bool TryParse(string? serverType, out ImpalaServerType serverTyp
4443
serverTypeValue = ImpalaServerType.Standard;
4544
return true;
4645
default:
47-
serverTypeValue = ImpalaServerType.Invalid;
46+
serverTypeValue = default;
4847
return false;
4948
}
5049
}

csharp/src/Drivers/Apache/Impala/ImpalaStandardConnection.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ protected override void ValidateAuthentication()
4040
Properties.TryGetValue(AdbcOptions.Username, out string? username);
4141
Properties.TryGetValue(AdbcOptions.Password, out string? password);
4242
Properties.TryGetValue(ImpalaParameters.AuthType, out string? authType);
43-
bool isValidAuthType = ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue);
43+
if (!ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue))
44+
{
45+
throw new ArgumentOutOfRangeException(ImpalaParameters.AuthType, authType, $"Unsupported {ImpalaParameters.AuthType} value.");
46+
}
4447
switch (authTypeValue)
4548
{
4649
case ImpalaAuthType.None:
@@ -120,7 +123,10 @@ protected override TOpenSessionReq CreateSessionRequest()
120123
Properties.TryGetValue(AdbcOptions.Username, out string? username);
121124
Properties.TryGetValue(AdbcOptions.Password, out string? password);
122125
Properties.TryGetValue(ImpalaParameters.AuthType, out string? authType);
123-
bool isValidAuthType = ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue);
126+
if (!ImpalaAuthTypeParser.TryParse(authType, out ImpalaAuthType authTypeValue))
127+
{
128+
throw new ArgumentOutOfRangeException(ImpalaParameters.AuthType, authType, $"Unsupported {ImpalaParameters.AuthType} value.");
129+
}
124130
TOpenSessionReq request = new TOpenSessionReq(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V7)
125131
{
126132
CanUseMultipleCatalogs = true,

csharp/src/Drivers/Apache/Spark/SparkAuthType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Apache.Arrow.Adbc.Drivers.Apache.Spark
1919
{
2020
internal enum SparkAuthType
2121
{
22-
Invalid = 0,
2322
None,
2423
UsernameOnly,
2524
Basic,
@@ -50,7 +49,7 @@ internal static bool TryParse(string? authType, out SparkAuthType authTypeValue)
5049
authTypeValue = SparkAuthType.Token;
5150
return true;
5251
default:
53-
authTypeValue = SparkAuthType.Invalid;
52+
authTypeValue = default;
5453
return false;
5554
}
5655
}

csharp/src/Drivers/Apache/Spark/SparkConnectionFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static SparkConnection NewConnection(IReadOnlyDictionary<string, string>
4040
// TODO: Re-enable when properly supported
4141
//SparkServerType.Standard => new SparkStandardConnection(properties),
4242
_ => throw new ArgumentOutOfRangeException(nameof(properties), $"Unsupported or unknown value '{type}' given for property '{SparkParameters.Type}'. Supported types: {ServerTypeParser.SupportedList}"),
43-
4443
};
4544
}
4645

csharp/src/Drivers/Apache/Spark/SparkHttpConnection.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ protected override void ValidateAuthentication()
5151
Properties.TryGetValue(AdbcOptions.Username, out string? username);
5252
Properties.TryGetValue(AdbcOptions.Password, out string? password);
5353
Properties.TryGetValue(SparkParameters.AuthType, out string? authType);
54-
bool isValidAuthType = SparkAuthTypeParser.TryParse(authType, out SparkAuthType authTypeValue);
54+
if (!SparkAuthTypeParser.TryParse(authType, out SparkAuthType authTypeValue))
55+
{
56+
throw new ArgumentOutOfRangeException(SparkParameters.AuthType, authType, $"Unsupported {SparkParameters.AuthType} value.");
57+
}
5558
switch (authTypeValue)
5659
{
5760
case SparkAuthType.Token:
@@ -138,7 +141,10 @@ protected override TTransport CreateTransport()
138141
Properties.TryGetValue(SparkParameters.Path, out string? path);
139142
Properties.TryGetValue(SparkParameters.Port, out string? port);
140143
Properties.TryGetValue(SparkParameters.AuthType, out string? authType);
141-
bool isValidAuthType = SparkAuthTypeParser.TryParse(authType, out SparkAuthType authTypeValue);
144+
if (!SparkAuthTypeParser.TryParse(authType, out SparkAuthType authTypeValue))
145+
{
146+
throw new ArgumentOutOfRangeException(SparkParameters.AuthType, authType, $"Unsupported {SparkParameters.AuthType} value.");
147+
}
142148
Properties.TryGetValue(SparkParameters.Token, out string? token);
143149
Properties.TryGetValue(AdbcOptions.Username, out string? username);
144150
Properties.TryGetValue(AdbcOptions.Password, out string? password);

0 commit comments

Comments
 (0)