Skip to content

[SPARK-52817][SQL] Fix Like Expression performance #51510

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -743,10 +743,10 @@ object SupportedBinaryExpr {
object LikeSimplification extends Rule[LogicalPlan] with PredicateHelper {
// if guards below protect from escapes on trailing %.
// Cases like "something\%" are not optimized, but this does not affect correctness.
private val startsWith = "([^_%]+)%".r
private val endsWith = "%([^_%]+)".r
private val startsAndEndsWith = "([^_%]+)%([^_%]+)".r
private val contains = "%([^_%]+)%".r
private val startsWith = "([^_%]+)%+".r
Copy link
Contributor

@cloud-fan cloud-fan Jul 17, 2025

Choose a reason for hiding this comment

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

So a single % is the same as more than one %? Can we leave a code comment to explain this case?

private val endsWith = "%+([^_%]+)".r
private val startsAndEndsWith = "([^_%]+)%+([^_%]+)".r
private val contains = "%+([^_%]+)%+".r
private val equalTo = "([^_%]*)".r

private def simplifyLike(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,54 @@ class LikeSimplificationSuite extends PlanTest {
comparePlans(optimized5, correctAnswer5)
}

test("SPARK-52817: Spark SQL LIKE expressions show poor performance when using multiple '%'") {
val originalQuery1 =
testRelation
.where($"a" like "abc%%")
val optimized1 = Optimize.execute(originalQuery1.analyze)
val correctAnswer1 = testRelation
.where(StartsWith($"a", "abc"))
.analyze
comparePlans(optimized1, correctAnswer1)

val originalQuery2 =
testRelation
.where($"a" like "%%xyz")
val optimized2 = Optimize.execute(originalQuery2.analyze)
val correctAnswer2 = testRelation
.where(EndsWith($"a", "xyz"))
.analyze
comparePlans(optimized2, correctAnswer2)

val originalQuery3 =
testRelation
.where($"a" like "abc%%def")
val optimized3 = Optimize.execute(originalQuery3.analyze)
val correctAnswer3 = testRelation
.where(
(Length($"a") >= 6 && (StartsWith($"a", "abc") && EndsWith($"a", "def"))))
.analyze
comparePlans(optimized3, correctAnswer3)

val originalQuery4 =
testRelation
.where(($"a" like "%%mn%%"))
val optimized4 = Optimize.execute(originalQuery4.analyze)
val correctAnswer4 = testRelation
.where(Contains($"a", "mn"))
.analyze
comparePlans(optimized4, correctAnswer4)

val originalQuery5 =
testRelation
.where(($"a" like "%%%mn%%%"))
val optimized5 = Optimize.execute(originalQuery5.analyze)
val correctAnswer5 = testRelation
.where(Contains($"a", "mn"))
.analyze
comparePlans(optimized5, correctAnswer5)
}

test("simplify LikeAll") {
val originalQuery =
testRelation
Expand Down