From d9274de72987a249bc450f7c783b774a155dfb9e Mon Sep 17 00:00:00 2001 From: Mike Alfare <13974384+mikealfare@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:30:46 -0500 Subject: [PATCH] [Bug] Initialize sqlparse lexer prior to using sqlparse (#711) * changelog * initialize sqlparse Lexer prior to using sqlparse --- .changes/unreleased/Fixes-20240206-132326.yaml | 7 +++++++ dbt/adapters/redshift/connections.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240206-132326.yaml diff --git a/.changes/unreleased/Fixes-20240206-132326.yaml b/.changes/unreleased/Fixes-20240206-132326.yaml new file mode 100644 index 000000000..45b3a74c8 --- /dev/null +++ b/.changes/unreleased/Fixes-20240206-132326.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: Initialize sqlparse.Lexer to resolve issue with `dbt docs generate` that includes + external tables +time: 2024-02-06T13:23:26.061133-05:00 +custom: + Author: mikealfare + Issue: "710" diff --git a/dbt/adapters/redshift/connections.py b/dbt/adapters/redshift/connections.py index 8a1b749d0..b0fc0825d 100644 --- a/dbt/adapters/redshift/connections.py +++ b/dbt/adapters/redshift/connections.py @@ -354,6 +354,7 @@ def add_query(self, sql, auto_begin=True, bindings=None, abridge_sql_log=False): connection = None cursor = None + self._initialize_sqlparse_lexer() queries = sqlparse.split(sql) for query in queries: @@ -385,3 +386,14 @@ def get_credentials(cls, credentials): @classmethod def data_type_code_to_name(cls, type_code: Union[int, str]) -> str: return get_datatype_name(type_code) + + @staticmethod + def _initialize_sqlparse_lexer(): + """ + Resolves: https://github.com/dbt-labs/dbt-redshift/issues/710 + Implementation of this fix: https://github.com/dbt-labs/dbt-core/pull/8215 + """ + from sqlparse.lexer import Lexer # type: ignore + + if hasattr(Lexer, "get_default_instance"): + Lexer.get_default_instance()