-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Support SearchQueryDimFilter in sql via new methods #10350
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
Merged
suneet-s
merged 12 commits into
apache:master
from
abhishekagarwal87:search-query-dim-filter
Sep 14, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6648e67
Support SearchQueryDimFilter in sql via new methods
abhishekagarwal87 6e86280
Contains is a reserved word
abhishekagarwal87 176d712
revert unnecessary change
abhishekagarwal87 d5a841a
Fix toDruidExpression method
abhishekagarwal87 b7d3cef
rename methods
abhishekagarwal87 bbfe4cb
java docs
abhishekagarwal87 7e7125d
Add native functions
abhishekagarwal87 38306ec
revert change in dockerfile
abhishekagarwal87 5499710
remove changes from dockerfile
abhishekagarwal87 1f408a7
More tests
abhishekagarwal87 cfd26bd
travis fix
abhishekagarwal87 a8ae169
Handle null values better
abhishekagarwal87 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
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
63 changes: 63 additions & 0 deletions
63
...ing/src/main/java/org/apache/druid/query/expression/CaseInsensitiveContainsExprMacro.java
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.druid.query.expression; | ||
|
||
import org.apache.druid.java.util.common.IAE; | ||
import org.apache.druid.math.expr.Expr; | ||
import org.apache.druid.math.expr.ExprMacroTable; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* This class implements a function that checks if one string contains another string. It is required that second | ||
* string be a literal. This expression is case-insensitive. | ||
* signature: | ||
* long contains_string(string, string) | ||
* <p> | ||
* Examples: | ||
* - {@code contains_string("foobar", "bar") - 1 } | ||
* - {@code contains_string("foobar", "car") - 0 } | ||
* - {@code contains_string("foobar", "Bar") - 1 } | ||
* <p> | ||
* See {@link ContainsExprMacro} for the case-sensitive version. | ||
*/ | ||
|
||
public class CaseInsensitiveContainsExprMacro implements ExprMacroTable.ExprMacro | ||
{ | ||
public static final String FN_NAME = "icontains_string"; | ||
|
||
@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 searchStr = args.get(1); | ||
return new ContainsExpr(FN_NAME, arg, searchStr, false); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
processing/src/main/java/org/apache/druid/query/expression/ContainsExpr.java
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,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.function.Function; | ||||||
|
||||||
/** | ||||||
* {@link Expr} class returned by {@link ContainsExprMacro} and {@link CaseInsensitiveContainsExprMacro} for | ||||||
* evaluating the expression. | ||||||
*/ | ||||||
class ContainsExpr extends ExprMacroTable.BaseScalarUnivariateMacroFunctionExpr | ||||||
{ | ||||||
private final Function<String, Boolean> searchFunction; | ||||||
private final Expr searchStrExpr; | ||||||
abhishekagarwal87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
ContainsExpr(String functioName, Expr arg, Expr searchStrExpr, boolean caseSensitive) | ||||||
{ | ||||||
super(functioName, arg); | ||||||
this.searchStrExpr = validateSearchExpr(searchStrExpr, functioName); | ||||||
// Creates the function eagerly to avoid branching in eval. | ||||||
this.searchFunction = createFunction(searchStrExpr, caseSensitive); | ||||||
} | ||||||
|
||||||
private ContainsExpr(String functioName, Expr arg, Expr searchStrExpr, Function<String, Boolean> searchFunction) | ||||||
{ | ||||||
super(functioName, arg); | ||||||
this.searchFunction = searchFunction; | ||||||
this.searchStrExpr = validateSearchExpr(searchStrExpr, functioName); | ||||||
} | ||||||
|
||||||
@Nonnull | ||||||
@Override | ||||||
public ExprEval eval(final Expr.ObjectBinding bindings) | ||||||
{ | ||||||
final String s = NullHandling.nullToEmptyIfNeeded(arg.eval(bindings).asString()); | ||||||
|
||||||
if (s == null) { | ||||||
// same behavior as regexp_like. | ||||||
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. nit
Suggested change
Luckily the behavior is the same as regexp_like
|
||||||
return ExprEval.of(false, ExprType.LONG); | ||||||
} else { | ||||||
final boolean doesContain = searchFunction.apply(s); | ||||||
return ExprEval.of(doesContain, ExprType.LONG); | ||||||
} | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Expr visit(Expr.Shuttle shuttle) | ||||||
{ | ||||||
Expr newArg = arg.visit(shuttle); | ||||||
return shuttle.visit(new ContainsExpr(name, newArg, searchStrExpr, searchFunction)); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public String stringify() | ||||||
{ | ||||||
return StringUtils.format("%s(%s, %s)", name, arg.stringify(), searchStrExpr.stringify()); | ||||||
} | ||||||
|
||||||
private Function<String, Boolean> createFunction(Expr searchStrExpr, boolean caseSensitive) | ||||||
{ | ||||||
String searchStr = StringUtils.nullToEmptyNonDruidDataString((String) searchStrExpr.getLiteralValue()); | ||||||
if (caseSensitive) { | ||||||
return s -> s.contains(searchStr); | ||||||
} | ||||||
return s -> org.apache.commons.lang.StringUtils.containsIgnoreCase(s, searchStr); | ||||||
} | ||||||
|
||||||
private Expr validateSearchExpr(Expr searchExpr, String functioName) | ||||||
{ | ||||||
if (!ExprUtils.isStringLiteral(searchExpr)) { | ||||||
throw new IAE("Function[%s] substring must be a string literal", functioName); | ||||||
} | ||||||
return searchExpr; | ||||||
} | ||||||
} |
62 changes: 62 additions & 0 deletions
62
processing/src/main/java/org/apache/druid/query/expression/ContainsExprMacro.java
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,62 @@ | ||
/* | ||
* 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.java.util.common.IAE; | ||
import org.apache.druid.math.expr.Expr; | ||
import org.apache.druid.math.expr.ExprMacroTable; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* This class implements a function that checks if one string contains another string. It is required that second | ||
* string be a literal. This expression is case-sensitive. | ||
* signature: | ||
* long contains_string(string, string) | ||
* <p> | ||
* Examples: | ||
* - {@code contains_string("foobar", "bar") - 1 } | ||
* - {@code contains_string("foobar", "car") - 0 } | ||
* - {@code contains_string("foobar", "Bar") - 0 } | ||
* <p> | ||
* See {@link CaseInsensitiveContainsExprMacro} for the case-insensitive version. | ||
*/ | ||
abhishekagarwal87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class ContainsExprMacro implements ExprMacroTable.ExprMacro | ||
{ | ||
public static final String FN_NAME = "contains_string"; | ||
|
||
@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 searchStr = args.get(1); | ||
return new ContainsExpr(FN_NAME, arg, searchStr, true); | ||
} | ||
} |
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: It would be good to link to the DimFilter whose behavior we are trying to mimic here
ContainsSearchQuerySpec
since we want the logic between these 2 classes to stay the same. I wonder if we can future proof this so they stay in sync if someone makes an update to
ContainsSearchQuerySpec