You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by do...@apache.org on 2026/08/01 02:11:37 UTC

(spark-connect-swift) branch main updated: [SPARK-58491] Support `expr` in `Functions`

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 07e9161  [SPARK-58491] Support `expr` in `Functions`
07e9161 is described below

commit 07e91613c9bc2ab127084427e1fc4f20dff9a39a
Author: Dongjoon Hyun <do...@apache.org>
AuthorDate: Fri Jul 31 19:11:29 2026 -0700

    [SPARK-58491] Support `expr` in `Functions`
    
    ### What changes were proposed in this pull request?
    
    This PR aims to support `expr` function in `Functions`.
    
    ```swift
    public func expr(_ sqlExpr: String) -> Column
    ```
    
    ### Why are the changes needed?
    
    To improve the API compatibility with the Scala client's `functions.expr`, allowing users to build a `Column` from a SQL expression string.
    
    ```swift
    df.select(expr("id + 1"))
    df.filter(expr("id > 1"))
    ```
    
    ### 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 test cases.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Fable 5
    
    Closes #464 from dongjoon-hyun/SPARK-58491.
    
    Authored-by: Dongjoon Hyun <do...@apache.org>
    Signed-off-by: Dongjoon Hyun <do...@apache.org>
---
 Sources/SparkConnect/Functions.swift         | 12 ++++++++++++
 Tests/SparkConnectTests/FunctionsTests.swift | 16 ++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/Sources/SparkConnect/Functions.swift b/Sources/SparkConnect/Functions.swift
index 4943bd2..edf6ab6 100644
--- a/Sources/SparkConnect/Functions.swift
+++ b/Sources/SparkConnect/Functions.swift
@@ -175,6 +175,18 @@ public func desc(_ name: String) -> Column {
   return Column(name).desc()
 }
 
+/// Parses the expression string into the column that it represents.
+///
+/// ```swift
+/// df.select(expr("id + 1"))
+/// ```
+///
+/// - Parameter sqlExpr: A SQL expression string.
+/// - Returns: A ``Column``.
+public func expr(_ sqlExpr: String) -> Column {
+  return Column(sqlExpr.toExpression)
+}
+
 /// Returns the number of items in a group.
 /// - Parameter col: A ``Column`` to count.
 /// - Returns: A ``Column``.
diff --git a/Tests/SparkConnectTests/FunctionsTests.swift b/Tests/SparkConnectTests/FunctionsTests.swift
index a54a358..cd5e456 100644
--- a/Tests/SparkConnectTests/FunctionsTests.swift
+++ b/Tests/SparkConnectTests/FunctionsTests.swift
@@ -59,6 +59,11 @@ struct FunctionsTests {
     #expect(lit("a").expr.literal.string == "a")
   }
 
+  @Test
+  func exprFunction() throws {
+    #expect(expr("id + 1").expr.expressionString.expression == "id + 1")
+  }
+
   @Test
   func alias() throws {
     let expr = col("id").alias("x").expr
@@ -359,6 +364,17 @@ struct FunctionsTests {
     await spark.stop()
   }
 
+  @Test
+  func selectWithExpr() async throws {
+    let spark = try await SparkSession.builder.getOrCreate()
+    let df = try await spark.range(3)
+    #expect(
+      try await df.select(expr("id + 1").alias("plus")).orderBy("plus").collect()
+        == [Row(1), Row(2), Row(3)])
+    #expect(try await df.filter(expr("id > 1")).count() == 1)
+    await spark.stop()
+  }
+
   @Test
   func filterWithColumn() async throws {
     let spark = try await SparkSession.builder.getOrCreate()


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