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
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,16 @@ case class RepartitionByExpression(
child: LogicalPlan,
optNumPartitions: Option[Int]) extends RepartitionOperation {

val numPartitions = optNumPartitions.getOrElse(SQLConf.get.numShufflePartitions)
val numPartitions = if (optNumPartitions.nonEmpty) {
optNumPartitions.get
} else {
if (partitionExpressions.forall(_.foldable)) {
1
} else {
SQLConf.get.numShufflePartitions
}
}

require(numPartitions > 0, s"Number of partitions ($numPartitions) must be positive.")

val partitioning: Partitioning = {
Expand Down
15 changes: 14 additions & 1 deletion sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.apache.spark.scheduler.{SparkListener, SparkListenerJobStart}
import org.apache.spark.sql.catalyst.expressions.GenericRow
import org.apache.spark.sql.catalyst.expressions.aggregate.{Complete, Partial}
import org.apache.spark.sql.catalyst.optimizer.{ConvertToLocalRelation, NestedColumnAliasingSuite}
import org.apache.spark.sql.catalyst.plans.logical.Project
import org.apache.spark.sql.catalyst.plans.logical.{Project, RepartitionByExpression}
import org.apache.spark.sql.catalyst.util.StringUtils
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec}
Expand Down Expand Up @@ -3732,6 +3732,19 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
checkAnswer(sql("SELECT s LIKE 'm@@ca' ESCAPE '@' FROM df"), Row(true))
}
}

test("limit partition num to 1 when distributing by foldable expressions") {
withSQLConf((SQLConf.SHUFFLE_PARTITIONS.key, "5")) {
Seq(1, "1, 2", null, "version()").foreach { expr =>
val plan = sql(s"select * from values (1), (2), (3) t(a) distribute by $expr")
.queryExecution.optimizedPlan
val res = plan.collect {
case r: RepartitionByExpression if r.numPartitions == 1 => true
}
assert(res.nonEmpty)
}
}
}
}

case class Foo(bar: Option[String])