[ 
https://issues.apache.org/jira/browse/CALCITE-7009?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17950973#comment-17950973
 ] 

Konstantin Orlov commented on CALCITE-7009:
-------------------------------------------

The reason is validation in 
\{{org.apache.calcite.sql2rel.SqlToRelConverter#getCorrelationUse}}:

{code}
    // All correlations must refer the same namespace since correlation
    // produces exactly one correlation source.
    // The same source might be referenced by different variables since
    // DeferredLookups are not de-duplicated at create time.
    SqlValidatorNamespace prevNs = null;

    for (CorrelationId correlName : correlatedVariables) {
      ...
      lookup.bb.scope.resolve(ImmutableList.of(lookup.originalRelName),
          nameMatcher, false, resolved);
      assert resolved.count() == 1;
      final SqlValidatorScope.Resolve resolve = resolved.only();
      final SqlValidatorNamespace foundNs = resolve.namespace;
      ...
      final SqlValidatorScope ancestorScope = resolve.scope;
      boolean correlInCurrentScope = bb.scope.isWithin(ancestorScope);

      if (!correlInCurrentScope) {
        continue;
      }

      if (prevNs == null) {
        prevNs = foundNs;
      } else {
        assert prevNs == foundNs : "All correlation variables should resolve"
            + " to the same namespace."
            + " Prev ns=" + prevNs
            + ", new ns=" + foundNs;
      }

      ...
{code}

As far as I understand correlates, every such variable represents not a 
particular namespace, but an entire scope (i.e. in case of correlated subquery 
in Project node, the input of the Project will be a correlation source, 
therefore it spans over all tables in JOIN which is placed under the project in 
relational tree). The only thing which does matter is that every scope of 
correlated variables is within the current one. And this invariant already 
covered by these lines:
{code}
      final SqlValidatorScope ancestorScope = resolve.scope;
      boolean correlInCurrentScope = bb.scope.isWithin(ancestorScope);

      if (!correlInCurrentScope) {
        continue;
      }
{code}

That being said, the solution seems simple - delete the assertion in question. 
I've done this in local branch, and all tests in {{SqlToRelConverterTest}} and 
{{QuidemTest}} have passed successfully (including the one in description). 
I've also tried to run a few test cases on Apache Ignite with updated calcite 
library, and results fine as well.

Anyway, I'm going to prepare patch removing the assertion, but it would be 
great if anyone with knowledge of how correlates work in calcite validate my 
observation.

> AssertionError when converting query containing multiple correlated 
> subqueries referencing different tables in FROM
> -------------------------------------------------------------------------------------------------------------------
>
>                 Key: CALCITE-7009
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7009
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.39.0
>            Reporter: Konstantin Orlov
>            Priority: Major
>
> If SELECT list contains several correlated subqueries referencing different 
> tables in FROM list, then AssertionError is thrown during sql-to-rel 
> conversion phase:
> {code}
> // org.apache.calcite.test.SqlToRelConverterTest
>   @Test void 
> testMultipleCorrelatedSubQueriesInSelectReferencingDifferentTablesInFrom() {
>     final String sql = "select\n"
>         + "(select ename from emp where empno = empnos.empno) as emp_name,\n"
>         + "(select name from dept where deptno = deptnos.deptno) as 
> dept_name\n"
>         + " from (values (1), (2)) as empnos(empno), (values (1), (2)) as 
> deptnos(deptno)";
>     sql(sql).withExpand(false).ok();
>   }
> // Throws following error
> java.lang.AssertionError: All correlation variables should resolve to the 
> same namespace. Prev 
> ns=org.apache.calcite.sql.validate.AliasNamespace@2b3c7f6, new 
> ns=org.apache.calcite.sql.validate.AliasNamespace@2803f6c9
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.getCorrelationUse(SqlToRelConverter.java:3147)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertNonAggregateSelectList(SqlToRelConverter.java:4843)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectList(SqlToRelConverter.java:4765)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(SqlToRelConverter.java:809)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(SqlToRelConverter.java:735)
>       at 
> org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(SqlToRelConverter.java:3939)
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to