11"""Amazon Clean Rooms Module hosting read_* functions."""
22
33import logging
4- from typing import Any , Dict , Iterator , Optional , Union
4+ from typing import TYPE_CHECKING , Any , Dict , Iterator , Optional , Union
55
66import boto3
77
88import awswrangler .pandas as pd
9- from awswrangler import _utils , s3
9+ from awswrangler import _utils , exceptions , s3
1010from awswrangler ._sql_formatter import _process_sql_params
1111from awswrangler .cleanrooms ._utils import wait_query
1212
13+ if TYPE_CHECKING :
14+ from mypy_boto3_cleanrooms .type_defs import ProtectedQuerySQLParametersTypeDef
15+
1316_logger : logging .Logger = logging .getLogger (__name__ )
1417
1518
@@ -23,10 +26,11 @@ def _delete_after_iterate(
2326
2427
2528def read_sql_query (
26- sql : str ,
27- membership_id : str ,
28- output_bucket : str ,
29- output_prefix : str ,
29+ sql : Optional [str ] = None ,
30+ analysis_template_arn : Optional [str ] = None ,
31+ membership_id : str = "" ,
32+ output_bucket : str = "" ,
33+ output_prefix : str = "" ,
3034 keep_files : bool = True ,
3135 params : Optional [Dict [str , Any ]] = None ,
3236 chunksize : Optional [Union [int , bool ]] = None ,
@@ -36,10 +40,16 @@ def read_sql_query(
3640) -> Union [Iterator [pd .DataFrame ], pd .DataFrame ]:
3741 """Execute Clean Rooms Protected SQL query and return the results as a Pandas DataFrame.
3842
43+ Note
44+ ----
45+ One of `sql` or `analysis_template_arn` must be supplied, not both.
46+
3947 Parameters
4048 ----------
41- sql : str
49+ sql : str, optional
4250 SQL query
51+ analysis_template_arn: str, optional
52+ ARN of the analysis template
4353 membership_id : str
4454 Membership ID
4555 output_bucket : str
@@ -49,9 +59,13 @@ def read_sql_query(
4959 keep_files : bool, optional
5060 Whether files in S3 output bucket/prefix are retained. 'True' by default
5161 params : Dict[str, any], optional
52- Dict of parameters used for constructing the SQL query. Only named parameters are supported.
62+ (Client-side) If used in combination with the `sql` parameter, it's the Dict of parameters used
63+ for constructing the SQL query. Only named parameters are supported.
5364 The dict must be in the form {'name': 'value'} and the SQL query must contain
54- `:name`. Note that for varchar columns and similar, you must surround the value in single quotes
65+ `:name`. Note that for varchar columns and similar, you must surround the value in single quotes.
66+
67+ (Server-side) If used in combination with the `analysis_template_arn` parameter, it's the Dict of parameters
68+ supplied with the analysis template. It must be a string to string dict in the form {'name': 'value'}.
5569 chunksize : Union[int, bool], optional
5670 If passed, the data is split into an iterable of DataFrames (Memory friendly).
5771 If `True` an iterable of DataFrames is returned without guarantee of chunksize.
@@ -82,13 +96,33 @@ def read_sql_query(
8296 >>> output_bucket='output-bucket',
8397 >>> output_prefix='output-prefix',
8498 >>> )
99+
100+ >>> import awswrangler as wr
101+ >>> df = wr.cleanrooms.read_sql_query(
102+ >>> analysis_template_arn='arn:aws:cleanrooms:...',
103+ >>> params={'param1': 'value1'},
104+ >>> membership_id='membership-id',
105+ >>> output_bucket='output-bucket',
106+ >>> output_prefix='output-prefix',
107+ >>> )
85108 """
86109 client_cleanrooms = _utils .client (service_name = "cleanrooms" , session = boto3_session )
87110
111+ if sql :
112+ sql_parameters : "ProtectedQuerySQLParametersTypeDef" = {
113+ "queryString" : _process_sql_params (sql , params , engine_type = "partiql" )
114+ }
115+ elif analysis_template_arn :
116+ sql_parameters = {"analysisTemplateArn" : analysis_template_arn }
117+ if params :
118+ sql_parameters ["parameters" ] = params
119+ else :
120+ raise exceptions .InvalidArgumentCombination ("One of `sql` or `analysis_template_arn` must be supplied" )
121+
88122 query_id : str = client_cleanrooms .start_protected_query (
89123 type = "SQL" ,
90124 membershipIdentifier = membership_id ,
91- sqlParameters = { "queryString" : _process_sql_params ( sql , params , engine_type = "partiql" )} ,
125+ sqlParameters = sql_parameters ,
92126 resultConfiguration = {
93127 "outputConfiguration" : {
94128 "s3" : {
0 commit comments