Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spark SQL engine supports Spark 4.0 #6920

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ on:
schedule:
- cron: 0 4 * * *

pull_request:
branches:
- master
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will revert after passing test


jobs:
build:
name: Build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.time.ZoneId
import org.apache.spark.kyuubi.{SparkProgressMonitor, SQLOperationListener}
import org.apache.spark.kyuubi.SparkUtilsHelper.redact
import org.apache.spark.sql.{DataFrame, Row, SparkSession}
import org.apache.spark.sql.execution.SQLExecution
import org.apache.spark.sql.execution.SparkSQLExecutionHelper
import org.apache.spark.sql.types.{BinaryType, StructField, StructType}
import org.apache.spark.ui.SparkUIUtils.formatDuration

Expand Down Expand Up @@ -155,7 +155,7 @@ abstract class SparkOperation(session: Session)
spark.sparkContext.setLocalProperty

protected def withLocalProperties[T](f: => T): T = {
SQLExecution.withSQLConfPropagated(spark) {
SparkSQLExecutionHelper.withSQLConfPropagated(spark) {
val originalSession = SparkSession.getActiveSession
try {
SparkSession.setActiveSession(spark)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.execution

import org.apache.spark.sql.SparkSession

import org.apache.kyuubi.util.reflect.{DynClasses, DynMethods}

object SparkSQLExecutionHelper {

private val sparkSessionClz = DynClasses.builder()
.impl("org.apache.spark.sql.classic.SparkSession") // SPARK-49700 (4.0.0)
.impl("org.apache.spark.sql.SparkSession")
.build()

private val withSQLConfPropagatedMethod =
DynMethods.builder("withSQLConfPropagated")
.impl(SQLExecution.getClass, sparkSessionClz, classOf[() => Any])
.buildChecked(SQLExecution)

def withSQLConfPropagated[T](sparkSession: SparkSession)(body: => T): T = {
withSQLConfPropagatedMethod.invokeChecked[T](sparkSession, () => body)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.apache.spark.sql.types._
import org.apache.kyuubi.engine.spark.KyuubiSparkUtil
import org.apache.kyuubi.engine.spark.schema.RowSet
import org.apache.kyuubi.engine.spark.util.SparkCatalogUtils.quoteIfNeeded
import org.apache.kyuubi.util.reflect.DynMethods
import org.apache.kyuubi.util.reflect.{DynClasses, DynMethods}

object SparkDatasetHelper extends Logging {

Expand All @@ -63,8 +63,18 @@ object SparkDatasetHelper extends Logging {
toArrowBatchRdd(plan).collect()
}

private val datasetClz = DynClasses.builder()
.impl("org.apache.spark.sql.classic.Dataset") // SPARK-49700 (4.0.0)
.impl("org.apache.spark.sql.Dataset")
.build()

private val toArrowBatchRddMethod =
DynMethods.builder("toArrowBatchRdd")
.impl(datasetClz)
.buildChecked()

def toArrowBatchRdd[T](ds: Dataset[T]): RDD[Array[Byte]] = {
ds.toArrowBatchRdd
toArrowBatchRddMethod.bind(ds).invoke()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.apache.spark.sql.{QueryTest, Row, SparkSession}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.plans.logical.Project
import org.apache.spark.sql.execution.{CollectLimitExec, LocalTableScanExec, QueryExecution, SparkPlan}
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanExec
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanExec, QueryStageExec}
import org.apache.spark.sql.execution.exchange.Exchange
import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, SortMergeJoinExec}
import org.apache.spark.sql.execution.metric.SparkMetricsTestUtils
Expand Down Expand Up @@ -163,6 +163,7 @@ class SparkArrowbasedOperationSuite extends WithSparkSQLEngine with SparkDataTyp
sparkPlan.schema,
"",
true,
true, // spark.sql.execution.arrow.useLargeVarTypes
KyuubiSparkContextHelper.dummyTaskContext())
assert(rows.size == expectSize)
}
Expand Down Expand Up @@ -247,7 +248,11 @@ class SparkArrowbasedOperationSuite extends WithSparkSQLEngine with SparkDataTyp
|) LIMIT 1
|""".stripMargin)
val smj = plan.collect { case smj: SortMergeJoinExec => smj }
val bhj = adaptivePlan.collect { case bhj: BroadcastHashJoinExec => bhj }
val bhj = (adaptivePlan match {
// SPARK-51008 (4.0.0) adds ResultQueryStageExec
case queryStage: QueryStageExec => queryStage.plan
case plan => plan
}).collect { case bhj: BroadcastHashJoinExec => bhj }
assert(smj.size == 1)
assert(bhj.size == 1)
}
Expand Down Expand Up @@ -505,33 +510,49 @@ class SparkArrowbasedOperationSuite extends WithSparkSQLEngine with SparkDataTyp
}
}

// the signature of function [[ArrowConverters.fromBatchIterator]] is changed in SPARK-43528
// (since Spark 3.5)
private lazy val fromBatchIteratorMethod = DynMethods.builder("fromBatchIterator")
.hiddenImpl( // for Spark 3.4 or previous
"org.apache.spark.sql.execution.arrow.ArrowConverters$",
classOf[Iterator[Array[Byte]]],
classOf[StructType],
classOf[String],
classOf[TaskContext])
.hiddenImpl( // for Spark 3.5 or later
.hiddenImpl( // SPARK-43528: Spark 3.5
"org.apache.spark.sql.execution.arrow.ArrowConverters$",
classOf[Iterator[Array[Byte]]],
classOf[StructType],
classOf[String],
classOf[Boolean],
classOf[TaskContext])
.hiddenImpl( // SPARK-51079: Spark 4.0 or later
"org.apache.spark.sql.execution.arrow.ArrowConverters$",
classOf[Iterator[Array[Byte]]],
classOf[StructType],
classOf[String],
classOf[Boolean],
classOf[Boolean],
classOf[TaskContext])
.build()

def fromBatchIterator(
arrowBatchIter: Iterator[Array[Byte]],
schema: StructType,
timeZoneId: String,
errorOnDuplicatedFieldNames: JBoolean,
largeVarTypes: Boolean,
context: TaskContext): Iterator[InternalRow] = {
val className = "org.apache.spark.sql.execution.arrow.ArrowConverters$"
val instance = DynFields.builder().impl(className, "MODULE$").build[Object]().get(null)
if (SPARK_ENGINE_RUNTIME_VERSION >= "3.5") {
if (SPARK_ENGINE_RUNTIME_VERSION >= "4.0") {
fromBatchIteratorMethod.invoke[Iterator[InternalRow]](
instance,
arrowBatchIter,
schema,
timeZoneId,
errorOnDuplicatedFieldNames,
largeVarTypes,
context)
} else if (SPARK_ENGINE_RUNTIME_VERSION === "3.5") {
fromBatchIteratorMethod.invoke[Iterator[InternalRow]](
instance,
arrowBatchIter,
Expand Down
Loading