[calcite] branch master updated: [CALCITE-4160] Add configuration(SqlToRelConverter.Config) to retain ORDER BY in sub-query (Jiatao Tao) (part2)

2020-09-03 Thread danny0405
This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
 new 42785e4  [CALCITE-4160] Add configuration(SqlToRelConverter.Config) to 
retain ORDER BY in sub-query (Jiatao Tao) (part2)
42785e4 is described below

commit 42785e4f63f5d841655a8fdc4f9b81b310e33841
Author: yuzhao.cyz 
AuthorDate: Fri Sep 4 10:06:51 2020 +0800

[CALCITE-4160] Add configuration(SqlToRelConverter.Config) to retain ORDER 
BY in sub-query (Jiatao Tao) (part2)

Rename 'RemoveSortInSubquery' to 'RemoveSortInSubQuery'.
---
 .../apache/calcite/sql2rel/SqlToRelConverter.java  | 34 +++---
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java 
b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
index 3da06dd..273e4b1 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
@@ -824,10 +824,10 @@ public class SqlToRelConverter {
   List orderExprList,
   SqlNode offset,
   SqlNode fetch) {
-if (removeSortInSubquery(bb.top)
+if (removeSortInSubQuery(bb.top)
 || select.getOrderList() == null
 || select.getOrderList().getList().isEmpty()) {
-  assert removeSortInSubquery(bb.top) || 
collation.getFieldCollations().isEmpty();
+  assert removeSortInSubQuery(bb.top) || 
collation.getFieldCollations().isEmpty();
   if ((offset == null
 || (offset instanceof SqlLiteral
 && ((SqlLiteral) 
offset).bigDecimalValue().equals(BigDecimal.ZERO)))
@@ -868,10 +868,10 @@ public class SqlToRelConverter {
   /**
* Returns whether we should remove the sort for the subsequent query 
conversion.
*
-   * @param topQuery Whether the query is in the top level.
+   * @param top Whether the rel to convert is the root of the query
*/
-  private boolean removeSortInSubquery(boolean topQuery) {
-return config.isRemoveSortInSubquery() && !topQuery;
+  private boolean removeSortInSubQuery(boolean top) {
+return config.isRemoveSortInSubQuery() && !top;
   }
 
   /**
@@ -3258,7 +3258,7 @@ public class SqlToRelConverter {
   return;
 }
 
-if (removeSortInSubquery(bb.top)) {
+if (removeSortInSubQuery(bb.top)) {
   SqlNode offset = select.getOffset();
   if ((offset == null
   || (offset instanceof SqlLiteral
@@ -5882,7 +5882,7 @@ public class SqlToRelConverter {
  *
  *  Default is true.
  */
-boolean isRemoveSortInSubquery();
+boolean isRemoveSortInSubQuery();
   }
 
   /** Builder for a {@link Config}. */
@@ -5892,7 +5892,7 @@ public class SqlToRelConverter {
 private boolean createValuesRel = true;
 private boolean explain;
 private boolean expand = true;
-private boolean removeSortInSubquery = true;
+private boolean removeSortInSubQuery = true;
 private int inSubQueryThreshold = DEFAULT_IN_SUB_QUERY_THRESHOLD;
 private UnaryOperator relBuilderConfigTransform = c ->
 c.withPushJoinCondition(true);
@@ -5908,7 +5908,7 @@ public class SqlToRelConverter {
   this.createValuesRel = config.isCreateValuesRel();
   this.explain = config.isExplain();
   this.expand = config.isExpand();
-  this.removeSortInSubquery = config.isRemoveSortInSubquery();
+  this.removeSortInSubQuery = config.isRemoveSortInSubQuery();
   this.inSubQueryThreshold = config.getInSubQueryThreshold();
   this.relBuilderConfigTransform = config.getRelBuilderConfigTransform();
   this.relBuilderFactory = config.getRelBuilderFactory();
@@ -5941,8 +5941,8 @@ public class SqlToRelConverter {
   return this;
 }
 
-public ConfigBuilder withRemoveSortInSubQuery(boolean 
removeSortInSubquery) {
-  this.removeSortInSubquery = removeSortInSubquery;
+public ConfigBuilder withRemoveSortInSubQuery(boolean 
removeSortInSubQuery) {
+  this.removeSortInSubQuery = removeSortInSubQuery;
   return this;
 }
 
@@ -5984,7 +5984,7 @@ public class SqlToRelConverter {
 /** Builds a {@link Config}. */
 public Config build() {
   return new ConfigImpl(decorrelationEnabled,
-  trimUnusedFields, createValuesRel, explain, expand, 
removeSortInSubquery,
+  trimUnusedFields, createValuesRel, explain, expand, 
removeSortInSubQuery,
   inSubQueryThreshold, relBuilderConfigTransform, relBuilderFactory,
   hintStrategyTable);
 }
@@ -5998,7 +5998,7 @@ public class SqlToRelConverter {
 private final boolean createValuesRel;
 private final boolean explain;
 private final boolean expand;
-private final boolean removeSortInSubquery;
+private final boolean removeSortInSubQuery;
 private final int inSubQueryThreshold;
 priv

[calcite] branch master updated: [CALCITE-4160] Add configuration(SqlToRelConverter.Config) to retain ORDER BY in sub-query (Jiatao Tao)

2020-09-02 Thread danny0405
This is an automated email from the ASF dual-hosted git repository.

danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
 new 734a7ac  [CALCITE-4160] Add configuration(SqlToRelConverter.Config) to 
retain ORDER BY in sub-query (Jiatao Tao)
734a7ac is described below

commit 734a7acd4cef2e3c2ef529844cf34e9fa8173aea
Author: Jiatao Tao <245915...@qq.com>
AuthorDate: Tue Sep 1 12:50:17 2020 +0800

[CALCITE-4160] Add configuration(SqlToRelConverter.Config) to retain ORDER 
BY in sub-query (Jiatao Tao)

close apache/calcite#2127
---
 .../apache/calcite/sql2rel/SqlToRelConverter.java  | 41 +++---
 .../apache/calcite/test/SqlToRelConverterTest.java |  6 
 .../apache/calcite/test/SqlToRelConverterTest.xml  | 19 ++
 3 files changed, 61 insertions(+), 5 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java 
b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
index 1b0a25c..3da06dd 100644
--- a/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
+++ b/core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
@@ -824,10 +824,10 @@ public class SqlToRelConverter {
   List orderExprList,
   SqlNode offset,
   SqlNode fetch) {
-if (!bb.top
+if (removeSortInSubquery(bb.top)
 || select.getOrderList() == null
 || select.getOrderList().getList().isEmpty()) {
-  assert !bb.top || collation.getFieldCollations().isEmpty();
+  assert removeSortInSubquery(bb.top) || 
collation.getFieldCollations().isEmpty();
   if ((offset == null
 || (offset instanceof SqlLiteral
 && ((SqlLiteral) 
offset).bigDecimalValue().equals(BigDecimal.ZERO)))
@@ -866,6 +866,15 @@ public class SqlToRelConverter {
   }
 
   /**
+   * Returns whether we should remove the sort for the subsequent query 
conversion.
+   *
+   * @param topQuery Whether the query is in the top level.
+   */
+  private boolean removeSortInSubquery(boolean topQuery) {
+return config.isRemoveSortInSubquery() && !topQuery;
+  }
+
+  /**
* Returns whether a given node contains a {@link SqlInOperator}.
*
* @param node a RexNode tree
@@ -3249,7 +3258,7 @@ public class SqlToRelConverter {
   return;
 }
 
-if (!bb.top) {
+if (removeSortInSubquery(bb.top)) {
   SqlNode offset = select.getOffset();
   if ((offset == null
   || (offset instanceof SqlLiteral
@@ -5865,6 +5874,15 @@ public class SqlToRelConverter {
  * the relational expressions. Default is
  * {@link HintStrategyTable#EMPTY}. */
 HintStrategyTable getHintStrategyTable();
+
+/**
+ * Returns whether to remove sort operator for a sub-query
+ * if the sort has no offset and fetch limit attributes.
+ * Because the remove does not change the semantics, in many cases, this 
is a promotion.
+ *
+ *  Default is true.
+ */
+boolean isRemoveSortInSubquery();
   }
 
   /** Builder for a {@link Config}. */
@@ -5874,6 +5892,7 @@ public class SqlToRelConverter {
 private boolean createValuesRel = true;
 private boolean explain;
 private boolean expand = true;
+private boolean removeSortInSubquery = true;
 private int inSubQueryThreshold = DEFAULT_IN_SUB_QUERY_THRESHOLD;
 private UnaryOperator relBuilderConfigTransform = c ->
 c.withPushJoinCondition(true);
@@ -5889,6 +5908,7 @@ public class SqlToRelConverter {
   this.createValuesRel = config.isCreateValuesRel();
   this.explain = config.isExplain();
   this.expand = config.isExpand();
+  this.removeSortInSubquery = config.isRemoveSortInSubquery();
   this.inSubQueryThreshold = config.getInSubQueryThreshold();
   this.relBuilderConfigTransform = config.getRelBuilderConfigTransform();
   this.relBuilderFactory = config.getRelBuilderFactory();
@@ -5921,6 +5941,11 @@ public class SqlToRelConverter {
   return this;
 }
 
+public ConfigBuilder withRemoveSortInSubQuery(boolean 
removeSortInSubquery) {
+  this.removeSortInSubquery = removeSortInSubquery;
+  return this;
+}
+
 /** Whether to push down join conditions; default true. */
 public ConfigBuilder withPushJoinCondition(boolean pushJoinCondition) {
   return withRelBuilderConfigTransform(
@@ -5959,7 +5984,7 @@ public class SqlToRelConverter {
 /** Builds a {@link Config}. */
 public Config build() {
   return new ConfigImpl(decorrelationEnabled,
-  trimUnusedFields, createValuesRel, explain, expand,
+  trimUnusedFields, createValuesRel, explain, expand, 
removeSortInSubquery,
   inSubQueryThreshold, relBuilderConfigTransform, relBuilderFactory,
   hintStrategyTable);
 }
@@ -5973,6 +5998,7 @@ public class SqlToRelConverter {
 private final boolean createValuesRel;
 privat