dongjoon-hyun commented on code in PR #7: URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990467780
########## Sources/SparkConnect/SparkConnectClient.swift: ########## @@ -0,0 +1,228 @@ +// +// 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. + +import Foundation +import GRPCCore +import GRPCNIOTransportHTTP2 +import GRPCProtobuf +import Synchronization + +/// Conceptually the remote spark session that communicates with the server +public actor SparkConnectClient { + let clientType: String = "swift" + let url: URL + let host: String + let port: Int + let userContext: UserContext + var sessionID: String? = nil + + /// Create a client to use GRPCClient. + /// - Parameters: + /// - remote: A string to connect `Spark Connect` server. + /// - user: A string for the user ID of this connection. + init(remote: String, user: String) { + self.url = URL(string: remote)! + self.host = url.host() ?? "localhost" + self.port = self.url.port ?? 15002 + self.userContext = user.toUserContext + } + + /// Stop the connection. Currently, this API is no-op because we don't reuse the connection yet. + func stop() { + } + + /// Connect to the `Spark Connect` server with the given session ID string. + /// As a test connection, this sends the server `SparkVersion` request. + /// - Parameter sessionID: A string for the session ID. + /// - Returns: An `AnalyzePlanResponse` instance for `SparkVersion` + func connect(_ sessionID: String) async throws -> AnalyzePlanResponse { + try await withGRPCClient( + transport: .http2NIOPosix( + target: .dns(host: self.host, port: self.port), + transportSecurity: .plaintext + ) + ) { client in + // To prevent server-side `INVALID_HANDLE.FORMAT (SQLSTATE: HY000)` exception. + if UUID(uuidString: sessionID) == nil { + throw SparkConnectError.InvalidSessionIDException + } + + self.sessionID = sessionID + let service = SparkConnectService.Client(wrapping: client) + let version = AnalyzePlanRequest.SparkVersion() + var request = AnalyzePlanRequest() + request.clientType = clientType + request.userContext = userContext + request.sessionID = sessionID + request.analyze = .sparkVersion(version) + let response = try await service.analyzePlan(request) + return response + } + } + + /// Create a ``ConfigRequest`` instance for `Set` operation. + /// - Parameter map: A map of key-value string pairs. + /// - Returns: A ``ConfigRequest`` instance. + func getConfigRequestSet(map: [String: String]) -> ConfigRequest { + var request = ConfigRequest() + request.operation = ConfigRequest.Operation() + var set = ConfigRequest.Set() + set.pairs = map.toSparkConnectKeyValue + request.operation.opType = .set(set) + return request + } + + /// Request the server to set a map of configurations for this session. + /// - Parameter map: A map of key-value pairs to set. + /// - Returns: Always return true. + func setConf(map: [String: String]) async throws -> Bool { Review Comment: This is a different implementation choice to encapsulate those details. Technically, each language client maintains its `SparkConnectClient` in the `private` scope like `private[sql]`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org