Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-6204

When do not expand the plan when SqlToRel, the final plan with SET_SEMANTICS_TABLE is wrong

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • None
    • None

    Description

      Modify the test SqlToRelConverterTest#testTableFunctionWithPartitionKey to reproduce this bug:

      @Test void testTableFunctionWithPartitionKey() {
        final String sql = "select *\n"
            + "from table(topn(table orders partition by productid, 3))";
        sql(sql).withConfig(c -> c.withExpand(false)).ok();
      } 

      The final plan is like:

      LogicalProject(ROWTIME=[$0], PRODUCTID=[$1], ORDERID=[$2], RANK_NUMBER=[$3])
        LogicalTableFunctionScan(invocation=[TOPN(3)], rowType=[RecordType(TIMESTAMP(0) ROWTIME, INTEGER PRODUCTID, INTEGER ORDERID, BIGINT RANK_NUMBER)])
       

      We can see that in the final plan the TableFunctionScan lost the partition keys and lost the subquery that contains the original table source `orders`.

      =========================================================

      The following is my attempt to find out where the bug lies. Some pictures with variables for some key steps are attached to make it easier for others to understand.

      When I dug into the code, I found that the problem may be in the following steps:

        1. When not expanding plan, we will retain subquery in SqlToRelConverter#convertCollectionTable 

        2. When converting the SqlCall of the "topn" function in StandardConvertletTable, the first SET_SEMANTICS_TABLE operand will be forcibly ignored, so only the arg "3" in the "topn" function is retained, and the original table "order" and partition key are removed. For details, please refer to StandardConvertletTable#convertOperands 

        3. After converting the expression with "topn" SqlCall to RexCall, when building LogicalTableFunctionScan, we only use the RexCall that does not contain the original table "order" and partition key, and the complete subquery retained by step 1 is not used..

      The plan for the relevant steps is as follows:
      1. Subquery retained when bb is not expanded


      2.1
      SqlCall before entering StandardConvertletTable

      2.2
      RexCall after StandardConvertletTable processing

      3. The constructed LogicalTableFunctionScan

       

       

      Attachments

        1. image-2024-01-16-10-12-37-047.png
          827 kB
          xuyang
        2. image-2024-01-16-10-14-04-036.png
          658 kB
          xuyang
        3. image-2024-01-16-10-15-20-310.png
          380 kB
          xuyang
        4. image-2024-01-16-10-16-54-471.png
          756 kB
          xuyang

        Issue Links

          Activity

            People

              Unassigned Unassigned
              xuyangzhong xuyang
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated: