-
-
Notifications
You must be signed in to change notification settings - Fork 110
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 pudl.duckdb
from parquet files
#3741
Draft
bendnorman
wants to merge
17
commits into
main
Choose a base branch
from
create-duckdb-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
11faecb
Working duckdb schema creation without constraints or enums
bendnorman e090808
Add check contraints to duckdb dialect
bendnorman 218460d
Load parquet files into existing duckdb file
bendnorman 6649cfd
Merge branch 'main' into create-duckdb-output
bendnorman 7ed930f
Add code pk type migration
bendnorman c94abf7
Filter out large tables for sqlite but include for duckdb
bendnorman 0eee1e1
Merge branch 'main' into create-duckdb-output
bendnorman c727735
Add parquet_to_duckdb cli to package and add to nightly build script
bendnorman 75883dc
Merge branch 'main' into create-duckdb-output
zaneselvans 7344506
Update conda lockfiles after merging main.
zaneselvans 6662078
Add table comments in SQL schemas.
zaneselvans 7aa56d7
Include table and column comments in DuckDB schema
zaneselvans 9fbf4b2
Lol jk it was putting comments in there all along.
zaneselvans 98a137b
Swap conditional and context manager order for parquet loading
zaneselvans faeae02
Merge branch 'main' into create-duckdb-output
zaneselvans f0b61f6
Merge main and update dependencies b/c both branches had changes.
zaneselvans 221ce77
Merge alembic migrations from this branch and main
zaneselvans 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#! /usr/bin/env python | ||
"""Script that creates a DuckDB database from a collection of PUDL Parquet files.""" | ||
|
||
import logging | ||
from pathlib import Path | ||
|
||
import click | ||
import sqlalchemy as sa | ||
|
||
from pudl.metadata import PUDL_PACKAGE | ||
from pudl.metadata.classes import Package | ||
|
||
# Configure logging | ||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@click.command() | ||
@click.argument("parquet_dir", type=click.Path(exists=True, resolve_path=True)) | ||
@click.argument( | ||
"duckdb_path", type=click.Path(resolve_path=True, writable=True, allow_dash=False) | ||
) | ||
def convert_parquet_to_duckdb(parquet_dir: str, duckdb_path: str): | ||
"""Convert a directory of Parquet files to a DuckDB database. | ||
|
||
Args: | ||
parquet_dir: Path to a directory of parquet files. | ||
duckdb_path: Path to the new DuckDB database file (should not exist). | ||
|
||
Example: | ||
python parquet_to_duckdb.py /path/to/parquet/directory duckdb.db | ||
""" | ||
parquet_dir = Path(parquet_dir) | ||
duckdb_path = Path(duckdb_path) | ||
|
||
# Check if DuckDB file already exists | ||
if duckdb_path.exists(): | ||
click.echo( | ||
f"Error: DuckDB file '{duckdb_path}' already exists. Please provide a new filename." | ||
) | ||
return | ||
|
||
# create duck db schema from pudl package | ||
resource_ids = (r.name for r in PUDL_PACKAGE.resources if len(r.name) <= 63) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be removed once we rename the tables to be less than 63 characters |
||
package = Package.from_resource_ids(resource_ids) | ||
|
||
metadata = package.to_sql(dialect="duckdb") | ||
engine = sa.create_engine(f"duckdb:///{duckdb_path}") | ||
metadata.create_all(engine) | ||
|
||
# Iterate through the tables in order of foreign key dependency | ||
for table in metadata.sorted_tables: | ||
parquet_file_path = parquet_dir / f"{table.name}.parquet" | ||
logger.info(f"Loading table: {table.name} into DuckDB") | ||
if parquet_file_path.exists(): | ||
sql_command = f""" | ||
COPY {table.name} FROM '{parquet_file_path}' (FORMAT PARQUET); | ||
""" | ||
with engine.connect() as conn: | ||
conn.execute(sa.text(sql_command)) | ||
else: | ||
print("File not found: ", parquet_file_path) | ||
# raise FileNotFoundError("Parquet file not found for: ", table.name) | ||
|
||
|
||
if __name__ == "__main__": | ||
convert_parquet_to_duckdb() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Further down the road do we want this functionality to stay outside of the core package? Or do we think we'll want to run it at the end of the ETL all the time? Would it make sense to put it in say
pudl.convert.parquet_to_duckdb
? and add the CLI to our entry points inpyproject.toml
so we can test it along with all the other CLIs?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.
Yeah let's add it to
pudl.convert
so we can test it with our other CLIs.