From 1ac3b2fe458b2cabb76bdbaa3fd9b39109a4c0fb Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Fri, 12 Jul 2024 19:41:11 -0400 Subject: [PATCH] establish a minimal permissions workaround for session logging --- dbt/adapters/snowflake/impl.py | 4 +++ .../adapter/python_model_tests/_files.py | 21 ----------- .../python_model_tests/test_logging.py | 36 +++++++++++-------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/dbt/adapters/snowflake/impl.py b/dbt/adapters/snowflake/impl.py index 6854b199d..31e70f980 100644 --- a/dbt/adapters/snowflake/impl.py +++ b/dbt/adapters/snowflake/impl.py @@ -363,6 +363,8 @@ def submit_python_job(self, parsed_model: dict, compiled_code: str): if use_anonymous_sproc: proc_name = f"{identifier}__dbt_sp" python_stored_procedure = f""" +ALTER SESSION SET LOG_LEVEL = DEBUG; +ALTER SESSION SET TRACE_LEVEL = ALWAYS; WITH {proc_name} AS PROCEDURE () {common_procedure_code} CALL {proc_name}(); @@ -372,6 +374,8 @@ def submit_python_job(self, parsed_model: dict, compiled_code: str): python_stored_procedure = f""" CREATE OR REPLACE PROCEDURE {proc_name} () {common_procedure_code}; +ALTER SESSION SET LOG_LEVEL = DEBUG; +ALTER SESSION SET TRACE_LEVEL = ALWAYS; CALL {proc_name}(); """ diff --git a/tests/functional/adapter/python_model_tests/_files.py b/tests/functional/adapter/python_model_tests/_files.py index fe19edbb4..dd69f37fa 100644 --- a/tests/functional/adapter/python_model_tests/_files.py +++ b/tests/functional/adapter/python_model_tests/_files.py @@ -46,24 +46,3 @@ def model(dbt, session): {% do return(_table) %} {% endmacro %} """ - - -MODEL__LOGGING = """ -import logging - -import snowflake.snowpark as snowpark -import snowflake.snowpark.functions as f -from snowflake.snowpark.functions import * - - -logger = logging.getLogger("dbt_logger") -logger.info("******Inside Logging module.******") - - -def model(dbt, session): - session.sql(f"ALTER SESSION SET LOG_LEVEL = INFO").collect() - logger.info("******Logging start.******") - df=session.sql(f"select current_user() as session_user, current_role() as session_role") - logger.info("******Logging End.******") - return df -""" diff --git a/tests/functional/adapter/python_model_tests/test_logging.py b/tests/functional/adapter/python_model_tests/test_logging.py index 461eebd47..9792d6436 100644 --- a/tests/functional/adapter/python_model_tests/test_logging.py +++ b/tests/functional/adapter/python_model_tests/test_logging.py @@ -1,24 +1,32 @@ from dbt.tests.util import run_dbt import pytest -from tests.functional.adapter.python_model_tests._files import MODEL__LOGGING - -EVENT_TABLE_SQL = """ -SELECT - RECORD['severity_text']::STRING AS log_level, - VALUE::STRING AS message, - RESOURCE_ATTRIBUTES['snow.query.id']::STRING AS query_id -FROM - DXRX_OPERATIONS.LOGGING.EVENTS -WHERE - SCOPE['name']::STRING = 'dbt_logger' -ORDER BY - 'TIMESTAMP' DESC -; + +MODEL__LOGGING = """ +import logging + +import snowflake.snowpark as snowpark +import snowflake.snowpark.functions as f +from snowflake.snowpark.functions import * + + +logger = logging.getLogger("dbt_logger") +logger.info("******Inside Logging module.******") + + +def model(dbt, session: snowpark.Session): + logger.info("******Logging start.******") + df=session.sql(f"select current_user() as session_user, current_role() as session_role") + logger.info("******Logging End.******") + return df """ class TestPythonModelLogging: + """ + This test case addresses bug report https://github.com/dbt-labs/dbt-snowflake/issues/846 + """ + @pytest.fixture(scope="class") def models(self): return {"logging_model.py": MODEL__LOGGING}