This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
execute_sql
magic function (#340)
* Provide `execute_sql` function * Parse jinja in execute_sql * Pass runtime config into execute_sql * execute_sql integration tests * Test execute sql on with dbt v > 1.0.0 * Check dbt version before running execute_sql * Pr comments * Add hack comment [skip ci]
- Loading branch information
Showing
11 changed files
with
144 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@dbtv1 | ||
Feature: `execute_sql` function | ||
Background: Project Setup | ||
Given the project 009_execute_sql_function | ||
|
||
Scenario: Use execute_sql function | ||
When the following command is invoked: | ||
""" | ||
fal flow run --profiles-dir $profilesDir --project-dir $baseDir --experimental-flow | ||
""" | ||
Then the following models are calculated: | ||
| execute_sql_model_one | execute_sql_model_two | | ||
And the following scripts are ran: | ||
| execute_sql_model_one.query_other_model.py | | ||
And the script execute_sql_model_one.query_other_model.py output file has the lines: | ||
| Model dataframe information: | RangeIndex: 1 entries, 0 to 0 | |
Empty file.
9 changes: 9 additions & 0 deletions
9
integration_tests/projects/009_execute_sql_function/dbt_project.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name: "fal_test" | ||
version: "1.0.0" | ||
config-version: 2 | ||
profile: "fal_test" | ||
source-paths: ["models"] | ||
data-paths: ["data"] | ||
macro-paths: ["macros"] | ||
snapshot-paths: ["snapshots"] | ||
target-path: "{{ env_var('temp_dir') }}/target" |
20 changes: 20 additions & 0 deletions
20
integration_tests/projects/009_execute_sql_function/fal_scripts/query_other_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pandas as pd | ||
import io | ||
import os | ||
from functools import reduce | ||
|
||
df: pd.DataFrame = execute_sql('SELECT * FROM {{ ref("execute_sql_model_one")}}') | ||
|
||
buf = io.StringIO() | ||
df.info(buf=buf, memory_usage=False) | ||
info = buf.getvalue() | ||
output = f"\nModel dataframe information:\n{info}" | ||
temp_dir = os.environ["temp_dir"] | ||
write_dir = open( | ||
reduce( | ||
os.path.join, [temp_dir, context.current_model.name + ".query_other_model.txt"] | ||
), | ||
"w", | ||
) | ||
write_dir.write(output) | ||
write_dir.close() |
10 changes: 10 additions & 0 deletions
10
integration_tests/projects/009_execute_sql_function/models/execute_sql_model_one.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{{ config(materialized='table') }} | ||
-- {{ ref("execute_sql_model_two") }} | ||
|
||
WITH data AS ( | ||
SELECT | ||
'some text' AS my_text | ||
) | ||
|
||
SELECT * | ||
FROM data |
9 changes: 9 additions & 0 deletions
9
integration_tests/projects/009_execute_sql_function/models/execute_sql_model_two.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{{ config(materialized='table') }} | ||
|
||
WITH data AS ( | ||
SELECT | ||
cast(1 AS integer) AS my_int | ||
) | ||
|
||
SELECT * | ||
FROM data |
17 changes: 17 additions & 0 deletions
17
integration_tests/projects/009_execute_sql_function/models/schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: 2 | ||
|
||
sources: | ||
- name: results | ||
database: "{{ env_var('DBT_DATABASE', 'test') }}" | ||
schema: "{{ env_var('DBT_SCHEMA', 'dbt_fal') }}" | ||
tables: | ||
- name: some_source | ||
|
||
models: | ||
- name: execute_sql_model_one | ||
meta: | ||
fal: | ||
scripts: | ||
after: | ||
- fal_scripts/query_other_model.py | ||
- name: execute_sql_model_two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters