-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 REGEXP_LIKE, fix bugs in REGEXP_EXTRACT. #9893
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
73d8fd1
Add REGEXP_LIKE, fix empty-pattern bug in REGEXP_EXTRACT.
gianm 215971a
Changes based on PR review.
gianm 0f3170b
Fix arg check.
gianm 00698bf
Important fixes!
gianm dd0c16f
Add speller.
gianm 89dcaa8
Merge branch 'master' into regexp-stuff
gianm 8b95b9e
Merge branch 'master' into regexp-stuff
gianm c72cab0
wip
gianm 0f67d2c
Merge branch 'master' into regexp-stuff
gianm 192ac33
Additional tests.
gianm cb89950
Merge branch 'master' into regexp-stuff
gianm ce4197c
Fix up tests.
gianm 71b9e29
Add validation error tests.
gianm 6641cab
Additional tests.
gianm 292836a
Merge branch 'master' into regexp-stuff
gianm bb32227
Remove useless call.
gianm 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
101 changes: 101 additions & 0 deletions
101
processing/src/main/java/org/apache/druid/query/expression/RegexpLikeExprMacro.java
This file contains 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,101 @@ | ||
/* | ||
* 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.druid.query.expression; | ||
|
||
import org.apache.druid.common.config.NullHandling; | ||
import org.apache.druid.java.util.common.IAE; | ||
import org.apache.druid.java.util.common.StringUtils; | ||
import org.apache.druid.math.expr.Expr; | ||
import org.apache.druid.math.expr.ExprEval; | ||
import org.apache.druid.math.expr.ExprMacroTable; | ||
import org.apache.druid.math.expr.ExprType; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class RegexpLikeExprMacro implements ExprMacroTable.ExprMacro | ||
{ | ||
private static final String FN_NAME = "regexp_like"; | ||
|
||
@Override | ||
public String name() | ||
{ | ||
return FN_NAME; | ||
} | ||
|
||
@Override | ||
public Expr apply(final List<Expr> args) | ||
{ | ||
if (args.size() != 2) { | ||
throw new IAE("Function[%s] must have 2 arguments", name()); | ||
} | ||
|
||
final Expr arg = args.get(0); | ||
final Expr patternExpr = args.get(1); | ||
|
||
if (!ExprUtils.isStringLiteral(patternExpr)) { | ||
throw new IAE("Function[%s] pattern must be a string literal", name()); | ||
} | ||
|
||
// Precompile the pattern. | ||
final Pattern pattern = Pattern.compile( | ||
StringUtils.nullToEmptyNonDruidDataString((String) patternExpr.getLiteralValue()) | ||
); | ||
|
||
class RegexpLikeExpr extends ExprMacroTable.BaseScalarUnivariateMacroFunctionExpr | ||
{ | ||
private RegexpLikeExpr(Expr arg) | ||
{ | ||
super(FN_NAME, arg); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public ExprEval eval(final ObjectBinding bindings) | ||
{ | ||
final String s = NullHandling.nullToEmptyIfNeeded(arg.eval(bindings).asString()); | ||
|
||
if (s == null) { | ||
// True nulls do not match anything. Note: this branch only executes in SQL-compatible null handling mode. | ||
return ExprEval.of(false, ExprType.LONG); | ||
} else { | ||
final Matcher matcher = pattern.matcher(NullHandling.nullToEmptyIfNeeded(s)); | ||
return ExprEval.of(matcher.find(), ExprType.LONG); | ||
} | ||
} | ||
|
||
@Override | ||
public Expr visit(Shuttle shuttle) | ||
{ | ||
Expr newArg = arg.visit(shuttle); | ||
return shuttle.visit(new RegexpLikeExpr(newArg)); | ||
} | ||
|
||
@Override | ||
public String stringify() | ||
{ | ||
return StringUtils.format("%s(%s, %s)", FN_NAME, arg.stringify(), patternExpr.stringify()); | ||
} | ||
} | ||
return new RegexpLikeExpr(arg); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Is this
nullToEmptyIfNeeded
still needed because of the if block on line 77 - same comment forRegexpExtractMacro
Unclear to me if there's a performance loss from the extra function call (I'd think it's probably not measurable)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I removed it.