From 5bc99cd87b1aaa68076144f57288cf5d2e6a62d7 Mon Sep 17 00:00:00 2001 From: Dongjoon Hyun Date: Thu, 1 May 2025 09:31:54 -0700 Subject: [PATCH] [SPARK-51977] Improve `SparkSQLRepl` to support multiple lines --- README.md | 23 +++++++++++++++++++++++ Sources/SparkSQLRepl/main.swift | 9 +++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eee9add..304dc0a 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,29 @@ spark-sql (default)> DROP DATABASE db1 CASCADE; spark-sql (default)> exit; ``` +Apache Spark 4 supports [SQL Pipe Syntax](https://dist.apache.org/repos/dist/dev/spark/v4.0.0-rc4-docs/_site/sql-pipe-syntax.html). + +``` +$ swift run +... +Build of product 'SparkSQLRepl' complete! (2.33s) +Connected to Apache Spark 4.0.0 Server +spark-sql (default)> +FROM ORC.`/opt/spark/examples/src/main/resources/users.orc` +|> AGGREGATE COUNT(*) cnt + GROUP BY name +|> ORDER BY cnt DESC, name ASC +; ++------+---+ +| name|cnt| ++------+---+ +|Alyssa| 1| +| Ben| 1| ++------+---+ + +Time taken: 159 ms +``` + You can use `SPARK_REMOTE` to specify the [Spark Connect connection string](https://spark.apache.org/docs/latest/spark-connect-overview.html#set-sparkremote-environment-variable) in order to provide more options. ```bash diff --git a/Sources/SparkSQLRepl/main.swift b/Sources/SparkSQLRepl/main.swift index 68c5fb0..6c45ae0 100644 --- a/Sources/SparkSQLRepl/main.swift +++ b/Sources/SparkSQLRepl/main.swift @@ -26,15 +26,20 @@ let spark = try await SparkSession.builder.getOrCreate() print("Connected to Apache Spark \(await spark.version) Server") var isRunning = true +var lines = "" while isRunning { - print("spark-sql (\(try await spark.catalog.currentDatabase()))> ", terminator: "") + if lines.isEmpty { + print("spark-sql (\(try await spark.catalog.currentDatabase()))> ", terminator: "") + } guard let input = readLine() else { isRunning = false break } + lines += input + " " - let matches = input.matches(of: statement) + let matches = lines.matches(of: statement) for match in matches { + lines = "" switch match.1 { case "exit": isRunning = false