Skip to content
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

Add support for like() function #130

Merged
merged 3 commits into from
Mar 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ There's basic support for the most used commands like `addtotals`, `bin`, `colle
`streamstats`, `table`, `where`.

There's also basic support for functions like `auto()`, `cidr_match()`, `coalesce()`, `count()`,
`ctime()`, `earliest()`, `if()`, `isnotnull()`, `latest()`, `len()`, `lower()`, `max()`,
`ctime()`, `earliest()`, `if()`, `isnotnull()`, `latest()`, `len()`, `like()`, `lower()`, `max()`,
`memk()`, `min()`, `mvappend()`, `mvcount()`, `mvfilter()`, `mvindex()`, `none()`,
`null()`, `num()`, `replace()`, `rmcomma()`, `rmunit()`, `round()`, `strftime()`,
`substr()`, `sum()`, `term()`, `values()`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,30 @@
determineMax(ctx, call)
case "len" =>
Length(attrOrExpr(ctx, call.args.head))
case "like" =>
val field = attrOrExpr(ctx, call.args.head)
val pattern = attrOrExpr(ctx, call.args(1))
pattern match {
case Literal(patternLiteral: UTF8String, StringType) =>
val patternString = patternLiteral.toString
// If the pattern is a simple LIKE (%foo%) pattern, we can convert it into a CONTAINS
// expression.
// For this to be safe, the pattern must start with %, end with % (unescaped), and contain
// exactly 2 instances of the wildcard character %. Note that this approach is
// conservative, as there may exist cases like %foo\%bar% that can be safely converted
// (as the wildcard in the middle of the string is escaped).
if (patternString.length > 2 &&
patternString.charAt(0) == '%' &&
patternString.charAt(patternString.length - 1) == '%' &&
patternString.charAt(patternString.length - 2) != '\\' &&
patternString.count(_ == '%') == 2) {
Contains(field,
Literal(patternString.substring(1, patternString.length - 1)))
} else {
Like(field, pattern, '\\')
}
case _ => Like(field, pattern, '\\')

Check warning on line 198 in src/main/scala/com/databricks/labs/transpiler/spl/catalyst/SplToCatalyst.scala

View check run for this annotation

Codecov / codecov/patch

src/main/scala/com/databricks/labs/transpiler/spl/catalyst/SplToCatalyst.scala#L198

Added line #L198 was not covered by tests
}
case "substr" =>
val str = attrOrExpr(ctx, call.args.head)
val pos = expression(ctx, call.args(1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,39 @@ class SplToCatalystTest extends AnyFunSuite with PlanTestBase {
)
}

test("simple LIKE converted to CONTAINS") {
check(ast.SearchCommand(
ast.Call("like", Seq(ast.Field("a"), ast.StrValue("%foo%")))),
(_, tree) =>
Filter(
Contains(
UnresolvedAttribute("a"),
Literal.create("foo")),
tree)
)
}

test("complex LIKE not converted to CONTAINS") {
check(ast.SearchCommand(
ast.Call("like", Seq(ast.Field("a"), ast.StrValue("%foo%bar%")))),
(_, tree) =>
Filter(
Like(
UnresolvedAttribute("a"),
Literal.create("%foo%bar%"), '\\'),
tree)
)
check(ast.SearchCommand(
ast.Call("like", Seq(ast.Field("a"), ast.StrValue("%foo\\%")))),
(_, tree) =>
Filter(
Like(
UnresolvedAttribute("a"),
Literal.create("%foo\\%"), '\\'),
tree)
)
}

test("eventstats max(colA) AS maxA by colC") {
check(ast.EventStatsCommand(
allNum = false,
Expand Down
Loading