Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double quote column names in postgres when using "include_missing_columns": true #170

Closed
Gitznik opened this issue Sep 5, 2023 · 4 comments
Labels
question Further information is requested

Comments

@Gitznik
Copy link

Gitznik commented Sep 5, 2023

When using "include_missing_columns": true with my postgres data and one of the column names of the table to mock includes a reserved keyword (in my case reference), the test fails to compile. For context - the mocked source table contains the dbt_utils.star macro, in case that's relevant.

12:26:22  Database Error in test test_manual_overrides (tests/unit/test_manual_overrides.sql)
12:26:22    syntax error at or near "references"
12:26:22    LINE 14: ...col1,col2,col3,references...

Looking at the query that is written based on the test we can see, that the column names that are selected are not quoted, leading to the above error.

This is the specific part of the query causing problems:

select
	col1,
	col2,
	col3,
	references
from
(
	select
		*
	from
		"db"."raw"."table_name"
	where
		false) as m2

Any way to change this to always quote the column names?

This is how dbt_utils does it, see here:

                {%- if quote_identifiers -%}
                    {{ adapter.quote(col)|trim }} {%- if prefix!='' or suffix!='' %} as {{ adapter.quote(prefix ~ col ~ suffix)|trim }} {%- endif -%}
                {%- else -%}
                    {{ col|trim }} {%- if prefix!='' or suffix!='' %} as {{ (prefix ~ col ~ suffix)|trim }} {%- endif -%}
                {% endif %}
@rensoostenbach
Copy link

rensoostenbach commented Sep 7, 2023

I would also like to have this implemented for a different use case. My source table columns contain spaces in them which is currently not handled well and would be fixed by quoting the column name.

Ofcourse another solution would be to get rid of spaces in the column names, but that is not always in our control.

Seems like there is already a pull request for it :).

@psousa50
Copy link
Collaborator

I think this was fixed in v0.3.4, could you please check?

Thank you!

@psousa50 psousa50 added the question Further information is requested label Jan 11, 2024
@rensoostenbachBL
Copy link

Can confirm 0.3.4 fixed this issue for me! Tried running a test on a source table with spaces in columns, and it worked as expected.

@psousa50
Copy link
Collaborator

That's great @rensoostenbach , thank you for your feedback 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants