You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2022/04/21 00:34:58 UTC

[GitHub] [calcite] JiajunBernoulli opened a new pull request, #2774: [CALCITE-5091] RelMdRowCount can return more accurate rowCount

JiajunBernoulli opened a new pull request, #2774:
URL: https://github.com/apache/calcite/pull/2774

   Here is the sql that fetch is deterministic and offset is dynamic 
   `select * from emp order by ename limit 1 offset ?`
   **RelMdRowCount#getRowCount** will return 14d, because it always return rowCount when offset is dynamic.
   
   ```
     if (rel.offset instanceof RexDynamicParam) {
       return rowCount;
     }
   ```
   Obviously, rowCount will not exceed 1 because LIMIT 1. We can improve it


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] JiajunBernoulli commented on a diff in pull request #2774: [CALCITE-5091] RelMdRowCount can return more accurate rowCount

Posted by GitBox <gi...@apache.org>.
JiajunBernoulli commented on code in PR #2774:
URL: https://github.com/apache/calcite/pull/2774#discussion_r855679082


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java:
##########
@@ -147,45 +146,27 @@ public Double getRowCount(Calc rel, RelMetadataQuery mq) {
     if (rowCount == null) {
       return null;
     }
-    if (rel.offset instanceof RexDynamicParam) {
-      return rowCount;
-    }
-    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+
+    final int offset = rel.offset instanceof RexLiteral ? RexLiteral.intValue(rel.offset) : 0;
     rowCount = Math.max(rowCount - offset, 0D);
 
-    if (rel.fetch != null) {
-      if (rel.fetch instanceof RexDynamicParam) {
-        return rowCount;
-      }
-      final int limit = RexLiteral.intValue(rel.fetch);
-      if (limit < rowCount) {
-        return (double) limit;
-      }
-    }
-    return rowCount;
+    final double limit =
+        rel.fetch instanceof RexLiteral ? RexLiteral.intValue(rel.fetch) : rowCount;
+    return limit < rowCount ? limit : rowCount;
   }
 
   public @Nullable Double getRowCount(EnumerableLimit rel, RelMetadataQuery mq) {
     Double rowCount = mq.getRowCount(rel.getInput());
     if (rowCount == null) {
       return null;
     }
-    if (rel.offset instanceof RexDynamicParam) {
-      return rowCount;
-    }
-    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+
+    final int offset = rel.offset instanceof RexLiteral ? RexLiteral.intValue(rel.offset) : 0;
     rowCount = Math.max(rowCount - offset, 0D);

Review Comment:
   Yes, I think the row count value will be wrong. 
   Do you think **14d** is right for the sql?
   `select * from emp order by ename limit 1 offset ?`
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] NobiGo commented on a diff in pull request #2774: [CALCITE-5091] RelMdRowCount can return more accurate rowCount

Posted by GitBox <gi...@apache.org>.
NobiGo commented on code in PR #2774:
URL: https://github.com/apache/calcite/pull/2774#discussion_r854954008


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java:
##########
@@ -147,45 +146,27 @@ public Double getRowCount(Calc rel, RelMetadataQuery mq) {
     if (rowCount == null) {
       return null;
     }
-    if (rel.offset instanceof RexDynamicParam) {
-      return rowCount;
-    }
-    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+
+    final int offset = rel.offset instanceof RexLiteral ? RexLiteral.intValue(rel.offset) : 0;
     rowCount = Math.max(rowCount - offset, 0D);
 
-    if (rel.fetch != null) {
-      if (rel.fetch instanceof RexDynamicParam) {
-        return rowCount;
-      }
-      final int limit = RexLiteral.intValue(rel.fetch);
-      if (limit < rowCount) {
-        return (double) limit;
-      }
-    }
-    return rowCount;
+    final double limit =
+        rel.fetch instanceof RexLiteral ? RexLiteral.intValue(rel.fetch) : rowCount;
+    return limit < rowCount ? limit : rowCount;
   }
 
   public @Nullable Double getRowCount(EnumerableLimit rel, RelMetadataQuery mq) {
     Double rowCount = mq.getRowCount(rel.getInput());
     if (rowCount == null) {
       return null;
     }
-    if (rel.offset instanceof RexDynamicParam) {
-      return rowCount;
-    }
-    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+
+    final int offset = rel.offset instanceof RexLiteral ? RexLiteral.intValue(rel.offset) : 0;
     rowCount = Math.max(rowCount - offset, 0D);

Review Comment:
   If we don't care about the offset value when it is a dynamic param, the row count value will be wrong? I think the origin logic is good.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] chunweilei merged pull request #2774: [CALCITE-5091] RelMdRowCount can return more accurate rowCount

Posted by GitBox <gi...@apache.org>.
chunweilei merged PR #2774:
URL: https://github.com/apache/calcite/pull/2774


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [calcite] NobiGo commented on a diff in pull request #2774: [CALCITE-5091] RelMdRowCount can return more accurate rowCount

Posted by GitBox <gi...@apache.org>.
NobiGo commented on code in PR #2774:
URL: https://github.com/apache/calcite/pull/2774#discussion_r855799246


##########
core/src/main/java/org/apache/calcite/rel/metadata/RelMdRowCount.java:
##########
@@ -147,45 +146,27 @@ public Double getRowCount(Calc rel, RelMetadataQuery mq) {
     if (rowCount == null) {
       return null;
     }
-    if (rel.offset instanceof RexDynamicParam) {
-      return rowCount;
-    }
-    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+
+    final int offset = rel.offset instanceof RexLiteral ? RexLiteral.intValue(rel.offset) : 0;
     rowCount = Math.max(rowCount - offset, 0D);
 
-    if (rel.fetch != null) {
-      if (rel.fetch instanceof RexDynamicParam) {
-        return rowCount;
-      }
-      final int limit = RexLiteral.intValue(rel.fetch);
-      if (limit < rowCount) {
-        return (double) limit;
-      }
-    }
-    return rowCount;
+    final double limit =
+        rel.fetch instanceof RexLiteral ? RexLiteral.intValue(rel.fetch) : rowCount;
+    return limit < rowCount ? limit : rowCount;
   }
 
   public @Nullable Double getRowCount(EnumerableLimit rel, RelMetadataQuery mq) {
     Double rowCount = mq.getRowCount(rel.getInput());
     if (rowCount == null) {
       return null;
     }
-    if (rel.offset instanceof RexDynamicParam) {
-      return rowCount;
-    }
-    final int offset = rel.offset == null ? 0 : RexLiteral.intValue(rel.offset);
+
+    final int offset = rel.offset instanceof RexLiteral ? RexLiteral.intValue(rel.offset) : 0;
     rowCount = Math.max(rowCount - offset, 0D);

Review Comment:
   The limit number is the max row count that the SQL can return.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org