[ 
https://issues.apache.org/jira/browse/CALCITE-5284?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17637108#comment-17637108
 ] 

Ulrich Kramer commented on CALCITE-5284:
----------------------------------------

Hi [~zabetak],

thanks for your answer. Your suspicion was absolutely correct. Adding the same 
logic from {{RelMdRowCount}} to {{JdbcSort::computeSeltCost}} solves the issue.

 {code:java}
    @Override public @Nullable RelOptCost computeSelfCost(RelOptPlanner planner,
        RelMetadataQuery mq) {
      RelOptCost cost = super.computeSelfCost(planner, mq);
      if (cost == null) {
        return null;
      }
      Double rowCount = cost.getRows();
      if (rowCount == null ) {
        return null;
      }
      if ( !rowCount.equals(0D)) {
        final int offset = this.offset instanceof RexLiteral ? 
RexLiteral.intValue(this.offset) : 0;
        rowCount = Math.max(rowCount - offset, 0D);
  
        final double limit =
            this.fetch instanceof RexLiteral ? RexLiteral.intValue(this.fetch) 
: rowCount;
        cost = cost.multiplyBy(limit < rowCount ? limit : rowCount / 
cost.getRows());
      }
      return cost.multiplyBy(0.9);
    }
{code}

> JDBC rules create inefficient plan
> ----------------------------------
>
>                 Key: CALCITE-5284
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5284
>             Project: Calcite
>          Issue Type: Bug
>         Environment: Calcite 1.31.1. on Mac
>            Reporter: Ulrich Kramer
>            Priority: Major
>
> The following unit test for {{JdbcAdapterTest}} fails:
> {code:java}
>   @Test void testOffset() {
>     CalciteAssert.model(FoodmartSchema.FOODMART_MODEL)
>         .query("select * from \"sales_fact_1997\" limit 10 offset 20")
>         .explainContains("PLAN=JdbcToEnumerableConverter\n" +
>             "  JdbcSort(offset=[20], fetch=[10])\n" +
>             "    JdbcTableScan(table=[[foodmart, sales_fact_1997]])")
>         .runs();
>   }
> {code}
> For an offset less than 13, an efficient plan is created. With an offset 
> above 13, the plan looks like this:
> {code}
> EnumerableLimit(offset=[20], fetch=[10])
>   JdbcToEnumerableConverter
>     JdbcTableScan(table=[[foodmart, sales_fact_1997]])
> {code}
> which can lead to enormous latency times, since the entire table is loaded 
> via JDBC.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to