
    8                          d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ d Zd Zy)zBHelpers for writing commands interacting with Cloud Dataflow SQL.
    )absolute_import)division)unicode_literalsN)arg_parsers)
exceptions)concepts)dataflow_util)	job_utils)concept_parsers)presentation_specs)
propertiesc                 >   t        j                  |        | j                  ddd       | j                  ddd       | j                  d	t        j                  d
d      dt
        j                  z   d       | j                  dd      }t        j                  t        j                  dt        j                  ddt        j                  dd      t        j                  dd      t        j                  dd            dd|      t        j                  dt        j                  dd t        j                  d!d"      t        j                  d#d$      %      d&d|      g      j                  |        | j                  d'd(g d)d*+       | j                  d,d-d.d/gd.+       | j!                         }|j                  d0d1d23       |j                  d4d56       | j                  d7d8d93       | j                  d:dd;d<d=gd<>       | j                  d?dd@A       yB)CzyRegister flags for running a SQL query.

  Args:
    parser: The argparse.ArgParser to configure with query arguments.
  queryQUERYzThe SQL query to execute.)metavarhelpz
--job-namez4The unique name to assign to the Cloud Dataflow job.T)r   requiredz--regionz	\w+-\w+\dzmust provide a valid regionz*Region ID of the job's regional endpoint. )typer   r   z/The destination(s) for the output of the query.)r   r   --bigquery-tablezbigquery.tableszBigQuery tablezbigquery-tablezThe BigQuery table ID.)name	help_textzbigquery-projectzThe BigQuery project ID.zbigquery-datasetzThe BigQuery dataset ID.)resource_nametableId	projectId	datasetIdz,The BigQuery table to write query output to.F)prefixesgroupz--pubsub-topiczpubsub.projects.topicszPub/Sub topiczpubsub-topiczThe Pub/Sub topic ID.zpubsub-projectzThe Pub/Sub project ID.)r   topicsId
projectsIdz1The Cloud Pub/Sub topic to write query output to.z--bigquery-write-dispositionz-The behavior of the BigQuery write operation.write-emptyzwrite-truncatezwrite-appendr!   )r   choicesdefaultz--pubsub-create-dispositionz-The behavior of the Pub/Sub create operation.create-if-not-foundfail-if-not-foundz--parameterappendztParameters to pass to a query. Parameters must use the format name:type:value, for example min_word_count:INT64:250.)actionr   z--parameters-filezPath to a file containing query parameters in JSON format. e.g. [{"parameterType": {"type": "STRING"}, "parameterValue": {"value": "foo"}, "name": "x"}, {"parameterType": {"type": "FLOAT64"}, "parameterValue": {"value": "1.0"}, "name": "y"}])r   z	--dry-run
store_truez=Construct but do not run the SQL pipeline, for smoke testing.z--sql-launcher-template-enginez9The template engine to use for the SQL launcher template.flexdynamic)hiddenr   r"   r#   z--sql-launcher-templatea,  The full GCS path to a SQL launcher template spec, e.g. gs://dataflow-sql-templates-us-west1/cloud_dataflow_sql_launcher_template_20201208_RC00/sql_launcher_flex_template. If None is specified, default to the latest release in the region. Note that older releases are not guaranteed to be compatible.)r+   r   N)r
   
CommonArgsadd_argumentr   RegexpValidatorr	   DEFAULT_REGION_MESSAGE	add_groupr   ConceptParserr   ResourcePresentationSpecr   ResourceSpec ResourceParameterAttributeConfigAddToParseradd_mutually_exclusive_group)parseroutput_groupparameter_groups      3lib/googlecloudsdk/command_lib/dataflow/sql_util.pyArgsForSqlQueryr;   "   s    vw%@  B 	A  
 	&&|'DF9223   !!K " M, 11



,??'3KM AA)68 !AA)68
9 9  11



&+@@%1HJ!BB'578 >#! < [$:?	   	#:$&9:#	  % 779/?  @ G  H 	J  L
 	&Fy!   	F  G    c                 T   g }| j                   rd}d}d}| j                   j                  d      }t        |      dk(  r|\  }}}n=t        |      dk(  r|\  }}n)t        |      dk(  r|\  }nt        j                  dd      |K| j
                  r| j
                  n1t        j                  j                  j                  j                         }nK| j
                  r?| j
                  |k7  r0t        j                  dd	j                  | j
                  |            |/| j                  st        j                  d
d      | j                  }nK| j                  r?| j                  |k7  r0t        j                  d
dj                  | j                  |            t        j                  d|fd|fd|fg      }dddd| j                      }t        j                  dd|fd|fg      }|j#                  |       | j$                  rddd| j&                     }	t        j                  dd| j(                  r| j(                  n1t        j                  j                  j                  j                         fd| j$                  fd|	fg      }
|j#                  |
       t+        j,                  |      S )zCParses outputs from args, returning a JSON string with the results.N.         r   z?Malformed table identifier. Use format "project.dataset.table".z--bigquery-projectzE"{}" does not match project "{}" set in qualified `--bigquery-table`.z--bigquery-datasetz9Must be specified when `--bigquery-table` is unqualified.zE"{}" does not match dataset "{}" set in qualified `--bigquery-table`.r   r   r   WRITE_EMPTYWRITE_TRUNCATEWRITE_APPENDr    )r   bigquerytablewriteDispositionCREATE_IF_NOT_FOUNDFAIL_IF_NOT_FOUND)r$   r%   )r   pubsubtopiccreateDisposition)bigquery_tablesplitlenr   InvalidArgumentExceptionbigquery_projectr   VALUEScoreproject	GetOrFailformatbigquery_datasetRequiredArgumentExceptioncollectionsOrderedDictbigquery_write_dispositionr&   pubsub_topicpubsub_create_dispositionpubsub_projectjsondumps)argsoutputs
bq_projectdatasetrF   table_partstable_configwrite_disposition	bq_configcreate_dispositionpubsub_configs              r:   ExtractOutputsrk      s   '	JGE%%++C0K
;1#. j'5	[	Q	"ngu	[	Q	fe//

KM M ,0,A,A4((zGXGXG]G]GeGeGoGo Hj			4#8#8J#F//

Q6$''46 6
 ""22 GI 	I %%g			4#8#8G#C//

Q6$''13 3
 **[*,E-8',B-6,>,@ AL %*& %%	'
 '')=*1<)@*<*;*=)> ?I NN9	40 $$&  ++	 $ 3 3								'	'	1	1	3	5 
$##$	01- M NN=!	G	r<   )__doc__
__future__r   r   r   rY   r_   googlecloudsdk.callioper   r    googlecloudsdk.calliope.conceptsr   #googlecloudsdk.command_lib.dataflowr	   r
   (googlecloudsdk.command_lib.util.conceptsr   r   googlecloudsdk.corer   r;   rk    r<   r:   <module>rt      sA    '  '   / . 5 = 9 D G *hGVDr<   