-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-48551][SQL] Perf improvement for escapePathName #46894
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eff6d77
[SPARK-48548][SQL] Perf improvement for escapePathName
yaooqinn 019cfb2
[SPARK-48548][SQL] Perf improvement for escapePathName
yaooqinn d04b589
Update sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/cata…
yaooqinn 3fac8fc
Update sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/cata…
yaooqinn e3e783d
tests
yaooqinn 072a957
nit
yaooqinn e118917
address comments
yaooqinn bac8b9d
update golden files
yaooqinn c6a0cf7
corner case
yaooqinn 74dee2b
address comments
yaooqinn bd38e8b
address comments
yaooqinn b7c1e0c
address comments
yaooqinn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ object ExternalCatalogUtils { | |
| // The following string escaping code is mainly copied from Hive (o.a.h.h.common.FileUtils). | ||
| ////////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| val charToEscape = { | ||
| final val charToEscape = { | ||
| val bitSet = new java.util.BitSet(128) | ||
|
|
||
| /** | ||
|
|
@@ -63,29 +63,35 @@ object ExternalCatalogUtils { | |
| bitSet | ||
| } | ||
|
|
||
| @inline private final def needsEscaping(c: Char): Boolean = { | ||
| private final val HEX_CHARS = "0123456789ABCDEF".toCharArray | ||
|
|
||
| @inline final def needsEscaping(c: Char): Boolean = { | ||
| c < charToEscape.size() && charToEscape.get(c) | ||
| } | ||
|
|
||
| def escapePathName(path: String): String = { | ||
| val firstIndex = path.indexWhere(needsEscaping) | ||
| if (firstIndex == -1) { | ||
| var firstIndex = 0 | ||
| val length = path.length | ||
| while (firstIndex < length && !needsEscaping(path.charAt(firstIndex))) { | ||
| firstIndex += 1 | ||
| } | ||
| if (firstIndex == 0) { | ||
| path | ||
| } else { | ||
| val builder = new StringBuilder(path.substring(0, firstIndex)) | ||
| path.substring(firstIndex).foreach { c => | ||
| val sb = new StringBuilder(path.substring(0, firstIndex)) | ||
| while(firstIndex < length) { | ||
| val c = path.charAt(firstIndex) | ||
| if (needsEscaping(c)) { | ||
| builder.append('%') | ||
| builder.append(f"${c.asInstanceOf[Int]}%02X") | ||
| sb.append('%').append(HEX_CHARS((c & 0xF0) >> 4)).append(HEX_CHARS(c & 0x0F)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } else { | ||
| builder.append(c) | ||
| sb.append(c) | ||
| } | ||
| firstIndex += 1 | ||
| } | ||
| builder.toString() | ||
| sb.toString() | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def unescapePathName(path: String): String = { | ||
| val sb = new StringBuilder | ||
| var i = 0 | ||
|
|
||
63 changes: 63 additions & 0 deletions
63
sql/core/src/test/scala/org/apache/spark/sql/EscapePathBenchmark.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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 | ||
yaooqinn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import org.apache.spark.benchmark.{Benchmark, BenchmarkBase} | ||
| import org.apache.spark.sql.catalyst.catalog.ExternalCatalogUtils | ||
|
|
||
| object EscapePathBenchmark extends BenchmarkBase { | ||
| override def runBenchmarkSuite(mainArgs: Array[String]): Unit = { | ||
| val N = 1000000 | ||
| runBenchmark("Escape") { | ||
| val benchmark = new Benchmark("Escape Tests", N, 10, output = output) | ||
| val paths = Seq( | ||
| "https://issues.apache.org/jira/browse/SPARK-48551", | ||
| "https...issues.apache.org/jira/browse/SPARK-48551", | ||
| "https...issues.apache.org.jira/browse/SPARK-48551", | ||
| "https...issues.apache.org.jira.browse/SPARK-48551", | ||
| "https...issues.apache.org.jira.browse.SPARK-48551") | ||
| benchmark.addCase("Legacy") { _ => | ||
| (1 to N).foreach(_ => paths.foreach(escapePathNameLegacy)) | ||
| } | ||
|
|
||
| benchmark.addCase("New") { _ => | ||
| (1 to N).foreach(_ => { | ||
| paths.foreach(ExternalCatalogUtils.escapePathName) | ||
| }) | ||
| } | ||
| benchmark.run() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Legacy implementation of escapePathName before Spark 4.0 | ||
| */ | ||
| def escapePathNameLegacy(path: String): String = { | ||
| val builder = new StringBuilder() | ||
| path.foreach { c => | ||
| if (ExternalCatalogUtils.needsEscaping(c)) { | ||
| builder.append('%') | ||
| builder.append(f"${c.asInstanceOf[Int]}%02X") | ||
| } else { | ||
| builder.append(c) | ||
| } | ||
| } | ||
|
|
||
| builder.toString() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.