Java 类com.facebook.presto.sql.tree.Unnest 实例源码

项目:presto    文件:StatementAnalyzer.java   
@Override
protected RelationType visitUnnest(Unnest node, AnalysisContext context)
{
    ImmutableList.Builder<Field> outputFields = ImmutableList.builder();
    for (Expression expression : node.getExpressions()) {
        ExpressionAnalysis expressionAnalysis = analyzeExpression(expression, context.getLateralTupleDescriptor(), context);
        Type expressionType = expressionAnalysis.getType(expression);
        if (expressionType instanceof ArrayType) {
            outputFields.add(Field.newUnqualified(Optional.empty(), ((ArrayType) expressionType).getElementType()));
        }
        else if (expressionType instanceof MapType) {
            outputFields.add(Field.newUnqualified(Optional.empty(), ((MapType) expressionType).getKeyType()));
            outputFields.add(Field.newUnqualified(Optional.empty(), ((MapType) expressionType).getValueType()));
        }
        else {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Cannot unnest type: " + expressionType);
        }
    }
    if (node.isWithOrdinality()) {
        outputFields.add(Field.newUnqualified(Optional.empty(), BigintType.BIGINT));
    }
    RelationType descriptor = new RelationType(outputFields.build());
    analysis.setOutputDescriptor(node, descriptor);
    return descriptor;
}
项目:presto    文件:TestSqlParser.java   
@Test
public void testUnnest()
        throws Exception
{
    assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a)",
            simpleQuery(
                    selectList(new AllColumns()),
                    new Join(
                            Join.Type.CROSS,
                            new Table(QualifiedName.of("t")),
                            new Unnest(ImmutableList.of(new QualifiedNameReference(QualifiedName.of("a"))), false),
                            Optional.empty())));
    assertStatement("SELECT * FROM t CROSS JOIN UNNEST(a) WITH ORDINALITY",
            simpleQuery(
                    selectList(new AllColumns()),
                    new Join(
                            Join.Type.CROSS,
                            new Table(QualifiedName.of("t")),
                            new Unnest(ImmutableList.of(new QualifiedNameReference(QualifiedName.of("a"))), true),
                            Optional.empty())));
}
项目:presto    文件:RelationPlanner.java   
private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node)
{
    RelationType outputDescriptor = analysis.getOutputDescriptor(joinNode);
    RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
    // Create symbols for the result of unnesting
    ImmutableList.Builder<Symbol> unnestedSymbolsBuilder = ImmutableList.builder();
    for (Field field : unnestOutputDescriptor.getVisibleFields()) {
        Symbol symbol = symbolAllocator.newSymbol(field);
        unnestedSymbolsBuilder.add(symbol);
    }
    ImmutableList<Symbol> unnestedSymbols = unnestedSymbolsBuilder.build();

    // Add a projection for all the unnest arguments
    PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
    planBuilder = appendProjections(planBuilder, node.getExpressions());
    TranslationMap translations = planBuilder.getTranslations();
    ProjectNode projectNode = checkType(planBuilder.getRoot(), ProjectNode.class, "planBuilder.getRoot()");

    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    UnmodifiableIterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Symbol inputSymbol = translations.get(expression);
        if (type instanceof ArrayType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
        }
        else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        }
        else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");

    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), projectNode, leftPlan.getOutputSymbols(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, outputDescriptor, unnestNode.getOutputSymbols(), Optional.empty());
}
项目:presto    文件:RelationPlanner.java   
@Override
protected RelationPlan visitUnnest(Unnest node, Void context)
{
    RelationType descriptor = analysis.getOutputDescriptor(node);
    ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
    for (Field field : descriptor.getVisibleFields()) {
        Symbol symbol = symbolAllocator.newSymbol(field);
        outputSymbolsBuilder.add(symbol);
    }
    List<Symbol> unnestedSymbols = outputSymbolsBuilder.build();

    // If we got here, then we must be unnesting a constant, and not be in a join (where there could be column references)
    ImmutableList.Builder<Symbol> argumentSymbols = ImmutableList.builder();
    ImmutableList.Builder<Expression> values = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    Iterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        Object constantValue = evaluateConstantExpression(expression, analysis.getCoercions(), metadata, session, analysis.getColumnReferences());
        Type type = analysis.getType(expression);
        values.add(LiteralInterpreter.toExpression(constantValue, type));
        Symbol inputSymbol = symbolAllocator.newSymbol(expression, type);
        argumentSymbols.add(inputSymbol);
        if (type instanceof ArrayType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
        }
        else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        }
        else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    ValuesNode valuesNode = new ValuesNode(idAllocator.getNextId(), argumentSymbols.build(), ImmutableList.<List<Expression>>of(values.build()));

    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), valuesNode, ImmutableList.<Symbol>of(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, descriptor, unnestedSymbols, Optional.empty());
}
项目:presto-query-formatter    文件:StatementFormatter.java   
@Override
protected Void visitUnnest(Unnest node, Integer indent)
{
    builder.append(node.toString());
    return null;
}
项目:presto    文件:SqlFormatter.java   
@Override
protected Void visitUnnest(Unnest node, Integer indent)
{
    builder.append(node.toString());
    return null;
}
项目:presto    文件:AstBuilder.java   
@Override
public Node visitUnnest(SqlBaseParser.UnnestContext context)
{
    return new Unnest(getLocation(context), visit(context.expression(), Expression.class), context.ORDINALITY() != null);
}
项目:EchoQuery    文件:SqlFormatter.java   
@Override
protected Void visitUnnest(Unnest node, Integer indent)
{
    builder.append(node.toString());
    return null;
}