
    H(                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr   " S	 S
5      r " S S\5      r " S S\5      r " S S\5      rg)z1The different TableReader options for the BQ CLI.    )absolute_import)division)print_function)Optional)	discovery)bq_error)bq_id_utilsc            
           \ rS rSrSr   SS\\   S\\   S\\   4S jjr SS\\   S\\   S\\   4S jjr	S	 r
S
\4S jrS
\4S jrS
\4S jr  SS\\   S\\   S\\   S\\   4S jjrSrg)_TableReader   zBase class that defines the TableReader interface.

_TableReaders provide a way to read paginated rows and schemas from a table.
N	start_rowmax_rowsselected_fieldsc                 ,    U R                  UUUS9u  pEU$ )aC  Read at most max_rows rows from a table.

Args:
  start_row: first row to return.
  max_rows: maximum number of rows to return.
  selected_fields: a subset of fields to return.

Raises:
  BigqueryInterfaceError: when bigquery returns something unexpected.

Returns:
  list of rows, each of which is a list of field values.
)r   r   r   )ReadSchemaAndRows)selfr   r   r   _rowss         #platform/bq/clients/table_reader.pyReadRows_TableReader.ReadRows   s+    & &&' ' IQ
 K    c                 *   Uc  [        S5      eUc  [        S5      eSn/ n0 n[        U5      U:  a  U[        U5      -
  n[        U S5      (       d  [        S5      eU R                  (       a  [        U R                  U5      nU R                  U(       a  SOUUUUS9u  pn	U(       d  U	(       a  U	R                  S/ 5      nU H(  n
UR                  U R                  Xj5      5        US-  nM*     U(       a  U(       d   Xe4$ [        U5      U:  a  M  Xe4$ )	a  Read at most max_rows rows from a table and the schema.

Args:
  start_row: first row to read.
  max_rows: maximum number of rows to return.
  selected_fields: a subset of fields to return.

Raises:
  BigqueryInterfaceError: when bigquery returns something unexpected.
  ValueError: when start_row is None.
  ValueError: when max_rows is None.

Returns:
  A tuple where the first item is the list of fields and the
  second item a list of rows.
Nzstart_row is requiredzmax_rows is requiredmax_rows_per_requestz9Subclass must have max_rows_per_request instance variable)r   
page_tokenr   fields   )

ValueErrorlenhasattrNotImplementedErrorr   min_ReadOnePagegetappend_ConvertFromFV)r   r   r   r   r   r   schemarows_to_read	more_rowscurrent_schemarows              r   r   _TableReader.ReadSchemaAndRows0   s$   , .//-..JDF
d)h
D	)lT122!G
 	
 
	"	"444lC040A0A$))	 1B 1-yn ##Hb1#D''45Q	  y>+ d)h
* >r   c                 4   U(       d  gUR                  S/ 5       Vs/ s H  o3R                  SS5      PM     nn/ n[        X5       GH=  u  pgSU;  a  [        R                  " S5      eUS   R	                  5       S:X  a  UR                  S/ 5      nUR                  S	S
5      R	                  5       S:X  aC  UR                  U V	s/ s H$  n	U R                  XR                  SS5      5      PM&     sn	5        M  UR                  U R                  X5      5        M  UR                  S	S
5      R	                  5       S:X  a4  UR                  U V	s/ s H  oR                  SS5      PM     sn	5        GM,  UR                  U5        GM@     U$ s  snf s  sn	f s  sn	f )z;Converts from FV format to possibly nested lists of values.Nfv typez'Invalid response: missing type propertyRECORDr   modeNULLABLEREPEATED)r$   zipr   BigqueryCommunicationErrorupperr%   r&   )
r   r'   r+   entryvaluesresultfieldr/   	subfieldssubvalues
             r   r&   _TableReader._ConvertFromFVd   se   .1ggc2.>?.>UiiR .>F?F'	u	115
 	
 
v			(	*IIh+	99VZ(..0J>
--( !!)\\#r-BC  --++I9
:99VZ(..0J>Q?Q||C,Q?@ 	a- (. M3 @ @s   F+F
F
returnc                 "    U R                  5       $ N_GetPrintContextr   s    r   __str___TableReader.__str__         ""r   c                 "    U R                  5       $ rB   rC   rE   s    r   __repr___TableReader.__repr__   rH   r   c                     [        S5      e)z'Returns context for what is being read.z'Subclass must implement GetPrintContextr!   rE   s    r   rD   _TableReader._GetPrintContext   s    
G
HHr   r   c                     [        S5      e)a  Read one page of data, up to max_rows rows.

Assumes that the table is ready for reading. Will signal an error otherwise.

Args:
  start_row: first row to read.
  max_rows: maximum number of rows to return.
  page_token: Optional. current page token.
  selected_fields: a subset of field to return.

Returns:
  tuple of:
  rows: the actual rows of the table, in f,v format.
  page_token: the page token of the next page of results.
  schema: the schema of the table.
z$Subclass must implement _ReadOnePagerM   )r   r   r   r   r   s        r   r#   _TableReader._ReadOnePage   s    . D
EEr    )r   NNrB   NN)__name__
__module____qualname____firstlineno____doc__r   intstrr   r   r&   rF   rJ   rD   r#   __static_attributes__rQ   r   r   r   r      s     "# $'+	#   }	< (,	2#2 2  }	2h>#s ## #I I #''+F#F F 3-	F
  }F Fr   r   c            
           \ rS rSrSrS\R                  S\S\R                  R                  4S jrS\4S jr  SS
\\   S\\   S\\   S\\   4S jjrSrg	)TableTableReader   z&A TableReader that reads from a table.local_apiclientr   	table_refc                 (    X0l         X l        Xl        g rB   )r_   r   
_apiclient)r   r^   r   r_   s       r   __init__TableTableReader.__init__   s     N 4%Or   r@   c                     U R                   < $ rB   )r_   rE   s    r   rD   !TableTableReader._GetPrintContext   s    >>#r   Nr   r   r   r   c                    [        U R                  5      nX%S'   U(       a  X5S'   OXS'   S nUb  XES'   Uc8  U R                  R                  5       R                  " S0 UD6R                  5       nUR                  SS 5      nUR                  S/ 5      n[        U R                  5      nUb  XES'   U R                  R                  5       R                  " S0 UD6R                  5       nUR                  S0 5      n	XsU	4$ )N
maxResults	pageToken
startIndexselectedFieldsr   r'   rQ   )dictr_   ra   	tabledatalistexecuter$   tables)
r   r   r   r   r   kwdsdatar   
table_infor'   s
             r   r#   TableTableReader._ReadOnePage   s     D!$;$<D".|__&&(--55==?d+t,J88FBDD".'')--55==?J^^Hb)Ff%%r   )ra   r   r_   rR   )rS   rT   rU   rV   rW   r   ResourcerX   r	   ApiClientHelperTableReferencerb   rY   rD   r   r#   rZ   rQ   r   r   r\   r\      s    .& ))&  & ,,;;	&$ $ #''+&#& & 3-	&
  }& &r   r\   c            
           \ rS rSrSrS\R                  S\S\R                  R                  4S jrS\4S jr  SS
\\   S\\   S\\   S\\   4S jjrSrg	)JobTableReader   z.A TableReader that reads from a completed job.r^   r   job_refc                 (    X0l         X l        Xl        g rB   )rz   r   ra   )r   r^   r   rz   s       r   rb   JobTableReader.__init__   s     L 4%Or   r@   c                     U R                   < $ rB   rz   rE   s    r   rD   JobTableReader._GetPrintContext       <<!r   Nr   r   r   r   c                    [        U R                  5      nX%S'   SUS'   U(       a  X5S'   OXS'   U R                  R                  5       R                  " S0 UD6R                  5       nUS   (       d  [        R                  " SU < S35      eUR                  SS 5      nUR                  S	S 5      nUR                  S
/ 5      nXU4$ )Nrg   r   	timeoutMsrh   ri   jobCompleteJob  is not doner'   r   rQ   )	rk   rz   ra   jobsgetQueryResultsrn   r   BigqueryErrorr$   )	r   r   r   r   r   rp   rq   r'   r   s	            r   r#   JobTableReader._ReadOnePage   s     D!D$;$<??!119D9AACD""4#ABB+t,JXXh%F88FBDf%%r   )ra   rz   r   rR   rS   rT   rU   rV   rW   r   rt   rX   r	   ru   JobReferencerb   rY   rD   r   r#   rZ   rQ   r   r   rx   rx      s    6& ))&  & **77	&" " #''+&#& & 3-	&
  }& &r   rx   c            
           \ rS rSrSrS\R                  S\S\R                  R                  4S jrS\4S jr  SS
\\   S\\   S\\   S\\   4S jjrSrg	)QueryTableReader   z0A TableReader that reads from a completed query.r^   r   rz   c                 4    X0l         X l        Xl        X@l        g rB   )rz   r   ra   _results)r   r^   r   rz   resultss        r   rb   QueryTableReader.__init__   s     L 4%OMr   r@   c                     U R                   < $ rB   r~   rE   s    r   rD   !QueryTableReader._GetPrintContext  r   r   Nr   r   r   r   c                    U R                   (       a  [        U R                   5      O0 nX%S'   SUS'   U(       a  X5S'   OXS'   U R                  S   (       d  [        R                  " SU < S35      eU R                  R                  S	S 5      nU R                  R                  S
S 5      nU R                  R                  SS 5      nUcN  UbK  [        U5      S:X  a<  U R                  R                  SS 5      n	U R                  R                  S	/ 5      n
S nGOKUb  Ub  Ub  [        U5      [        [        U5      X-   5      :  a  U R                  R                  SS 5      n[        U5      [        U5      :  a  Uc  [        R                  " SU < S35      eU R                  R                  SS 5      n	U R                  R                  S	/ 5      n
OU R                  R                  5       R                  " S0 UD6R                  5       nUS   (       d  [        R                  " SU < S35      eUR                  SS 5      nUR                  SS 5      n	UR                  S	/ 5      n
XU	4$ )Nrg   r   r   rh   ri   r   r   r   r   	totalRowsjobReferencer'   zSynchronous query z< did not return all rows, yet it did not return a page tokenrQ   )rz   rk   r   r   r   r$   rX   r   r"   ra   r   r   rn   )r   r   r   r   r   rp   result_rows
total_rowsjob_referencer'   r   rq   s               r   r#   QueryTableReader._ReadOnePage  s    "&42D!D$;$<=='""4#ABB --##FD1K"";5JMM%%nd;M3z?a#7 }}  40f]]vr*dj#!C
OY5I JJ==$$[$7j	[	C
O	+
0B$$&*-
 	
 }}  40f]]vr*d__!!#33;d;CCEd- $$T%CDD88K.jxx$'fXXfb!df%%r   )ra   r   rz   r   rR   r   rQ   r   r   r   r      s    8
 ))
  
 **77	
" " #''+3&#3& 3& 3-	3&
  }3& 3&r   r   N)rW   
__future__r   r   r   typingr   googleapiclientr   utilsr   r	   r   r\   rx   r   rQ   r   r   <module>r      sT    7 &  %  %  TF TFn+&| +&\%&\ %&PE&| E&r   