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

Move to PyArrow parquet writer for data extraction #87

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions cytotable/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,25 @@ def _source_chunk_to_parquet(

result_filepath = f"{result_filepath_base}-{offset}.parquet"

# attempt to read the data to parquet from duckdb
# with exception handling to read mixed-type data
# using sqlite3 and special utility function
# Attempt to read the data to parquet file
# using duckdb for extraction and pyarrow for
# writing data to a parquet file.
try:
# isolate using new connection to read data with chunk size + offset
# and export directly to parquet via duckdb (avoiding need to return data to python)
_duckdb_reader().execute(
f"""
COPY (
# read data with chunk size + offset
# and export to parquet
parquet.write_table(
table=_duckdb_reader()
.execute(
f"""
{base_query}
LIMIT {chunk_size} OFFSET {offset}
) TO '{result_filepath}'
(FORMAT PARQUET);
"""
"""
)
.arrow(),
where=result_filepath,
)
# Include exception handling to read mixed-type data
# using sqlite3 and special utility function.
except duckdb.Error as e:
# if we see a mismatched type error
# run a more nuanced query through sqlite
Expand All @@ -332,6 +336,9 @@ def _source_chunk_to_parquet(
and str(AnyPath(source["source_path"]).suffix).lower() == ".sqlite"
):
parquet.write_table(
# here we use sqlite instead of duckdb to extract
# data for special cases where column and value types
# may not align (which is valid functionality in SQLite).
table=_sqlite_mixed_type_query_to_parquet(
source_path=str(source["source_path"]),
table_name=str(source["table_name"]),
Expand Down