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 Request: support for sql headers so we can test temp user-defined functions #185

Open
darist opened this issue Nov 1, 2023 · 0 comments
Labels
needs investigation This issue must be investigated

Comments

@darist
Copy link
Contributor

darist commented Nov 1, 2023

Hey there!

Just ran into this situation today: we are using sql_header to define a temp user-defined function in BigQuery so we can then call it from the SQL code in the model.

It goes something like this:

my_model.sql

{{ config(sql_header=create_my_udf()) }}
SELECT *, my_udf(1, 2, 3) as udf_result
FROM other_model

The create_my_udf macro is something like this:

{% macro create_my_udf() %}
CREATE TEMP FUNCTION my_udf(x INT, y INT, z INT)
RETURNS INT
LANGUAGE js
AS """
 ... js code here ...
"""
{% endmacro %}

I want to be able to test my_model.sql but currently the query that dbt_unit_testing builds does not include the sql_header, so the UDF is never defined and we get a database error.

I would like to have dbt_unit_testing include the sql_header in the query (queries?) that it executes during the test.

Some ideas:

  • Look at the config of the model under test and if it has a sql_header, grab it and put it at the top of the query generated by dbt_unit_testing
  • Expose new sql_header option as one of the options that some of the macros take.
  • Provide a new macro, eg dbt_unit_testing.set_sql_header that we can call during the test, eg:
{{ config(tags=['unit-test']) }}

{% call dbt_unit_testing.test('my_model', '...') %}

  {% call dbt_unit_testing.set_sql_header() %}
    {{ create_my_udf() }}
  {% endcall %}
  
  {% call dbt_unit_testing.mock_ref ('other_model') %}
     ...
  {% endcall %}
  
  {% call dbt_unit_testing.expect() %}
    ...
  {% endcall %}
{% endcall %}
@psousa50 psousa50 added the wontfix This will not be worked on label Jan 8, 2024
@psousa50 psousa50 added needs investigation This issue must be investigated and removed wontfix This will not be worked on labels Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation This issue must be investigated
Projects
None yet
Development

No branches or pull requests

2 participants