mihaibudiu commented on code in PR #4748:
URL: https://github.com/apache/calcite/pull/4748#discussion_r2695515719
##########
core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java:
##########
@@ -2242,12 +2243,37 @@ public SqlSelect asSelect() {
if (node instanceof SqlSelect) {
return (SqlSelect) node;
}
- if (!dialect.hasImplicitTableAlias()) {
+ if (!dialect.hasImplicitTableAlias() || hasConflictTableAlias(node)) {
return wrapSelect(asFrom());
}
return wrapSelect(node);
}
+ private boolean hasConflictTableAlias(SqlNode node) {
+ if (!(node instanceof SqlIdentifier)) {
+ return false;
+ }
+ if (correlTableMap.isEmpty()) {
Review Comment:
Is this the only source of possibly conflicting aliases?
Well, maybe we don't need to know the answer to this question, it may be
good enough that this IS a place where conflicting aliases can arise.
##########
core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java:
##########
@@ -6426,6 +6426,25 @@ private void checkLiteral2(String expression, String
expected) {
sql(query).withConfig(c -> c.withExpand(false)).ok(expected);
}
+ /** Test case of
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7378">[CALCITE-7378]
+ * Potential incorrect column attribution in RelToSqlConverter due to
implicit
+ * table alias handling</a>. */
+ @Test void testSubQueryWithSelfJoin() {
+ String query = "select \"product_name\" from \"product\" t1 "
+ + "where \"product_id\" not in (select \"product_id\" "
+ + "from \"product\" t2 "
+ + "where t2.\"product_id\" = t1.\"product_id\" "
+ + "and t1.\"product_id\" = 2 and t2.\"product_id\" = 1)";
+ String expected = "SELECT \"product_name\"\n"
Review Comment:
how about a quidem test with this query? It's still not very easy to tell
that this query is the right one.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]