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

EnumerableSortedAggregate returns incorrect result when input is empty

    XMLWordPrintableJSON

Details

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

    Description

      Performing a MAX on an empty table (or on a table where we apply a filter which does not return any value) shall return NULL, we can verify this with our "standard" EnumerableAggregate operator:

        @Test void enumerableAggregateOnEmptyInput() {
          tester(false, new HrSchema())
              .query("select max(deptno) as m from emps where deptno>100")
              .explainContains(
                  "EnumerableAggregate(group=[{}], m=[MAX($1)])\n"
                  + "  EnumerableCalc(expr#0..4=[{inputs}], expr#5=[100], expr#6=[>($t1, $t5)], proj#0..4=[{exprs}], $condition=[$t6])\n"
                  + "    EnumerableTableScan(table=[[s, emps]])")
              .returnsOrdered("m=null");
        }
      

      However, if we use Hook.PLANNER to force the aggregation to be implemented via EnumerableSortedAggregate on the same query:

        @Test void enumerableSortedAggregateOnEmptyInput() {
          tester(false, new HrSchema())
              .query("select max(deptno) as m from emps where deptno>100")
              .withHook(Hook.PLANNER, (Consumer<RelOptPlanner>) planner -> {
                planner.removeRule(EnumerableRules.ENUMERABLE_AGGREGATE_RULE);
                planner.addRule(EnumerableRules.ENUMERABLE_SORTED_AGGREGATE_RULE);
              })
              .explainContains(
                  "EnumerableSortedAggregate(group=[{}], m=[MAX($1)])\n"
                  + "  EnumerableCalc(expr#0..4=[{inputs}], expr#5=[100], expr#6=[>($t1, $t5)], proj#0..4=[{exprs}], $condition=[$t6])\n"
                  + "    EnumerableTableScan(table=[[s, emps]])")
              .returnsOrdered("m=null");
        }
      

      It fails, because the EnumerableSortedAggregate returns an empty result set rather than NULL:

      java.lang.AssertionError: 
      Expected: "m=null"
           but: was ""
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              rubenql Ruben Q L
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated: