Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Support ordinal aliases in GROUP and ORDER BY clauses #248

Merged
merged 15 commits into from Oct 24, 2019

Conversation

abbashus
Copy link
Contributor

@abbashus abbashus commented Oct 23, 2019

Issue #215

Description of changes:

  • Added a new Rewriter to map GROUP BY and ORDER By aliases to SELECT column fields

Testing

  • Passing Gradle build with existing unit and integration tests
  • Added new unit tests and integration tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@galkk
Copy link
Contributor

galkk commented Oct 23, 2019

I like this approach. In theory, it could be used to inline/fix aliases as well, in order by/group by expressions, instead of hackier approach that we have now (something that I've tried to do in https://github.com/opendistro-for-elasticsearch/sql/pull/168/files, that I needed to revert later)


MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlSelectQuery;

if (!hasGroupByWithOrdinals(query) && !hasOrderByWithOrdinals(query)) {
Copy link
Contributor

@galkk galkk Oct 23, 2019

Choose a reason for hiding this comment

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

I don't understand why we've supposedly have the same check twice, could you add more comments explaining what's going on? From the code it looks like this checks if we have ordinals, and the code below does the same, but in different way, on parsed raw sql query. Could only one check work? Why we need to parse sql again?

In general, could you add better description of algorithm into the PR or into javadoc, with examples. I find the intention of the code a bit hard to follow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some comments to help understand the code better. Removed the redundant check.

Comment on lines 169 to 170
SQLExpr newExpr = orderOrdinalToExpr.get(ordinalValue);
checkInvalidOrdinal(newExpr, ordinalValue, orderException);
Copy link
Contributor

@galkk galkk Oct 23, 2019

Choose a reason for hiding this comment

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

this is a bit repetitive, you can extract this to method like

getExpressionByOrdinal

and throw an exception inside (will simplify code a bt). Same for visiting group by expressions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored it into checkAndGet method.

Copy link
Contributor

@davidcui1225 davidcui1225 left a comment

Choose a reason for hiding this comment

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

LGTM

@abbashus abbashus merged commit 489511e into opendistro-for-elasticsearch:master Oct 24, 2019
@abbashus abbashus deleted the ordinal-alias branch October 24, 2019 19:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants