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

fix(isthmus): support expressions in Sort #322

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

yassram
Copy link

@yassram yassram commented Jan 6, 2025

Motivation

This PR adds support for expressions in Sort (today, only input references are supported #192).

Details

This PR uses Calcite's relbuilder.sort(rexNodes) rather than computing RelFieldCollation (which required input references only). Calcite then makes sure to expand the Sort into a LogicalSort and a LogicalProject to evaluate expressions if needed (an additional LogicalProject is applied which acts like a remap to remove any introduced fields to preserve the same output).

Examples:

e1:
SELECT foo FROM bar ORDER BY foo
LogicalSort(sort0=[$0], dir0=[ASC])
  TableScan(table=[bar])
e2:
SELECT foo FROM bar ORDER BY foo::int + 1 DESC NULLS LAST
LogicalProject(foo=[$0])
  LogicalSort(sort0=[$1], dir0=[DESC-nulls-last])
    LogicalProject(foo=[$0], $f1=[+(CAST($0):BIGINT, 1)])
      TableScan(table=[bar])

See ComplexSortTest.java for more examples

@CLAassistant
Copy link

CLAassistant commented Jan 6, 2025

CLA assistant check
All committers have signed the CLA.

@yassram yassram force-pushed the yr/isthmus-sort-expressions-192 branch from 374169d to 318f831 Compare January 7, 2025 20:52
@yassram yassram marked this pull request as ready for review January 7, 2025 20:54
@vbarua vbarua self-requested a review January 7, 2025 21:15
Copy link
Member

@vbarua vbarua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. It's good to be able to rely on built-in Calcite functionality to expand complex expressions in the sort fields.

I did leave one comment on your tests. The CollationRelWriter seems somewhat redundant given that that the same information is visible in the LogicalSorts

return relBuilder.push(child).sort(Collections.EMPTY_LIST).build();
}
RelNode node = relBuilder.push(child).sort(RelCollations.of(relFieldCollations)).build();
RelNode node = relBuilder.push(child).sort(sortExpressions).build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find! It's really helpful that we can rely on Calcite's machinery to do this for us.

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

Successfully merging this pull request may close these issues.

3 participants