PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/tools/LineageInfo.java

#
Java | 158 lines | 76 code | 24 blank | 58 comment | 7 complexity | ad37c9b110120f86b3396a0445746a00 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.ql.tools;
 20
 21import java.io.IOException;
 22import java.util.ArrayList;
 23import java.util.LinkedHashMap;
 24import java.util.Map;
 25import java.util.Stack;
 26import java.util.TreeSet;
 27
 28import org.apache.hadoop.hive.ql.lib.DefaultGraphWalker;
 29import org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher;
 30import org.apache.hadoop.hive.ql.lib.Dispatcher;
 31import org.apache.hadoop.hive.ql.lib.GraphWalker;
 32import org.apache.hadoop.hive.ql.lib.Node;
 33import org.apache.hadoop.hive.ql.lib.NodeProcessor;
 34import org.apache.hadoop.hive.ql.lib.NodeProcessorCtx;
 35import org.apache.hadoop.hive.ql.lib.Rule;
 36import org.apache.hadoop.hive.ql.parse.ASTNode;
 37import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
 38import org.apache.hadoop.hive.ql.parse.HiveParser;
 39import org.apache.hadoop.hive.ql.parse.ParseDriver;
 40import org.apache.hadoop.hive.ql.parse.ParseException;
 41import org.apache.hadoop.hive.ql.parse.SemanticException;
 42
 43/**
 44 *
 45 * This class prints out the lineage info. It takes sql as input and prints
 46 * lineage info. Currently this prints only input and output tables for a given
 47 * sql. Later we can expand to add join tables etc.
 48 *
 49 */
 50public class LineageInfo implements NodeProcessor {
 51
 52  /**
 53   * Stores input tables in sql.
 54   */
 55  TreeSet<String> inputTableList = new TreeSet<String>();
 56  /**
 57   * Stores output tables in sql.
 58   */
 59  TreeSet<String> OutputTableList = new TreeSet<String>();
 60
 61  /**
 62   *
 63   * @return java.util.TreeSet
 64   */
 65  public TreeSet<String> getInputTableList() {
 66    return inputTableList;
 67  }
 68
 69  /**
 70   * @return java.util.TreeSet
 71   */
 72  public TreeSet<String> getOutputTableList() {
 73    return OutputTableList;
 74  }
 75
 76  /**
 77   * Implements the process method for the NodeProcessor interface.
 78   */
 79  public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
 80      Object... nodeOutputs) throws SemanticException {
 81    ASTNode pt = (ASTNode) nd;
 82
 83    switch (pt.getToken().getType()) {
 84
 85    case HiveParser.TOK_TAB:
 86      OutputTableList.add(BaseSemanticAnalyzer.getUnescapedName((ASTNode)pt.getChild(0)));
 87      break;
 88
 89    case HiveParser.TOK_TABREF:
 90      ASTNode tabTree = (ASTNode) pt.getChild(0);
 91      String table_name = (tabTree.getChildCount() == 1) ?
 92          BaseSemanticAnalyzer.getUnescapedName((ASTNode)tabTree.getChild(0)) :
 93            BaseSemanticAnalyzer.getUnescapedName((ASTNode)tabTree.getChild(0)) + "." + tabTree.getChild(1);
 94      inputTableList.add(table_name);
 95      break;
 96    }
 97    return null;
 98  }
 99
100  /**
101   * parses given query and gets the lineage info.
102   *
103   * @param query
104   * @throws ParseException
105   */
106  public void getLineageInfo(String query) throws ParseException,
107      SemanticException {
108
109    /*
110     * Get the AST tree
111     */
112    ParseDriver pd = new ParseDriver();
113    ASTNode tree = pd.parse(query);
114
115    while ((tree.getToken() == null) && (tree.getChildCount() > 0)) {
116      tree = (ASTNode) tree.getChild(0);
117    }
118
119    /*
120     * initialize Event Processor and dispatcher.
121     */
122    inputTableList.clear();
123    OutputTableList.clear();
124
125    // create a walker which walks the tree in a DFS manner while maintaining
126    // the operator stack. The dispatcher
127    // generates the plan from the operator tree
128    Map<Rule, NodeProcessor> rules = new LinkedHashMap<Rule, NodeProcessor>();
129
130    // The dispatcher fires the processor corresponding to the closest matching
131    // rule and passes the context along
132    Dispatcher disp = new DefaultRuleDispatcher(this, rules, null);
133    GraphWalker ogw = new DefaultGraphWalker(disp);
134
135    // Create a list of topop nodes
136    ArrayList<Node> topNodes = new ArrayList<Node>();
137    topNodes.add(tree);
138    ogw.startWalking(topNodes, null);
139  }
140
141  public static void main(String[] args) throws IOException, ParseException,
142      SemanticException {
143
144    String query = args[0];
145
146    LineageInfo lep = new LineageInfo();
147
148    lep.getLineageInfo(query);
149
150    for (String tab : lep.getInputTableList()) {
151      System.out.println("InputTable=" + tab);
152    }
153
154    for (String tab : lep.getOutputTableList()) {
155      System.out.println("OutputTable=" + tab);
156    }
157  }
158}