-
Notifications
You must be signed in to change notification settings - Fork 209
Custom loader example in docs not working properly #4250
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
Comments
I have tried to do add a call to |
Hey @blecourt-private , thanks for reporting this. Yes, this is a known issue. Adding calls of new macros is not currently supported through the custom loader implementation. This is because SQLMesh collects references to macros at model creation time and then constructs the python environment based on references it discovered this way. Unfortunately, in the example above it's already too late for it to pick up new macro references so that's why you see the The example should still work if you add inline SQL instead and avoid calling any macros, or call a macro that is already used in the target model. |
The Modify every model example doesn't cache the modified models (which makes sense, since the models are not "reloaded"). Are there any side effects to be expected from the models in the cache corresponding to the ones that are in the model files instead of the modified models? |
Please see a suggestion of how the outlined issue could be resolved in this PR: #4301 |
Referencing this chapter in the docs: Customizing SQLMesh
Using SQLMesh version 0.176.0 I have tried this out using the example project scaffolded by
sqlmesh init duckdb
.The config part of the
config.py
looks like this:I couldn't get
VACUUM @this_model
to work with duckdb, even when I add it as a post statement directly tosqlmesh_example.incremental_model
. So instead I went with a different post statement, using the following@print_locals()
macro, present in themacros/
directory of the project.Using the macro in a post statement
First I tested it out modifying
sqlmesh_example.incremental_model
and used the standard loader (via the scaffolded
config.yaml
) to executesqlmesh plan
.This works fine. And as a sneak peek to what I think will turn out to be relevant wrt the custom loader, the model's
python_env
(from the statestore_snapshots
table):NB! Adding the
@print_locals();
post statement tosqlmesh_example.seed_model
results in the following error:Adding the post statement dynamically using a custom loader
On a side note: for this I removed the database and the SQLMesh cache folder since switching from
config.yaml
toconfig.py
seemed to have some side effects that I couldn't resolve bysqlmesh clean
. This might be a separate issue?As a next step I removed the post statement from
sqlmesh_example.incremental_model
and modified theconfig.py
from Customizing SQLMesh usingand also exempting
sqlmesh_example.seed_model
from being modified with a post statement. Config.py:Doing
sqlmesh plan
results in this error:Inspecting the
python_env
ofsqlmesh_example.incremental_model
from the printed messages:Before modifying:
After modifying:
From my current understanding of SQLMesh the definition of
@print_locals()
macro needs to be present inpython_env
for the plan to succeed.Doing a trick for adding the macro definition to python_env
As a little trick I added the post statement
@print_locals();
tosqlmesh_example.incremental_model
andsqlmesh_example.full_model
. This would add the definition of@print_locals()
to the python env. It would also result in these models having TWO post statements, one from the.sql
files and one from the custom loader.Doing
sqlmesh plan
succeeds.Inspecting the
python_env
ofsqlmesh_example.incremental_model
from the printed messages:Before modifying:
The macro definition is present because the macro is called in the post statement in
models/incremental_model.sql
and added bysuper()._load_models()
.After modifying:
Also, there are 2 post statements after the model has been modified:
If the post statement in the
config.py
is modified slightly to bethe plan is also successfully applied. But this all hinges on the definition of
@print_locals()
being present in the model'spython_env
beforehand.Replacing the post statement in
config.py
withresults in failure to execute the plan:
And this makes sense, since the
@gateway
macro isn't used anywhere else.Once again we can do a small trick, by including
in the select statements for
sqlmesh_example.incremental_model
andsqlmesh_example.full_model
.After doing this, the plan succeeds - in this case because the because the
python_env
now has an item{__sqlmesh__vars__': Executable<payload: {'gateway': 'duckdb'}, kind: value>}
.Hypothesis
Arriving at the following hypothesis:
I think this would require calling
load_sql_based_model()
with the right choice of arguments (and for a complete solution we would also need to load python models).And the above would also apply to other model changes we might make in the custom loader. Without loading the modified models we may easily end up with something that just doesn't work.
The text was updated successfully, but these errors were encountered: