-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathDatabricksConnection.cs
286 lines (250 loc) · 12.2 KB
/
DatabricksConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* 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.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Apache.Arrow.Adbc.Drivers.Apache;
using Apache.Arrow.Adbc.Drivers.Apache.Spark;
using Apache.Arrow.Adbc.Drivers.Databricks.CloudFetch;
using Apache.Arrow.Ipc;
using Apache.Hive.Service.Rpc.Thrift;
namespace Apache.Arrow.Adbc.Drivers.Databricks
{
internal class DatabricksConnection : SparkHttpConnection
{
private bool _applySSPWithQueries = false;
// CloudFetch configuration
private const long DefaultMaxBytesPerFile = 20 * 1024 * 1024; // 20MB
private bool _useCloudFetch = true;
private bool _canDecompressLz4 = true;
private long _maxBytesPerFile = DefaultMaxBytesPerFile;
public DatabricksConnection(IReadOnlyDictionary<string, string> properties) : base(properties)
{
ValidateProperties();
}
private void ValidateProperties()
{
if (Properties.TryGetValue(DatabricksParameters.ApplySSPWithQueries, out string? applySSPWithQueriesStr))
{
if (bool.TryParse(applySSPWithQueriesStr, out bool applySSPWithQueriesValue))
{
_applySSPWithQueries = applySSPWithQueriesValue;
}
else
{
throw new ArgumentException($"Parameter '{DatabricksParameters.ApplySSPWithQueries}' value '{applySSPWithQueriesStr}' could not be parsed. Valid values are 'true' and 'false'.");
}
}
// Parse CloudFetch options from connection properties
if (Properties.TryGetValue(DatabricksParameters.UseCloudFetch, out string? useCloudFetchStr))
{
if (bool.TryParse(useCloudFetchStr, out bool useCloudFetchValue))
{
_useCloudFetch = useCloudFetchValue;
}
else
{
throw new ArgumentException($"Parameter '{DatabricksParameters.UseCloudFetch}' value '{useCloudFetchStr}' could not be parsed. Valid values are 'true' and 'false'.");
}
}
if (Properties.TryGetValue(DatabricksParameters.CanDecompressLz4, out string? canDecompressLz4Str))
{
if (bool.TryParse(canDecompressLz4Str, out bool canDecompressLz4Value))
{
_canDecompressLz4 = canDecompressLz4Value;
}
else
{
throw new ArgumentException($"Parameter '{DatabricksParameters.CanDecompressLz4}' value '{canDecompressLz4Str}' could not be parsed. Valid values are 'true' and 'false'.");
}
}
if (Properties.TryGetValue(DatabricksParameters.MaxBytesPerFile, out string? maxBytesPerFileStr))
{
if (!long.TryParse(maxBytesPerFileStr, out long maxBytesPerFileValue))
{
throw new ArgumentException($"Parameter '{DatabricksParameters.MaxBytesPerFile}' value '{maxBytesPerFileStr}' could not be parsed. Valid values are positive integers.");
}
if (maxBytesPerFileValue <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(Properties),
maxBytesPerFileValue,
$"Parameter '{DatabricksParameters.MaxBytesPerFile}' value must be a positive integer.");
}
_maxBytesPerFile = maxBytesPerFileValue;
}
}
/// <summary>
/// Gets whether server side properties should be applied using queries.
/// </summary>
internal bool ApplySSPWithQueries => _applySSPWithQueries;
/// <summary>
/// Gets whether CloudFetch is enabled.
/// </summary>
internal bool UseCloudFetch => _useCloudFetch;
/// <summary>
/// Gets whether LZ4 decompression is enabled.
/// </summary>
internal bool CanDecompressLz4 => _canDecompressLz4;
/// <summary>
/// Gets the maximum bytes per file for CloudFetch.
/// </summary>
internal long MaxBytesPerFile => _maxBytesPerFile;
internal override IArrowArrayStream NewReader<T>(T statement, Schema schema, TGetResultSetMetadataResp? metadataResp = null)
{
// Get result format from metadata response if available
TSparkRowSetType resultFormat = TSparkRowSetType.ARROW_BASED_SET;
bool isLz4Compressed = false;
DatabricksStatement? databricksStatement = statement as DatabricksStatement;
if (databricksStatement == null)
{
throw new InvalidOperationException("Cannot obtain a reader for Databricks");
}
if (metadataResp != null)
{
if (metadataResp.__isset.resultFormat)
{
resultFormat = metadataResp.ResultFormat;
}
if (metadataResp.__isset.lz4Compressed)
{
isLz4Compressed = metadataResp.Lz4Compressed;
}
}
// Choose the appropriate reader based on the result format
if (resultFormat == TSparkRowSetType.URL_BASED_SET)
{
return new CloudFetchReader(databricksStatement, schema, isLz4Compressed);
}
else
{
return new DatabricksReader(databricksStatement, schema, isLz4Compressed);
}
}
internal override SchemaParser SchemaParser => new DatabricksSchemaParser();
public override AdbcStatement CreateStatement()
{
DatabricksStatement statement = new DatabricksStatement(this);
return statement;
}
protected override TOpenSessionReq CreateSessionRequest()
{
var req = new TOpenSessionReq
{
Client_protocol = TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7,
Client_protocol_i64 = (long)TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V7,
CanUseMultipleCatalogs = true,
};
// If not using queries to set server-side properties, include them in Configuration
if (!_applySSPWithQueries)
{
req.Configuration = new Dictionary<string, string>();
var serverSideProperties = GetServerSideProperties();
foreach (var property in serverSideProperties)
{
req.Configuration[property.Key] = property.Value;
}
}
return req;
}
/// <summary>
/// Gets a dictionary of server-side properties extracted from connection properties.
/// </summary>
/// <returns>Dictionary of server-side properties with prefix removed from keys.</returns>
private Dictionary<string, string> GetServerSideProperties()
{
return Properties
.Where(p => p.Key.StartsWith(DatabricksParameters.ServerSidePropertyPrefix))
.ToDictionary(
p => p.Key.Substring(DatabricksParameters.ServerSidePropertyPrefix.Length),
p => p.Value
);
}
/// <summary>
/// Applies server-side properties by executing "set key=value" queries.
/// </summary>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task ApplyServerSidePropertiesAsync()
{
if (!_applySSPWithQueries)
{
return;
}
var serverSideProperties = GetServerSideProperties();
if (serverSideProperties.Count == 0)
{
return;
}
using var statement = new DatabricksStatement(this);
foreach (var property in serverSideProperties)
{
if (!IsValidPropertyName(property.Key))
{
Debug.WriteLine($"Skipping invalid property name: {property.Key}");
continue;
}
string escapedValue = EscapeSqlString(property.Value);
string query = $"SET {property.Key}={escapedValue}";
statement.SqlQuery = query;
try
{
await statement.ExecuteUpdateAsync();
}
catch (Exception ex)
{
Debug.WriteLine($"Error setting server-side property '{property.Key}': {ex.Message}");
}
}
}
private bool IsValidPropertyName(string propertyName)
{
// Allow only letters and underscores in property names
return System.Text.RegularExpressions.Regex.IsMatch(
propertyName,
@"^[a-zA-Z_]+$");
}
private string EscapeSqlString(string value)
{
return "`" + value.Replace("`", "``") + "`";
}
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetSchemasResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSetMetadata);
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetCatalogsResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSetMetadata);
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetColumnsResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSetMetadata);
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetTablesResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSetMetadata);
protected internal override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetPrimaryKeysResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSetMetadata);
protected override Task<TRowSet> GetRowSetAsync(TGetTableTypesResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSet.Results);
protected override Task<TRowSet> GetRowSetAsync(TGetColumnsResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSet.Results);
protected override Task<TRowSet> GetRowSetAsync(TGetTablesResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSet.Results);
protected override Task<TRowSet> GetRowSetAsync(TGetCatalogsResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSet.Results);
protected override Task<TRowSet> GetRowSetAsync(TGetSchemasResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSet.Results);
protected internal override Task<TRowSet> GetRowSetAsync(TGetPrimaryKeysResp response, CancellationToken cancellationToken = default) =>
Task.FromResult(response.DirectResults.ResultSet.Results);
}
}