You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by "zabetak (via GitHub)" <gi...@apache.org> on 2025/07/29 10:58:27 UTC
[PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
zabetak opened a new pull request, #5998:
URL: https://github.com/apache/hive/pull/5998
### What changes were proposed in this pull request?
1. Collect all table aliases under joins
2. Introduce derived table whenever the same alias appears more than once
### Why are the changes needed?
Some queries that contain joins of the same table more than twice fail during compilation while trying to transform the optimized AST (obtained from CBO) to an Operator tree. The "Ambiguous table alias" exception is raised when a HiveJoin contains the same table scan multiple times (without an interleaving project) cause the generated AST is ambiguous.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
```
mvn test -Dtest=TestMiniLlapLocalCliDriver -Dqfile_regex=cbo_self_join.*
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5998:
URL: https://github.com/apache/hive/pull/5998#issuecomment-3164894991
## [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998) **Quality Gate passed**
Issues
 [2 New issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak merged PR #5998:
URL: https://github.com/apache/hive/pull/5998
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "kasakrisz (via GitHub)" <gi...@apache.org>.
kasakrisz commented on code in PR #5998:
URL: https://github.com/apache/hive/pull/5998#discussion_r2257017348
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java:
##########
@@ -230,6 +225,47 @@ private static void convertOpTree(RelNode rel, RelNode parent) {
}
}
+ /**
+ * A handler that detects self-joins in the plan and rewrites them to resolve ambiguous aliases.
+ * The handler traverses the plan and collects aliases of tables it encounters for each join branch.
+ * When the same alias occurs in both branches of a join, it introduces a derive table (Project)
+ * over the left branch to break the ambiguity.
+ */
+ private static class SelfJoinHandler extends RelHomogeneousShuttle {
+ private final Set<String> aliases = new HashSet<>();
+
+ @Override
+ public RelNode visit(final RelNode rel) {
+ if (rel instanceof Join join) {
Review Comment:
Can `HiveRelShuttleImpl` be used instead of `RelHomogeneousShuttle` to get rid of `rel instanceof <Something>`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on PR #5998:
URL: https://github.com/apache/hive/pull/5998#issuecomment-3173978318
Many thanks for the review @kasakrisz !
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on code in PR #5998:
URL: https://github.com/apache/hive/pull/5998#discussion_r2256098952
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java:
##########
@@ -123,29 +127,35 @@ public static RelNode convertOpTree(RelNode rel, List<FieldSchema> resultSchema,
return newTopNode;
}
- private static String getTblAlias(RelNode rel) {
+ private static class AliasCollector extends RelHomogeneousShuttle {
+ List<String> aliases = new ArrayList<>();
Review Comment:
@kasakrisz The PR is now ready for review, please take a look when you get the chance.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "kasakrisz (via GitHub)" <gi...@apache.org>.
kasakrisz commented on code in PR #5998:
URL: https://github.com/apache/hive/pull/5998#discussion_r2244781788
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java:
##########
@@ -123,29 +127,35 @@ public static RelNode convertOpTree(RelNode rel, List<FieldSchema> resultSchema,
return newTopNode;
}
- private static String getTblAlias(RelNode rel) {
+ private static class AliasCollector extends RelHomogeneousShuttle {
+ List<String> aliases = new ArrayList<>();
Review Comment:
No worries. Please let me know if you need another review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "kasakrisz (via GitHub)" <gi...@apache.org>.
kasakrisz commented on code in PR #5998:
URL: https://github.com/apache/hive/pull/5998#discussion_r2242540853
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java:
##########
@@ -123,29 +127,35 @@ public static RelNode convertOpTree(RelNode rel, List<FieldSchema> resultSchema,
return newTopNode;
}
- private static String getTblAlias(RelNode rel) {
+ private static class AliasCollector extends RelHomogeneousShuttle {
+ List<String> aliases = new ArrayList<>();
Review Comment:
What happens when `accept` is called more than once on the same instance ? Is this list reseted before each?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on code in PR #5998:
URL: https://github.com/apache/hive/pull/5998#discussion_r2242751351
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java:
##########
@@ -123,29 +127,35 @@ public static RelNode convertOpTree(RelNode rel, List<FieldSchema> resultSchema,
return newTopNode;
}
- private static String getTblAlias(RelNode rel) {
+ private static class AliasCollector extends RelHomogeneousShuttle {
+ List<String> aliases = new ArrayList<>();
Review Comment:
I discovered some issues with the previous code so I reworked the approach and force-pushed a new version. Apologies for rebasing and squashing, I haven't seen your comments before pushing the new version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5998:
URL: https://github.com/apache/hive/pull/5998#issuecomment-3136741905
## [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998) **Quality Gate passed**
Issues
 [3 New issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5998:
URL: https://github.com/apache/hive/pull/5998#issuecomment-3155304523
## [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998) **Quality Gate passed**
Issues
 [5 New issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_coverage&view=list)
 [1.4% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "zabetak (via GitHub)" <gi...@apache.org>.
zabetak commented on code in PR #5998:
URL: https://github.com/apache/hive/pull/5998#discussion_r2262330498
##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java:
##########
@@ -230,6 +225,47 @@ private static void convertOpTree(RelNode rel, RelNode parent) {
}
}
+ /**
+ * A handler that detects self-joins in the plan and rewrites them to resolve ambiguous aliases.
+ * The handler traverses the plan and collects aliases of tables it encounters for each join branch.
+ * When the same alias occurs in both branches of a join, it introduces a derive table (Project)
+ * over the left branch to break the ambiguity.
+ */
+ private static class SelfJoinHandler extends RelHomogeneousShuttle {
+ private final Set<String> aliases = new HashSet<>();
+
+ @Override
+ public RelNode visit(final RelNode rel) {
+ if (rel instanceof Join join) {
Review Comment:
@kasakrisz I applied your suggestion in https://github.com/apache/hive/pull/5998/commits/b12cff8cfbff6453e41fda561e9241c0b3a0cb67 and tests are again green. Please take another look when you find some bandwitdth.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org
Re: [PR] HIVE-28222: Ambiguous table alias exception for queries with self joins [hive]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5998:
URL: https://github.com/apache/hive/pull/5998#issuecomment-3132141136
## [](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998) **Quality Gate passed**
Issues
 [2 New issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_hive&pullRequest=5998&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_hive&pullRequest=5998&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_hive&pullRequest=5998&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_hive&pullRequest=5998)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org