Skip to content

Commit

Permalink
fix: Fallback to Spark for LIKE with custom escape character (#478)
Browse files Browse the repository at this point in the history
* Fallback to Spark for LIKE with custom escape character

Currently, LIKE with custom escape character produces incorrect results.

* For custom escape character, provide user with specific info message

* Test case for default escape char with checkSparkAnswerAndOperator
  • Loading branch information
sujithjay authored May 28, 2024
1 parent ee15460 commit fd90a28
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
33 changes: 19 additions & 14 deletions spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1074,23 +1074,28 @@ object QueryPlanSerde extends Logging with ShimQueryPlanSerde with CometExprShim
None
}

case Like(left, right, _) =>
// TODO escapeChar
val leftExpr = exprToProtoInternal(left, inputs)
val rightExpr = exprToProtoInternal(right, inputs)
case Like(left, right, escapeChar) =>
if (escapeChar == '\\') {
val leftExpr = exprToProtoInternal(left, inputs)
val rightExpr = exprToProtoInternal(right, inputs)

if (leftExpr.isDefined && rightExpr.isDefined) {
val builder = ExprOuterClass.Like.newBuilder()
builder.setLeft(leftExpr.get)
builder.setRight(rightExpr.get)
if (leftExpr.isDefined && rightExpr.isDefined) {
val builder = ExprOuterClass.Like.newBuilder()
builder.setLeft(leftExpr.get)
builder.setRight(rightExpr.get)

Some(
ExprOuterClass.Expr
.newBuilder()
.setLike(builder)
.build())
Some(
ExprOuterClass.Expr
.newBuilder()
.setLike(builder)
.build())
} else {
withInfo(expr, left, right)
None
}
} else {
withInfo(expr, left, right)
// TODO custom escape char
withInfo(expr, s"custom escape character $escapeChar not supported in LIKE")
None
}

Expand Down
18 changes: 18 additions & 0 deletions spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,24 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}

test("like with custom escape") {
val table = "names"
withTable(table) {
sql(s"create table $table(id int, name varchar(20)) using parquet")
sql(s"insert into $table values(1,'James Smith')")
sql(s"insert into $table values(2,'Michael_Rose')")
sql(s"insert into $table values(3,'Robert_R_Williams')")

// Filter column having values that include underscores
val queryDefaultEscape = sql("select id from names where name like '%\\_%'")
checkSparkAnswerAndOperator(queryDefaultEscape)

val queryCustomEscape = sql("select id from names where name like '%$_%' escape '$'")
checkAnswer(queryCustomEscape, Row(2) :: Row(3) :: Nil)

}
}

test("contains") {
assume(!isSpark32)

Expand Down

0 comments on commit fd90a28

Please sign in to comment.