-
Notifications
You must be signed in to change notification settings - Fork 25.6k
SQL: Fix issue with IN not resolving to underlying keyword field #38440
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,10 @@ | |
| */ | ||
| package org.elasticsearch.xpack.sql.expression.predicate.operator.comparison; | ||
|
|
||
| import org.elasticsearch.xpack.sql.analysis.index.MappingException; | ||
| import org.elasticsearch.xpack.sql.expression.Expression; | ||
| import org.elasticsearch.xpack.sql.expression.Expressions; | ||
| import org.elasticsearch.xpack.sql.expression.FieldAttribute; | ||
| import org.elasticsearch.xpack.sql.expression.Foldables; | ||
| import org.elasticsearch.xpack.sql.expression.Nullability; | ||
| import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction; | ||
|
|
@@ -21,6 +23,7 @@ | |
| 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; | ||
|
|
@@ -105,6 +108,26 @@ protected Pipe makePipe() { | |
| return new InPipe(source(), this, children().stream().map(Expressions::pipe).collect(Collectors.toList())); | ||
| } | ||
|
|
||
| @Override | ||
| protected TypeResolution resolveType() { | ||
| if (value instanceof FieldAttribute) { | ||
| 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)); | ||
| } | ||
| } | ||
|
|
||
| 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())); | ||
| } | ||
| return super.resolveType(); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(value, list); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,7 +105,6 @@ | |
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static java.util.Collections.singletonList; | ||
|
|
@@ -708,16 +707,6 @@ static class InComparisons extends ExpressionTranslator<In> { | |
|
|
||
| @Override | ||
| protected QueryTranslation asQuery(In in, boolean onAggs) { | ||
| Optional<Expression> firstNotFoldable = in.list().stream().filter(expression -> !expression.foldable()).findFirst(); | ||
|
|
||
| if (firstNotFoldable.isPresent()) { | ||
| throw new SqlIllegalArgumentException( | ||
| "Line {}:{}: Comparisons against variables are not (currently) supported; offender [{}] in [{}]", | ||
| firstNotFoldable.get().sourceLocation().getLineNumber(), | ||
| firstNotFoldable.get().sourceLocation().getColumnNumber(), | ||
| Expressions.name(firstNotFoldable.get()), | ||
| in.name()); | ||
| } | ||
|
|
||
| if (in.value() instanceof NamedExpression) { | ||
| NamedExpression ne = (NamedExpression) in.value(); | ||
|
|
@@ -735,7 +724,14 @@ protected QueryTranslation asQuery(In in, boolean onAggs) { | |
| else { | ||
| Query q = null; | ||
| if (in.value() instanceof FieldAttribute) { | ||
| q = new TermsQuery(in.source(), ne.name(), in.list()); | ||
| 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()); | ||
|
||
| } else { | ||
| q = new ScriptQuery(in.source(), in.asScript()); | ||
| } | ||
|
|
||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Created an issue to improve this (the exception part) globally: #38501