You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "dongjoon-hyun (via GitHub)" <gi...@apache.org> on 2025/03/11 18:30:40 UTC

[PR] [SPARK-51472] Add gRPC `Client` actor [spark-connect-swift]

dongjoon-hyun opened a new pull request, #7:
URL: https://github.com/apache/spark-connect-swift/pull/7

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990450408


##########
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
+  }

Review Comment:
   In Scala `Configuration`, there are a lot configs other than remote and user, e.g., `userAgent`, `isSslEnabled`, etc. Are you going to add more configurations later?



##########
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 {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestSet(map: map)
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let _ = try await service.config(request)
+      return true
+    }
+  }
+
+  /// Create a ``ConfigRequest`` instance for `Get` operation.
+  /// - Parameter keys: An array of keys to get.
+  /// - Returns: A `ConfigRequest` instance.
+  func getConfigRequestGet(keys: [String]) -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    var get = ConfigRequest.Get()
+    get.keys = keys
+    request.operation.opType = .get(get)
+    return request
+  }
+
+  /// Request the server to get a value of the given key.
+  /// - Parameter key: A string for key to look up.
+  /// - Returns: A string for the value of the key.
+  func getConf(_ key: String) async throws -> String {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGet(keys: [key])
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      return response.pairs[0].value
+    }
+  }
+
+  /// Create a ``ConfigRequest`` for `GetAll` operation.
+  /// - Returns: <#description#>

Review Comment:
   ```suggestion
     /// - Returns: A `ConfigRequest` instance.
   ```



##########
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

Review Comment:
   Is this `AnalyzePlanRequest` only used for test for now?



##########
Sources/SparkConnect/Extension.swift:
##########
@@ -0,0 +1,67 @@
+//
+// 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
+
+extension String {
+  /// Get a `Plan` instance from a string.
+  var toSparkConnectPlan: Plan {
+    var sql = Spark_Connect_SQL()
+    sql.query = self
+    var relation = Relation()
+    relation.sql = sql
+    var plan = Plan()
+    plan.opType = Plan.OneOf_OpType.root(relation)
+    return plan
+  }
+
+  /// Get a `UserContext` instance from a string.
+  var toUserContext: UserContext {
+    var context = UserContext()
+    context.userID = self
+    context.userName = self
+    return context
+  }
+
+  /// Get a `KeyValue` instance by using a string as the key.
+  var toKeyValue: KeyValue {
+    var keyValue = KeyValue()
+    keyValue.key = self
+    return keyValue
+  }
+}
+
+extension [String: String] {
+  /// Get an array of`KeyValue`from `[String: String]`.

Review Comment:
   ```suggestion
     /// Get an array of `KeyValue` from `[String: String]`.
   ```



##########
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 {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestSet(map: map)
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let _ = try await service.config(request)
+      return true
+    }
+  }
+
+  /// Create a ``ConfigRequest`` instance for `Get` operation.
+  /// - Parameter keys: An array of keys to get.
+  /// - Returns: A `ConfigRequest` instance.
+  func getConfigRequestGet(keys: [String]) -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    var get = ConfigRequest.Get()
+    get.keys = keys
+    request.operation.opType = .get(get)
+    return request
+  }
+
+  /// Request the server to get a value of the given key.
+  /// - Parameter key: A string for key to look up.
+  /// - Returns: A string for the value of the key.
+  func getConf(_ key: String) async throws -> String {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGet(keys: [key])
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      return response.pairs[0].value
+    }
+  }
+
+  /// Create a ``ConfigRequest`` for `GetAll` operation.
+  /// - Returns: <#description#>
+  func getConfigRequestGetAll() -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    let getAll = ConfigRequest.GetAll()
+    request.operation.opType = .getAll(getAll)
+    return request
+  }
+
+  /// Request the server to get all configurations.
+  /// - Returns: A map of key-value pairs.
+  func getConfAll() async throws -> [String: String] {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGetAll()
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      var map = [String: String]()
+      for pair in response.pairs {
+        map[pair.key] = pair.value
+      }
+      return map
+    }
+  }
+
+  /// Create a `Plan` instance for `Range` relation.
+  /// - Parameters:
+  ///   - start: A start of the range.
+  ///   - end: A end (exclusive) of the range.
+  ///   - step: A step value for the range from `start` to `end`.
+  /// - Returns: <#description#>
+  func getPlanRange(_ start: Int64, _ end: Int64, _ step: Int64) -> Plan {
+    var range = Range()
+    range.start = start
+    range.end = end
+    range.step = step
+    var relation = Relation()
+    relation.range = range
+    var plan = Plan()
+    plan.opType = .root(relation)
+    return plan
+  }
+
+  /// Create a ``ExecutePlanRequest`` instance with the given session ID and plan.
+  /// The operation ID is created by UUID.
+  /// - Parameters:
+  ///   - sessionID: A string for the existing session ID.
+  ///   - plan: A plan to execute.
+  /// - Returns: An ``ExecutePlanRequest`` instance.
+  func getExecutePlanRequest(_ sessionID: String, _ plan: Plan) async
+    -> ExecutePlanRequest
+  {
+    var request = ExecutePlanRequest()
+    request.clientType = clientType
+    request.userContext = userContext
+    request.sessionID = sessionID
+    request.operationID = UUID().uuidString
+    request.plan = plan
+    return request
+  }
+
+  /// Create a ``AnalyzePlanRequest`` instance with the given session ID and plan.
+  /// - Parameters:
+  ///   - sessionID: A string for the existing session ID.
+  ///   - plan: A plan to analyze.
+  /// - Returns: An ``AnalyzePlanRequest`` instance
+  func getAnalyzePlanRequest(_ sessionID: String, _ plan: Plan) async

