Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.util;
 
 
 import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public final class JsonPlanPrinter
 {
     private static final JsonCodec<QueryExplanationCODEC = JsonCodec.jsonCodec(QueryExplanation.class);
     private final ImmutableList.Builder<InputinputBuilder = ImmutableList.builder();
 
     private JsonPlanPrinter(PlanNode planMetadata metadata)
     {
         checkNotNull(plan"plan is null");
         checkNotNull(metadata"metadata is null");
         SourceVisitor visitor = new SourceVisitor(metadata);
         plan.accept(visitornull);
     }
 
     public static String getPlan(PlanNode planMetadata metadata)
     {
         return new JsonPlanPrinter(planmetadata).toString();
     }
 
     @Override
     public String toString()
     {
         return .toJson(new QueryExplanation(.build()));
     }
 
     private class SourceVisitor
             extends PlanVisitor<VoidVoid>
     {
         private final Metadata metadata;
 
         public SourceVisitor(Metadata metadata)
         {
             this. = checkNotNull(metadata);
         }
 
         @Override
         public Void visitJoin(JoinNode nodeVoid context)
         {
             node.getLeft().accept(thisnull);
             node.getRight().accept(thisnull);
 
             return null;
         }
 
         @Override
        public Void visitSemiJoin(SemiJoinNode nodeVoid context)
        {
            node.getSource().accept(thisnull);
            node.getFilteringSource().accept(thisnull);
            return null;
        }
        @Override
        public Void visitIndexJoin(IndexJoinNode nodeVoid context)
        {
            node.getProbeSource().accept(thisnull);
            node.getIndexSource().accept(thisnull);
            return null;
        }
        @Override
        public Void visitLimit(LimitNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitAggregation(AggregationNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitMarkDistinct(MarkDistinctNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitWindow(final WindowNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitRowNumber(final RowNumberNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitTopNRowNumber(final TopNRowNumberNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitTableScan(TableScanNode nodeVoid context)
        {
            TupleDomain<ColumnHandlepartitionsDomainSummary = node.getPartitionsDomainSummary();
            TableMetadata tableMetadata = .getTableMetadata(node.getTable());
            ImmutableList.Builder<ColumncolumnBuilder = ImmutableList.builder();
            for (Map.Entry<SymbolColumnHandleentry : node.getAssignments().entrySet()) {
                ColumnMetadata columnMetadata = .getColumnMetadata(node.getTable(), entry.getValue());
                Domain domain = null;
                if (!partitionsDomainSummary.isNone() && partitionsDomainSummary.getDomains().containsKey(entry.getValue())) {
                    domain = partitionsDomainSummary.getDomains().get(entry.getValue());
                }
                else if (partitionsDomainSummary.isNone()) {
                    domain = Domain.none(columnMetadata.getType().getJavaType());
                }
                Column column = new Column(
                        columnMetadata.getName(),
                        columnMetadata.getType().toString(),
                        Optional.ofNullable(SimpleDomain.fromDomain(domain)));
                columnBuilder.add(column);
            }
            Input input = new Input(
                    tableMetadata.getConnectorId(),
                    tableMetadata.getTable().getSchemaName(),
                    tableMetadata.getTable().getTableName(),
                    columnBuilder.build());
            .add(input);
            return null;
        }
        @Override
        public Void visitIndexSource(IndexSourceNode nodeVoid context)
        {
            TableMetadata tableMetadata = .getTableMetadata(node.getTableHandle());
            ImmutableList.Builder<ColumncolumnBuilder = ImmutableList.builder();
            for (Map.Entry<SymbolColumnHandleentry : node.getAssignments().entrySet()) {
                ColumnMetadata columnMetadata = .getColumnMetadata(node.getTableHandle(), entry.getValue());
                Domain domain = null;
                if (!node.getEffectiveTupleDomain().isNone() && node.getEffectiveTupleDomain().getDomains().containsKey(entry.getValue())) {
                    domain = node.getEffectiveTupleDomain().getDomains().get(entry.getValue());
                }
                else if (node.getEffectiveTupleDomain().isNone()) {
                    domain = Domain.none(columnMetadata.getType().getJavaType());
                }
                Column column = new Column(
                        columnMetadata.getName(),
                        columnMetadata.getType().toString(),
                        Optional.ofNullable(SimpleDomain.fromDomain(domain)));
                columnBuilder.add(column);
            }
            Input input = new Input(
                    tableMetadata.getConnectorId(),
                    tableMetadata.getTable().getSchemaName(),
                    tableMetadata.getTable().getTableName(),
                    columnBuilder.build());
            .add(input);
            return null;
        }
        @Override
        public Void visitFilter(FilterNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitProject(ProjectNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitOutput(OutputNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitTopN(final TopNNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitSort(final SortNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitRemoteSource(RemoteSourceNode nodeVoid context)
        {
            return null;
        }
        @Override
        public Void visitUnion(UnionNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitTableWriter(TableWriterNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        public Void visitTableCommit(TableCommitNode nodeVoid context)
        {
            return processChildren(node);
        }
        @Override
        protected Void visitPlan(PlanNode nodeVoid context)
        {
            throw new UnsupportedOperationException("not yet implemented: " + node.getClass().getName());
        }
        private Void processChildren(PlanNode node)
        {
            for (PlanNode child : node.getSources()) {
                child.accept(thisnull);
            }
            return null;
        }
    }
New to GrepCode? Check out our FAQ X