-
-
Notifications
You must be signed in to change notification settings - Fork 120
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
Create simple SQL view assets #2445
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
08e1f9f
Create sql view factory and plants_utils_ferc1 view
bendnorman 024a247
Add param to SQLiteIOManager to exclude tables from schema creation
bendnorman 22a8a25
Add example python denorm asset
bendnorman 2b75bda
Add denorm_utilities_eia860 to PudlTabl, add sql_asset_factory test, …
bendnorman e255b18
Merge dev into init-sql-views
bendnorman 425c4e3
Merge PudlSQLiteIOManager enforce_schema changes into init-sql-views
bendnorman 5d7c95b
Include sql files in MANIFEST and some other clean up
bendnorman b27a95a
Create new modules for denorm assets
bendnorman 4749611
Drop 860 from denorm asset name
bendnorman 1c02e32
Rename ferc to ferc1
bendnorman dd46383
Merge dev into init-sql-views
bendnorman bea1e62
Make table name and include_in_database attribute more verbose
bendnorman 2364040
Update denorm_plants_utilities_ferc1.sql file name
bendnorman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
graft src/pudl/metadata/templates | ||
graft src/pudl/output/sql | ||
|
||
prune .github | ||
prune devtools | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
""" | ||
from . import ( # noqa: F401 | ||
censusdp1tract, | ||
denorm_eia, | ||
denorm_ferc1, | ||
eia860, | ||
eia923, | ||
epacems, | ||
|
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,42 @@ | ||
"""A collection of denormalized EIA assets.""" | ||
import pandas as pd | ||
from dagster import asset | ||
|
||
import pudl | ||
from pudl.metadata.fields import apply_pudl_dtypes | ||
|
||
|
||
@asset(io_manager_key="pudl_sqlite_io_manager", compute_kind="Python") | ||
def denorm_utilities_eia( | ||
utilities_entity_eia: pd.DataFrame, | ||
utilities_eia860: pd.DataFrame, | ||
utilities_eia: pd.DataFrame, | ||
): | ||
"""Pull all fields from the EIA Utilities table. | ||
|
||
Args: | ||
utilities_entity_eia: EIA utility entity table. | ||
utilities_eia860: EIA 860 annual utility table. | ||
utilities_eia: Associations between EIA utilities and pudl utility IDs. | ||
|
||
Returns: | ||
A DataFrame containing all the fields of the EIA 860 Utilities table. | ||
""" | ||
utilities_eia = utilities_eia[["utility_id_eia", "utility_id_pudl"]] | ||
out_df = pd.merge( | ||
utilities_entity_eia, utilities_eia860, how="left", on=["utility_id_eia"] | ||
) | ||
out_df = pd.merge(out_df, utilities_eia, how="left", on=["utility_id_eia"]) | ||
out_df = ( | ||
out_df.assign(report_date=lambda x: pd.to_datetime(x.report_date)) | ||
.dropna(subset=["report_date", "utility_id_eia"]) | ||
.pipe(apply_pudl_dtypes, group="eia") | ||
) | ||
first_cols = [ | ||
"report_date", | ||
"utility_id_eia", | ||
"utility_id_pudl", | ||
"utility_name_eia", | ||
] | ||
out_df = pudl.helpers.organize_cols(out_df, first_cols) | ||
return out_df |
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,6 @@ | ||
"""A collection of denormalized FERC assets.""" | ||
from pudl.output.sql.helpers import sql_asset_factory | ||
|
||
denorm_plants_utilities_ferc1_asset = sql_asset_factory( | ||
"denorm_plants_utilities_ferc1", {"plants_ferc1", "utilities_ferc1"} | ||
) |
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
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 @@ | ||
"""A module of python helper functions and sql files for creating SQL views.""" |
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,5 @@ | ||
-- Build a view of useful FERC Plant & Utility information. | ||
CREATE VIEW denorm_plants_utilities_ferc1 AS | ||
SELECT * | ||
FROM plants_ferc1 | ||
INNER JOIN utilities_ferc1 USING(utility_id_ferc1); |
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,35 @@ | ||
"""Helper functions for creating output assets.""" | ||
import importlib | ||
|
||
from dagster import AssetsDefinition, asset | ||
|
||
|
||
def sql_asset_factory( | ||
name: str, | ||
non_argument_deps: set[str] = {}, | ||
io_manager_key: str = "pudl_sqlite_io_manager", | ||
compute_kind: str = "SQL", | ||
) -> AssetsDefinition: | ||
"""Factory for creating assets that run SQL statements.""" | ||
|
||
@asset( | ||
name=name, | ||
non_argument_deps=non_argument_deps, | ||
io_manager_key=io_manager_key, | ||
compute_kind=compute_kind, | ||
) | ||
def sql_view_asset() -> str: | ||
"""Asset that creates sql view in a database.""" | ||
sql_path_traversable = importlib.resources.files("pudl.output.sql").joinpath( | ||
f"{name}.sql" | ||
) | ||
try: | ||
with importlib.resources.as_file(sql_path_traversable) as sql_path: | ||
return sql_path.read_text() | ||
# Raise a helpful error here if a sql file doesn't exist | ||
except FileNotFoundError: | ||
raise FileNotFoundError( | ||
f"Could not find {sql_path}. Create a sql file in pudl.output.sql subpackage for {name} asset." | ||
) | ||
|
||
return sql_view_asset |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to fix this to merge, but I think we should probably avoid directing folks to use the
PudlTabl
outputs since we intend to deprecate that too -- if we're giving them directions for the future, it should probably be to use the DB tables directly.