jplauri commented on a change in pull request #19736: URL: https://github.com/apache/airflow/pull/19736#discussion_r754530593
########## File path: airflow/providers/databricks/utils/databricks.py ########## @@ -0,0 +1,69 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +from typing import Union + +from airflow.exceptions import AirflowException +from airflow.providers.databricks.hooks.databricks import RunState + + +def deep_string_coerce(content, json_path: str = 'json') -> Union[str, list, dict]: + """ + Coerces content or all values of content if it is a dict to a string. The + function will throw if content contains non-string or non-numeric types. + + The reason why we have this function is because the ``self.json`` field must be a + dict with only string values. This is because ``render_template`` will fail + for numerical values. + """ + coerce = deep_string_coerce + if isinstance(content, str): + return content + elif isinstance( + content, + ( + int, + float, + ), + ): + # Databricks can tolerate either numeric or string types in the API backend. + return str(content) + elif isinstance(content, (list, tuple)): + return [coerce(e, f'{json_path}[{i}]') for i, e in enumerate(content)] + elif isinstance(content, dict): + return {k: coerce(v, f'{json_path}[{k}]') for k, v in list(content.items())} + else: + param_type = type(content) + msg = f'Type {param_type} used for parameter {json_path} is not a number or a string' + raise AirflowException(msg) + + +def validate_trigger_event(event: dict): + """Validates correctness of the event received + from :class:`~airflow.providers.databricks.triggers.databricks.DatabricksExecutionTrigger + """ + keys_to_check = ['run_id', 'run_page_url', 'run_state'] + for key in keys_to_check: + if not event.get(key): Review comment: I think it's a tiny bit simpler to do `if key not in event` than to use get. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
