Skip to content

Commit 3fe1f0e

Browse files
committed
[SPARK-51621] Support sparkSession for DataFrame
### What changes were proposed in this pull request? This PR aims to support `sparkSession` API for `DataFrame`. ### Why are the changes needed? For feature parity. ### Does this PR introduce _any_ user-facing change? No, this is a new addition. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #28 from dongjoon-hyun/SPARK-51621. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 8dedd94 commit 3fe1f0e

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

Sources/SparkConnect/DataFrame.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public actor DataFrame: Sendable {
6262
self.batches.append(contentsOf: batches)
6363
}
6464

65+
/// Return the `SparkSession` of this `DataFrame`.
66+
/// - Returns: A `SparkSession`
67+
public func sparkSession() -> SparkSession {
68+
return self.spark
69+
}
70+
6571
/// A method to access the underlying Spark's `RDD`.
6672
/// In `Spark Connect`, this feature is not allowed by design.
6773
public func rdd() throws {

Sources/SparkConnect/Extension.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ extension Data {
7777
/// Get an `Int32` value from unsafe 4 bytes.
7878
var int32: Int32 { withUnsafeBytes({ $0.load(as: Int32.self) }) }
7979
}
80+
81+
extension SparkSession: Equatable {
82+
public static func == (lhs: SparkSession, rhs: SparkSession) -> Bool {
83+
return lhs.sessionID == rhs.sessionID
84+
}
85+
}

Sources/SparkConnect/SparkSession.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public actor SparkSession {
6262
}
6363

6464
/// A unique session ID for this session from client.
65-
var sessionID: String = UUID().uuidString
65+
nonisolated let sessionID: String = UUID().uuidString
6666

6767
/// Get the current session ID
6868
/// - Returns: the current session ID

Tests/SparkConnectTests/DataFrameTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ import Testing
2323

2424
/// A test suite for `DataFrame`
2525
struct DataFrameTests {
26+
@Test
27+
func sparkSession() async throws {
28+
let spark = try await SparkSession.builder.getOrCreate()
29+
#expect(try await spark.range(1).sparkSession() == spark)
30+
await spark.stop()
31+
}
32+
2633
@Test
2734
func rdd() async throws {
2835
let spark = try await SparkSession.builder.getOrCreate()

0 commit comments

Comments
 (0)