Repository: dagster-io/dagster
Pull request: Quote table identifiers for row count query in dbt integration
## Summary & Motivation

Fixes: #22625

Dbt integration fails to add row count materialization metadata when the asset's table
contains characters which require it to be quoted.

## How I Tested These Changes
Modified code and ran locally on asset that is failing with current code from `master`
I would have added unit tests, but I didn't see any for the exising row count feature
so it wasn't clear where to start there/if they were necessary.


Problem statement:
dbt row count breaks on identifier which need quoting
### Dagster version

1.7.10

### What's the issue?

When using the dbt integration and collecting row counts, the query fails if part of the relation identifier requires quoting.
```
An error occurred while fetching row count for model.rv.pixellog. Row count metadata will not be included in the event.

Exception: Runtime Error
  
  [INVALID_IDENTIFIER] The identifier rv-databricks_catalog_nonprod is invalid. Please, consider quoting it with back-quotes as `rv-databricks_catalog_nonprod`. SQLSTATE: 42602 (line 5, pos 24)
  
  == SQL ==
  
                      SELECT
                      count(*) as row_count
                      FROM
                      rv-databricks_catalog_nonprod.zzz_alauni.pixellog
  ----------------------^^^
  ```
                  
  

### What did you expect to happen?

The dbt adapter should appropriately quote identifiers

### How to reproduce?

have an asset whose name has a character which requires quoting (and listen i wish i could just rename this catalog but I didn't get to name it)

### Deployment type

Other Docker-based deployment

### Deployment details

ECS

### Additional information

_No response_

### Message from the maintainers

Impacted by this issue? Give it a 👍! We factor engagement into prioritization.

Hints from issue discussion:

Commit message: Quote table identifiers for row count query in dbt integration

Patch to review:
diff --git a/python_modules/libraries/dagster-dbt/dagster_dbt/core/resources_v2.py b/python_modules/libraries/dagster-dbt/dagster_dbt/core/resources_v2.py
index d980a9a12c976..0730bd24e0e74 100644
--- a/python_modules/libraries/dagster-dbt/dagster_dbt/core/resources_v2.py
+++ b/python_modules/libraries/dagster-dbt/dagster_dbt/core/resources_v2.py
@@ -1030,7 +1030,13 @@ def _fetch_row_count_metadata(
 
     unique_id = dbt_resource_props["unique_id"]
     logger.debug("Fetching row count for %s", unique_id)
-    table_str = f"{dbt_resource_props['database']}.{dbt_resource_props['schema']}.{dbt_resource_props['name']}"
+    table_str = ".".join(
+        [adapter.quote(part) for part in [
+            dbt_resource_props["database"],
+            dbt_resource_props["schema"],
+            dbt_resource_props["name"]]
+        ]
+    )
 
     try:
         with adapter.connection_named(f"row_count_{unique_id}"):


Code review comments:
diff_hunk: @@ -1030,7 +1030,13 @@ def _fetch_row_count_metadata(
 
     unique_id = dbt_resource_props["unique_id"]
     logger.debug("Fetching row count for %s", unique_id)
-    table_str = f"{dbt_resource_props['database']}.{dbt_resource_props['schema']}.{dbt_resource_props['name']}"
+    table_str = ".".join(
+        [adapter.quote(part) for part in [
+            dbt_resource_props["database"],
+            dbt_resource_props["schema"],
+            dbt_resource_props["name"]]
+        ]
+    )
path: python_modules/libraries/dagster-dbt/dagster_dbt/core/resources_v2.py
text: @user1:
Let's use `relation_name`

```suggestion
    relation_name = dbt_resource_props["relation_name"]
```