Search in sources :

Example 1 with DelegatingScope

use of org.apache.calcite.sql.validate.DelegatingScope in project calcite by apache.

the class SqlToRelConverter method isSubQueryNonCorrelated.

/**
 * Determines whether a sub-query is non-correlated. Note that a
 * non-correlated sub-query can contain correlated references, provided those
 * references do not reference select statements that are parents of the
 * sub-query.
 *
 * @param subq the sub-query
 * @param bb   blackboard used while converting the sub-query, i.e., the
 *             blackboard of the parent query of this sub-query
 * @return true if the sub-query is non-correlated
 */
private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
    Set<CorrelationId> correlatedVariables = RelOptUtil.getVariablesUsed(subq);
    for (CorrelationId correlName : correlatedVariables) {
        DeferredLookup lookup = mapCorrelToDeferred.get(correlName);
        String originalRelName = lookup.getOriginalRelName();
        final SqlNameMatcher nameMatcher = lookup.bb.scope.getValidator().getCatalogReader().nameMatcher();
        final SqlValidatorScope.ResolvedImpl resolved = new SqlValidatorScope.ResolvedImpl();
        lookup.bb.scope.resolve(ImmutableList.of(originalRelName), nameMatcher, false, resolved);
        SqlValidatorScope ancestorScope = resolved.only().scope;
        // If the correlated reference is in a scope that's "above" the
        // sub-query, then this is a correlated sub-query.
        SqlValidatorScope parentScope = bb.scope;
        do {
            if (ancestorScope == parentScope) {
                return false;
            }
            if (parentScope instanceof DelegatingScope) {
                parentScope = ((DelegatingScope) parentScope).getParent();
            } else {
                break;
            }
        } while (parentScope != null);
    }
    return true;
}
Also used : SqlValidatorScope(org.apache.calcite.sql.validate.SqlValidatorScope) SqlNameMatcher(org.apache.calcite.sql.validate.SqlNameMatcher) CorrelationId(org.apache.calcite.rel.core.CorrelationId) NlsString(org.apache.calcite.util.NlsString) DelegatingScope(org.apache.calcite.sql.validate.DelegatingScope)

Aggregations

CorrelationId (org.apache.calcite.rel.core.CorrelationId)1 DelegatingScope (org.apache.calcite.sql.validate.DelegatingScope)1 SqlNameMatcher (org.apache.calcite.sql.validate.SqlNameMatcher)1 SqlValidatorScope (org.apache.calcite.sql.validate.SqlValidatorScope)1 NlsString (org.apache.calcite.util.NlsString)1