Review Comment:
   `getAnalyzePlanRequest` and `getExecutePlanRequest` are not used in this PR, right? Will they be used in later PR?



##########
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 {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestSet(map: map)
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let _ = try await service.config(request)
+      return true
+    }
+  }
+
+  /// Create a ``ConfigRequest`` instance for `Get` operation.
+  /// - Parameter keys: An array of keys to get.
+  /// - Returns: A `ConfigRequest` instance.
+  func getConfigRequestGet(keys: [String]) -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    var get = ConfigRequest.Get()
+    get.keys = keys
+    request.operation.opType = .get(get)
+    return request
+  }
+
+  /// Request the server to get a value of the given key.
+  /// - Parameter key: A string for key to look up.
+  /// - Returns: A string for the value of the key.
+  func getConf(_ key: String) async throws -> String {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGet(keys: [key])
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      return response.pairs[0].value
+    }
+  }
+
+  /// Create a ``ConfigRequest`` for `GetAll` operation.
+  /// - Returns: <#description#>
+  func getConfigRequestGetAll() -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    let getAll = ConfigRequest.GetAll()
+    request.operation.opType = .getAll(getAll)
+    return request
+  }
+
+  /// Request the server to get all configurations.
+  /// - Returns: A map of key-value pairs.
+  func getConfAll() async throws -> [String: String] {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGetAll()
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      var map = [String: String]()
+      for pair in response.pairs {
+        map[pair.key] = pair.value
+      }
+      return map
+    }
+  }
+
+  /// Create a `Plan` instance for `Range` relation.
+  /// - Parameters:
+  ///   - start: A start of the range.
+  ///   - end: A end (exclusive) of the range.
+  ///   - step: A step value for the range from `start` to `end`.
+  /// - Returns: <#description#>
+  func getPlanRange(_ start: Int64, _ end: Int64, _ step: Int64) -> Plan {
+    var range = Range()
+    range.start = start
+    range.end = end
+    range.step = step
+    var relation = Relation()
+    relation.range = range
+    var plan = Plan()
+    plan.opType = .root(relation)
+    return plan
+  }
+
+  /// Create a ``ExecutePlanRequest`` instance with the given session ID and plan.
+  /// The operation ID is created by UUID.
+  /// - Parameters:
+  ///   - sessionID: A string for the existing session ID.
+  ///   - plan: A plan to execute.
+  /// - Returns: An ``ExecutePlanRequest`` instance.
+  func getExecutePlanRequest(_ sessionID: String, _ plan: Plan) async
+    -> ExecutePlanRequest
+  {
+    var request = ExecutePlanRequest()
+    request.clientType = clientType
+    request.userContext = userContext
+    request.sessionID = sessionID

Review Comment:
   Shouldn't be `self.sessionID`?



##########
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 {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestSet(map: map)
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let _ = try await service.config(request)
+      return true
+    }
+  }
+
+  /// Create a ``ConfigRequest`` instance for `Get` operation.
+  /// - Parameter keys: An array of keys to get.
+  /// - Returns: A `ConfigRequest` instance.
+  func getConfigRequestGet(keys: [String]) -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    var get = ConfigRequest.Get()
+    get.keys = keys
+    request.operation.opType = .get(get)
+    return request
+  }
+
+  /// Request the server to get a value of the given key.
+  /// - Parameter key: A string for key to look up.
+  /// - Returns: A string for the value of the key.
+  func getConf(_ key: String) async throws -> String {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGet(keys: [key])
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      return response.pairs[0].value
+    }
+  }
+
+  /// Create a ``ConfigRequest`` for `GetAll` operation.
+  /// - Returns: <#description#>
+  func getConfigRequestGetAll() -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    let getAll = ConfigRequest.GetAll()
+    request.operation.opType = .getAll(getAll)
+    return request
+  }
+
+  /// Request the server to get all configurations.
+  /// - Returns: A map of key-value pairs.
+  func getConfAll() async throws -> [String: String] {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGetAll()
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      var map = [String: String]()
+      for pair in response.pairs {
+        map[pair.key] = pair.value
+      }
+      return map
+    }
+  }
+
+  /// Create a `Plan` instance for `Range` relation.
+  /// - Parameters:
+  ///   - start: A start of the range.
+  ///   - end: A end (exclusive) of the range.
+  ///   - step: A step value for the range from `start` to `end`.
+  /// - Returns: <#description#>

