Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Sources/SparkConnect/DataFrame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,29 @@ public actor DataFrame: Sendable {

return self
}

public func explain() async throws {
try await explain("simple")
}

public func explain(_ extended: Bool) async throws {
if (extended) {
try await explain("extended")
} else {
try await explain("simple")
}
}

public func explain(_ mode: String) async throws {
try await withGRPCClient(
transport: .http2NIOPosix(
target: .dns(host: spark.client.host, port: spark.client.port),
transportSecurity: .plaintext
)
) { client in
let service = Spark_Connect_SparkConnectService.Client(wrapping: client)
let response = try await service.analyzePlan(spark.client.getExplain(spark.sessionID, plan, mode))
print(response.explain.explainString)
}
}
}
12 changes: 12 additions & 0 deletions Sources/SparkConnect/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ extension String {
expression.expression = self
return expression
}

var toExplainMode: ExplainMode {
let mode = switch self {
case "codegen": ExplainMode.codegen
case "cost": ExplainMode.cost
case "extended": ExplainMode.extended
case "formatted": ExplainMode.formatted
case "simple": ExplainMode.simple
default: ExplainMode.simple
}
return mode
}
}

extension [String: String] {
Expand Down
12 changes: 12 additions & 0 deletions Sources/SparkConnect/SparkConnectClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ public actor SparkConnectClient {
})
}

func getExplain(_ sessionID: String, _ plan: Plan, _ mode: String) async -> AnalyzePlanRequest
{
return analyze(
sessionID,
{
var explain = AnalyzePlanRequest.Explain()
explain.plan = plan
explain.explainMode = mode.toExplainMode
return OneOf_Analyze.explain(explain)
})
}

static func getProject(_ child: Relation, _ cols: [String]) -> Plan {
var project = Project()
project.input = child
Expand Down
1 change: 1 addition & 0 deletions Sources/SparkConnect/TypeAliases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typealias ConfigRequest = Spark_Connect_ConfigRequest
typealias DataSource = Spark_Connect_Read.DataSource
typealias DataType = Spark_Connect_DataType
typealias ExecutePlanRequest = Spark_Connect_ExecutePlanRequest
typealias ExplainMode = AnalyzePlanRequest.Explain.ExplainMode
typealias ExpressionString = Spark_Connect_Expression.ExpressionString
typealias Filter = Spark_Connect_Filter
typealias KeyValue = Spark_Connect_KeyValue
Expand Down
9 changes: 9 additions & 0 deletions Tests/SparkConnectTests/DataFrameTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ struct DataFrameTests {
await spark.stop()
}

@Test
func explain() async throws {
let spark = try await SparkSession.builder.getOrCreate()
try await spark.range(1).explain()
try await spark.range(1).explain(true)
try await spark.range(1).explain("formatted")
await spark.stop()
}

@Test
func count() async throws {
let spark = try await SparkSession.builder.getOrCreate()
Expand Down
Loading