Warn when SQL function parameter name shadows a column in a referenced model#12772
Warn when SQL function parameter name shadows a column in a referenced model#12772nitrajen wants to merge 1 commit into
Conversation
|
Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA. In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR. CLA has not been signed by users: @nitrajen |
I've signed the CLA |
|
Some CI checks I assume will fail because of changes to the dbt-protos lib which appears bot-managed. I've described the changed in the PR description. If someone from the dbt team advises me what to do, I'd be willing to do it. The CLA CI check keeps coming up as failed. I did get a email and I signed it - twice. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #12772 +/- ##
==========================================
- Coverage 91.41% 83.78% -7.63%
==========================================
Files 203 203
Lines 25883 25906 +23
==========================================
- Hits 23660 21705 -1955
- Misses 2223 4201 +1978
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
I opted for a functional test in lieu of adding unit tests because the change was only a few lines, and I thought it might be slightly better to have functional tests and not unit tests, because the existing unit tests covered udfs to a decent degree. Any reviewer who picks this up, let me know. Totally willing to add unit tests if you think they are needed. |
|
@jtcohen6 @drewbanin - just tagging you in since I've seen your participation in a number of issues. Is this PR and tagged issue worth looking at? Would love any thoughts. |
Resolves #12707
Problem
PostgreSQL SQL-language functions silently resolve a parameter name to a same-named column when both are in scope. Consider a function with a
customer_idparameter that queries a model which also has acustomer_idcolumn:Both sides of
WHERE customer_id = customer_idresolve to the column. The function runs without error but silently returns wrong results.Solution
Added a check in
FunctionRunner.execute(). At execution time, dbt has already run any models the function depends on. The check queries the database for their columns and warns if any parameter name matches. The check runs only for PostgreSQL. The warning is non-fatal and respects--warn-error.Also depends on: companion PR to
dbt-labs/dbt-adaptersto enable this warning when parameter and column names conflict in PostgreSQL: dbt-labs/dbt-adapters#1828Depends on a
dbt-protosupdate to add the following message:To test locally, I updated the serialized descriptor blob in
core_types_pb2.pyand appended the following type stubs tocore_types_pb2.pyi:How the change was tested
Added a functional test in

tests/functional/functions/test_udfs.pythat verifies warnings fire for shadowed parameters and not for clean ones.Also ran
dbt buildlocally against PostgreSQL with two clean functions and two shadowed functions to confirm the same behavior end to end.Checklist