Review Comment:
   ```suggestion
     /// - Returns:  A `Plan` instance.
   ```



##########
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:
   I wonder why don't return `ConfigResponse` like [Scala](https://github.com/apache/spark/blob/master/sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/SparkConnectClient.scala#L160) but a `Bool`?



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990462126


##########
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
+  }

Review Comment:
   Of course, this project aims to achieve the feature parity eventually. But, each language clients have different development speeds like we did in SparkR/PySpark/Scala Spark. For `Spark Connect` example, Spark Connect `Go` doesn't support `isSslEnabled`.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#issuecomment-2716194217

   Could you review this too when you have some time, @viirya , please?


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990462746


##########
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

Review Comment:
   Yes in this PR. #1 has `SparkSession` and `DataFrame` implementation and I'll make PRs soon after this.
   
   For example, this is the implementation for `SparkSession.version` API.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990477755


##########
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 {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestSet(map: map)
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let _ = try await service.config(request)
+      return true
+    }
+  }
+
+  /// Create a ``ConfigRequest`` instance for `Get` operation.
+  /// - Parameter keys: An array of keys to get.
+  /// - Returns: A `ConfigRequest` instance.
+  func getConfigRequestGet(keys: [String]) -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    var get = ConfigRequest.Get()
+    get.keys = keys
+    request.operation.opType = .get(get)
+    return request
+  }
+
+  /// Request the server to get a value of the given key.
+  /// - Parameter key: A string for key to look up.
+  /// - Returns: A string for the value of the key.
+  func getConf(_ key: String) async throws -> String {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGet(keys: [key])
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      return response.pairs[0].value
+    }
+  }
+
+  /// Create a ``ConfigRequest`` for `GetAll` operation.
+  /// - Returns: <#description#>
+  func getConfigRequestGetAll() -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    let getAll = ConfigRequest.GetAll()
+    request.operation.opType = .getAll(getAll)
+    return request
+  }
+
+  /// Request the server to get all configurations.
+  /// - Returns: A map of key-value pairs.
+  func getConfAll() async throws -> [String: String] {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGetAll()
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      var map = [String: String]()
+      for pair in response.pairs {
+        map[pair.key] = pair.value
+      }
+      return map
+    }
+  }
+
+  /// Create a `Plan` instance for `Range` relation.
+  /// - Parameters:
+  ///   - start: A start of the range.
+  ///   - end: A end (exclusive) of the range.
+  ///   - step: A step value for the range from `start` to `end`.
+  /// - Returns: <#description#>
+  func getPlanRange(_ start: Int64, _ end: Int64, _ step: Int64) -> Plan {
+    var range = Range()
+    range.start = start
+    range.end = end
+    range.step = step
+    var relation = Relation()
+    relation.range = range
+    var plan = Plan()
+    plan.opType = .root(relation)
+    return plan
+  }
+
+  /// Create a ``ExecutePlanRequest`` instance with the given session ID and plan.
+  /// The operation ID is created by UUID.
+  /// - Parameters:
+  ///   - sessionID: A string for the existing session ID.
+  ///   - plan: A plan to execute.
+  /// - Returns: An ``ExecutePlanRequest`` instance.
+  func getExecutePlanRequest(_ sessionID: String, _ plan: Plan) async
+    -> ExecutePlanRequest
+  {
+    var request = ExecutePlanRequest()
+    request.clientType = clientType
+    request.userContext = userContext
+    request.sessionID = sessionID
+    request.operationID = UUID().uuidString
+    request.plan = plan
+    return request
+  }
+
+  /// Create a ``AnalyzePlanRequest`` instance with the given session ID and plan.
+  /// - Parameters:
+  ///   - sessionID: A string for the existing session ID.
+  ///   - plan: A plan to analyze.
+  /// - Returns: An ``AnalyzePlanRequest`` instance
+  func getAnalyzePlanRequest(_ sessionID: String, _ plan: Plan) async

