Skip to content

[CALCITE-6221] JDBC adapter generates invalid query when the same table is joined multiple times#3658

Closed
kramerul wants to merge 1 commit intoapache:mainfrom
sap-contributions:join-fieldname-collisions
Closed

[CALCITE-6221] JDBC adapter generates invalid query when the same table is joined multiple times#3658
kramerul wants to merge 1 commit intoapache:mainfrom
sap-contributions:join-fieldname-collisions

Conversation

@kramerul
Copy link
Contributor

No description provided.

@kramerul kramerul force-pushed the join-fieldname-collisions branch 4 times, most recently from a4ec94a to 7aacd35 Compare January 29, 2024 12:43
oldSelectList.add(new SqlIdentifier(rightFieldNames.get(i), POS));
}
} else {
oldSelectList = ImmutableList.<SqlNode>copyOf(builder.select.getSelectList().getList());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please fix CI

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed the CheckerFramework finding.

@kramerul kramerul force-pushed the join-fieldname-collisions branch 3 times, most recently from dc820ad to 0ff0d50 Compare January 29, 2024 13:30
@kramerul
Copy link
Contributor Author

This PR could cause some problems. We had trouble with a statement like

SELECT 
FROM "TA" AS "A"  
INNER JOIN "TB" AS "B"
	ON "A"."APPLICATION_ID" =  "B"."CHILD_APPLICATION_ID"
INNER JOIN "TC" AS "C"
	ON "C"."APPLICATION_ID" = "B"."PARENT_APPLICATION_ID"

"B"."CHILD_APPLICATION_ID" was hidden from the additional SELECT.

return result(join, leftResult, rightResult);
}

private Result maybeFixRenamedFields(Result rightResult, Join e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem to have any relation to JDBC.
Perhaps the issue should be renamed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect that this change may affect other open issues on JIRA, it looks pretty basic.
Can you do a quick check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The class RelToSqlConverter is only used in JdbcImplementor and PigRelToSqlConverter. Both classes are related to JDBC. Therefore, I would like to keep the title of the issue. Additionally Julian Hyde added the JDBC relation to the title

+ "FROM (VALUES (1, 2)) AS \"t\" (\"K\", \"V\")\n"
+ "INNER JOIN "
+ "(VALUES (1)) AS \"t0\" (\"K\") ON \"t\".\"K\" = \"t0\".\"K\"");
+ "INNER JOIN (SELECT \"K\" AS \"K0\"\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not easy to review all these changes. I checked some of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it's quite hard to review all changes. I also had a look at most of the changes inside the tests.

SqlNode column = oldSelectList.get(i);
if (!rightFieldNames.get(i).equals(fieldNames.get(offset + i))) {
column =
SqlStdOperatorTable.AS.createCall(POS, SqlUtil.stripAs(column),
Copy link
Contributor

Choose a reason for hiding this comment

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

What guarantees that this name is unique?
This may be another column name that appears on the other side.
I think you need to create a Set with all "forbidden" names and make sure that this name is not part of that set.

Copy link
Contributor Author

@kramerul kramerul Jan 30, 2024

Choose a reason for hiding this comment

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

The uniqueness is currently guaranteed by the function SqlValidatorUtil.deriveJoinRowType which creates the unique field names for the Join relation. But this is not really visible here. Therefore, I will add a comment.

@kramerul kramerul force-pushed the join-fieldname-collisions branch from 0ff0d50 to 951b324 Compare January 30, 2024 12:35
@kramerul kramerul force-pushed the join-fieldname-collisions branch from 951b324 to e8f266d Compare January 30, 2024 12:44
@sonarqubecloud
Copy link

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

1 New issue
0 Security Hotspots
98.2% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

int offset = e.getLeft().getRowType().getFieldCount();
boolean hasFieldNameCollision = false;
for (int i = 0; i < rightFieldNames.size(); i++) {
if (!rightFieldNames.get(i).equals(fieldNames.get(offset + i))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why is it sufficient to compare with the field with the same relative index rather than compare all n^2 pairs?

@kramerul
Copy link
Contributor Author

I will rework the complete PR according to the last comment of Julian Hyde in https://issues.apache.org/jira/browse/CALCITE-6221

@kramerul
Copy link
Contributor Author

kramerul commented Feb 1, 2024

Closing this PR in favor of a new one.

@kramerul kramerul closed this Feb 1, 2024
@kramerul kramerul deleted the join-fieldname-collisions branch February 1, 2024 13:55
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