Search in sources :

Example 1 with TypeSignatureTranslator.toSqlType

use of io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType in project trino by trinodb.

the class SubqueryPlanner method planScalarSubquery.

private PlanBuilder planScalarSubquery(PlanBuilder subPlan, Cluster<SubqueryExpression> cluster) {
    // Plan one of the predicates from the cluster
    SubqueryExpression scalarSubquery = cluster.getRepresentative();
    RelationPlan relationPlan = planSubquery(scalarSubquery, subPlan.getTranslations());
    PlanBuilder subqueryPlan = newPlanBuilder(relationPlan, analysis, lambdaDeclarationToSymbolMap);
    PlanNode root = new EnforceSingleRowNode(idAllocator.getNextId(), subqueryPlan.getRoot());
    Type type = analysis.getType(scalarSubquery);
    RelationType descriptor = relationPlan.getDescriptor();
    List<Symbol> fieldMappings = relationPlan.getFieldMappings();
    Symbol column;
    if (descriptor.getVisibleFieldCount() > 1) {
        column = symbolAllocator.newSymbol("row", type);
        ImmutableList.Builder<Expression> fields = ImmutableList.builder();
        for (int i = 0; i < descriptor.getAllFieldCount(); i++) {
            Field field = descriptor.getFieldByIndex(i);
            if (!field.isHidden()) {
                fields.add(fieldMappings.get(i).toSymbolReference());
            }
        }
        Expression expression = new Cast(new Row(fields.build()), TypeSignatureTranslator.toSqlType(type));
        root = new ProjectNode(idAllocator.getNextId(), root, Assignments.of(column, expression));
    } else {
        column = getOnlyElement(fieldMappings);
    }
    return appendCorrelatedJoin(subPlan, root, scalarSubquery.getQuery(), CorrelatedJoinNode.Type.INNER, TRUE_LITERAL, mapAll(cluster, subPlan.getScope(), column));
}
Also used : Cast(io.trino.sql.tree.Cast) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) PlanBuilder.newPlanBuilder(io.trino.sql.planner.PlanBuilder.newPlanBuilder) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) Field(io.trino.sql.analyzer.Field) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) RelationType(io.trino.sql.analyzer.RelationType) Type(io.trino.spi.type.Type) PlanNode(io.trino.sql.planner.plan.PlanNode) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) NotExpression(io.trino.sql.tree.NotExpression) EnforceSingleRowNode(io.trino.sql.planner.plan.EnforceSingleRowNode) RelationType(io.trino.sql.analyzer.RelationType) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Row(io.trino.sql.tree.Row)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 Type (io.trino.spi.type.Type)1 Field (io.trino.sql.analyzer.Field)1 RelationType (io.trino.sql.analyzer.RelationType)1 TypeSignatureTranslator.toSqlType (io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType)1 PlanBuilder.newPlanBuilder (io.trino.sql.planner.PlanBuilder.newPlanBuilder)1 EnforceSingleRowNode (io.trino.sql.planner.plan.EnforceSingleRowNode)1 PlanNode (io.trino.sql.planner.plan.PlanNode)1 ProjectNode (io.trino.sql.planner.plan.ProjectNode)1 Cast (io.trino.sql.tree.Cast)1 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)1 Expression (io.trino.sql.tree.Expression)1 NotExpression (io.trino.sql.tree.NotExpression)1 QuantifiedComparisonExpression (io.trino.sql.tree.QuantifiedComparisonExpression)1 Row (io.trino.sql.tree.Row)1 SubqueryExpression (io.trino.sql.tree.SubqueryExpression)1