diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipe.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipe.java index 285470ad8fd..ac564e5b20c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipe.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipe.java @@ -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 diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/ConstantInput.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/ConstantInput.java index ddab153cc4f..4af4dfc5cb7 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/ConstantInput.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/ConstantInput.java @@ -30,7 +30,7 @@ public class ConstantInput extends LeafInput { @Override public final boolean supportedByAggsOnlyQuery() { - return true; + return false; } @Override diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java index deb4ccb7da2..033d46e1b1b 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java @@ -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 { if (plan instanceof Project) { Project p = (Project) plan; List 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())); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipesTests.java index e786b4345f8..991036d2da3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/gen/pipeline/BinaryPipesTests.java @@ -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()); } diff --git a/x-pack/qa/sql/src/main/resources/select.sql-spec b/x-pack/qa/sql/src/main/resources/select.sql-spec index ce57606e35b..a9de495b748 100644 --- a/x-pack/qa/sql/src/main/resources/select.sql-spec +++ b/x-pack/qa/sql/src/main/resources/select.sql-spec @@ -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