Search in sources :

Example 6 with Scope

use of io.trino.sql.analyzer.Scope in project trino by trinodb.

the class ScopeAware method scopeAwareComparison.

private Boolean scopeAwareComparison(Node left, Node right) {
    if (left instanceof Expression && right instanceof Expression) {
        Expression leftExpression = (Expression) left;
        Expression rightExpression = (Expression) right;
        if (analysis.isColumnReference(leftExpression) && analysis.isColumnReference(rightExpression)) {
            ResolvedField leftField = analysis.getResolvedField(leftExpression);
            ResolvedField rightField = analysis.getResolvedField(rightExpression);
            Scope leftScope = leftField.getScope();
            Scope rightScope = rightField.getScope();
            // For subqueries of the query associated with the current expression, compare by syntax
            // Note: it'd appear that hash() and equal() are inconsistent with each other in the case that:
            // * left.hasOuterParent(...) == true and right.hasOuterParent(...) == false
            // * leftField.getFieldId().equals(rightField.getFieldId()) == true
            // Both fields would seem to have different hashes but be equal to each other.
            // However, this cannot happen because we *require* that both expressions being compared by
            // rooted in the same "query scope" (i.e., sub-scopes that are local to each other) -- see ScopeAwareKey.equals().
            // If both fields have the same field id, by definition they will produce the same result for hasOuterParent().
            checkState(leftScope.hasOuterParent(queryScope) == rightScope.hasOuterParent(queryScope));
            if (leftScope.hasOuterParent(queryScope) && rightScope.hasOuterParent(queryScope)) {
                return treeEqual(leftExpression, rightExpression, CanonicalizationAware::canonicalizationAwareComparison);
            }
            // expression, compare by resolved field
            return leftField.getFieldId().equals(rightField.getFieldId());
        } else if (leftExpression instanceof Identifier && rightExpression instanceof Identifier) {
            return treeEqual(leftExpression, rightExpression, CanonicalizationAware::canonicalizationAwareComparison);
        }
    }
    if (!left.shallowEquals(right)) {
        return false;
    }
    return null;
}
Also used : ResolvedField(io.trino.sql.analyzer.ResolvedField) CanonicalizationAware(io.trino.sql.analyzer.CanonicalizationAware) Identifier(io.trino.sql.tree.Identifier) Scope(io.trino.sql.analyzer.Scope) Expression(io.trino.sql.tree.Expression)

Example 7 with Scope

use of io.trino.sql.analyzer.Scope in project trino by trinodb.

the class LogicalPlanner method createExplainAnalyzePlan.

private RelationPlan createExplainAnalyzePlan(Analysis analysis, ExplainAnalyze statement) {
    RelationPlan underlyingPlan = planStatementWithoutOutput(analysis, statement.getStatement());
    PlanNode root = underlyingPlan.getRoot();
    Scope scope = analysis.getScope(statement);
    Symbol outputSymbol = symbolAllocator.newSymbol(scope.getRelationType().getFieldByIndex(0));
    ImmutableList.Builder<Symbol> actualOutputs = ImmutableList.builder();
    RelationType outputDescriptor = analysis.getOutputDescriptor(statement.getStatement());
    for (Field field : outputDescriptor.getVisibleFields()) {
        int fieldIndex = outputDescriptor.indexOf(field);
        Symbol symbol = underlyingPlan.getSymbol(fieldIndex);
        actualOutputs.add(symbol);
    }
    root = new ExplainAnalyzeNode(idAllocator.getNextId(), root, outputSymbol, actualOutputs.build(), statement.isVerbose());
    return new RelationPlan(root, scope, ImmutableList.of(outputSymbol), Optional.empty());
}
Also used : Field(io.trino.sql.analyzer.Field) PlanNode(io.trino.sql.planner.plan.PlanNode) Scope(io.trino.sql.analyzer.Scope) ExplainAnalyzeNode(io.trino.sql.planner.plan.ExplainAnalyzeNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RelationType(io.trino.sql.analyzer.RelationType)

Aggregations

Scope (io.trino.sql.analyzer.Scope)7 PlanNode (io.trino.sql.planner.plan.PlanNode)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Field (io.trino.sql.analyzer.Field)4 RelationType (io.trino.sql.analyzer.RelationType)4 Expression (io.trino.sql.tree.Expression)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)3 TableHandle (io.trino.metadata.TableHandle)3 ColumnHandle (io.trino.spi.connector.ColumnHandle)3 Type (io.trino.spi.type.Type)3 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)3 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)3 ValuesNode (io.trino.sql.planner.plan.ValuesNode)3 Identifier (io.trino.sql.tree.Identifier)3 Query (io.trino.sql.tree.Query)3 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)2 Session (io.trino.Session)2 NOT_SUPPORTED (io.trino.spi.StandardErrorCode.NOT_SUPPORTED)2