-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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 query translation for scripted queries #35408
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,6 @@ | |
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeFunction; | ||
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeHistogramFunction; | ||
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate; | ||
import org.elasticsearch.xpack.sql.expression.predicate.nulls.IsNull; | ||
import org.elasticsearch.xpack.sql.expression.predicate.Range; | ||
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.MatchQueryPredicate; | ||
import org.elasticsearch.xpack.sql.expression.predicate.fulltext.MultiMatchQueryPredicate; | ||
|
@@ -40,6 +38,7 @@ | |
import org.elasticsearch.xpack.sql.expression.predicate.logical.Not; | ||
import org.elasticsearch.xpack.sql.expression.predicate.logical.Or; | ||
import org.elasticsearch.xpack.sql.expression.predicate.nulls.IsNotNull; | ||
import org.elasticsearch.xpack.sql.expression.predicate.nulls.IsNull; | ||
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.BinaryComparison; | ||
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.Equals; | ||
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.GreaterThan; | ||
|
@@ -94,6 +93,7 @@ | |
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.elasticsearch.xpack.sql.expression.Foldables.doubleValuesOf; | ||
|
@@ -487,11 +487,8 @@ protected QueryTranslation asQuery(Not not, boolean onAggs) { | |
if (onAggs) { | ||
aggFilter = new AggFilter(not.id().toString(), not.asScript()); | ||
} else { | ||
query = new NotQuery(not.location(), toQuery(not.field(), false).query); | ||
// query directly on the field | ||
if (not.field() instanceof FieldAttribute) { | ||
query = wrapIfNested(query, not.field()); | ||
} | ||
query = generateQuery(not, not.field(), | ||
() -> new NotQuery(not.location(), toQuery(not.field(), false).query)); | ||
} | ||
|
||
return new QueryTranslation(query, aggFilter); | ||
|
@@ -508,11 +505,8 @@ protected QueryTranslation asQuery(IsNotNull isNotNull, boolean onAggs) { | |
if (onAggs) { | ||
aggFilter = new AggFilter(isNotNull.id().toString(), isNotNull.asScript()); | ||
} else { | ||
query = new ExistsQuery(isNotNull.location(), nameOf(isNotNull.field())); | ||
// query directly on the field | ||
if (isNotNull.field() instanceof NamedExpression) { | ||
query = wrapIfNested(query, isNotNull.field()); | ||
} | ||
query = generateQuery(isNotNull, isNotNull.field(), | ||
() -> new ExistsQuery(isNotNull.location(), nameOf(isNotNull.field()))); | ||
} | ||
|
||
return new QueryTranslation(query, aggFilter); | ||
|
@@ -529,11 +523,8 @@ protected QueryTranslation asQuery(IsNull isNull, boolean onAggs) { | |
if (onAggs) { | ||
aggFilter = new AggFilter(isNull.id().toString(), isNull.asScript()); | ||
} else { | ||
query = new NotQuery(isNull.location(), new ExistsQuery(isNull.location(), nameOf(isNull.field()))); | ||
// query directly on the field | ||
if (isNull.field() instanceof NamedExpression) { | ||
query = wrapIfNested(query, isNull.field()); | ||
} | ||
query = generateQuery(isNull, isNull.field(), | ||
() -> new NotQuery(isNull.location(), new ExistsQuery(isNull.location(), nameOf(isNull.field())))); | ||
} | ||
|
||
return new QueryTranslation(query, aggFilter); | ||
|
@@ -564,12 +555,7 @@ protected QueryTranslation asQuery(BinaryComparison bc, boolean onAggs) { | |
aggFilter = new AggFilter(at.id().toString(), bc.asScript()); | ||
} | ||
else { | ||
// query directly on the field | ||
if (at instanceof FieldAttribute) { | ||
query = wrapIfNested(translateQuery(bc), ne); | ||
} else { | ||
query = new ScriptQuery(at.location(), bc.asScript()); | ||
} | ||
query = generateQuery(bc, ne, () -> translateQuery(bc)); | ||
} | ||
return new QueryTranslation(query, aggFilter); | ||
} | ||
|
@@ -646,17 +632,11 @@ protected QueryTranslation asQuery(In in, boolean onAggs) { | |
// | ||
// Agg context means HAVING -> PipelineAggs | ||
// | ||
ScriptTemplate script = in.asScript(); | ||
if (onAggs) { | ||
aggFilter = new AggFilter(at.id().toString(), script); | ||
aggFilter = new AggFilter(at.id().toString(), in.asScript()); | ||
} | ||
else { | ||
// query directly on the field | ||
if (at instanceof FieldAttribute) { | ||
query = wrapIfNested(new TermsQuery(in.location(), ne.name(), in.list()), ne); | ||
} else { | ||
query = new ScriptQuery(at.location(), script); | ||
} | ||
query = generateQuery(in, ne, () -> new TermsQuery(in.location(), ne.name(), in.list())); | ||
} | ||
return new QueryTranslation(query, aggFilter); | ||
} | ||
|
@@ -687,16 +667,9 @@ protected QueryTranslation asQuery(Range r, boolean onAggs) { | |
if (onAggs) { | ||
aggFilter = new AggFilter(at.id().toString(), r.asScript()); | ||
} else { | ||
// typical range; no scripting involved | ||
if (at instanceof FieldAttribute) { | ||
RangeQuery rangeQuery = new RangeQuery(r.location(), nameOf(r.value()), valueOf(r.lower()), r.includeLower(), | ||
valueOf(r.upper()), r.includeUpper(), dateFormat(r.value())); | ||
query = wrapIfNested(rangeQuery, r.value()); | ||
} | ||
// scripted query | ||
else { | ||
query = new ScriptQuery(at.location(), r.asScript()); | ||
} | ||
query = generateQuery(r, r.value(), | ||
() -> new RangeQuery(r.location(), nameOf(r.value()), valueOf(r.lower()), r.includeLower(), | ||
valueOf(r.upper()), r.includeUpper(), dateFormat(r.value()))); | ||
} | ||
return new QueryTranslation(query, aggFilter); | ||
} else { | ||
|
@@ -845,6 +818,14 @@ public QueryTranslation translate(Expression exp, boolean onAggs) { | |
|
||
protected abstract QueryTranslation asQuery(E e, boolean onAggs); | ||
|
||
|
||
protected static Query generateQuery(ScalarFunction sf, Expression field, Supplier<Query> query) { | ||
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. maybe better suggestion for a name here? 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.
|
||
if (field instanceof FieldAttribute) { | ||
return wrapIfNested(query.get(), field); | ||
} | ||
return new ScriptQuery(sf.location(), sf.asScript()); | ||
} | ||
|
||
protected static Query wrapIfNested(Query query, Expression exp) { | ||
if (exp instanceof FieldAttribute) { | ||
FieldAttribute fa = (FieldAttribute) exp; | ||
|
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
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.
ScalarFunction already implements ScriptWeaver