Skip to content

Conversation

mroy-seedbox
Copy link
Contributor

@mroy-seedbox mroy-seedbox commented Sep 25, 2024

Brief summary of the change made

  • Allows to use {{ this.identifier }} in DBT (but with the Jinja templater).
  • Also allows any other future attributes or methods.
  • Rename ThisEmulator to RelationEmulator.
  • Also return a RelationEmulator for ref() and source() as well.
  • Fixes Support for DBT's this.identifier #6253

Are there any other side effects of this change that we should be aware of?

No. Identical as before, but supports more possibilities.

Sample results

t = ThisEmulator()
print(t)
print(t.database)
print(t.schema)
print(t.name)
print(t.identifier)
print(t.type)
print(t.something_new)
print(t.is_table)
print(t.is_view)
print(t.is_materialized_view)
print(t.is_cte)
print(t.is_dynamic_table)
print(t.is_iceberg_format)
print(t.include())
print(t.include(database=False))
print(t.some_new_method())

Prints:

this_model
this_database
this_schema
this_model
this_model
this_model
this_model
True
True
True
True
True
True
this_model
this_model
this_model

Alternative Solutions

Initially, I tried this:

[sqlfluff:templater:jinja:context]
this = {"identifier": "this_identifier"}

But then that broke {{ this }}. 🤦‍♂️ So not a solution.

Then I tried this:

[sqlfluff:templater:jinja:context]
this.identifier = "this_identifier"

Which simply does not work at all. 😞 But... it would be pretty awesome if it did work! It would give us a lot more flexibility to customize things (in order to override only specific parts of objects, like this.*, target.*, and others).

@mroy-seedbox
Copy link
Contributor Author

Also changed the return of ref() and source() to a RelationEmulator.

Sample results

t = ThisEmulator("ref_model")
print(t)
print(t.database)
print(t.schema)
print(t.name)
print(t.identifier)
print(t.type)
print(t.something_new)
print(t.is_table)
print(t.is_view)
print(t.is_materialized_view)
print(t.is_cte)
print(t.is_dynamic_table)
print(t.is_iceberg_format)
print(t.include())
print(t.include(database=False))
print(t.some_new_method())

Prints:

ref_model
this_database
this_schema
ref_model
ref_model
ref_model
ref_model
True
True
True
True
True
True
ref_model
ref_model
ref_model

Copy link
Member

@alanmcruickshank alanmcruickshank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! I like the approach. Will review properly once I've seen what it does to the test suite, but on a first pass I think this looks like a good approach.

@mroy-seedbox
Copy link
Contributor Author

@alanmcruickshank: I fixed a few things! The next test run should work better. 🤞

@mroy-seedbox
Copy link
Contributor Author

Ah crap.... typing.Self was only added in Python 3.11. 🤦‍♂️

@mroy-seedbox
Copy link
Contributor Author

@alanmcruickshank: Fixed!

@mroy-seedbox
Copy link
Contributor Author

Thanks for the quick fix! 🙌

Not sure why it can't find typing_extensions... I added it to requirements_dev.txt. 🤷‍♂️

Copy link
Contributor

github-actions bot commented Sep 26, 2024

Coverage Results ✅

Name    Stmts   Miss  Cover   Missing
-------------------------------------
TOTAL   18416      0   100%

236 files skipped due to complete coverage.

@mroy-seedbox
Copy link
Contributor Author

Ah, duh.... it's not just a dev dependency. 🤦‍♂️

@coveralls
Copy link

coveralls commented Sep 26, 2024

Coverage Status

coverage: 99.976% (-0.009%) from 99.985%
when pulling 08e9324 on mroy-seedbox:main
into d69a511 on sqlfluff:main.

@mroy-seedbox
Copy link
Contributor Author

@alanmcruickshank: Just added typing-extensions to pyproject.toml, so that should fix the remaining issues!

@alanmcruickshank
Copy link
Member

@alanmcruickshank: Just added typing-extensions to pyproject.toml, so that should fix the remaining issues!

I'm not sure you should need to add typing-extensions, I've been wrestling a lot with typing recently so have a suggestion on that front. I've also removed the # pragma: no cover comments, because I think we should try and get coverage on those methods.

@alanmcruickshank
Copy link
Member

@mroy-seedbox I think the last outstanding thing is some test cases, I think some of the samples you posted above would be great 👍

In order to support the optional `version` parameter
Through `dbt_builtins`
@mroy-seedbox
Copy link
Contributor Author

@alanmcruickshank: Tests are ready!

@mroy-seedbox
Copy link
Contributor Author

@alanmcruickshank: Just need to run the test coverage on the latest commit, and it should pass! 👌

@mroy-seedbox
Copy link
Contributor Author

Actually, just checking this now, and there's a little improvement that could be made to conform better to how DBT Relations work!

Won't take long!

@mroy-seedbox
Copy link
Contributor Author

The full test suite is still running on my end, but everything is looking good so far!

@mroy-seedbox
Copy link
Contributor Author

mroy-seedbox commented Sep 28, 2024

@alanmcruickshank: Alright, all good on my end! ✅ Although I can't get the coverage report to complete. It says "No data to combine". 🤷‍♂️

Apologies for my laziness & lack of time/effort/thoroughness earlier in this process. 😞

Copy link
Member

@alanmcruickshank alanmcruickshank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Looks great. Thanks for contributing this 👍

@alanmcruickshank alanmcruickshank added this pull request to the merge queue Oct 4, 2024
Merged via the queue into sqlfluff:main with commit c86e1f7 Oct 4, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for DBT's this.identifier
3 participants