|
12 | 12 | threshold_value: int, |
13 | 13 | alert_priority: DataQualityRuleAlertPriority, |
14 | 14 | dimension: DataQualityDimension, |
| 15 | + custom_sql_return_type: Optional[DataQualityRuleCustomSQLReturnType] = None, |
15 | 16 | description: Optional[str] = None, |
16 | 17 | ) -> DataQualityRule: |
17 | 18 | validate_required_fields( |
|
40 | 41 | attributes = DataQualityRule.Attributes.creator( |
41 | 42 | client=client, |
42 | 43 | rule_name=rule_name, |
43 | | - rule_type="Custom SQL", |
| 44 | + rule_type=DataQualityRuleTemplateType.CUSTOM_SQL, |
44 | 45 | asset=asset, |
45 | 46 | threshold_compare_operator=threshold_compare_operator, |
46 | 47 | threshold_value=threshold_value, |
47 | 48 | alert_priority=alert_priority, |
48 | 49 | dimension=dimension, |
49 | 50 | custom_sql=custom_sql, |
| 51 | + custom_sql_return_type=custom_sql_return_type, |
50 | 52 | description=description, |
51 | 53 | column=None, |
52 | 54 | threshold_unit=None, |
|
59 | 61 | cls, |
60 | 62 | *, |
61 | 63 | client: AtlanClient, |
62 | | - rule_type: str, |
| 64 | + rule_type: DataQualityRuleTemplateType, |
63 | 65 | asset: Asset, |
64 | | - threshold_compare_operator: DataQualityRuleThresholdCompareOperator, |
65 | 66 | threshold_value: int, |
66 | 67 | alert_priority: DataQualityRuleAlertPriority, |
| 68 | + threshold_compare_operator: Optional[ |
| 69 | + DataQualityRuleThresholdCompareOperator |
| 70 | + ] = None, |
| 71 | + threshold_unit: Optional[DataQualityRuleThresholdUnit] = None, |
| 72 | + rule_conditions: Optional[str] = None, |
| 73 | + row_scope_filtering_enabled: Optional[bool] = False, |
67 | 74 | ) -> DataQualityRule: |
68 | 75 | validate_required_fields( |
69 | 76 | [ |
70 | 77 | "client", |
71 | 78 | "rule_type", |
72 | 79 | "asset", |
73 | | - "threshold_compare_operator", |
74 | 80 | "threshold_value", |
75 | 81 | "alert_priority", |
76 | 82 | ], |
77 | 83 | [ |
78 | 84 | client, |
79 | 85 | rule_type, |
80 | 86 | asset, |
81 | | - threshold_compare_operator, |
82 | 87 | threshold_value, |
83 | 88 | alert_priority, |
84 | 89 | ], |
85 | 90 | ) |
| 91 | + template_config = client.dq_template_config_cache.get_template_config( |
| 92 | + rule_type.value |
| 93 | + ) |
| 94 | + |
| 95 | + asset_for_validation, target_table_asset = ( |
| 96 | + DataQualityRule.Attributes._fetch_assets_for_row_scope_validation( |
| 97 | + client, asset, rule_conditions, row_scope_filtering_enabled or False |
| 98 | + ) |
| 99 | + ) |
| 100 | + |
| 101 | + validated_threshold_operator = ( |
| 102 | + DataQualityRule.Attributes._validate_template_features( |
| 103 | + rule_type, |
| 104 | + rule_conditions, |
| 105 | + row_scope_filtering_enabled, |
| 106 | + template_config, |
| 107 | + threshold_compare_operator, |
| 108 | + asset_for_validation, |
| 109 | + target_table_asset, |
| 110 | + ) |
| 111 | + ) |
| 112 | + |
| 113 | + final_threshold_compare_operator = ( |
| 114 | + validated_threshold_operator |
| 115 | + or threshold_compare_operator |
| 116 | + or DataQualityRuleThresholdCompareOperator.LESS_THAN_EQUAL |
| 117 | + ) |
86 | 118 |
|
87 | 119 | attributes = DataQualityRule.Attributes.creator( |
88 | 120 | client=client, |
89 | 121 | rule_type=rule_type, |
90 | 122 | asset=asset, |
91 | | - threshold_compare_operator=threshold_compare_operator, |
| 123 | + threshold_compare_operator=final_threshold_compare_operator, |
92 | 124 | threshold_value=threshold_value, |
93 | 125 | alert_priority=alert_priority, |
94 | 126 | rule_name=None, |
95 | 127 | column=None, |
96 | | - threshold_unit=None, |
| 128 | + threshold_unit=threshold_unit, |
97 | 129 | dimension=None, |
98 | 130 | custom_sql=None, |
99 | 131 | description=None, |
| 132 | + rule_conditions=rule_conditions, |
| 133 | + row_scope_filtering_enabled=row_scope_filtering_enabled, |
100 | 134 | ) |
101 | 135 | return cls(attributes=attributes) |
102 | 136 |
|
|
106 | 140 | cls, |
107 | 141 | *, |
108 | 142 | client: AtlanClient, |
109 | | - rule_type: str, |
| 143 | + rule_type: DataQualityRuleTemplateType, |
110 | 144 | asset: Asset, |
111 | 145 | column: Asset, |
112 | 146 | threshold_value: int, |
|
136 | 170 | alert_priority, |
137 | 171 | ], |
138 | 172 | ) |
139 | | - template_config = client.dq_template_config_cache.get_template_config(rule_type) |
140 | | - |
141 | | - asset_for_validation = asset |
142 | | - if row_scope_filtering_enabled and asset.qualified_name: |
143 | | - from pyatlan.model.fluent_search import FluentSearch |
| 173 | + template_config = client.dq_template_config_cache.get_template_config( |
| 174 | + rule_type.value |
| 175 | + ) |
144 | 176 |
|
145 | | - search_request = ( |
146 | | - FluentSearch() |
147 | | - .where(Asset.QUALIFIED_NAME.eq(asset.qualified_name)) |
148 | | - .include_on_results( |
149 | | - Asset.ASSET_DQ_ROW_SCOPE_FILTER_COLUMN_QUALIFIED_NAME |
150 | | - ) |
151 | | - ).to_request() |
152 | | - results = client.asset.search(search_request) |
153 | | - if results.count == 1: |
154 | | - asset_for_validation = results.current_page()[0] |
| 177 | + asset_for_validation, target_table_asset = ( |
| 178 | + DataQualityRule.Attributes._fetch_assets_for_row_scope_validation( |
| 179 | + client, asset, rule_conditions, row_scope_filtering_enabled or False |
| 180 | + ) |
| 181 | + ) |
155 | 182 |
|
156 | 183 | validated_threshold_operator = ( |
157 | 184 | DataQualityRule.Attributes._validate_template_features( |
|
161 | 188 | template_config, |
162 | 189 | threshold_compare_operator, |
163 | 190 | asset_for_validation, |
| 191 | + target_table_asset, |
164 | 192 | ) |
165 | 193 | ) |
166 | 194 |
|
|
202 | 230 | threshold_unit: Optional[DataQualityRuleThresholdUnit] = None, |
203 | 231 | dimension: Optional[DataQualityDimension] = None, |
204 | 232 | custom_sql: Optional[str] = None, |
| 233 | + custom_sql_return_type: Optional[DataQualityRuleCustomSQLReturnType] = None, |
205 | 234 | rule_name: Optional[str] = None, |
206 | 235 | description: Optional[str] = None, |
207 | 236 | rule_conditions: Optional[str] = None, |
|
224 | 253 | .include_on_results(DataQualityRule.DQ_RULE_ALERT_PRIORITY) |
225 | 254 | .include_on_results(DataQualityRule.DISPLAY_NAME) |
226 | 255 | .include_on_results(DataQualityRule.DQ_RULE_CUSTOM_SQL) |
| 256 | + .include_on_results(DataQualityRule.DQ_RULE_CUSTOM_SQL_RETURN_TYPE) |
227 | 257 | .include_on_results(DataQualityRule.USER_DESCRIPTION) |
228 | 258 | .include_on_results(DataQualityRule.DQ_RULE_DIMENSION) |
229 | 259 | .include_on_results(DataQualityRule.DQ_RULE_CONFIG_ARGUMENTS) |
|
242 | 272 | search_result = results.current_page()[0] |
243 | 273 |
|
244 | 274 | retrieved_custom_sql = search_result.dq_rule_custom_s_q_l # type: ignore[attr-defined] |
| 275 | + retrieved_custom_sql_return_type = ( |
| 276 | + search_result.dq_rule_custom_s_q_l_return_type # type: ignore[attr-defined] |
| 277 | + ) |
245 | 278 | retrieved_rule_name = search_result.display_name |
246 | 279 | retrieved_dimension = search_result.dq_rule_dimension # type: ignore[attr-defined] |
247 | 280 | retrieved_column = search_result.dq_rule_base_column # type: ignore[attr-defined] |
|
275 | 308 | else None |
276 | 309 | ) # type: ignore[attr-defined] |
277 | 310 |
|
278 | | - retrieved_rule_type = retrieved_template_rule_name |
279 | | - template_config = client.dq_template_config_cache.get_template_config( |
280 | | - retrieved_rule_type |
281 | | - ) |
282 | | - validated_threshold_operator = ( |
283 | | - DataQualityRule.Attributes._validate_template_features( |
284 | | - retrieved_rule_type, |
285 | | - rule_conditions, |
286 | | - row_scope_filtering_enabled, |
287 | | - template_config, |
288 | | - threshold_compare_operator or retrieved_threshold_compare_operator, |
289 | | - retrieved_asset, |
| 311 | + template_config = None |
| 312 | + if retrieved_template_rule_name: |
| 313 | + template_config = client.dq_template_config_cache.get_template_config( |
| 314 | + retrieved_template_rule_name |
| 315 | + ) |
| 316 | + |
| 317 | + if rule_conditions: |
| 318 | + final_rule_conditions = rule_conditions |
| 319 | + elif search_result.dq_rule_config_arguments is not None: # type: ignore[attr-defined] |
| 320 | + final_rule_conditions = ( |
| 321 | + search_result.dq_rule_config_arguments.dq_rule_config_rule_conditions # type: ignore[attr-defined] |
290 | 322 | ) |
| 323 | + else: |
| 324 | + final_rule_conditions = None |
| 325 | + |
| 326 | + final_row_scope_filtering_enabled = ( |
| 327 | + row_scope_filtering_enabled or retrieved_row_scope_filtering_enabled |
291 | 328 | ) |
| 329 | + if retrieved_asset: |
| 330 | + retrieved_asset, target_table_asset = ( |
| 331 | + DataQualityRule.Attributes._fetch_assets_for_row_scope_validation( |
| 332 | + client, |
| 333 | + retrieved_asset, |
| 334 | + final_rule_conditions, |
| 335 | + final_row_scope_filtering_enabled, |
| 336 | + ) |
| 337 | + ) |
| 338 | + else: |
| 339 | + target_table_asset = None |
| 340 | + |
| 341 | + validated_threshold_operator = None |
| 342 | + if retrieved_template_rule_name and template_config: |
| 343 | + try: |
| 344 | + retrieved_rule_type = DataQualityRuleTemplateType( |
| 345 | + retrieved_template_rule_name |
| 346 | + ) |
| 347 | + validated_threshold_operator = ( |
| 348 | + DataQualityRule.Attributes._validate_template_features( |
| 349 | + retrieved_rule_type, |
| 350 | + final_rule_conditions, |
| 351 | + final_row_scope_filtering_enabled, |
| 352 | + template_config, |
| 353 | + threshold_compare_operator |
| 354 | + or retrieved_threshold_compare_operator, |
| 355 | + retrieved_asset, |
| 356 | + target_table_asset, |
| 357 | + ) |
| 358 | + ) |
| 359 | + except ValueError: |
| 360 | + pass |
292 | 361 |
|
293 | 362 | final_compare_operator = ( |
294 | 363 | validated_threshold_operator |
|
306 | 375 | or retrieved_threshold_value, |
307 | 376 | dq_rule_threshold_unit=threshold_unit or retrieved_threshold_unit, |
308 | 377 | ), |
309 | | - dq_rule_config_rule_conditions=rule_conditions, |
| 378 | + dq_rule_config_rule_conditions=final_rule_conditions, |
310 | 379 | ), |
311 | 380 | dq_rule_base_dataset_qualified_name=retrieved_asset.qualified_name, |
312 | 381 | dq_rule_alert_priority=alert_priority or retrieved_alert_priority, |
313 | | - dq_rule_row_scope_filtering_enabled=row_scope_filtering_enabled |
314 | | - or retrieved_row_scope_filtering_enabled, |
| 382 | + dq_rule_row_scope_filtering_enabled=final_row_scope_filtering_enabled, |
315 | 383 | dq_rule_base_dataset=retrieved_asset, |
316 | 384 | qualified_name=qualified_name, |
317 | 385 | dq_rule_dimension=dimension or retrieved_dimension, |
|
329 | 397 | if custom_sql is not None: |
330 | 398 | attr_dq.dq_rule_custom_s_q_l = custom_sql |
331 | 399 | attr_dq.display_name = rule_name or retrieved_rule_name |
| 400 | + attr_dq.dq_rule_custom_s_q_l_return_type = ( |
| 401 | + custom_sql_return_type or retrieved_custom_sql_return_type |
| 402 | + ) |
332 | 403 | if description is not None: |
333 | 404 | attr_dq.user_description = description or retrieved_description |
334 | 405 |
|
|
0 commit comments