-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2445 from catalyst-cooperative/init-sql-views
Create simple SQL view assets
- Loading branch information
Showing
20 changed files
with
519 additions
and
31 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
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