Repository: dbt-labs/dbt-core

Issue:
Generated test CTE names should be lowercase per style guide
### Describe the feature
Firstly, this feels like the most nit-picky thing ever. The motivation comes from development of [SQLFluff](https://github.com/sqlfluff/sqlfluff) with the dbt templater. We have to lint pure SQL currently as opposed to jinja-SQL for parsing reasons. 

In the case of dbt tests where we're linting the compiled, wrapped query, I've noticed that the CTE name being used has inconsistent capitalisation, and per the dbt coding conventions should be lowercase. At the minute it makes SQLFluff fail.

Compiled dbt test:
```

with dbt__CTE__INTERNAL_test as (

select * from schema.table
where x != 0
)select count(*) from dbt__CTE__INTERNAL_test
```

### Describe alternatives you've considered
We are now able to map from jinja-sql to post-templated SQL to match the error lines correctly, but not yet able to exclude linting errors which only occur in the compiled SQL (like this one). We'll do that at some point, which will make this change unnecessary from a SQLFluff POV.

### Who will this benefit?
Anyone who wants to use SQLFluff with the dbt templater or who likes looking at compiled dbt tests and feels extraordinarily passionate about the dbt coding conventions.

### Are you interested in contributing this feature?
Sure



Discussion:

Resolving patch:
diff --git a/core/dbt/adapters/base/relation.py b/core/dbt/adapters/base/relation.py
--- a/core/dbt/adapters/base/relation.py
+++ b/core/dbt/adapters/base/relation.py
@@ -203,7 +203,7 @@ def create_from_source(
 
     @staticmethod
     def add_ephemeral_prefix(name: str):
-        return f'__dbt__CTE__{name}'
+        return f'__dbt__cte__{name}'
 
     @classmethod
     def create_ephemeral_from_node(
diff --git a/core/dbt/compilation.py b/core/dbt/compilation.py
--- a/core/dbt/compilation.py
+++ b/core/dbt/compilation.py
@@ -191,11 +191,11 @@ def _inject_ctes_into_sql(self, sql: str, ctes: List[InjectedCTE]) -> str:
             [
                 InjectedCTE(
                     id="cte_id_1",
-                    sql="__dbt__CTE__ephemeral as (select * from table)",
+                    sql="__dbt__cte__ephemeral as (select * from table)",
                 ),
                 InjectedCTE(
                     id="cte_id_2",
-                    sql="__dbt__CTE__events as (select id, type from events)",
+                    sql="__dbt__cte__events as (select id, type from events)",
                 ),
             ]
 
@@ -206,8 +206,8 @@ def _inject_ctes_into_sql(self, sql: str, ctes: List[InjectedCTE]) -> str:
 
         This will spit out:
 
-          "with __dbt__CTE__ephemeral as (select * from table),
-                __dbt__CTE__events as (select id, type from events),
+          "with __dbt__cte__ephemeral as (select * from table),
+                __dbt__cte__events as (select id, type from events),
                 with internal_cte as (select * from sessions)
            select * from internal_cte"
 
@@ -246,7 +246,7 @@ def _inject_ctes_into_sql(self, sql: str, ctes: List[InjectedCTE]) -> str:
         return str(parsed)
 
     def _get_dbt_test_name(self) -> str:
-        return 'dbt__CTE__INTERNAL_test'
+        return 'dbt__cte__internal_test'
 
     # This method is called by the 'compile_node' method. Starting
     # from the node that it is passed in, it will recursively call
diff --git a/core/dbt/utils.py b/core/dbt/utils.py
--- a/core/dbt/utils.py
+++ b/core/dbt/utils.py
@@ -298,7 +298,7 @@ def filter_null_values(input: Dict[K_T, Optional[V_T]]) -> Dict[K_T, V_T]:
 
 
 def add_ephemeral_model_prefix(s: str) -> str:
-    return '__dbt__CTE__{}'.format(s)
+    return '__dbt__cte__{}'.format(s)
 
 
 def timestring() -> str:


Test patch:
diff --git a/test/integration/020_ephemeral_test/test_ephemeral.py b/test/integration/020_ephemeral_test/test_ephemeral.py
--- a/test/integration/020_ephemeral_test/test_ephemeral.py
+++ b/test/integration/020_ephemeral_test/test_ephemeral.py
@@ -29,15 +29,15 @@ def test__postgres(self):
 
         sql_file = re.sub(r'\d+', '', sql_file)
         expected_sql = ('create view "dbt"."test_ephemeral_"."double_dependent__dbt_tmp" as ('
-                        'with __dbt__CTE__base as ('
+                        'with __dbt__cte__base as ('
                         'select * from test_ephemeral_.seed'
-                        '),  __dbt__CTE__base_copy as ('
-                        'select * from __dbt__CTE__base'
+                        '),  __dbt__cte__base_copy as ('
+                        'select * from __dbt__cte__base'
                         ')-- base_copy just pulls from base. Make sure the listed'
                         '-- graph of CTEs all share the same dbt_cte__base cte'
-                        "select * from __dbt__CTE__base where gender = 'Male'"
+                        "select * from __dbt__cte__base where gender = 'Male'"
                         'union all'
-                        "select * from __dbt__CTE__base_copy where gender = 'Female'"
+                        "select * from __dbt__cte__base_copy where gender = 'Female'"
                         ');')
         sql_file = "".join(sql_file.split())
         expected_sql = "".join(expected_sql.split())
@@ -79,11 +79,11 @@ def test__postgres(self):
         sql_file = re.sub(r'\d+', '', sql_file)
         expected_sql = (
             'create view "dbt"."test_ephemeral_"."root_view__dbt_tmp" as ('
-            'with __dbt__CTE__ephemeral_level_two as ('
+            'with __dbt__cte__ephemeral_level_two as ('
             'select * from "dbt"."test_ephemeral_"."source_table"'
-            '),  __dbt__CTE__ephemeral as ('
-            'select * from __dbt__CTE__ephemeral_level_two'
-            ')select * from __dbt__CTE__ephemeral'
+            '),  __dbt__cte__ephemeral as ('
+            'select * from __dbt__cte__ephemeral_level_two'
+            ')select * from __dbt__cte__ephemeral'
             ');')
 
         sql_file = "".join(sql_file.split())
diff --git a/test/integration/100_rpc_test/test_rpc.py b/test/integration/100_rpc_test/test_rpc.py
--- a/test/integration/100_rpc_test/test_rpc.py
+++ b/test/integration/100_rpc_test/test_rpc.py
@@ -87,11 +87,11 @@ def query_url(url, query):
     return requests.post(url, headers=headers, data=json.dumps(query))
 
 
-_select_from_ephemeral = '''with __dbt__CTE__ephemeral_model as (
+_select_from_ephemeral = '''with __dbt__cte__ephemeral_model as (
 
 
 select 1 as id
-)select * from __dbt__CTE__ephemeral_model'''
+)select * from __dbt__cte__ephemeral_model'''
 
 
 def addr_in_use(err, *args):
diff --git a/test/unit/test_compiler.py b/test/unit/test_compiler.py
--- a/test/unit/test_compiler.py
+++ b/test/unit/test_compiler.py
@@ -80,7 +80,7 @@ def setUp(self):
 
         def mock_generate_runtime_model_context(model, config, manifest):
             def ref(name):
-                result = f'__dbt__CTE__{name}'
+                result = f'__dbt__cte__{name}'
                 unique_id = f'model.root.{name}'
                 model.extra_ctes.append(InjectedCTE(id=unique_id, sql=None))
                 return result
@@ -121,7 +121,7 @@ def test__prepend_ctes__already_has_cte(self):
                     extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql='select * from source_table')],
                     compiled_sql=(
                         'with cte as (select * from something_else) '
-                        'select * from __dbt__CTE__ephemeral'),
+                        'select * from __dbt__cte__ephemeral'),
                     checksum=FileHash.from_contents(''),
                 ),
                 'model.root.ephemeral': CompiledModelNode(
@@ -168,10 +168,10 @@ def test__prepend_ctes__already_has_cte(self):
         self.assertEqual(result.extra_ctes_injected, True)
         self.assertEqualIgnoreWhitespace(
             result.compiled_sql,
-            ('with __dbt__CTE__ephemeral as ('
+            ('with __dbt__cte__ephemeral as ('
              'select * from source_table'
              '), cte as (select * from something_else) '
-             'select * from __dbt__CTE__ephemeral'))
+             'select * from __dbt__cte__ephemeral'))
 
         self.assertEqual(
             manifest.nodes['model.root.ephemeral'].extra_ctes_injected,
@@ -296,7 +296,7 @@ def test__prepend_ctes(self):
                     compiled=True,
                     extra_ctes_injected=False,
                     extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql='select * from source_table')],
