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

SQL: Fix issue with IN not resolving to underlying keyword field #38440

Merged
merged 3 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -23,7 +23,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
Expand Down Expand Up @@ -113,17 +112,18 @@ protected TypeResolution resolveType() {
if (value instanceof FieldAttribute) {
Copy link
Member

Choose a reason for hiding this comment

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

The underlying error message should bubble up since it's important. It might make sense for these cases to have a non-exception method as the MappingException itself seems to heavy for the analyzer.

Copy link
Contributor Author

@matriv matriv Feb 6, 2019

Choose a reason for hiding this comment

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

Created an issue to improve this (the exception part) globally: #38501

try {
((FieldAttribute) value).exactAttribute();
} catch (MappingException ex) {
return new TypeResolution(format(null, "[{}] cannot operate on first argument field of data type [{}]",
functionName(), value().dataType().esType));
} catch (MappingException e) {
return new TypeResolution(format(null, "[{}] cannot operate on field of data type [{}]: {}",
functionName(), value().dataType().esType, e.getMessage()));
}
}

Optional<Expression> firstNotFoldable = list.stream().filter(expression -> !expression.foldable()).findFirst();
if (firstNotFoldable.isPresent()) {
return new TypeResolution(format(null, "Comparisons against variables are not (currently) supported; offender [{}] in [{}]",
Expressions.name(firstNotFoldable.get()),
name()));
for (Expression ex : list) {
if (ex.foldable() == false) {
return new TypeResolution(format(null, "Comparisons against variables are not (currently) supported; offender [{}] in [{}]",
Expressions.name(ex),
name()));
}
}
return super.resolveType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,8 @@ protected QueryTranslation asQuery(In in, boolean onAggs) {
Query q = null;
if (in.value() instanceof FieldAttribute) {
FieldAttribute fa = (FieldAttribute) in.value();
String name = fa.name();
// equality should always be against an exact match
// (which is important for strings)
if (fa.isInexact()) {
name = fa.exactAttribute().name();
}
q = new TermsQuery(in.source(), name, in.list());
// equality should always be against an exact match (which is important for strings)
q = new TermsQuery(in.source(), fa.isInexact() ? fa.exactAttribute().name() : fa.name(), in.list());
} else {
q = new ScriptQuery(in.source(), in.asScript());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ public void testInWithFieldInListOfValues() {
}

public void testInOnFieldTextWithNoKeyword() {
assertEquals("1:26: [IN] cannot operate on first argument field of data type [text]",
assertEquals("1:26: [IN] cannot operate on field of data type [text]: " +
"No keyword/multi-field defined exact matches for [text]; define one or use MATCH/QUERY instead",
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this one.

error("SELECT * FROM test WHERE text IN ('foo', 'bar')"));
}

Expand Down