Skip to content

Commit 7595966

Browse files
astefankcm
authored andcommitted
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 #31863
1 parent 179ca33 commit 7595966

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Pipe right() {
4141

4242
@Override
4343
public boolean supportedByAggsOnlyQuery() {
44-
return left.supportedByAggsOnlyQuery() && right.supportedByAggsOnlyQuery();
44+
return left.supportedByAggsOnlyQuery() || right.supportedByAggsOnlyQuery();
4545
}
4646

4747
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/ConstantInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Processor asProcessor() {
3030

3131
@Override
3232
public final boolean supportedByAggsOnlyQuery() {
33-
return true;
33+
return false;
3434
}
3535

3636
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.LessThan;
5252
import org.elasticsearch.xpack.sql.expression.predicate.operator.comparison.LessThanOrEqual;
5353
import org.elasticsearch.xpack.sql.plan.logical.Aggregate;
54+
import org.elasticsearch.xpack.sql.plan.logical.EsRelation;
5455
import org.elasticsearch.xpack.sql.plan.logical.Filter;
5556
import org.elasticsearch.xpack.sql.plan.logical.Limit;
5657
import org.elasticsearch.xpack.sql.plan.logical.LocalRelation;
@@ -1796,7 +1797,7 @@ protected LogicalPlan rule(LogicalPlan plan) {
17961797
if (plan instanceof Project) {
17971798
Project p = (Project) plan;
17981799
List<Object> values = extractConstants(p.projections());
1799-
if (values.size() == p.projections().size()) {
1800+
if (values.size() == p.projections().size() && !(p.child() instanceof EsRelation)) {
18001801
return new LocalRelation(p.location(), new SingletonExecutable(p.output(), values.toArray()));
18011802
}
18021803
}

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipesTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public void testSupportedByAggsOnlyQuery() {
2424
Pipe unsupported = new DummyPipe(false);
2525

2626
assertFalse(new DummyBinaryPipe(unsupported, unsupported).supportedByAggsOnlyQuery());
27-
assertFalse(new DummyBinaryPipe(unsupported, supported).supportedByAggsOnlyQuery());
28-
assertFalse(new DummyBinaryPipe(supported, unsupported).supportedByAggsOnlyQuery());
27+
assertTrue(new DummyBinaryPipe(unsupported, supported).supportedByAggsOnlyQuery());
28+
assertTrue(new DummyBinaryPipe(supported, unsupported).supportedByAggsOnlyQuery());
2929
assertTrue(new DummyBinaryPipe(supported, supported).supportedByAggsOnlyQuery());
3030
}
3131

x-pack/qa/sql/src/main/resources/select.sql-spec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ SELECT first_name, last_name FROM "test_emp" ORDER BY emp_no LIMIT 5;
3434
multipleColumnWithAliasWithAndWithoutAsWithLimit
3535
SELECT first_name f, last_name AS l FROM "test_emp" ORDER BY emp_no LIMIT 5;
3636

37+
//
38+
// SELECT constant literals with FROM
39+
//
40+
41+
constantWithLimit
42+
SELECT 3 FROM "test_emp" LIMIT 5;
43+
constantAndColumnWithLimit
44+
SELECT 3, first_name, last_name FROM "test_emp" ORDER BY emp_no LIMIT 5;
45+
constantComparisonWithLimit
46+
SELECT 1=1 AS bool FROM "test_emp" LIMIT 5;
47+
constantComparisonAndColumnWithLimit
48+
SELECT 1=1 AS bool, first_name, last_name FROM "test_emp" ORDER BY emp_no LIMIT 5;
49+
castWithLiteralWithFrom
50+
SELECT CAST(1 AS INT) AS constant FROM "test_emp" LIMIT 5;
51+
castWithLiteralAndColumnWithFrom
52+
SELECT CAST((CAST(languages AS BIT) OR CAST(1 AS BIT)) AS INT) AS bool FROM test_emp LIMIT 5;
53+
castWithColumnAndLiteralCombinedAndSelectColumnWithFrom
54+
SELECT CAST((CAST(languages AS BIT) OR CAST(1 AS BIT)) AS INT) AS bool, languages FROM test_emp ORDER BY languages LIMIT 5;
3755

3856
//
3957
// SELECT with CAST

0 commit comments

Comments
 (0)