-                    compiled_sql='select * from __dbt__CTE__ephemeral',
+                    compiled_sql='select * from __dbt__cte__ephemeral',
                     checksum=FileHash.from_contents(''),
                 ),
                 'model.root.ephemeral': CompiledModelNode(
@@ -345,10 +345,10 @@ def test__prepend_ctes(self):
         self.assertTrue(result.extra_ctes_injected)
         self.assertEqualIgnoreWhitespace(
             result.compiled_sql,
-            ('with __dbt__CTE__ephemeral as ('
+            ('with __dbt__cte__ephemeral as ('
              'select * from source_table'
              ') '
-             'select * from __dbt__CTE__ephemeral'))
+             'select * from __dbt__cte__ephemeral'))
         print(f"\n---- line 349 ----")
 
         self.assertFalse(manifest.nodes['model.root.ephemeral'].extra_ctes_injected)
@@ -423,7 +423,7 @@ def test__prepend_ctes__cte_not_compiled(self):
                     compiled=True,
                     extra_ctes_injected=False,
                     extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql='select * from source_table')],
-                    compiled_sql='select * from __dbt__CTE__ephemeral',
+                    compiled_sql='select * from __dbt__cte__ephemeral',
                     checksum=FileHash.from_contents(''),
                 ),
                 'model.root.ephemeral': parsed_ephemeral,
