You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2024/07/29 14:07:17 UTC
(spark) branch master updated: [SPARK-48910][SQL] Use HashSet/HashMap to avoid linear searches in PreprocessTableCreation
This is an automated email from the ASF dual-hosted git repository.
wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new db036537ab55 [SPARK-48910][SQL] Use HashSet/HashMap to avoid linear searches in PreprocessTableCreation
db036537ab55 is described below
commit db036537ab5593b2520742b3b1a1028bb0fcc7fa
Author: Vladimir Golubev <vl...@databricks.com>
AuthorDate: Mon Jul 29 22:07:02 2024 +0800
[SPARK-48910][SQL] Use HashSet/HashMap to avoid linear searches in PreprocessTableCreation
### What changes were proposed in this pull request?
Use `HashSet`/`HashMap` instead of doing linear searches over the `Seq`. In case of 1000s of partitions this significantly improves the performance.
### Why are the changes needed?
To avoid the O(n*m) passes in the `PreprocessTableCreation`
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Existing UTs
### Was this patch authored or co-authored using generative AI tooling?
No
Closes #47484 from vladimirg-db/vladimirg-db/get-rid-of-linear-searches-preprocess-table-creation.
Authored-by: Vladimir Golubev <vl...@databricks.com>
Signed-off-by: Wenchen Fan <we...@databricks.com>
---
.../spark/sql/execution/datasources/rules.scala | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
index 9bc0793650ac..9279862c9196 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/rules.scala
@@ -19,6 +19,7 @@ package org.apache.spark.sql.execution.datasources
import java.util.Locale
+import scala.collection.mutable.{HashMap, HashSet}
import scala.jdk.CollectionConverters._
import org.apache.spark.sql.{AnalysisException, SaveMode, SparkSession}
@@ -248,10 +249,14 @@ case class PreprocessTableCreation(catalog: SessionCatalog) extends Rule[Logical
DDLUtils.checkTableColumns(tableDesc.copy(schema = analyzedQuery.schema))
val output = analyzedQuery.output
+
+ val outputByName = HashMap(output.map(o => o.name -> o): _*)
val partitionAttrs = normalizedTable.partitionColumnNames.map { partCol =>
- output.find(_.name == partCol).get
+ outputByName(partCol)
}
- val newOutput = output.filterNot(partitionAttrs.contains) ++ partitionAttrs
+ val partitionAttrsSet = HashSet(partitionAttrs: _*)
+ val newOutput = output.filterNot(partitionAttrsSet.contains) ++ partitionAttrs
+
val reorderedQuery = if (newOutput == output) {
analyzedQuery
} else {
@@ -263,12 +268,14 @@ case class PreprocessTableCreation(catalog: SessionCatalog) extends Rule[Logical
DDLUtils.checkTableColumns(tableDesc)
val normalizedTable = normalizeCatalogTable(tableDesc.schema, tableDesc)
+ val normalizedSchemaByName = HashMap(normalizedTable.schema.map(s => s.name -> s): _*)
val partitionSchema = normalizedTable.partitionColumnNames.map { partCol =>
- normalizedTable.schema.find(_.name == partCol).get
+ normalizedSchemaByName(partCol)
}
-
- val reorderedSchema =
- StructType(normalizedTable.schema.filterNot(partitionSchema.contains) ++ partitionSchema)
+ val partitionSchemaSet = HashSet(partitionSchema: _*)
+ val reorderedSchema = StructType(
+ normalizedTable.schema.filterNot(partitionSchemaSet.contains) ++ partitionSchema
+ )
c.copy(tableDesc = normalizedTable.copy(schema = reorderedSchema))
}
@@ -360,8 +367,9 @@ case class PreprocessTableCreation(catalog: SessionCatalog) extends Rule[Logical
messageParameters = Map.empty)
}
+ val normalizedPartitionColsSet = HashSet(normalizedPartitionCols: _*)
schema
- .filter(f => normalizedPartitionCols.contains(f.name))
+ .filter(f => normalizedPartitionColsSet.contains(f.name))
.foreach { field =>
if (!PartitioningUtils.canPartitionOn(field.dataType)) {
throw QueryCompilationErrors.invalidPartitionColumnDataTypeError(field)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org