dbt-sqlserver icon indicating copy to clipboard operation
dbt-sqlserver copied to clipboard

clean up relation construction

Open dataders opened this issue 3 years ago • 4 comments

we should try and standardize on a convention of using {{ relation }} wherever possible over these variants:

  • {{ relation.schema }}.{{ relation.identifier }}
  • to_relation.schema ~ '.' ~ to_relation.identifier

@jtcohen6, how do we disable by default database being included when calling {{ relation }}? Do we have create a child of the Relation class?

https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L49

https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L111

https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L118

https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L130

https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L152-L154

https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L168-L169

dataders avatar Jan 26 '21 18:01 dataders

+1 to using {{ relation }} instead of concatenating.

Check out how we did this same thing in dbt-spark:

jtcohen6 avatar Jan 26 '21 18:01 jtcohen6

beautiful. I'll open a PR.

maybe being extra here... but how about concatenating a relation by _ instead of .? We do this for index naming. Looking for a cleaner way than the below. https://github.com/dbt-msft/dbt-sqlserver/blob/4b3f7bce48f3ffa8509a6b16bcd55976a973c2c1/dbt/include/sqlserver/macros/adapters.sql#L116-L139

potential solutions

  1. duplicate BaseRelation.render(), but with '_'.join() instead of '.'.join(). Seems like overkill to me
  2. just quote the rendered relation with square brackets so that the . is ignored? @mikaelene, can you look up an index name that has a . in it? I'm pretty sure you can
  3. just do something like below. not sure if you can call .replace() on relation directly like that though.
    {% set cci_name = relation.replace(".", "_") ~ "_cci" %}
    

dataders avatar Jan 26 '21 19:01 dataders

Option 1 doesn't sound crazy to me. You could define a special method on SQLServerRelation, something like index_render, and then call {{ full_relation.index_render() }} as needed.

It's fewer moving pieces if you handle in Jinja or SQL, of course.

jtcohen6 avatar Jan 27 '21 14:01 jtcohen6

Should happen together with #325

sdebruyn avatar May 15 '23 20:05 sdebruyn