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-5530] RelToSqlConverter[ORDER BY] generates an incorrect fie… #3085

Closed
wants to merge 7 commits into from

Conversation

JiajunBernoulli
Copy link
Contributor

…ld alias when 2 projection fields have the same name

…ld alias when 2 projection fields have the same name
Copy link
Member

@libenchao libenchao left a comment

Choose a reason for hiding this comment

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

@JiajunBernoulli Thanks for your PR, I've left my comments.

+ "FROM \"scott\".\"EMP\"\n"
+ "ORDER BY 2) AS \"t0\"";
assertThat(actualSql1, isLinux(expectedSql1));
// SqlConformance#isSortByAlias is false for Spark.
Copy link
Member

Choose a reason for hiding this comment

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

SqlConformance is used by parser, not RelToSqlConverter, hence it does not affact anything here, you can see that your expected sql is still sorting by ordinal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your careful review.

I added a new commit to solve it.

@libenchao
Copy link
Member

@JiajunBernoulli The tests seem still fail~

@JiajunBernoulli
Copy link
Contributor Author

The PR also solve CALCITE-5656

Would you please review it again? @libenchao

@libenchao
Copy link
Member

I'll do a final review

&& clauses.contains(Clause.ORDER_BY)
&& dialect.getConformance().isSortByOrdinal()) {
// Cannot merge a Project that contains sort by ordinal under it.
return hasSortByOrdinal();
Copy link
Member

Choose a reason for hiding this comment

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

To be honest, I felt uneasy about moving this up. I'm not sure if we will miss some opportunity to add a new sub query. The changes in #3057 was also merged by me, and now I think that is not perfect as well, since it also suffers from this.

For needNewSubQuery, we should only bail out when we returns true. In that way, we could safely add new conditions into this method, and all the cases are orthogonal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, we should return true avoid return false blocking other conditions.

@@ -1791,6 +1791,15 @@ private boolean needNewSubQuery(
return false;
}
final Clause maxClause = Collections.max(clauses);

if (rel instanceof Project
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason that you we need to move this block from L1819-L1825 to here? If not, I would suggest to keep it there to make the change of git history minimal and trackable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to L1819-L1825

* RelToSqlConverter[ORDER BY] generates an incorrect field alias
* when 2 projection fields have the same name</a>.
*/
@Test void testSortWithAnFieldNotInTheProjectionWithASameAliasAsThatInTheProjection() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@Test void testSortWithAnFieldNotInTheProjectionWithASameAliasAsThatInTheProjection() {
@Test void testOrderByFieldNotInTheProjectionWithASameAliasAsThatInTheProjection() {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

assertThat(actualSql2, isLinux(expectedSql2));
}

@Test void testSortWithAnExpressionNotInTheProjectionThatRefersToUnderlyingFieldWithSameAlias() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@Test void testSortWithAnExpressionNotInTheProjectionThatRefersToUnderlyingFieldWithSameAlias() {
@Test void testOrderByExpressionNotInTheProjectionThatRefersToUnderlyingFieldWithSameAlias() {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@sonarqubecloud
Copy link

sonarqubecloud bot commented May 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

@libenchao libenchao left a comment

Choose a reason for hiding this comment

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

+1

@libenchao libenchao closed this in cb34e78 May 3, 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

Successfully merging this pull request may close these issues.

2 participants