public static Pair<Boolean, String> containsTokenOfType(ASTNode root, Integer ... tokens) {
    final Set<Integer> tokensToMatch = new HashSet<>(Arrays.asList(tokens));
    final String[] matched = {null};

    boolean check =  ParseUtils.containsTokenOfType(root, new PTFUtils.Predicate<ASTNode>() {
      @Override
      public boolean apply(ASTNode node) {
        if (tokensToMatch.contains(node.getType())) {
          matched[0] = node.getText();
          return true;
        }

        return false;
      }
    });

    return Pair.of(check, matched[0]);
  }
Performs a descent of the leftmost branch of a tree, stopping when either a
node with a non-null token is found or the leaf level is encountered.

@param tree
         candidate node from which to start searching

@return node at which descent stopped
ql/src/java/org/apache/hadoop/hive/ql/parse/ParseUtils.java