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

RelBuilder sort-combining optimization treats aliases incorrectly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.11.0
    • 1.12.0
    • core
    • None

    Description

      This optimization in RelBuilder.sortLimit() manipulates the stack using replaceTop() which preserves the current field names. The current field names are not guaranteed to be the same as any descendant the Project and thus the replaceTop(sort2.getInput()) call places a RelNode on top of the stack with incorrect field names.

            if (top instanceof Project) {
              final Project project = (Project) top;
              if (project.getInput() instanceof Sort) {
                final Sort sort2 = (Sort) project.getInput();
                if (sort2.offset == null && sort2.fetch == null) {
                  replaceTop(sort2.getInput());
                  final RelNode sort =
                      sortFactory.createSort(peek(), sort2.collation,
                          offsetNode, fetchNode);
                  replaceTop(sort);
                  project(project.getProjects());
                  return this;
                }
              }
            }
      

      Test:

          builder.scan("EMP")
                 .sort(0)
                 .project(builder.field(1))
                 .limit(0, 1);
      

      Throws:

      java.lang.IndexOutOfBoundsException: index (1) must be less than size (1)
      	at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1177)
      	at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:1159)
      	at com.google.common.collect.SingletonImmutableList.get(SingletonImmutableList.java:41)
      	at org.apache.calcite.tools.RelBuilder.inferAlias(RelBuilder.java:942)
      	at org.apache.calcite.tools.RelBuilder.project(RelBuilder.java:869)
      	at org.apache.calcite.tools.RelBuilder.project(RelBuilder.java:830)
      	at org.apache.calcite.tools.RelBuilder.project(RelBuilder.java:819)
      	at org.apache.calcite.tools.RelBuilder.sortLimit(RelBuilder.java:1549)
      	at org.apache.calcite.tools.RelBuilder.limit(RelBuilder.java:1467)
      

      Attachments

        Issue Links

          Activity

            People

              julianhyde Julian Hyde
              jbalint@gmail.com Jess Balint
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: