
                         8   d Z ddlmZ ddlmZ ddlmZ ddlm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dZ ej$                  ej&                  j(                  ej&                  j*                         G d dej,                               Zy)z6Implementation of `gcloud dataflow sql query` command.    )absolute_import)division)print_function)unicode_literalsN)apis)sql_query_parameters)base)dataflow_util)sql_util)
propertieszExecute the user-specified SQL query on Dataflow. Queries must comply to the ZetaSQL dialect (https://github.com/google/zetasql). Results may be written to either BigQuery or Cloud Pub/Sub.a        To execute a simple SQL query on Dataflow that reads from and writes to BigQuery, run:

        $ {command} 'SELECT word FROM bigquery.table.`my-project`.input_dataset.input_table where count > 3' --job-name=my-job --region=us-west1 --bigquery-dataset=my_output_dataset --bigquery-table=my_output_table

      To execute a simple SQL query on Dataflow that reads from and writes to Cloud
      Pub/Sub, run:

        $ {command} 'SELECT word FROM pubsub.topic.`my-project`.input_topic where count > 3' --job-name=my-job --region=us-west1 --pubsub-topic=my_output_topic

      To join data from BigQuery and Cloud Pub/Sub and write the result to Cloud
      Pub/Sub, run:

        $ {command} 'SELECT bq.name AS name FROM pubsub.topic.`my-project`.input_topic p INNER JOIN bigquery.table.`my-project`.input_dataset.input_table bq ON p.id = bq.id' --job-name=my-job --region=us-west1 --pubsub-topic=my_output_topic

      To execute a parameterized SQL query that reads from and writes to BigQuery, run:

        $ {command} 'SELECT word FROM bigquery.table.`my-project`.input_dataset.input_table where count > @threshold' --parameter=threshold:INT64:5 --job-name=my-job --region=us-west1 --bigquery-dataset=my_output_dataset --bigquery-table=my_output_table

    )DESCRIPTIONEXAMPLESc                   *    e Zd ZdZeZed        Zd Zy)Queryz1Execute the user-specified SQL query on Dataflow.c                 .    t        j                  |        y)zrRegister flags for this command.

    Args:
      parser: argparse.ArgumentParser to register arguments with.
    N)r   ArgsForSqlQuery)parsers    !lib/surface/dataflow/sql/query.pyArgsz
Query.ArgsB   s     V$    c                    |j                   dk(  }t        j                  |      }|j                  r|j                  }n|rd}nd}dj	                  ||      }|j
                  r t        j                  |j
                        }n.|j                  r t        j                  |j                        }nd}t        j                  d|j                  rdndfd	t        j                  |      fd
|fd|j                  fg      }t!        j"                  di dt$        j&                  j(                  j*                  j-                         d|d|j.                  d|d|j0                  d|j2                  dt$        j&                  j4                  j6                  j9                         d|d|j:                  d|j<                  d|j>                  d|j@                  d|jB                  d|jD                  d|jF                  d|jH                  }|rt         jJ                  jM                  |      S t         jJ                  jO                  |      S )Ndynamicsql_launcher_templatesql_launcher_flex_templatez(gs://dataflow-sql-templates-{}/latest/{}z[]dryRuntruefalseoutputsqueryParametersqueryString
project_id	region_idjob_namegcs_locationzonemax_workersdisable_public_ips
parametersservice_account_emailkms_key_namenum_workersnetwork
subnetworkworker_machine_typeworker_regionworker_zone )(sql_launcher_template_enginer
   	GetRegionr   formatparameters_filer   ParseParametersFile	parameterParseParametersListcollectionsOrderedDictdry_runr   ExtractOutputsqueryr   TemplateArgumentsr   VALUEScoreproject	GetOrFailr#   r%   r&   dataflowr'   GetBoolr)   dataflow_kms_keyr+   r,   r-   r.   r/   r0   	TemplatesLaunchDynamicTemplateCreateJobFromFlexTemplate)	selfargsuse_dynamic_engineregionr$   suffixquery_parameterstemplate_parameters	argumentss	            r   Runz	Query.RunK   s<   ;;yH$$T*F!!//l	(-?FF
&l-AA


 	-AA
.. %11	T\\6w7	H++D12	,-	

#	3  && &$$))11;;=&& & "	&
 YY& $$& &,,55HH	& '& #88& **& $$& & ??& !44&  ((!&" $$#&I$ ^^11)<<>>33I>>r   N)	__name__
__module____qualname____doc__DETAILED_HELPdetailed_helpstaticmethodr   rQ   r1   r   r   r   r   <   s!    9-% %.?r   r   )rU   
__future__r   r   r   r   r9   googlecloudsdk.api_lib.dataflowr   r   googlecloudsdk.callioper	   #googlecloudsdk.command_lib.dataflowr
   r   googlecloudsdk.corer   rV   ReleaseTracksReleaseTrackBETAGACommandr   r1   r   r   <module>rc      s    = &  % '  0 @ ( = 8 *	F	: D%%**D,=,=,@,@A<?DLL <? B<?r   