Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calcite Aggregates Built w/ Out Of Order Grouping Keys Have Incorrect Layout #155

Open
vbarua opened this issue Jun 26, 2023 · 0 comments
Open
Assignees

Comments

@vbarua
Copy link
Member

vbarua commented Jun 26, 2023

Code Example

public class SubstraitRelNodeConverterTest extends PlanTestBase {
      @Test
      void groupingKeyOrdering() {
          Plan.Root root = b.root(
          b.aggregate(input -> b.grouping(input, 2, 0), input -> List.of(),
                  b.namedScan(List.of("foo"), List.of("a", "b", "c"), List.of(R.I64, R.I64, R.STRING))));
          var relNode = converter.convert(root.getInput());
          // Fails because the output record has shape (a, c) because Calcite "forgets" the grouping key order
          assertRowMatch(relNode.getRowType(), R.STRING, R.I64);
      }
}

Explanation

When build an aggregate using the RelBuilder, Calcite converts the grouping keys into an immutable bit set and then uses the order of the keys in the bit set to generate the output record shape. That means that if you hand it a grouping key like in the example of (2, 0), Calcite will output the row as (0, 2) (which the rest of the Substrait plan is not necessarily expecting).

This behaviour in Calcite appears to be somewhat expected, as the Calcite Sql Parser appears to apply projections before aggregations to account for this behaviour.

@vbarua vbarua changed the title Aggregates Built w/ Out Of Order Grouping Keys Calcite Aggregates Built w/ Out Of Order Grouping Keys Have Incorrect Layout Jun 26, 2023
@vbarua vbarua self-assigned this Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant