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 34ca440 [SPARK-58477] Support `when/otherwise` in `Column` and
`Functions`
34ca440 is described below
commit 34ca4403b5c8c9f9c694bee17fbddc01de669b12
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Fri Jul 31 11:07:35 2026 -0700
[SPARK-58477] Support `when/otherwise` in `Column` and `Functions`
### What changes were proposed in this pull request?
This PR aims to support `when`/`otherwise` conditional expressions in
`Column` and `Functions`.
```swift
df.select(when(col("age") > 60, "senior").when(col("age") > 21,
"adult").otherwise("minor"))
```
### Why are the changes needed?
For feature parity with the Scala and Python Spark clients.
### Does this PR introduce _any_ user-facing change?
No. This is a new API addition.
### How was this patch tested?
Pass the CIs with newly added tests.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Fable 5
Closes #459 from dongjoon-hyun/SPARK-58477.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
---
Sources/SparkConnect/Column.swift | 65 ++++++++++++++++++++++++++++
Sources/SparkConnect/Functions.swift | 26 +++++++++++
Tests/SparkConnectTests/FunctionsTests.swift | 47 ++++++++++++++++++++
3 files changed, 138 insertions(+)
diff --git a/Sources/SparkConnect/Column.swift
b/Sources/SparkConnect/Column.swift
index d7663f0..5698436 100644
--- a/Sources/SparkConnect/Column.swift
+++ b/Sources/SparkConnect/Column.swift
@@ -308,6 +308,71 @@ public struct Column: Sendable {
return Column(expr)
}
+ // MARK: - Conditional expressions
+
+ /// Evaluates a list of conditions and returns one of multiple possible
result expressions.
+ /// If `otherwise(_:)` is not defined at the end, null is returned for
+ /// unmatched conditions. This can only be applied on a ``Column``
previously generated
+ /// by `when(_:_:)` before `otherwise` is applied.
+ ///
+ /// ```swift
+ /// df.select(when(col("age") > 60, "senior").when(col("age") > 21,
"adult").otherwise("minor"))
+ /// ```
+ /// - Parameters:
+ /// - condition: A condition ``Column``.
+ /// - value: A value ``Column`` to return when the condition is true.
+ /// - Returns: A ``Column``.
+ public func when(_ condition: Column, _ value: Column) throws -> Column {
+ guard isChainableCaseWhen else {
+ throw SparkConnectError.InvalidArgument
+ }
+ var function = self.expr.unresolvedFunction
+ function.arguments += [condition.expr, value.expr]
+ var expr = Spark_Connect_Expression()
+ expr.unresolvedFunction = function
+ return Column(expr)
+ }
+
+ /// Evaluates a list of conditions and returns one of multiple possible
result expressions.
+ /// - Parameters:
+ /// - condition: A condition ``Column``.
+ /// - value: A literal value to return when the condition is true.
+ /// - Returns: A ``Column``.
+ public func when(_ condition: Column, _ value: some SparkLiteral) throws ->
Column {
+ return try when(condition, value.toLiteralColumn)
+ }
+
+ /// Returns the given value if all conditions of the preceding `when(_:_:)`
are unmatched.
+ /// This can only be applied once on a ``Column`` previously generated by
`when(_:_:)`.
+ /// - Parameter value: A value ``Column``.
+ /// - Returns: A ``Column``.
+ public func otherwise(_ value: Column) throws -> Column {
+ guard isChainableCaseWhen else {
+ throw SparkConnectError.InvalidArgument
+ }
+ var function = self.expr.unresolvedFunction
+ function.arguments.append(value.expr)
+ var expr = Spark_Connect_Expression()
+ expr.unresolvedFunction = function
+ return Column(expr)
+ }
+
+ /// Returns the given value if all conditions of the preceding `when(_:_:)`
are unmatched.
+ /// This can only be applied once on a ``Column`` previously generated by
`when(_:_:)`.
+ /// - Parameter value: A literal value.
+ /// - Returns: A ``Column``.
+ public func otherwise(_ value: some SparkLiteral) throws -> Column {
+ return try otherwise(value.toLiteralColumn)
+ }
+
+ /// Whether this is a `CASE WHEN` expression without an `otherwise` value
yet. A `when`
+ /// expression holds interleaved (condition, value) argument pairs, so an
odd number of
+ /// arguments means `otherwise` is already applied.
+ private var isChainableCaseWhen: Bool {
+ return expr.unresolvedFunction.functionName == "when"
+ && expr.unresolvedFunction.arguments.count % 2 == 0
+ }
+
private func sortOrder(
_ direction: Spark_Connect_Expression.SortOrder.SortDirection,
_ nullOrdering: Spark_Connect_Expression.SortOrder.NullOrdering
diff --git a/Sources/SparkConnect/Functions.swift
b/Sources/SparkConnect/Functions.swift
index b3c97fb..a31fe33 100644
--- a/Sources/SparkConnect/Functions.swift
+++ b/Sources/SparkConnect/Functions.swift
@@ -217,6 +217,32 @@ public func max(_ col: Column) -> Column {
return fn("max", col)
}
+// MARK: - Condition functions
+
+/// Evaluates a list of conditions and returns one of multiple possible result
expressions.
+/// If `otherwise` is not defined at the end, null is returned for unmatched
conditions.
+///
+/// ```swift
+/// df.select(when(col("age") > 21, "adult").otherwise("minor").alias("group"))
+/// ```
+/// - Parameters:
+/// - condition: A condition ``Column``.
+/// - value: A value ``Column`` to return when the condition is true.
+/// - Returns: A ``Column``.
+public func when(_ condition: Column, _ value: Column) -> Column {
+ return fn("when", condition, value)
+}
+
+/// Evaluates a list of conditions and returns one of multiple possible result
expressions.
+/// If `otherwise` is not defined at the end, null is returned for unmatched
conditions.
+/// - Parameters:
+/// - condition: A condition ``Column``.
+/// - value: A literal value to return when the condition is true.
+/// - Returns: A ``Column``.
+public func when(_ condition: Column, _ value: some SparkLiteral) -> Column {
+ return when(condition, value.toLiteralColumn)
+}
+
private func fn(_ name: String, _ args: Column...) -> Column {
var function = Spark_Connect_Expression.UnresolvedFunction()
function.functionName = name
diff --git a/Tests/SparkConnectTests/FunctionsTests.swift
b/Tests/SparkConnectTests/FunctionsTests.swift
index 3d18289..a54a358 100644
--- a/Tests/SparkConnectTests/FunctionsTests.swift
+++ b/Tests/SparkConnectTests/FunctionsTests.swift
@@ -107,6 +107,38 @@ struct FunctionsTests {
}
}
+ @Test
+ func whenFunction() throws {
+ let expr = when(col("a") > 1, lit(10)).expr
+ #expect(expr.unresolvedFunction.functionName == "when")
+ #expect(expr.unresolvedFunction.arguments.count == 2)
+
+ let chained = try when(col("a") > 2, 10).when(col("a") > 1,
20).otherwise(0).expr
+ #expect(chained.unresolvedFunction.functionName == "when")
+ #expect(chained.unresolvedFunction.arguments.count == 5)
+ #expect(chained.unresolvedFunction.arguments[1].literal.long == 10)
+ #expect(chained.unresolvedFunction.arguments[3].literal.long == 20)
+ #expect(chained.unresolvedFunction.arguments[4].literal.long == 0)
+ }
+
+ @Test
+ func whenMisuse() throws {
+ // `when`/`otherwise` can only be applied on a column generated by `when`.
+ #expect(throws: SparkConnectError.InvalidArgument) {
+ try col("a").when(col("b") > 1, 1)
+ }
+ #expect(throws: SparkConnectError.InvalidArgument) {
+ try col("a").otherwise(1)
+ }
+ // `when`/`otherwise` cannot be applied once `otherwise` is applied.
+ #expect(throws: SparkConnectError.InvalidArgument) {
+ try when(col("a") > 1, 1).otherwise(2).when(col("a") > 0, 3)
+ }
+ #expect(throws: SparkConnectError.InvalidArgument) {
+ try when(col("a") > 1, 1).otherwise(2).otherwise(3)
+ }
+ }
+
@Test
func comparisonOperators() throws {
for (column, name) in [
@@ -412,4 +444,19 @@ struct FunctionsTests {
#expect(rows == [Row(1, 10, 7)])
await spark.stop()
}
+
+ @Test
+ func selectWhenOtherwise() async throws {
+ let spark = try await SparkSession.builder.getOrCreate()
+ let df = try await spark.range(4)
+ let rows = try await df.select(
+ when(col("id") < 1, "zero").when(col("id") < 3,
"few").otherwise("many").alias("group")
+ ).collect()
+ #expect(rows == [Row("zero"), Row("few"), Row("few"), Row("many")])
+
+ // Without `otherwise`, null is returned for unmatched conditions.
+ let nullable = try await df.select(when(col("id") == 0,
"zero").alias("group")).collect()
+ #expect(nullable == [Row("zero"), Row(nil), Row(nil), Row(nil)])
+ await spark.stop()
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]