@@ -454,10 +454,10 @@ def test__prepend_ctes__cte_not_compiled(self):
         self.assertTrue(result.extra_ctes_injected)
         self.assertEqualIgnoreWhitespace(
             result.compiled_sql,
-            ('with __dbt__CTE__ephemeral as ('
+            ('with __dbt__cte__ephemeral as ('
              'select * from source_table'
              ') '
-             'select * from __dbt__CTE__ephemeral'))
+             'select * from __dbt__cte__ephemeral'))
 
         self.assertTrue(manifest.nodes['model.root.ephemeral'].extra_ctes_injected)
 
@@ -488,7 +488,7 @@ def test__prepend_ctes__multiple_levels(self):
                     compiled=True,
                     extra_ctes_injected=False,
                     extra_ctes=[InjectedCTE(id='model.root.ephemeral', sql=None)],
-                    compiled_sql='select * from __dbt__CTE__ephemeral',
+                    compiled_sql='select * from __dbt__cte__ephemeral',
                     checksum=FileHash.from_contents(''),
 
                 ),
@@ -552,12 +552,12 @@ def test__prepend_ctes__multiple_levels(self):
         self.assertTrue(result.extra_ctes_injected)
         self.assertEqualIgnoreWhitespace(
             result.compiled_sql,
-            ('with __dbt__CTE__ephemeral_level_two as ('
+            ('with __dbt__cte__ephemeral_level_two as ('
              'select * from source_table'
-             '), __dbt__CTE__ephemeral as ('
-             'select * from __dbt__CTE__ephemeral_level_two'
+             '), __dbt__cte__ephemeral as ('
+             'select * from __dbt__cte__ephemeral_level_two'
              ') '
-             'select * from __dbt__CTE__ephemeral'))
+             'select * from __dbt__cte__ephemeral'))
 
         self.assertTrue(manifest.nodes['model.root.ephemeral'].compiled)
         self.assertTrue(manifest.nodes['model.root.ephemeral_level_two'].compiled)