A constant can be used outside aggregation only queries (#34576)

A constant can now be used outside aggregation only queries.
Don't skip an ES query in case of constants-only selects.
Loosen the binary pipe restriction of being used only in aggregation queries.
Fixes https://github.com/elastic/elasticsearch/issues/31863
This commit is contained in:
Andrei Stefan 2018-10-18 13:33:01 +03:00 committed by GitHub
parent 0d62f6102c
commit 60b8118d3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 5 deletions

View File

@ -41,7 +41,7 @@ public abstract class BinaryPipe extends Pipe {
@Override
public boolean supportedByAggsOnlyQuery() {
return left.supportedByAggsOnlyQuery() && right.supportedByAggsOnlyQuery();
return left.supportedByAggsOnlyQuery() || right.supportedByAggsOnlyQuery();
}
@Override

View File

@ -30,7 +30,7 @@ public class ConstantInput extends LeafInput<Object> {
@Override
public final boolean supportedByAggsOnlyQuery() {
return true;
return false;
}
@Override

View File

@ -51,6 +51,7 @@ import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.Grea
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.LessThan;
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.LessThanOrEqual;
import org.elasticsearch.xpack.sql.plan.logical.Aggregate;
import org.elasticsearch.xpack.sql.plan.logical.EsRelation;
import org.elasticsearch.xpack.sql.plan.logical.Filter;
import org.elasticsearch.xpack.sql.plan.logical.Limit;
import org.elasticsearch.xpack.sql.plan.logical.LocalRelation;
@ -1796,7 +1797,7 @@ public class Optimizer extends RuleExecutor<LogicalPlan> {
if (plan instanceof Project) {
Project p = (Project) plan;
List<Object> values = extractConstants(p.projections());
if (values.size() == p.projections().size()) {
if (values.size() == p.projections().size() && !(p.child() instanceof EsRelation)) {
return new LocalRelation(p.location(), new SingletonExecutable(p.output(), values.toArray()));
}
}

View File

@ -24,8 +24,8 @@ public class BinaryPipesTests extends ESTestCase {
Pipe unsupported = new DummyPipe(false);
assertFalse(new DummyBinaryPipe(unsupported, unsupported).supportedByAggsOnlyQuery());
assertFalse(new DummyBinaryPipe(unsupported, supported).supportedByAggsOnlyQuery());
assertFalse(new DummyBinaryPipe(supported, unsupported).supportedByAggsOnlyQuery());
assertTrue(new DummyBinaryPipe(unsupported, supported).supportedByAggsOnlyQuery());
assertTrue(new DummyBinaryPipe(supported, unsupported).supportedByAggsOnlyQuery());
assertTrue(new DummyBinaryPipe(supported, supported).supportedByAggsOnlyQuery());
}

View File

@ -34,6 +34,24 @@ SELECT first_name, last_name FROM "test_emp" ORDER BY emp_no LIMIT 5;
multipleColumnWithAliasWithAndWithoutAsWithLimit
SELECT first_name f, last_name AS l FROM "test_emp" ORDER BY emp_no LIMIT 5;
//
// SELECT constant literals with FROM
//
constantWithLimit
SELECT 3 FROM "test_emp" LIMIT 5;
constantAndColumnWithLimit
SELECT 3, first_name, last_name FROM "test_emp" ORDER BY emp_no LIMIT 5;
constantComparisonWithLimit
SELECT 1=1 AS bool FROM "test_emp" LIMIT 5;
constantComparisonAndColumnWithLimit
SELECT 1=1 AS bool, first_name, last_name FROM "test_emp" ORDER BY emp_no LIMIT 5;
castWithLiteralWithFrom
SELECT CAST(1 AS INT) AS constant FROM "test_emp" LIMIT 5;
castWithLiteralAndColumnWithFrom
SELECT CAST((CAST(languages AS BIT) OR CAST(1 AS BIT)) AS INT) AS bool FROM test_emp LIMIT 5;
castWithColumnAndLiteralCombinedAndSelectColumnWithFrom
SELECT CAST((CAST(languages AS BIT) OR CAST(1 AS BIT)) AS INT) AS bool, languages FROM test_emp ORDER BY languages LIMIT 5;
//
// SELECT with CAST