This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 02904822bf987206c660e2afcdcb6e299d307094 Author: Julian Hyde <[email protected]> AuthorDate: Fri Sep 18 17:30:58 2020 -0700 [CALCITE-4209] In RelBuilder, add an option to not simplify LIMIT 0 to an empty relation Add configuration method RelBuilder.Config.simplifyLimit(boolean). If false, retain the LIMIT, so we would generate say SELECT * FROM Emp LIMIT 0 If true (the default, and current behavior), simplify to the empty relation, which would result in SQL containing an empty VALUES. Close apache/calcite#2159 --- .../main/java/org/apache/calcite/tools/RelBuilder.java | 10 +++++++++- .../java/org/apache/calcite/test/RelBuilderTest.java | 16 +++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java index bcd860b..89abcb8 100644 --- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java +++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java @@ -2617,7 +2617,7 @@ public class RelBuilder { final RexNode offsetNode = offset <= 0 ? null : literal(offset); final RexNode fetchNode = fetch < 0 ? null : literal(fetch); - if (offsetNode == null && fetch == 0) { + if (offsetNode == null && fetch == 0 && config.simplifyLimit()) { return empty(); } if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) { @@ -3387,6 +3387,14 @@ public class RelBuilder { /** Sets {@link #simplify}. */ Config withSimplify(boolean simplify); + /** Whether to simplify LIMIT 0 to an empty relation; default true. */ + @ImmutableBeans.Property + @ImmutableBeans.BooleanDefault(true) + boolean simplifyLimit(); + + /** Sets {@link #simplifyLimit()}. */ + Config withSimplifyLimit(boolean simplifyLimit); + /** Whether to create an Aggregate even if we know that the input is * already unique; default false. */ @ImmutableBeans.Property diff --git a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java index ea74078..103b6df 100644 --- a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java +++ b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java @@ -2938,13 +2938,19 @@ public class RelBuilderTest { // SELECT * // FROM emp // ORDER BY deptno DESC FETCH 0 - final RelBuilder builder = RelBuilder.create(config().build()); - final RelNode root = - builder.scan("EMP") - .sortLimit(-1, 0, builder.desc(builder.field("DEPTNO"))) + final Function<RelBuilder, RelNode> f = b -> + b.scan("EMP") + .sortLimit(-1, 0, b.desc(b.field("DEPTNO"))) .build(); final String expected = "LogicalValues(tuples=[[]])\n"; - assertThat(root, hasTree(expected)); + final String expectedNoSimplify = "" + + "LogicalSort(sort0=[$7], dir0=[DESC], fetch=[0])\n" + + " LogicalTableScan(table=[[scott, EMP]])\n"; + assertThat(f.apply(createBuilder()), hasTree(expected)); + assertThat(f.apply(createBuilder(c -> c.withSimplifyLimit(true))), + hasTree(expected)); + assertThat(f.apply(createBuilder(c -> c.withSimplifyLimit(false))), + hasTree(expectedNoSimplify)); } /** Test case for
