[
https://issues.apache.org/jira/browse/CALCITE-6157?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17802063#comment-17802063
]
Ulrich Kramer edited comment on CALCITE-6157 at 1/3/24 8:47 AM:
----------------------------------------------------------------
The problem is caused by a change in {{SqlValidatorImpl::registerNamespace}}
{code}
- namespaces.put(requireNonNull(ns.getNode(), () -> "ns.getNode() for " +
ns), ns);
+ SqlValidatorNamespace namespace =
+ namespaces.get(requireNonNull(ns.getNode(), () -> "ns.getNode() for "
+ ns));
+ if (namespace == null) {
+ namespaces.put(requireNonNull(ns.getNode()), ns);
+ namespace = ns;
+ }
{code}
Since namespaces is an {{IdentityHashMap}} and we use a cache for all
{{SqlIdentifier}} generated for {{SqlWithItem}}, {{ns}} and {{namespace}} have
different values at the end.
In our case, the first identifier in {{"it1 as t1"}} and {{"it1 as t2"}} point
the same object. Therefore, {{namespace.get(..)}} returns the namespace with
the wrong {{enclosingScope}}
was (Author: kramerul):
The problem is caused by a change in {{SqlValidatorImpl::registerNamespace}}
{code}
- namespaces.put(requireNonNull(ns.getNode(), () -> "ns.getNode() for " +
ns), ns);
+ SqlValidatorNamespace namespace =
+ namespaces.get(requireNonNull(ns.getNode(), () -> "ns.getNode() for "
+ ns));
+ if (namespace == null) {
+ namespaces.put(requireNonNull(ns.getNode()), ns);
+ namespace = ns;
+ }
{code}
Since namespaces is an {{IdentityHashMap}} and we use a cache for all
{{SqlIdentifier}} generated for {{SqlWithItem}}, {{ns}} and {{namespace}} have
different values at the end.
In out case, the first identifier in {{"it1 as t1"}} and {{"it1 as t2"}} point
the same object. Therefore, {{namespace.get(..)}} returns the namespace with
the wrong {{enclosingScope}}
> SqlValidatorImpl breaks join condition when inlining table alias
> ----------------------------------------------------------------
>
> Key: CALCITE-6157
> URL: https://issues.apache.org/jira/browse/CALCITE-6157
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.36.0
> Reporter: Ulrich Kramer
> Priority: Major
>
> When switching from 1.35.0 to 1.36.0, {{SqlValidatorImpl}} breaks the join
> condition on the following statement
> {code:SQL}
> WITH `it1` AS (
> SELECT
> `company`,
> `area`,
> `revenue`,
> `regionId`
> FROM
> `12345678-1234-1234-1234-000100000001`.`revenues`
> )
> SELECT
> *
> FROM
> (
> SELECT
> `T1`.`company` AS `company`,
> MAX(`T1`.`revenue`) AS `revenue`
> FROM
> `it1` AS `T1`
> GROUP BY
> `company`
> ) AS `T`
> LEFT JOIN `it1` AS `T2` ON `T`.`company` = `T2`.`company`
> AND `T`.`revenue` = `T2`.`revenue`
> {code}
> It modifies the join condition [at this
> location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java#L3603-L3604]
> to
> {code:SQL}
> `T`.`company` = `T1`.`company`
> AND `T`.`revenue` = `T1`.`revenue`
> {code}
> which causes a "{{Table `T1`not found}}" exception.
> I already tried to reproduce the problem by adding the following test to
> {{SqlValidatorTest}}, but everything worked fine
> {code:java}
> @Test void testWith2() {
> sql("with it1 as (select empno, sal, deptno from emp)\n"
> + "select * from ( select t1.empno as empno, max(t1.deptno) as deptno
> from it1 as t1 group by empno) as t\n"
> + "left outer join it1 as t2 on t.empno = t2.empno and t.deptno =
> t2.deptno")
> .ok();
> }
> {code}
> During debugging, I recognized that the {{DelegatingScope}} does something
> different in our case [at this
> location|https://github.com/apache/calcite/blob/d3ab0bc8e4d4c9ebc0fc4e33ce478d276f5d11e4/core/src/main/java/org/apache/calcite/sql/validate/DelegatingScope.java#L333-L337].
> In our case the {{fromNs}} resolved to {{`it1` AS `T1`}}, which is wrong.
> But I was not able to find the root cause yet.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)