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

[Feature] Enable the empty feature for tables requiring quotations #776

Open
3 tasks done
Gudsfile opened this issue Feb 4, 2025 · 0 comments
Open
3 tasks done
Labels
pkg:dbt-postgres Issue affects dbt-postgres

Comments

@Gudsfile
Copy link

Gudsfile commented Feb 4, 2025

Is this your first time submitting a feature request?

  • I have read the expectations for open source contributors
  • I have searched the existing issues, and I could not find an existing issue for this feature
  • I am requesting a straightforward extension of existing dbt functionality, rather than a Big Idea better suited to a discussion

Describe the feature

Hi, am using dbt pg adaptator.
I want to use the --empty feature but it doesn't work in my context.
In fact, we have source tables with hyphens in the name (e.g. my-table).
This feature does not take quoting configuration into account.

dbt run --select my_marts --empty
16:33:54  Running with dbt=1.9.1
16:33:54  Registered adapter: postgres=1.9.0
16:33:54
16:33:54  …
16:33:54
16:33:55  1 of 1 START sql table model public.my_marts ....... [RUN]
16:33:56  1 of 2 ERROR creating sql table model public.my_marts  [ERROR in 0.45s]
16:33:56
16:33:56 …
16:33:56
16:33:56    Database Error in model my_marts (models/marts/my_marts.sql)
  syntax error at or near "-"
  LINE 17: ..." where false limit 0) _dbt_limit_subq_my-table
                                                                  ^
  compiled code at target/run/my_project/models/marts/my_marts.sql
16:33:56
16:33:56  Done. PASS=0 WARN=0 ERROR=1 SKIP=0 TOTAL=1
cat target/run/my_project/models/marts/my_marts.sql
create  table "host"."public"."my_marts__dbt_tmp" as (

with my_table as (
    select * from (
        select * from "host"."public"."my-table" where false limit 0
    ) _dbt_limit_subq_my-table
)

select * from my_table
);

The problem is in the alias of the subquery: _dbt_limit_subq_my-table, there's a hyphen in the alias.


I think the _render_subquery_alias function (dbt-adapters/src/dbt/adapters/base/relation.py) should take into account the self.quote_policy.

This test should then pass:

def test_render_limited_with_quoting():
    my_relation = BaseRelation.create(
        database="test_database",
        schema="test_schema",
        identifier="test_identifier",
        limit=0,
        require_alias=True,
        quote_policy=Policy(False, False, True)
    )
    actual_result = my_relation.render_limited()
    expected_result= '(select * from test_database.test_schema.test_identifier where false limit 0) "_dbt_limit_subq_test_identifier"'
    assert actual_result == expected_result
    assert str(my_relation) == expected_result

Describe alternatives you've considered

No response

Who will this benefit?

Enable use of the --empty feature for all dbt users who have source tables with a special format.

Are you interested in contributing this feature?

No response

Anything else?

No response

@amychen1776 amychen1776 added the pkg:dbt-postgres Issue affects dbt-postgres label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:dbt-postgres Issue affects dbt-postgres
Projects
None yet
Development

No branches or pull requests

2 participants