Skip to content

Commit 0896186

Browse files
committed
Add isEmpty
1 parent 8f10457 commit 0896186

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Sources/SparkConnect/DataFrame.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,37 @@ public actor DataFrame: Sendable {
193193
}
194194
}
195195

196+
/// Projects a set of expressions and returns a new ``DataFrame``.
197+
/// - Parameter cols: Column names
198+
/// - Returns: A ``DataFrame`` with subset of columns.
196199
public func select(_ cols: String...) -> DataFrame {
197200
return DataFrame(spark: self.spark, plan: SparkConnectClient.getProject(self.plan.root, cols))
198201
}
199202

203+
/// Return a new ``DataFrame`` sorted by the specified column(s).
204+
/// - Parameter cols: Column names.
205+
/// - Returns: A sorted ``DataFrame``
200206
public func sort(_ cols: String...) -> DataFrame {
201207
return DataFrame(spark: self.spark, plan: SparkConnectClient.getSort(self.plan.root, cols))
202208
}
203209

210+
/// <#Description#>
211+
/// - Parameter cols: <#cols description#>
212+
/// - Returns: <#description#>
204213
public func orderBy(_ cols: String...) -> DataFrame {
205214
return DataFrame(spark: self.spark, plan: SparkConnectClient.getSort(self.plan.root, cols))
206215
}
207216

217+
/// Limits the result count to the number specified.
218+
/// - Parameter n: Number of records to return. Will return this number of records or all records if the ``DataFrame`` contains less than this number of records.
219+
/// - Returns: A subset of the records
208220
public func limit(_ n: Int32) -> DataFrame {
209221
return DataFrame(spark: self.spark, plan: SparkConnectClient.getLimit(self.plan.root, n))
210222
}
223+
224+
/// Chec if the ``DataFrame`` is empty and returns a boolean value.
225+
/// - Returns: `true` if the ``DataFrame`` is empty, `false` otherwise.
226+
public func isEmpty() async throws -> Bool {
227+
return try await select().limit(1).count() == 0
228+
}
211229
}

Tests/SparkConnectTests/DataFrameTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ struct DataFrameTests {
116116
await spark.stop()
117117
}
118118

119+
@Test
120+
func isEmpty() async throws {
121+
let spark = try await SparkSession.builder.getOrCreate()
122+
#expect(try await spark.range(0).isEmpty())
123+
#expect(!(try await spark.range(1).isEmpty()))
124+
await spark.stop()
125+
}
126+
119127
@Test
120128
func sort() async throws {
121129
let spark = try await SparkSession.builder.getOrCreate()

0 commit comments

Comments
 (0)