This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/spark-connect-swift.git
The following commit(s) were added to refs/heads/main by this push:
new 4760847 [SPARK-57063] Support `csv(DataFrame)` in `DataFrameReader`
4760847 is described below
commit 47608475a0b528af63466e9e932fff49c5ce89d8
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Tue May 26 08:10:22 2026 -0700
[SPARK-57063] Support `csv(DataFrame)` in `DataFrameReader`
### What changes were proposed in this pull request?
This PR aims to support `csv(DataFrame)` overload in `DataFrameReader`.
### Why are the changes needed?
For feature parity with PySpark/Scala `spark.read.csv(csvDataset)`, and to
exercise `Spark_Connect_Parse.ParseFormat.csv` (Apache Spark 4.2.0+).
- https://github.com/apache/spark/pull/55274
### Does this PR introduce _any_ user-facing change?
No behavior change. New public overload added.
### How was this patch tested?
Pass the CIs with the newly added test case.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 4.7
Closes #391 from dongjoon-hyun/SPARK-57063.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
Sources/SparkConnect/DataFrameReader.swift | 21 +++++++++++++++++++++
Tests/SparkConnectTests/DataFrameReaderTests.swift | 14 ++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/Sources/SparkConnect/DataFrameReader.swift
b/Sources/SparkConnect/DataFrameReader.swift
index 5556fdf..28d07ac 100644
--- a/Sources/SparkConnect/DataFrameReader.swift
+++ b/Sources/SparkConnect/DataFrameReader.swift
@@ -194,6 +194,27 @@ public actor DataFrameReader: Sendable {
return load(paths)
}
+ /// Loads a CSV dataset and returns the result as a ``DataFrame``.
+ /// The input ``DataFrame`` must have a single string column whose values
are CSV records.
+ /// - Parameter csvDataset: A ``DataFrame`` with a single string column.
+ /// - Returns: A ``DataFrame``.
+ public func csv(_ csvDataset: DataFrame) async -> DataFrame {
+ var parse = Parse()
+ parse.format = .csv
+ parse.options = self.extraOptions.toStringDictionary()
+ if case .root(let input) = await csvDataset.plan.opType {
+ parse.input = input
+ }
+
+ var relation = Relation()
+ relation.parse = parse
+
+ var plan = Plan()
+ plan.opType = .root(relation)
+
+ return DataFrame(spark: sparkSession, plan: plan)
+ }
+
/// Loads a JSON file and returns the result as a ``DataFrame``.
/// - Parameter path: A path string
/// - Returns: A ``DataFrame``.
diff --git a/Tests/SparkConnectTests/DataFrameReaderTests.swift
b/Tests/SparkConnectTests/DataFrameReaderTests.swift
index 25162d4..a4ce325 100644
--- a/Tests/SparkConnectTests/DataFrameReaderTests.swift
+++ b/Tests/SparkConnectTests/DataFrameReaderTests.swift
@@ -39,6 +39,20 @@ struct DataFrameReaderTests {
await spark.stop()
}
+ @Test
+ func csvDataset() async throws {
+ let spark = try await SparkSession.builder.getOrCreate()
+ if await spark.version >= "4.2.0" {
+ let csvDF = try await spark.sql(
+ "SELECT * FROM VALUES "
+ + "('Alice,25'), "
+ + "('Bob,30') AS T(value)"
+ )
+ #expect(try await spark.read.csv(csvDF).count() == 2)
+ }
+ await spark.stop()
+ }
+
@Test
func json() async throws {
let spark = try await SparkSession.builder.getOrCreate()
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]