Skip to content

Commit

Permalink
A constant can be used outside aggregation only queries (elastic#34576)
Browse files Browse the repository at this point in the history
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 elastic#31863
  • Loading branch information
astefan committed Oct 18, 2018
1 parent 0b9e86f commit c6f55b2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Pipe right() {

@Override
public boolean supportedByAggsOnlyQuery() {
return left.supportedByAggsOnlyQuery() && right.supportedByAggsOnlyQuery();
return left.supportedByAggsOnlyQuery() || right.supportedByAggsOnlyQuery();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Processor asProcessor() {

@Override
public final boolean supportedByAggsOnlyQuery() {
return true;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;
Expand Down Expand Up @@ -1796,7 +1797,7 @@ protected LogicalPlan rule(LogicalPlan plan) {
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()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void testSupportedByAggsOnlyQuery() {
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());
}

Expand Down
18 changes: 18 additions & 0 deletions x-pack/qa/sql/src/main/resources/select.sql-spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c6f55b2

Please sign in to comment.