Review Comment:
   Yes, it's going to used in `SparkSession` and `DataFrame` in the next PRs.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#issuecomment-2716135412

   Could you review this PR when you have some time, @HyukjinKwon ?


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990474308


##########
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 {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestSet(map: map)
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let _ = try await service.config(request)
+      return true
+    }
+  }
+
+  /// Create a ``ConfigRequest`` instance for `Get` operation.
+  /// - Parameter keys: An array of keys to get.
+  /// - Returns: A `ConfigRequest` instance.
+  func getConfigRequestGet(keys: [String]) -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    var get = ConfigRequest.Get()
+    get.keys = keys
+    request.operation.opType = .get(get)
+    return request
+  }
+
+  /// Request the server to get a value of the given key.
+  /// - Parameter key: A string for key to look up.
+  /// - Returns: A string for the value of the key.
+  func getConf(_ key: String) async throws -> String {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGet(keys: [key])
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      return response.pairs[0].value
+    }
+  }
+
+  /// Create a ``ConfigRequest`` for `GetAll` operation.
+  /// - Returns: <#description#>
+  func getConfigRequestGetAll() -> ConfigRequest {
+    var request = ConfigRequest()
+    request.operation = ConfigRequest.Operation()
+    let getAll = ConfigRequest.GetAll()
+    request.operation.opType = .getAll(getAll)
+    return request
+  }
+
+  /// Request the server to get all configurations.
+  /// - Returns: A map of key-value pairs.
+  func getConfAll() async throws -> [String: String] {
+    try await withGRPCClient(
+      transport: .http2NIOPosix(
+        target: .dns(host: self.host, port: self.port),
+        transportSecurity: .plaintext
+      )
+    ) { client in
+      let service = SparkConnectService.Client(wrapping: client)
+      var request = getConfigRequestGetAll()
+      request.clientType = clientType
+      request.userContext = userContext
+      request.sessionID = self.sessionID!
+      let response = try await service.config(request)
+      var map = [String: String]()
+      for pair in response.pairs {
+        map[pair.key] = pair.value
+      }
+      return map
+    }
+  }
+
+  /// Create a `Plan` instance for `Range` relation.
+  /// - Parameters:
+  ///   - start: A start of the range.
+  ///   - end: A end (exclusive) of the range.
+  ///   - step: A step value for the range from `start` to `end`.
+  /// - Returns: <#description#>
+  func getPlanRange(_ start: Int64, _ end: Int64, _ step: Int64) -> Plan {
+    var range = Range()
+    range.start = start
+    range.end = end
+    range.step = step
+    var relation = Relation()
+    relation.range = range
+    var plan = Plan()
+    plan.opType = .root(relation)
+    return plan
+  }
+
+  /// Create a ``ExecutePlanRequest`` instance with the given session ID and plan.
+  /// The operation ID is created by UUID.
+  /// - Parameters:
+  ///   - sessionID: A string for the existing session ID.
+  ///   - plan: A plan to execute.
+  /// - Returns: An ``ExecutePlanRequest`` instance.
+  func getExecutePlanRequest(_ sessionID: String, _ plan: Plan) async
+    -> ExecutePlanRequest
+  {
+    var request = ExecutePlanRequest()
+    request.clientType = clientType
+    request.userContext = userContext
+    request.sessionID = sessionID

Review Comment:
   You are correct, @viirya . Let me fix this.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#discussion_r1990462746


##########
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

Review Comment:
   Yes, `spark.sql()` is the first MVP (Minimum Viable Product) for Apache Spark 4.1.0 timeframe.



-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#issuecomment-2716252115

   Thank you so much, @viirya !


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#issuecomment-2716237805

   Looking into this.


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #7:
URL: https://github.com/apache/spark-connect-swift/pull/7#issuecomment-2716316357

   Thank you so much for your review, Liang-Chi! Merged to main.


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #7: [SPARK-51472] Add gRPC `SparkConnectClient` actor
URL: https://github.com/apache/spark-connect-swift/pull/7


-- 
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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-51472] Add gRPC `SparkConnectClient` actor [spark-connect-swift]

Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
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 it's `SparkConnectClient` in 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-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org