diff --git a/semantic_model_generator/data_processing/cte_utils.py b/semantic_model_generator/data_processing/cte_utils.py index 557f785..66d4b84 100644 --- a/semantic_model_generator/data_processing/cte_utils.py +++ b/semantic_model_generator/data_processing/cte_utils.py @@ -92,12 +92,13 @@ def remove_ltable_cte(sql_w_ltable_cte: str, table_names: list[str]) -> str: if not is_logical_table(with_.expressions[0].alias): raise ValueError("Analyst queries must contain the logical CTE.") + table_names_lower = [table_name.lower() for table_name in table_names] # Iterate through all CTEs. If a CTE starts with the logical table prefix and matches a table name, remove it. non_logical_cte = [ cte for cte in with_.expressions if not is_logical_table(cte.alias) - or cte.alias.replace(_LOGICAL_TABLE_PREFIX, "") not in table_names + or cte.alias.replace(_LOGICAL_TABLE_PREFIX, "").lower() not in table_names_lower ] # Replace the original expressions list with the filtered list diff --git a/semantic_model_generator/data_processing/cte_utils_test.py b/semantic_model_generator/data_processing/cte_utils_test.py index fca12ee..9d90aa8 100644 --- a/semantic_model_generator/data_processing/cte_utils_test.py +++ b/semantic_model_generator/data_processing/cte_utils_test.py @@ -11,7 +11,7 @@ def test_removes_logical_table_cte(self) -> None: Testing that we remove logical table CTEs corresponding to existing table names. """ query = "WITH __logical_table AS (SELECT * FROM table1) SELECT * FROM __logical_table" - table_names = ["logical_table"] + table_names = ["LOGICAL_TABLE"] expected_query = "SELECT * FROM __logical_table" actual_output = remove_ltable_cte(query, table_names=table_names)